)L irdZddlmZddlmZmZmZmZmZddl 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/ddl0m1Z1ddgZ2 d dZ3Gdde4Z5Gddee6e*fZ7Gddee6e8fZ9Gdde Z:GddZ; d! d"dZ}|j$|}|j$|}|j&|}t$dz ||}t$dz ||}d}||kr||}||}|j(xsd}|j*|j*k7s|j,|j,k7rN|t||||vr |||||tj.|zj0||z }||kr|s ||ks|t|dz|#|j3A||jkDr|td|dz |r$|td||jn+||j5|j6j8|s|s|j;#|j<r|j=fS) a Render the diff between this screen and the previous screen. This takes two `Screen` instances. The one that represents the output like it was during the last rendering and one that represents the current output raster. Looking at these two `Screen` instances, this function will render the difference by calling the appropriate methods of the `Output` object that only paint the changes to the terminal. This is some performance-critical code which is heavily optimized. Don't change things without profiling first. :param current_pos: Current cursor position. :param last_style: The style string, used for drawing the last drawn character. (Color/attributes.) :param attrs_for_style_string: :class:`._StyleStringToAttrsCache` instance. :param width: The width of the terminal. :param previous_width: The width of the terminal during the last rendering. cdy)z'Wrapper around Output.reset_attributes.N)_output_reset_attributes last_styles]/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/prompt_toolkit/renderer.pyreset_attributesz-_output_screen_diff..reset_attributesZs !" cjj}}|j|kDr5 d|j|z zd}|j|S|j|kr||jz |dz k\r d|j|S|j|ks|dz k\r||jz |S|j|kDr|j|z |S)z9Move cursor to this `new` point. Returns the given Point. r xy) new current_x current_y_output_cursor_backward_output_cursor_forward_output_cursor_up current_posr.widthwrites r- move_cursorz(_output_screen_diff..move_cursor`s*}}kmm9 559    &CEEI-. /I "355 )J UUY  i#%%/ 0  ! $K "355 )  UUY )uqy"8 #I$5 6 UUY  "3559#4 5 r/c|jk(r|jy|j}r|k7r ||j|jy)z5 Write the output of this character. N)stylechar)rC new_attrs_output_set_attributesattrs_for_style_string color_depthr,r?s r- output_charz(_output_screen_diff..output_char{s\  # $))  /tzz:I.DZ.P!P&y+> $)) Jr/cPfd|jD}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). c3dK|]'\}}|jdk7s|jr|)yw) N)rCrB).0indexcellstyle_string_has_styles r- zD_output_screen_diff..get_max_column_index..s5 tyyC#9$**#E  s-0r)default)itemsmax)rownumbersrOs r-get_max_column_indexz1_output_screen_diff..get_max_column_indexs' "yy{  7A&&r/rr4r2returnNone)r7rrXr)rCrrXrY)rTzdict[int, Char]rXint)columnsrowsr? write_rawset_attributesr.cursor_forward cursor_upcursor_backward hide_cursordisable_autowrapr erase_downrminheightrSrange data_bufferzero_width_escapesr>rCrBr5r6erase_end_of_lineget_cursor_positionlayoutcurrent_windowenable_autowrap show_cursor)&appoutputscreenr=rGprevious_screenr,is_done full_screenrFrOsizeprevious_widthrfr]r@rHrVcurrent_height row_countr6new_row previous_rowzero_width_escapes_rownew_max_line_lenprevious_max_line_lencnew_charold_char char_widthr:r;r<r+rEr.r>r?s& `` ` `` @@@@@@@@r-_output_screen_diffr'sDLL$))ME6 LLE  I$22%66#22(($44  6$$,'*  +! .E*A!%!q/2  ( /NC '='=>GI 9 "'$$Q'&2215 !'!:!:1!=uqy*>w*GH #EAI/CL/Q R ##qzH#AH!,1J }} -8>>1Q)%!q/: ..4Q78H%#kmmj&@KMMR OA%##* /2GG%e.>.Ba&HIK    $ $ &E"'\...!%!~/A"BC !%!~">? !&"<"Information unavailable. Did not yet receive the CPR response.N)__name__ __module__ __qualname____doc__r*r/r-rr sDr/rc,eZdZdZ ddZddZy)r&zk A cache structure that maps style strings to :class:`.Attr`. (This is an important speed up.) c ||_||_yN)get_attrs_for_style_strstyle_transformation)selfrrs r-__init__z!_StyleStringToAttrsCache.__init__s (?$$8!r/ch|j|}|jj|}|||<|Sr)rrtransform_attrs)r style_strattrss r- __missing__z$_StyleStringToAttrsCache.__missing__s6,,Y7))99%@Y r/N)rzCallable[[str], Attrs]rr!rXrY)rstrrXrrrrrrrr*r/r-r&r&s- 9!7929  9r/c eZdZdZddZddZy)r'a 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. c||_yr)style_string_to_attrs)rrs r-rz"_StyleStringHasStyleCache.__init__/s %:"r/c|j|}t|jxsD|jxs6|jxs(|j xs|j xs |j}|||<|Sr)rboolcolorbgcolor underlinestrikeblinkreverse)rrr is_defaults r-rz%_StyleStringHasStyleCache.__missing__2sv**95 KK }}  || {{   }}  %Yr/N)rzdict[str, Attrs]rXrY)rrrXrrr*r/r-r'r'$s; r/ceZdZdZdZdZdZy) CPR_Supportz&Enum: whether or not CPR is supported. SUPPORTED NOT_SUPPORTEDUNKNOWNN)rrrrrrrr*r/r-rrAs,I#MGr/rceZdZdZdZ d ddZdddZeddZeddZ eddZ dd Z dd Z edd Z ddd Z d dd ZdddZddZy)r$z Typical usage: :: output = Vt100_Output.from_pty(sys.stdout) r = Renderer(style, output) r.render(app, layout=...) Nc||_||_||_t||_||_d|_d|_d|_d|_ t|_ tj|_|jstj |_d|_d|_d|_d|_d|_|j-dy)NFT)_scroll)rBrqrur mouse_supportcpr_not_supported_callback_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)rrBrqrurrs r-rzRenderer.__init__Vs  &&}5*D'%*!&+#(-%&+#>CW%&..%%*88D BFIM$15:>&48 4  r/crtdd|_d|_d|_d|_d|_t |_d|_|r|jj|jr#|r!|jjd|_ |jr!|jjd|_ |jr!|jj!d|_|jj#|jj%|jj'y)Nrr4F)r _cursor_pos _last_screen _last_size _last_style_last_cursor_shapermouse_handlers_min_available_heightrqscroll_buffer_to_promptrquit_alternate_screenrdisable_mouse_supportrdisable_bracketed_pastereset_cursor_shaperoflush)rrleave_alternate_screens r-rzRenderer.reset~s 1? ,0'+'+6:,o&'"  KK / / 1  $ $)? KK - - /(-D %  & & KK - - /*/D '  ( ( KK / / 1,1D ) &&( ! r/c|jS)zn The `Screen` class that was generated during the last rendering. This can be `None`. )rrs r-last_rendered_screenzRenderer.last_rendered_screens    r/c|js|jdkDry |jj|_y#t$rYywxYw)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)rurrqget_rows_below_cursor_positionNotImplementedErrorrs r-height_is_knownzRenderer.height_is_knownsL   t99A= )-)S)S)UD &"  s> A  A c|jry|jdkDra|jjj}|j r|j j nd}|t|j|z Std)zU Return the number of rows visible in the terminal above the layout. rzRows above layout is unknown.) rrrqget_sizer\rrfrSr)r total_rowslast_screen_heights r-rows_above_layoutzRenderer.rows_above_layoutsx  $ $  ' '! +--/44J=A=N=N!2!2!9!9TU D$>$>@R SS S&'FG Gr/c jjdk(sJjr*jj j _y jj_y#t$rYnwxYwjtjk(rydfd }jtjk(r|yjry|dfd }tj|y)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.) rNcjjtjj yr)rappendrrq ask_for_cprrsr-do_cprz9Renderer.request_absolute_cursor_position..do_cprs)  ) ) 0 0 : KK # # %r/cKtjd{jtjk(r3tj _j rj yyy7Uwr)r CPR_TIMEOUTrrrrrrsr-timerz8Renderer.request_absolute_cursor_position..timersc(() ) );#6#66#.#<#< 2233537 *sA4A2AA4rW)rr6rurqrr\rrrrrrrwaiting_for_cprrcreate_background_task)rrrs` r- request_absolute_cursor_positionz)Renderer.request_absolute_cursor_positions!!Q&&&   )-)=)=)?)D)DD &  )-)S)S)UD & "      {88 8  &   {44 4 H      6  ((1sA44 B?Bc tj|_|jj j }||z dz}||_ |jj}|jdy#t$rYywxYw)z To be called when we know the absolute cursor position. (As an answer of a "Cursor Position Request" response.) r2N) rrrrqrr\rrpopleft set_result IndexError)rrTrrows_below_cursorfs r-report_absolute_cursor_rowz#Renderer.report_absolute_cursor_rows '00[[))+00 &,q0&7" --557A LL    s A66 BBc,t|jS)zi Waiting for CPR flag. True when we send the request, but didn't got a response. )rrrs r-rzRenderer.waiting_for_cpr(s D1122r/cBKtjrjtjk(rydfd }dfd }t |t |h}t |td{\}}|D]}|jy7w)z* Wait for a CPR response. Nc4KD] }|d{y7wrr*) response_f cpr_futuress r-wait_for_responsesz;Renderer.wait_for_cpr_responses..wait_for_responses:s") !     ! s cKtd{D]}|jt_y7+wr)rcancelr r)rrrtimeouts r-wait_for_timeoutz9Renderer.wait_for_cpr_responses..wait_for_timeout>s@. * $ !!# $,1GD ) !sA>,A) return_whenrW) listrrrrrrrr) rrrrtasks_pendingtaskrs `` @r-wait_for_cpr_responseszRenderer.wait_for_cpr_responses0s4889 d..+2K2KK ! 4 ,. / *, -  ?CC 7 D KKM DsA8B=B> Bc |j}|jr#|jsd|_|j|js!|jj d|_|j s!|jjd|_|j}|r$|js|jd|_ n%|s#|jr|jd|_ |j}t}d|_t}|jr |j } n|r;|j"j%|j&|j j(} ns|j*r|j*j,nd} t/|j0| |j"j%|j&|j j(} t3| |j } |j4|k7rd|_|j6j9|j:k7s@|j<j9|j>k7s|j@|jBk7rd|_d|_"d|_#|jD/tI|j6jJ|j<|_"|jFtM|jD|_#|j6j9|_|j<j9|_|j@|_!|j"jO||tQdd|j&| ddd|jS|jTr|jW|jTtY||||jZ|j@|j*|j\||j|jD|jF||j4r|j4j&nd \|_-|_.||_||_||_/|j`jc|} |jd|jd| k7r|jg| | |_2|ji|jj|jl_5|r|joyy) 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)xposyposr>rf) parent_styleerase_bgz_index)rurFrOrvrw)8rqrurenter_alternate_screenrenable_bracketed_pasterreset_cursor_key_moderrenable_mouse_supportrrrrorr\ containerpreferred_heightr[ preferredrrfrSrrerrBinvalidation_hashrrrrGrrrr&rr'write_to_screenrdraw_all_floats exit_styleappend_style_to_contentrrrrcursorget_cursor_shaperset_cursor_shapervisible_windowsrlr) rrprlrtrqneeds_mouse_supportrvrrrrf last_heightnew_cursor_shapes r-renderzRenderer.renderNs   D$=$=(,D %  ) ) +,, KK . . 0,0D )** KK - - /*.D '#002 t'B'B  ' ' )*.D '$)D)D  ( ( **/D ' "&   YYF %%66 diii 7;6G6G$++22QK**  11$,, JTTF VTYY' ??d " $D  JJ ( ( *d.C.C C''99;--.$"8"88 $D $(D !+/D (  ($< 22C4L4L%D !  ' ' /+D%%,D (!% < < >),)A)A)S)S)U&!$((   qq V L )    >>  * *3>> :.A       OO       ((#'#8#8#'#?#?7;DOO33A. *$*#,::66s;  # # +&&*::  # #$4 5&6D #  &,%;%; "  JJL r/cT|j}|j|jj|j |jj |j |j|j|j|j|y)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) rqrarr5r`r6rdr.rnrr)rrrqs r-erasezRenderer.erases}t//112))++,!   *@ Ar/c|j|j}|j|jdd|j |j y)z, Clear screen and go to 0,0 rN)rrq erase_screen cursor_gotorr)rrqs r-clearzRenderer.clearsI 1a   --/r/)FFN) rBrrqrrurrrrzCallable[[], None] | NonerXrY)FT)rrrrrXrY)rX Screen | None)rXr)rXrZrW)rTrZrXrY)r2)rrZrXrY)F)rpApplication[Any]rlr#rtrrXrY)T)rrrXrY)rrrrrrrpropertyrrrrrrrrrrr*r/r-r$r$IsK "&+@D &!&!&! &! $ &! %> &! &!P/b!!   H H;2z.33>FKN#N-3N>BN N`B*0r/Nct|}|xs t}|xs|j}|j|j d}t |j |}|D]^}} } ||} | |k7r%| r|j| |n|j| }d|vr|j| M| jdd} | jdd} |j| |j|jy)zT Print a list of (style_str, text) tuples in the given style to the output. Nz[ZeroWidthEscape]r3r r1) rr get_default_color_depthr.rnr&rr^r]replacer?r) rqformatted_textrBrrG fragments last_attrsrFrtextrrs r-r%r%s".1I/M3K3MA!?!?!AK  #J6 %%'; ) 4!&y1 J %%e[9'')  ) +   T "<<b)D<<f-D LL ).  LLNr/)rprrqrrrrr=rrGrrsrr,z str | NonertrrurrFr&rOr'rvrrwrZrXztuple[Point, str | None])NN) rqrr"rrBrrzStyleTransformation | NonerGzColorDepth | NonerXrY)=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 Exceptionrrr&rr'rr$r%r*r/r-r8s #GG??646:M>DD463  `# `# `# `# `#  `# # `#`#`#`#5`#6`# `#`#`#FE9EtCJ/,S$Y:$x0x0~ 8<%) 0 0$0 05 0 # 0  0r/