qBFjrdZddlmZddlmZmZmZmZmZddl m Z ddl m Z ddl mZmZmZmZmZddlmZddlmZdd lmZmZdd lmZmZdd lmZmZdd l m!Z!dd l"m#Z#m$Z$m%Z%ddl&m'Z'm(Z(ddl)m*Z*m+Z+m,Z,m-Z-er ddl.m/Z/ddl0m1Z1ddgZ2dAd/Z3Gd0d1e4Z5Gd2d&ee6e*fZ7Gd3d(ee6e8fZ9Gd4d5e Z:Gd6dZ; dBdCd@Z.reset_attributesZs ! """ newrcjj}}|j|kr4 d|j|z zd}|j|S|j|kr||jz |dz kr d|jnF|j|ks |dz kr||jz n|j|kr|j|z |S)z9Move cursor to this `new` point. Returns the given Point. r xy) rD current_x current_y_output_cursor_backward_output_cursor_forward_output_cursor_upr*rBwidthwrites rA move_cursorz(_output_screen_diff..move_cursor`s*}km9 59        E&CEI-. / / /I " "35 ) ) )J UY    i#%/ 0 0 0  ! ! E$KKK " "35 ) ) ) ) UY  )uqy"8"8 # #I$5 6 6 6 6 UY   " "359#4 5 5 5 rCcharrc|jkr|jdS|j}r |kr ||j|jdS)z5 Write the output of this character. N)stylerT)rT new_attrs_output_set_attributesr3r+r.rRs rA output_charz(_output_screen_diff..output_char{s  # # E$)      /tz:I ?.DZ.P!P!P&&y+>>> E$)   JJJrCrowdict[int, Char]r9cffd|D}t|dS)a6 Return max used column index, ignoring whitespace (without style) at the end of the line. This is important for people that copy/paste terminal output. There are two reasons we are sometimes seeing whitespace at the end: - `BufferControl` adds a trailing space to each line, because it's a possible cursor position, so that the line wrapping won't change if the cursor position moves around. - The `Window` adds a style class to the current line for highlighting (cursor-line). c3RK|]!\}}|jdks |j|V"dS) N)rTrV).0indexcellr5s rA zD_output_screen_diff..get_max_column_index..sM  tyC#9$*#E   rCr)default)itemsmax)rZnumbersr5s rAget_max_column_indexz1_output_screen_diff..get_max_column_indexsH    "yy{{    7A&&&&rCrrIrGr:r=)rDrr:r)rTrr:r=)rZr[r:r9)columnsrowsrR write_rawset_attributesrBcursor_forward cursor_upcursor_backward hide_cursordisable_autowrapr erase_downrminheightrerange data_bufferzero_width_escapesrQrTrVrJrKerase_end_of_lineget_cursor_positionlayoutcurrent_windowenable_autowrap show_cursor)&r&r(r)r*r+r,r.r0r2r3r5r7r8rtrkrSrYrgcurrent_height row_countrKnew_row previous_rowzero_width_escapes_rownew_max_line_lenprevious_max_line_lencnew_charold_char char_widthrNrOrPr@rXrBrQrRs& `` ` `` @@@@@@@@rA_output_screen_diffr'sDL$)ME6 LE I$2%6#2($4  6$$$$$$$$$$,''''''*  "+"!!! #&#*8E*A*A!k%!q///22  (( //NC '=>>GGI 9  "'"'$Q'&215 !'!:1!=uqy*>*>w*G*GHH #EAI/C/CL/Q/Q R R ###qzH#AH!,1J } --8>1Q1Q)k%!q///:: ...I4Q7888 H%%%#kmj&@KMRRR OA%###*  '/2GGG%+e.>.Ba&H&H&HIIK       $ $ & & &...!k%!~/A"B"B"BCC Y!k%!~">">">?? !k&"<"Information unavailable. Did not yet receive the CPR response.N)__name__ __module__ __qualname____doc__r?rCrArr sDDDDrCrc"eZdZdZddZdd Zd S)r4zk A cache structure that maps style strings to :class:`.Attr`. (This is an important speed up.) get_attrs_for_style_strCallable[[str], Attrs]style_transformationr!r:r=c"||_||_dSN)rr)selfrrs rA__init__z!_StyleStringToAttrsCache.__init__s (?$$8!!!rC style_strstrrcn||}|j|}|||<|Sr)rrtransform_attrs)rrattrss rA __missing__z$_StyleStringToAttrsCache.__missing__s9,,Y77)99%@@Y rCN)rrrr!r:r=)rrr:rrrrrrrr?rCrAr4r4sF 9999rCc"eZdZdZd dZd d Zd S)r6a Cache for remember which style strings don't render the default output style (default fg/bg, no underline and no reverse and no blink). That way we know that we should render these cells, even when they're empty (when they contain a space). Note: we don't consider bold/italic/hidden because they don't change the output if there's no text in the cell. style_string_to_attrsdict[str, Attrs]r:r=c||_dSr)r)rrs rArz"_StyleStringHasStyleCache.__init__/s%:"""rCrrr1c|j|}t|jp"|jp|jp|jp |jp|j}|||<|Sr)rr1colorbgcolor underlinestrikeblinkreverse)rrr is_defaults rArz%_StyleStringHasStyleCache.__missing__2sl*95 K }  | {   }   %YrCN)rrr:r=)rrr:r1rr?rCrAr6r6$sF;;;;      rCceZdZdZdZdZdZdS) CPR_Supportz&Enum: whether or not CPR is supported. SUPPORTED NOT_SUPPORTEDUNKNOWNN)rrrrrrrr?rCrArrAs#,,I#MGGGrCrceZdZdZdZ d*d+dZd,d-dZed.dZed/dZ ed0dZ d1dZ d2dZ ed/dZ d3d4d!Z d5d6d'Zd7d8d(Zd1d)ZdS)9r$z Typical usage: :: output = Vt100_Output.from_pty(sys.stdout) r = Renderer(style, output) r.render(app, layout=...) FNrVrr(rr2r1 mouse_supportrcpr_not_supported_callbackCallable[[], None] | Noner:r=c||_||_||_t||_||_d|_d|_d|_d|_ t|_ tj |_|jstj|_d|_d|_d|_d|_d|_|ddS)NFT)_scroll)rVr(r2rrr_in_alternate_screen_mouse_support_enabled_bracketed_paste_enabled_cursor_key_mode_resetr _waiting_for_cpr_futuresrr cpr_supportresponds_to_cprr_attrs_for_style_style_string_has_style_last_style_hash_last_transformation_hash_last_color_depthreset)rrVr(r2rrs rArzRenderer.__init__Vs  &&}55*D'%*!&+#(-%&+#>CWW%&.% 9*8D BFIM$15:>&48 4      rCTrleave_alternate_screencXtdd|_d|_d|_d|_d|_t |_d|_|r|j |j r"|r |j d|_ |j r |j d|_ |jr |j d|_|j |j |j dS)NrrIF)r _cursor_pos _last_screen _last_size _last_style_last_cursor_shapermouse_handlers_min_available_heightr(scroll_buffer_to_promptrquit_alternate_screenrdisable_mouse_supportrdisable_bracketed_pastereset_cursor_shaper}flush)rrrs rArzRenderer.reset~s2 1??? ,0'+'+6:,oo&'"  2 K / / 1 1 1  $ .)? . K - - / / /(-D %  & 0 K - - / / /*/D '  ( 2 K / / 1 1 1,1D ) &&((( !!! rCr-c|jS)zn The `Screen` class that was generated during the last rendering. This can be `None`. )rrs rAlast_rendered_screenzRenderer.last_rendered_screens   rCc|js |jdkrdS |j|_dS#t$rYdSwxYw)z True when the height from the cursor until the bottom of the terminal is known. (It's often nicer to draw bottom toolbars only if the height is known, in order to avoid flickering when the CPR response arrives.) rTF)r2rr(get_rows_below_cursor_positionNotImplementedErrorrs rAheight_is_knownzRenderer.height_is_knownsc   t9A==4 )-)S)S)U)UD &4"   55 s6 AAr9c|jrdS|jdkrK|jj}|jr |jjnd}|t|j|z Std)zU Return the number of rows visible in the terminal above the layout. rzRows above layout is unknown.) rrr(get_sizerjrrtrer)r total_rowslast_screen_heights rArows_above_layoutzRenderer.rows_above_layouts{  $ H1  '! + +--//4J=A=N!U!2!9!9TU D$>@R S SS S&'FGG GrCcjjdksJjr%jj_dS j_dS#t$rYnwxYwj tj krdSdfd }j tj kr |dSj rdS|dfd }t|dS)aS Get current cursor position. We do this to calculate the minimum available height that we can consume for rendering the prompt. This is the available space below te cursor. For vt100: Do CPR request. (answer will arrive later.) For win32: Do API call. (Answer comes immediately.) rNr:r=cjtjdSr)rappendrr( ask_for_cprrsrAdo_cprz9Renderer.request_absolute_cursor_position..do_cprs8  ) 0 0 : : : K # # % % % % %rCcKtjd{Vjtjkr.tj_jrdSdSdSr)r CPR_TIMEOUTrrrrrrsrAtimerz8Renderer.request_absolute_cursor_position..timers}()) ) ) ) ) ) ) );#666#.#< 263355555 7666rCrh)rrKr2r(rrjrrrrrrrwaiting_for_cprrcreate_background_task)rrrs` rA request_absolute_cursor_positionz)Renderer.request_absolute_cursor_positionsA!Q&&&&   )-)=)=)?)?)DD & F )-)S)S)U)UD & F"    D   {8 8 8 F & & & & & &  {4 4 4 FHHH F    F 6 6 6 6 6 6  ((11111sA!! A.-A.rZctj|_|jj}||z dz}||_ |j}| ddS#t$rYdSwxYw)z To be called when we know the absolute cursor position. (As an answer of a "Cursor Position Request" response.) rGN) rrrr(rrjrrpopleft set_result IndexError)rrZrrows_below_cursorfs rAreport_absolute_cursor_rowz#Renderer.report_absolute_cursor_rows '0[))++0 &,q0&7" -5577A LL         DD sA00 A>=A>c*t|jS)zi Waiting for CPR flag. True when we send the request, but didn't got a response. )r1rrs rArzRenderer.waiting_for_cpr(s D1222rCrGtimeoutcRKtjrjtjkrdSdfd }dfd }t |t |h}t |td{V\}}|D]}|dS)z* Wait for a CPR response. Nr:r=c&KD] }|d{V dSrr?) response_f cpr_futuress rAwait_for_responsesz;Renderer.wait_for_cpr_responses..wait_for_responses:s;) ! !           ! !rCcKtd{VD]}|t_dSr)rcancelr r)rrrrs rAwait_for_timeoutz9Renderer.wait_for_cpr_responses..wait_for_timeout>s].. * $ $ !!####,1GGD ) ) )rC) return_whenrh) listrrrrrrrr) rrrrtasks_pendingtaskrs `` @rAwait_for_cpr_responseszRenderer.wait_for_cpr_responses0s4899  d.+2KKK4 ! ! ! ! ! ! 4 4 4 4 4 4 4 4 ,,.. / / **,, - -  ?CCCCCCCCC 7  D KKMMMM  rCr&r'rzr#r0cV|j}|jr"|jsd|_||js |jd|_|js |jd|_|}|r#|j s| d|_ n$|s"|j r| d|_ | }t}d|_t}|jr|j} n|r+|j|j|jj} nS|jr |jjnd} t/|j| |j|j|jj} t3| |j} |j|krd|_|j|jks2|j|jks|j |j!krd|_d|_"d|_#|j"$tI|jj%|j|_"|j#tM|j"|_#|j|_|j|_|j |_!|j'||tQdd|j| ddd|)|j*r|+|j*tY||||j-|j |j|j.||j|j"|j#||jr |jjnd \|_-|_.||_||_||_/|j01|} |j2 |j2| kr|3| | |_2|4|j5|j6_5|r|7dSdS) z Render the current interface to the output. :param is_done: When True, put the cursor at the end of the interface. We won't print any changes to this part. TFrN)xposyposrQrt) parent_styleerase_bgz_index)r2r3r5r7r8)8r(r2renter_alternate_screenrenable_bracketed_pasterreset_cursor_key_moderrenable_mouse_supportrrrr}rrj containerpreferred_heightri preferredrrtrerrsrrVinvalidation_hashrrrr+rrrr4rr6write_to_screenrdraw_all_floats exit_styleappend_style_to_contentrrrrcursorget_cursor_shaperset_cursor_shapervisible_windowsrzr) rr&rzr0r(needs_mouse_supportr7r)rrt last_heightnew_cursor_shapes rArenderzRenderer.renderNsd   ,D$= ,(,D %  ) ) + + +, 1 K . . 0 0 0,0D )* / K - - / / /*.D '#0022  0t'B 0  ' ' ) ) )*.D ' '$ 0)D 0  ( ( * * **/D '  "&   YFF  %66 di F7;6GN$+22QK* 11$, JJTF VTY'' ?d " " $D  J ( ( * *d.C C C'99;;-..$"888 $D $(D !+/D (  ($< 2C4L%%D !  ' /+D%,,D (!% < < > >),)A)S)S)U)U&!$((   qq V L L L )        > ;  * *3> : : :.A      O     (#'#8#'#?7;MDO33A. . . *$*#,:66s;;  # +&*:::  # #$4 5 5 5&6D #  &,%; "   JJLLLLL  rCc\|j}||jj||jj|||| | |dS)aR Hide all output and put the cursor back at the first line. This is for instance used for running a system command (while hiding the CLI) and later resuming the same CLI.) :param leave_alternate_screen: When True, and when inside an alternate screen buffer, quit the alternate screen. )rN) r(rorrJrnrKrrrBr|rr)rrr(s rAerasezRenderer.erasest/1222)+,,,!!!     *@ AAAAArCc||j}||dd||dS)z, Clear screen and go to 0,0 rN)rr( erase_screen cursor_gotorr)rr(s rAclearzRenderer.clearsg 1a     --/////rC)FFN) rVrr(rr2r1rrrrr:r=)FT)rr1rr1r:r=)r:r-)r:r1)r:r9rh)rZr9r:r=)rG)rr9r:r=)F)r&r'rzr#r0r1r:r=)T)rr1r:r=)rrrrrrrpropertyrrrrrrrrrr"r?rCrAr$r$IsK "&+@D &!&!&!&!&!P/////b!!!X!   X  H H HX H;2;2;2;2z.333X3>FKNNNNN`BBBBB*000000rCNformatted_textrrVrrStyleTransformation | NoneColorDepth | Noner=c~t|}|p t}|p|}||d}t |j|}|D]^}} } ||} | |kr-| r|| |n|| }d|vr|| \| dd} | dd} | | || dS)zT Print a list of (style_str, text) tuples in the given style to the output. Nz[ZeroWidthEscape]rHr rF) rr get_default_color_depthrBr|r4rrlrkreplacerRr) r(r$rVrr+ fragments last_attrsr3rtextrrs rAr%r%sn".11I/M3K3M3MA!?!?!A!AK  #J6 %'; ) 4!&y1 J   *%%e[9999'')))  ) + +   T " " " "<<b))D<<f--D LL      LLNNNNNrC)r&r'r(rr)rr*rr+rr,r-r.r/r0r1r2r1r3r4r5r6r7rr8r9r:r;)NN) r(rr$rrVrrr%r+r&r:r=)=r __future__rasynciorrrrr collectionsr enumr typingr r r rr"prompt_toolkit.application.currentrprompt_toolkit.cursor_shapesrprompt_toolkit.data_structuresrrprompt_toolkit.filtersrrprompt_toolkit.formatted_textrr$prompt_toolkit.layout.mouse_handlersrprompt_toolkit.layout.screenrrrprompt_toolkit.outputrrprompt_toolkit.stylesrrr r!prompt_toolkit.applicationr"prompt_toolkit.layout.layoutr#__all__r Exceptionrrr4r1r6rr$r%r?rCrAr@s #"""""GGGGGGGGGGGGGG??????????????66666644444466666666::::::::MMMMMMMM>>>>>>DDDDDDDDDD444444444666666333333  `#`#`#`#FEEEEE9EEEtCJ/,S$Y:$x0x0x0x0x0x0x0x0~ 8<%) 0000000rC