)L idZddlmZddlZddlmZmZddlmZm Z m Z m Z ddl m Z ddlmZddlmZdd lmZmZmZdd lmZmZmZdd lmZmZdd lmZdd lm Z m!Z!ddl"m#Z#erddl$m%Z%m&Z&gdZ'GddeZ(e e)ge)fZ*e e)ge)fZ+GddZ,GddZ-Gdde(Z.Gdde(Z/Gdde/Z0Gdd e(Z1Gd!d"e(Z2Gd#d$e(Z3Gd%d&e(Z4Gd'd(e(Z5Gd)d*e5Z6Gd+d,e(Z7Gd-d.e(Z8Gd/d0e(Z9Gd1d2e(Z:Gd3d4e(Z;Gd5d6e(Z<Gd7d8e(Z=Gd9d:e(Z>d>d;Z?Gd<d=e(Z@y)?z Processors are little transformation blocks that transform the fragments list from a buffer before the BufferControl will render it to the screen. They can insert fragments before or after, or highlight fragments by replacing the fragment types. ) annotationsN)ABCMetaabstractmethod) TYPE_CHECKINGCallableHashablecast)get_app) SimpleCache)Document) FilterOrBool to_filtervi_insert_multiple_mode)AnyFormattedTextStyleAndTextTuplesto_formatted_text)fragment_list_lenfragment_list_to_text)SearchDirection)to_intto_str)explode_text_fragments) BufferControl UIContent) ProcessorTransformationInputTransformationDummyProcessorHighlightSearchProcessor#HighlightIncrementalSearchProcessorHighlightSelectionProcessorPasswordProcessor!HighlightMatchingBracketProcessorDisplayMultipleCursors BeforeInputShowArg AfterInputAppendAutoSuggestionConditionalProcessorShowLeadingWhiteSpaceProcessorShowTrailingWhiteSpaceProcessor TabsProcessorReverseSearchProcessorDynamicProcessormerge_processorsc*eZdZdZe ddZy)rzt Manipulate the fragments for a given line in a :class:`~prompt_toolkit.layout.controls.BufferControl`. c,t|jS)z Apply transformation. Returns a :class:`.Transformation` instance. :param transformation_input: :class:`.TransformationInput` object. r fragmentsselftransformation_inputs f/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/prompt_toolkit/layout/processors.pyapply_transformationzProcessor.apply_transformation@s2<<==Nr7rreturnr)__name__ __module__ __qualname____doc__rr9r:r8rr:s+ >$7> >>r:r) metaclasscJeZdZdZ d ddZ ddZy)ra :param buffer_control: :class:`.BufferControl` instance. :param lineno: The number of the line to which we apply the processor. :param source_to_display: A function that returns the position in the `fragments` for any position in the source string. (This takes previous processors into account.) :param fragments: List of fragments that we can transform. (Received from the previous processor.) :param get_line: Optional ; a callable that returns the fragments of another line in the current buffer; This can be used to create processors capable of affecting transforms across multiple lines. Nc t||_||_||_||_||_||_||_||_yN)buffer_controldocumentlinenosource_to_displayr4widthheightget_line) r6rFrGrHrIr4rJrKrLs r8__init__zTransformationInput.__init__^s>-   !2"    r:c|j|j|j|j|j|j |j fSrE)rFrGrHrIr4rJrKr6s r8unpackzTransformationInput.unpackrsC    MM KK  " " NN JJ KK  r:rE)rFrrGr rHintrISourceToDisplayr4rrJrQrKrQrLz*Callable[[int], StyleAndTextTuples] | Noner<None)r<zRtuple[BufferControl, Document, int, SourceToDisplay, StyleAndTextTuples, int, int])r=r>r?r@rMrPrAr:r8rrPsz ,@D!%!! ! + ! & !!!=! !(     r:rc,eZdZdZ d ddZy)ra) Transformation result, as returned by :meth:`.Processor.apply_transformation`. Important: Always make sure that the length of `document.text` is equal to the length of all the text in `fragments`! :param fragments: The transformed fragments. To be displayed, or to pass to the next processor. :param source_to_display: Cursor position transformation from original string to transformed string. :param display_to_source: Cursor position transformed from source string to original string. NcB||_|xsd|_|xsd|_y)Nc|SrErAis r8z)Transformation.__init__..r:c|SrErArWs r8rYz)Transformation.__init__..rZr:)r4rIdisplay_to_source)r6r4rIr\s r8rMzTransformation.__init__s% #!2!C{!2!C{r:)NN)r4rrIzSourceToDisplay | Noner\zDisplayToSource | Noner<rS)r=r>r?r@rMrAr:r8rrs? "5948 D%D2D2 D  Dr:rc eZdZdZ ddZy)rz1 A `Processor` that doesn't do anything. c,t|jSrEr3r5s r8r9z#DummyProcessor.apply_transformations2<<==r:Nr;r=r>r?r@r9rAr:r8rrs>$7> >r:rc0eZdZdZdZdZddZ ddZy) r z Processor that highlights search matches in the document. Note that this doesn't support multiline search matches yet. The style classes 'search' and 'search.current' will be applied to the content. searchzsearch.currentc.|jjS)0 The text we are searching for. ) search_statetext)r6rFs r8_get_search_textz)HighlightSearchProcessor._get_search_texts**///r:c@|j\}}}}}}}|j|}d|jd} d|jd} |rHt j s3t |} t|}|jjrtj} ntjd} |j|k(r||j} nd} tjtj || | D]}| -|j#| cxkxr|j%knc}nd}t'|j#|j%D]-}||^}}}|r|| z||df||<|| z||df||</t)|S)Nz class: r)flagsFr)rPrf _classname_classname_currentr is_donerrrd ignore_casere IGNORECASE RegexFlagcursor_position_rowcursor_position_colfinditerescapestartendranger)r6r7rFrGrHrIr4_ search_textsearchmatch_fragmentsearchmatch_current_fragment line_textri cursor_columnmatch on_cursorrX old_fragmentres r8r9z-HighlightSearchProcessor.apply_transformations ! ' ' )      ++N; !((9;)01H1H0I'K$ wy00-i8I.y9I**668  Q++v5 1(2N2N O $ RYY{%;YeT  , %  L LI %Iu{{}eiik: A-6q\*L$ (+GG%aLO( ! )+??%aLO( !    &i((r:NrFrr<strr;)r=r>r?r@rjrkrfr9rAr:r8r r s.J)0 5)$75) 5)r:r c eZdZdZdZdZddZy)r!ai Highlight the search terms that are used for highlighting the incremental search. The style class 'incsearch' will be applied to the content. Important: this requires the `preview_search=True` flag to be set for the `BufferControl`. Otherwise, the cursor position won't be set to the search match while searching, and nothing happens. incsearchzincsearch.currentcP|j}||jr |jSy)rc) search_bufferre)r6rFrs r8rfz4HighlightIncrementalSearchProcessor._get_search_texts, '44  $);); %% %r:Nr)r=r>r?r@rjrkrfrAr:r8r!r!sJ,r:r!c eZdZdZ ddZy)r"zB Processor that highlights the selection in the document. c|j\}}}}}}}d}|j|} | r| \} } || } || } t|}| dk(r!| dk(rt|dk(rt |dfgSt | | D]E} | t|kr|| ^} }}| |z|f|| <$| t|k(s3|j |dfGt |S)Nz class:selected rrh)rPselection_range_at_linerlenrrwappend)r6r7rFrGrHrIr4rxselected_fragmentselection_at_linefrom_torXrold_texts r8r9z0HighlightSelectionProcessor.apply_transformations ! ' ' )      /%<)5>q\2 h(47H(H('S ! c)n,!((*;S)AB Ci((r:Nr;r_rAr:r8r"r" s%)$7%) %)r:r"c"eZdZdZdddZddZy)r#z{ Processor that masks the input. (For passwords.) :param char: (string) Character to be used. "*" by default. c||_yrE)char)r6rs r8rMzPasswordProcessor.__init__=s  r:c tt|jDcgc]"^}}}||jt |zg|$c}}}}t |Scc}}}wrE)r rr4rrr)r6tistylerehandlerr4s r8r9z&PasswordProcessor.apply_transformation@s_(, .0\\  )E4' CI-88 ) i((  s'AN)*)rrr<rSrrr<rr=r>r?r@rMr9rAr:r8r#r#6s  )r:r#cBeZdZdZdZ d ddZd dZ d dZy) r$a When the cursor is on or right after a bracket, it highlights the matching bracket. :param max_cursor_distance: Only highlight matching brackets when the cursor is within this distance. (From inside a `Processor`, we can't know which lines will be visible on the screen. But we also don't want to scan the whole document for matching brackets on each key press, so we limit to this value.) z])}>cB||_||_td|_y)N)maxsize)charsmax_cursor_distancer _positions_cache)r6rrs r8rMz*HighlightMatchingBracketProcessor.__init__Zs% #6   " r:c|jrZ|j|jvrB|j|j|jz |j|jz}n|j r|j |j vr}|j |jvret|j|jdz }|j|j|jz |j|jz}nd}|r?||jz }|j|\}}||f|j|jfgSgS)zQ Return a list of (row, col) tuples that need to be highlighted. ) start_posend_posrN) current_charrfind_matching_bracket_positioncursor_positionrchar_before_cursor_closing_bracesr retranslate_index_to_positionrqrr)r6rGposrowcols r8_get_positions_to_highlightz=HighlightMatchingBracketProcessor._get_positions_to_highlightds@  X%:%:djj%H99"22T5M5MM 0043K3KK:C  ' '++t/C/CC++tzz9 x/G/G!/KLH99"22T5M5MM 0043K3KK:C C  8++ +C;;C@HCc --x/K/KL  Ir:c |j\} }}}}}tjr t|Stj j j f}jj| fd}|rL|D]G\} } | |k(s || } t|}|| ^} } }| jk(r| dz } n| dz } | | f|| <It|S)Nc&jSrE)r)rGr6sr8rYzHHighlightMatchingBracketProcessor.apply_transformation..s99(Cr:z class:matching-bracket.cursor z class:matching-bracket.other ) rPr rlrrender_counterrerrgetrrr)r6r7rFrHrIr4rxkey positionsrrrrerGs` @r8r9z6HighlightMatchingBracketProcessor.apply_transformations ! ' ' )       9  !), ,y''8P8PQ))-- C % 3S&=+C0C 6y AI&/nOE4!h:::!BB!AA&+T]IcN 3i((r:N)z[](){}<>i)rrrrQr<rS)rGr r<zlist[tuple[int, int]]r;)r=r>r?r@rrMrr9rAr:r8r$r$LsM OCG  . !n*<r:c|z SrErArs r8rYz2BeforeInput.apply_transformation..rr:rIr\)rHrrerr4rr)r6rfragments_beforer4rIr\rs @r8r9z BeforeInput.apply_transformationsu 99>0DJJG (2<<7I./?@N <  <  I $  $  //  r:c<d|jd|jdS)Nz BeforeInput(z, )rrOs r8__repr__zBeforeInput.__repr__ sdii]"TZZN!<r?r@rMr9rrAr:r8r&r&s .=r:r&c4eZdZdZdfd ZddZddZxZS)r'z Display the 'arg' in front of the input. This was used by the `PromptSession`, but now it uses the `Window.get_line_prefix` function instead. c8t||jyrE)superrM_get_text_fragments)r6 __class__s r8rMzShowArg.__init__s 112r:ct}|jjgS|jj}ddt|fdgS)N)class:prompt.argz(arg: zclass:prompt.arg.text)rz) )r key_processorargr)r6apprs r8rzShowArg._get_text_fragmentssOi    (I##''C/(#c(3* r:cy)Nz ShowArg()rArOs r8rzShowArg.__repr__%sr:)r<rS)r<rr)r=r>r?r@rMrr __classcell__)rs@r8r'r' s3 r:r'c*eZdZdZdddZddZd dZy) r(z Insert text after the input. :param text: This can be either plain text or formatted text (or a callable that returns any of those). :param style: style to be applied to this prompt/prefix. c ||_||_yrErrs r8rMzAfterInput.__init__2rr:c|j|jjdz k(r9t|j|j }t |j|zSt |jS)Nrr4)rHrG line_countrrerrr4)r6rfragments_afters r8r9zAfterInput.apply_transformation6sT 99 ..2 2/ 4::FO!BLL?,JK K!BLL9 9r:ch|jjd|jd|jdS)N(z, style=r)rr=rerrOs r8rzAfterInput.__repr__?s...))*!DII=aPPr:NrrrrrrAr:r8r(r()s:Qr:r(c"eZdZdZdddZddZy)r)z{ Append the auto suggestion to the input. (The user can then press the right arrow the insert the suggestion.) c||_yrE)r)r6rs r8rMzAppendAutoSuggestion.__init__Is  r:ch|j|jjdz k(rw|jj}|j r-|jj r|j j}nd}t|j|j|fgzSt|jS)Nrrr) rHrGrrFr suggestionis_cursor_at_the_endrerr4r)r6rrrs r8r9z)AppendAutoSuggestion.apply_transformationLs 99 ..2 2&&--F  R[[%E%E#..33  !BLLTZZ.default_get_charg,}}WY--668)DLr:rrget_charr6rrrs r8rMz'ShowLeadingWhiteSpaceProcessor.__init__b     4$4 r:c |j}|rvt|jdr\|j|j f}t |}t t|D]}||ddk(r|||<t|St|S)Nrhr) r4r startswithrrrrwrr)r6rr4trXs r8r9z3ShowLeadingWhiteSpaceProcessor.apply_transformationpsLL  .y9DDSIT]]_-A.y9I3y>* Q<?c)#$IaLi((   i((r:)Nzclass:leading-whitespacerzCallable[[], str] | Nonerrr<rSrrrAr:r8r+r+[s5.2/ 5* 5 5  5)r:r+c0eZdZdZ d ddZddZy)r,zf Make trailing whitespace visible. :param get_char: Callable that returns one character. Nc0dd}||_|xs||_y)Ncpdjtjjddk(ryyrrrAr:r8rzBShowTrailingWhiteSpaceProcessor.__init__..default_get_charrr:rrrs r8rMz(ShowTrailingWhiteSpaceProcessor.__init__rr:c(|j}|rz|ddjdrc|j|jf}t |}t t |dz ddD] }||d}|dk(r|||<t|St|S)Nrrh)r4endswithrrrrwrr)r6rr4rrXrs r8r9z4ShowTrailingWhiteSpaceProcessor.apply_transformationsLL 2q)2237T]]_-A.y9I3y>A-r26  |A3;#$IaLi(( i((r:)Nzclass:training-whitespacerrrrAr:r8r,r,s5.20 5* 5 5  5)r:r,c<eZdZdZ d ddZddZy)r-a Render tabs as spaces (instead of ^I) or make them visible (for instance, by replacing them with dots.) :param tabstop: Horizontal space taken by a tab. (`int` or callable that returns an `int`). :param char1: Character or callable that returns a character (text of length one). This one is used for the first space taken by the tab. :param char2: Like `char1`, but for the rest of the space. c<||_||_||_||_yrE)char1char2tabstopr)r6rr rrs r8rMzTabsProcessor.__init__s     r:c6t|j}|j}t|j}t|j }t |j}ig}d}t|D]i\} } || <| ddk(rA|||zz } | dk(r|} |j||f|j||| dz zf|| z }T|j| |dz }k|t|<|dzt|dz<dfd } dfd } t|| | S) Nrr c|S)z-Maps original cursor position to the new one.rA) from_positionposition_mappingss r8rIz=TabsProcessor.apply_transformation..source_to_displays$]3 3r:cjDcic]\}}|| }}}|dk\r ||Sycc}}w#t$r|dz}YnwxYw|dk\r'")z1Maps display cursor position to the original one.rr)itemsKeyError) display_poskvposition_mappings_reversedrs r8r\z=TabsProcessor.apply_transformation..display_to_sourcesq;L;R;R;T)U41a!Q$)U &)U"%5kBB*V  %1$K%"s 17AAr)rrQr<rQ)rrQr<rQ) rrrrr rrr4 enumeraterrr)r6rrr separator1 separator2r4result_fragmentsrrXfragment_and_textcountrIr\rs @r8r9z"TabsProcessor.apply_transformationsD& DJJ' DJJ' +2<<8 /1$-i$8  A #& a  #t+3=1A:#E!'' (;< '' eai0H(IJu  ''(9:q "-0#i.)14a#i.1,- 4  //  r:N)|u┈z class:tab) rzint | Callable[[], int]r str | Callable[[], str]rr$rrr<rSrrrAr:r8r-r-sO ,-),)1 ( ' '     8 r:r-cLeZdZUdZeeeegZde d<ddZ d dZ d dZ y) r.z Process to display the "(reverse-i-search)`...`:..." stuff around the search buffer. Note: This processor is meant to be applied to the BufferControl that contains the search buffer, it's not meant for the original input. zlist[type[Processor]]_excluded_input_processorscddlm}tjj}t ||r|j |k(r|Sy)Nrr)prompt_toolkit.layout.controlsrr layoutsearch_target_buffer_control isinstancesearch_buffer_control)r6rFr prev_controls r8_get_main_bufferz'ReverseSearchProcessor._get_main_buffer s9@y''DD |] 322nD r:c ddlm}t|j d fd  t |j xsg}t }|r||g}n|g}ddlm}t|j|sJ||j|d|jd|j}|j|j|jd S) Nrr(cBt|tr;|jDcgc] }| }}t|Dcgc]}|| c}St|tr+|j }|rt ||j Syt|s|Sycc}wcc}w)zFilter processors from the main control that we want to disable here. This returns either an accepted processor or None.N)r,_MergedProcessor processorsr0r* processorfilter)itemraccepted_processorsexcluded_processorsfilter_processors r8r9z9ReverseSearchProcessor._content..filter_processors$ 01DHOO&Tq'7':&T#&T' 3E1q}QE D"67$T^^4/4;;??"$(;<K!'UEsBBBrSearchBufferControlFT)rinput_processors include_default_input_processorslexerpreview_searchr-)r?)r6rr<zProcessor | None)r)rtupler&r0r<r!controlsr;r,rFrr>create_contentrJrK) r6 main_controlrrfiltered_processorhighlight_processornew_processorsr;rFr8r9s @@r8_contentzReverseSearchProcessor._contents A$D$C$CD .. \::@b A BC 02EFN12N1"++-@AAA&&&+-2$$"$"3"3  ,,RXXryyQU,VVr:c ddlm}t|j|sJd|j |j}|j dk(r|r|j ||}|j|jj}|jjtjk(rd}nd}dd|fd g}|d t|jfd gz|z}t!| fd } fd } nd} d} |j}t#|| | S)Nrr:zK`ReverseSearchProcessor` should be applied to a `SearchBufferControl` only.rzi-searchzreverse-i-search)class:prompt.searchrrI)rIz)`zclass:prompt.search.text)rz': c|zSrErArs r8rYz=ReverseSearchProcessor.apply_transformation..vrr:c|z SrErArs r8rYz=ReverseSearchProcessor.apply_transformation..wrr:r)rAr;r,rFr/rHrGrLryrd directionrFORWARDrr4rr) r6rr;rCcontentline_fragmentsdirection_textrr4rIr\rs @r8r9z+ReverseSearchProcessor.apply_transformationOs-1"++-@A Y A,,R->->? 99>lmmL"5G%--g.E.E.G.GHN((22o6M6MM!+!3-&7-4 !/1Fr||1TU ! ! //?@N <  <  $  $  I //  r:N)rFrr<zBufferControl | None)rCrrrr<rr) r=r>r?r@r r"r&r(r&__annotations__r/rGr9rAr:r8r.r.sQ !# 9 5 7W)7W/B7W 7Wr2 r:r.c0eZdZdZddZ ddZddZy) r*a Processor that applies another processor, according to a certain condition. Example:: # Create a function that returns whether or not the processor should # currently be applied. def highlight_enabled(): return true_or_false # Wrapped it in a `ConditionalProcessor` for usage in a `BufferControl`. BufferControl(input_processors=[ ConditionalProcessor(HighlightSearchProcessor(), Condition(highlight_enabled))]) :param processor: :class:`.Processor` instance. :param filter: :class:`~prompt_toolkit.filters.Filter` instance. c2||_t||_yrE)r4rr5)r6r4r5s r8rMzConditionalProcessor.__init__s"' r:c|jr|jj|St|jSrE)r5r4r9rr4r5s r8r9z)ConditionalProcessor.apply_transformations5 ;;=>>667KL L!"6"@"@A Ar:ch|jjd|jd|jdS)Nz (processor=z , filter=r)rr=r4r5rOs r8rzConditionalProcessor.__repr__s4..))*+dnn5GyQUQ\Q\P__`aar:N)r4rr5r r<rSr;rrrAr:r8r*r*s*$(B$7B Bbr:r*c eZdZdZddZddZy)r/z Processor class that dynamically returns any Processor. :param get_processor: Callable that returns a :class:`.Processor` instance. c||_yrE) get_processor)r6rYs r8rMzDynamicProcessor.__init__s *r:c\|jxs t}|j|SrE)rYrr9)r6rr4s r8r9z%DynamicProcessor.apply_transformations)&&(.source_to_displays"1 aD Hr:c8tD] }||} |SrE)reversed)rXradisplay_to_source_functionss r8r\z@_MergedProcessor.apply_transformation..display_to_sources&9: aD Hr:r)rXrQr<rQ)rIr4r3r9rrFrGrHrJrKrLrr\r) r6rr4rIrtransformationr\rerbs @@r8r9z%_MergedProcessor.apply_transformations')';';&<#&(#LL   QA33#%%KKII%HHIIKK  N'00I ' . .~/O/O P ' . .~/O/O P Q"  ( +i):rvs# '::6,,SS Y1/)2 2>'>$C5#:&C5#:&/ / dDD4>Y>G)yG)T*B0*))*)Z) ),e) e)P.-Y.-b%=)%=Pk8QQ4:9:0#)Y#)L$)i$)NP IP fF YF R!b9!bH 2y 2 (4Oy4Or: