)L idZddlmZddlZddlmZmZddlmZm Z m Z m Z m Z ddl mZddlmZddlmZdd lmZdd lmZdd lmZmZdd lmZmZmZdd lmZm Z m!Z!ddl"m#Z#m$Z$ddl%m&Z&m'Z'm(Z(ddl)m*Z*ddl+m,Z,ddl-m.Z.ddl/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6erddl7m8Z8m9Z9ddl-m:Z:gdZ;e eGddZ?Gdde>Z@Gdd e>ZAGd!d"e ZBGd#d$e>ZCGd%d&eCZDy)'z) User interface Controls for the layout. ) annotationsN)ABCMetaabstractmethod) TYPE_CHECKINGCallableHashableIterable NamedTuple)get_app)Buffer) SimpleCache)Point)Document) FilterOrBool to_filter)AnyFormattedTextStyleAndTextTuplesto_formatted_text)fragment_list_to_textfragment_list_width split_lines)Lexer SimpleLexer) MouseButton MouseEventMouseEventType) SearchState) SelectionType) get_cwidth)DisplayMultipleCursors#HighlightIncrementalSearchProcessorHighlightSearchProcessorHighlightSelectionProcessor ProcessorTransformationInputmerge_processors)KeyBindingsBaseNotImplementedOrNone)Event) BufferControlSearchBufferControl DummyControlFormattedTextControl UIControl UIContentc~eZdZdZd dZddZ ddZddZeddZ ddZ d dZ d d Z dd Z dd Zy )r/z5 Base class for all user interface controls. cyNselfs d/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/prompt_toolkit/layout/controls.pyresetzUIControl.resetDs cyr3r4r6max_available_widths r7preferred_widthzUIControl.preferred_widthHsr9cyr3r4)r6widthmax_available_height wrap_linesget_line_prefixs r7preferred_heightzUIControl.preferred_heightKsr9cy)z> Tell whether this user control is focusable. Fr4r5s r7 is_focusablezUIControl.is_focusableTsr9cy)zn Generate the content for this user control. Returns a :class:`.UIContent` instance. Nr4)r6r?heights r7create_contentzUIControl.create_contentZr9ctS)a; Handle mouse events. When `NotImplemented` is returned, it means that the given event is not handled by the `UIControl` itself. The `Window` or key bindings can decide to handle this event as scrolling or changing focus. :param mouse_event: `MouseEvent` instance. )NotImplemented)r6 mouse_events r7 mouse_handlerzUIControl.mouse_handlerbs r9cy)z Request to move the cursor down. This happens when scrolling down and the cursor is completely at the top. Nr4r5s r7move_cursor_downzUIControl.move_cursor_downnrIr9cy)z0 Request to move the cursor up. Nr4r5s r7move_cursor_upzUIControl.move_cursor_upurIr9cy)z The key bindings that are specific for this user control. Return a :class:`.KeyBindings` object if some key bindings are specified, or `None` otherwise. Nr4r5s r7get_key_bindingszUIControl.get_key_bindingszrIr9cgS)z Return a list of `Event` objects. This can be a generator. (The application collects all these events, in order to bind redraw handlers to these events.) r4r5s r7get_invalidate_eventszUIControl.get_invalidate_eventss  r9NreturnNoner<intrW int | None r?rZr@rZrAboolrBGetLinePrefixCallable | NonerWr[rWr]r?rZrGrZrWr0rLrrWr)rWKeyBindingsBase | NonerWzIterable[Event[object]])__name__ __module__ __qualname____doc__r8r=rCrErrHrMrOrQrSrUr4r9r7r/r/?sx "  6          r9r/) metaclassc^eZdZdZdddddf d dZd dZ d d dZy) r0a Content generated by a user control. This content consists of a list of lines. :param get_line: Callable that takes a line number and returns the current line. This is a list of (style_str, text) tuples. :param line_count: The number of lines. :param cursor_position: a :class:`.Point` for the cursor position. :param menu_position: a :class:`.Point` for the menu position. :param show_cursor: Make the cursor visible. cgSr3r4is r7zUIContent.s2r9rNTcv||_||_|xs tdd|_||_||_i|_yNrxy)get_line line_countrcursor_position menu_position show_cursor_line_heights_cache)r6rtrurvrwrxs r7__init__zUIContent.__init__s?! $.A%!q/*&9; r9cN||jkr|j|St)z(Make it iterable (iterate line by line).)rurt IndexError)r6linenos r7 __getitem__zUIContent.__getitem__s# DOO #==( ( r9c 8tj|||f} |j|S#t$r|dk(rd}nt |j |d|}t |}|rl|tt||dz }d}||kDr||dz }||z}t|||dz } t t | } | |k\rd}nA|| z }||kDrFn5 t||\} } | r| dz } td| }n#t$rd}YnwxYw||j|<|cYSwxYw)a Return the height that a given line would need if it is rendered in a space with the given width (using line wrapping). :param get_line_prefix: None or a `Window.get_line_prefix` callable that returns the prefix to be inserted before this line. :param slice_stop: Wrap only "line[:slice_stop]" and return that partial result. This is needed for scrolling the window correctly when line wrapping. :returns: The computed height. riNr ) r render_counterryKeyErrorrrtrrrdivmodmaxZeroDivisionError) r6r}r?rB slice_stopkeyrGline text_width fragments2 prefix_widthquotient remainders r7get_height_for_linezUIContent.get_height_for_linese*i&&zA0 ++C0 0. z-T]]6-BCKZP'- ""5)/&!*DE#J F%u,! "e+ %6+FFQJ?& (22G 2S'T '50%*F!"l2 %u, 2.4Z.G+)%$MH!$Q!1 -'!&'-3D $ $S )M]. s:)B%DDC4 D4 D?DDDD) rt#Callable[[int], StyleAndTextTuples]rurZrv Point | Nonerwrrxr])r}rZrWrr3) r}rZr?rZrBr^rr[rWrZ)rerfrgrhrzr~rr4r9r7r0r0s :F(,&* ;5;;& ; $ ;  ;""& GGG6 G  G Gr9r0ceZdZdZ d ddZddZddZddZddZddZ dd Z dd Z dd Z dd Z dd Zy)r.a Control that displays formatted text. This can be either plain text, an :class:`~prompt_toolkit.formatted_text.HTML` object an :class:`~prompt_toolkit.formatted_text.ANSI` object, a list of ``(style_str, text)`` tuples or a callable that takes no argument and returns one of those, depending on how you prefer to do the formatting. See ``prompt_toolkit.layout.formatted_text`` for more information. (It's mostly optimized for rather small widgets, like toolbars, menus, etc...) When this UI control has the focus, the cursor will be shown in the upper left corner of this control by default. There are two ways for specifying the cursor position: - Pass a `get_cursor_position` function which returns a `Point` instance with the current cursor position. - If the (formatted) text is passed as a list of ``(style, text)`` tuples and there is one that looks like ``('[SetCursorPosition]', '')``, then this will specify the cursor position. Mouse support: The list of fragments can also contain tuples of three items, looking like: (style_str, text, handler). When mouse support is enabled and the user clicks on this fragment, then the given handler is called. That handler should accept two inputs: (Application, MouseEvent) and it should either handle the event or return `NotImplemented` in case we want the containing Window to handle this event. :param focusable: `bool` or :class:`.Filter`: Tell whether this control is focusable. :param text: Text or formatted text to be displayed. :param style: Style string applied to the content. (If you want to style the whole :class:`~prompt_toolkit.layout.Window`, pass the style to the :class:`~prompt_toolkit.layout.Window` instead.) :param key_bindings: a :class:`.KeyBindings` object. :param get_cursor_position: A callable that returns the cursor position as a `Point` instance. Nc||_||_t||_||_||_||_||_td|_ td|_ d|_ y)Nmaxsizer ) textstyler focusable key_bindingsrxmodalget_cursor_positionr _content_cache_fragment_cache _fragments)r6rrrrrxrrs r7rzzFormattedTextControl.__init__%sk  "9-)& #6 ALTV@WEPF  6:r9cd|_yr3)rr5s r7r8zFormattedTextControl.resetCs r9c"|jSr3rr5s r7rEz!FormattedTextControl.is_focusableF~~r9cN|jjd|jdS)N()) __class__rerr5s r7__repr__zFormattedTextControl.__repr__Is$..))*!DII=::r9cfjjtjfdS)z Get fragments, but only retrieve fragments once during one render run. (This function is called several times during one rendering, because we also need those for calculating the dimensions.) cDtjjSr3)rrrr5sr7rnzAFormattedTextControl._get_formatted_text_cached..Ss.? 4::.Vr9)rgetr rr5s`r7_get_formatted_text_cachedz/FormattedTextControl._get_formatted_text_cachedLs, ##'' I $ $&V  r9ct|j}|jdDcgc] }t|}}t |Scc}w)zm Return the preferred width for this control. That is the width of the longest line.  )rrsplitrr)r6r<rl line_lengthss r7r=z$FormattedTextControl.preferred_widthVsH %T%D%D%FG/3zz$/?@! 1 @ @<  AsA c|j|d}|r=d}t|jD]!}||j|||z }||k\s|cS|S|jS)z? Return the preferred height for this control. Nr)rHrangerur)r6r?r@rArBcontentrGrms r7rCz%FormattedTextControl.preferred_height_ss%%eT2 F7--. 0'55aPP11// 0M%% %r9c z j}tt|}|Dcgc]}|Dcgc] }|d|dfc}c}} |_ d d fd d fd jxs t || f}d fd }j j||Scc}wcc}}w) Nrr ctD]4\}}d}|D](^}}}||vrt||ccS|t|z }*6yrp) enumeraterlen)fragmentrsrrr style_strr_fragment_liness r7rz@FormattedTextControl.create_content..get_cursor_positions^%^4 #4+/#'Ita9,$qA.TNA# # r9cdS)Nz[SetMenuPosition]r4)rsr7get_menu_positionz>FormattedTextControl.create_content..get_menu_positions&':; ;r9cZtfdtjS)Nc|Sr3r4)rmrs r7rnzJFormattedTextControl.create_content..get_content..s >!#4r9)rtrurxrvrw)r0rrx)rvrrr6sr7 get_contentz8FormattedTextControl.create_content..get_contents-4~. ,, //1  r9)z[SetCursorPosition])rstrrWr)rWr)rWr0)rlistrrrtuplerr) r6r?rGfragments_with_mouse_handlers"fragment_lines_with_mouse_handlersritemrrrvrrrs ` @@@@r7rHz#FormattedTextControl.create_contentts(,(G(G(I%-1 5 6. *;4 -1 1Dd1gtAw  14 8 2     <K433J7JM23UOL  ""&&sK88O 24 s B7B2 B72B7c~|jrtt|j} ||jj}|jj }d}|D]<}|t |dz }||kDst |dk\r|d}||cStStStS#t$r tcYSwxYw)aU Handle mouse events. (When the fragment list contained mouse handlers and the user clicked on on any of these, the matching handler is called. This handler can still return `NotImplemented` in case we want the :class:`~prompt_toolkit.layout.Window` to handle this particular event.) rr ) rrrpositionrsrrrr|rK)r6rLfragments_for_line fragmentsxposcountrhandlers r7rMz"FormattedTextControl.mouse_handlers ??!%k$//&B!C  ".{/C/C/E/EF #++--% "DSa\)Et|t9>'+1gG#*;#77! "~+ &%% &sB**B<;B<c|jSr3)rr5s r7is_modalzFormattedTextControl.is_modals zzr9c|jSr3rr5s r7rSz%FormattedTextControl.get_key_bindingss   r9)rFNTFN)rrrrrrrrcrxr]rr]rz!Callable[[], Point | None] | NonerWrXrVr_rWr)rWr)r<rZrWrZr\)r?rZrGr[rWr0rarb)rerfrgrhrzr8rErrr=rCrHrMrrSr4r9r7r.r.s(X"$"'/3 AE::: : - :  ::?: :< ; !&&"& & 6 &  &*09d%N!r9r.c eZdZdZddZddZy)r-z A dummy control object that doesn't paint any content. Useful for filling a :class:`~prompt_toolkit.layout.Window`. (The `fragment` and `char` attributes of the `Window` class can be used to define the filling.) c*dd}t|ddzS)NcgSr3r4rls r7rtz-DummyControl.create_content..get_linesIr9d)rtrurmrZrWr)r0)r6r?rGrts r7rHzDummyControl.create_contents (sCx@@r9cy)NFr4r5s r7rEzDummyControl.is_focusablesr9Nr`r_)rerfrgrhrHrEr4r9r7r-r-sA r9r-c,eZdZUded<ded<ded<y)_ProcessedLinerrzCallable[[int], int]source_to_displaydisplay_to_sourceN)rerfrg__annotations__r4r9r7rrs!!++++r9rc(eZdZdZ d ddZddZeddZeddZeddZ ddZ dd Z dd Z dd Z dd Z d dd Zd dZd!dZd!dZd"dZd#dZy)$r+a Control for visualizing the content of a :class:`.Buffer`. :param buffer: The :class:`.Buffer` object to be displayed. :param input_processors: A list of :class:`~prompt_toolkit.layout.processors.Processor` objects. :param include_default_input_processors: When True, include the default processors for highlighting of selection, search and displaying of multiple cursors. :param lexer: :class:`.Lexer` instance for syntax highlighting. :param preview_search: `bool` or :class:`.Filter`: Show search while typing. When this is `True`, probably you want to add a ``HighlightIncrementalSearchProcessor`` as well. Otherwise only the cursor position will move, but the text won't be highlighted. :param focusable: `bool` or :class:`.Filter`: Tell whether this control is focusable. :param focus_on_click: Focus this buffer when it's click, but not yet focused. :param key_bindings: a :class:`.KeyBindings` object. Nc ||_||_ttt t g|_t||_t||_ t| |_ |xs t|_ ||_ |xs t|_| |_||_t%d|_d|_d|_y)Nr)input_processors include_default_input_processorsr#r"r$r!default_input_processorsrpreview_searchrfocus_on_clickr bufferrwrlexerr_search_buffer_controlr r_last_click_timestamp_last_get_processed_line) r6rrrrrrsearch_buffer_controlrwrrs r7rzzBufferControl.__init__s!10P- % & / 1 ' ) " $ ) %(7"9-'7( *+km (&;#  " 48"PT%r9chd|jjd|jdt|dS)N)rreridr5s r7rzBufferControl.__repr__/s14>>**+8DKK?$r$xlRSTTr9ct|jr|j}n |j}|t|tsJ|Sr3)callabler isinstancer,)r6results r7rz#BufferControl.search_buffer_control2sE D// 0002F00F~F4G!HHH r9c8|j}| |jSyr3)rr)r6controls r7 search_bufferzBufferControl.search_buffer>s ,,  >> !r9cJ|j}|r |jStS)a Return the `SearchState` for searching this `BufferControl`. This is always associated with the search control. If one search bar is used for searching multiple `BufferControls`, then they share the same `SearchState`. )rsearcher_search_stater)r6rs r7 search_statezBufferControl.search_stateEs'!% : : (>> >= r9c"|jSr3rr5s r7rEzBufferControl.is_focusableSrr9cy)a This should return the preferred width. Note: We don't specify a preferred width according to the content, because it would be too expensive. Calculating the preferred width can be done by calculating the longest line, but this would require applying all the processors to each line. This is unfeasible for a larger document, and doing it for small documents only would result in inconsistent behavior. Nr4r;s r7r=zBufferControl.preferred_widthVsr9cd}|j|d}|s |jS|j|k\r|St|jD]!}||j|||z }||k\s|cS|S)Nrr )rG)rHrurr)r6r?r@rArBrGrrms r7rCzBufferControl.preferred_heightcs%%eA%6%% %   !5 5' 'w))* ,A g11!UOL LF--++  ,  r9cdfd }jjjf}jj ||S)zP Create a function that returns the fragments for a given line. c:jjSr3)r lex_document)documentr6sr7get_formatted_text_for_linezTBufferControl._get_formatted_text_for_line_func..get_formatted_text_for_lines::**84 4r9)rWr)rrinvalidation_hashrr)r6rrrs`` r7!_get_formatted_text_for_line_funcz/BufferControl._get_formatted_text_for_line_funcs? 5}}djj::<=##''-HIIr9cjxsg}jrj|z}t| dfd dfd }|S)z Create a function that takes a line number of the current document and returns a _ProcessedLine(processed_fragments, source_to_display, display_to_source) tuple. c dd}jt||| |}t|j|j|j S)z0Transform the fragments for a given line number.c|S)zX position from the buffer to the x position in the processed fragment list. By default, we start from the 'identity' operation.r4rls r7rz[BufferControl._create_get_processed_line_func..transform..source_to_displays r9)rmrZrWrZ)apply_transformationr&rrrr) r}rrtrtransformationrrGmerged_processorr6r?s r7 transformz@BufferControl._create_get_processed_line_func..transformsd  .BB#%  N"((0000 r9cBjidfd }|S)Nc\ |S#t$r||}||<|cYSwxYwr3)r)rmprocessed_linecachertr s r7get_processed_linez^BufferControl._create_get_processed_line_func..create_func..get_processed_linesA* 8O*%.q(1+x%HN-E!H))*s  ++)rmrZrWr)r)rrrtrr6r s @@r7 create_funczBBufferControl._create_get_processed_line_func..create_funcs%==hGH/1E *& %r9)r}rZrrrtrrWr)rWCallable[[int], _ProcessedLine])rrrr')r6rr?rGrrr r s```` @@r7_create_get_processed_line_funcz-BufferControl._create_get_processed_line_funcs{ 006B  0 0#<.translate_rowcols!-c2DDSISQ Qr9c8|j}|dgz}|S)z-Return the fragments for a given line number.)r )r)rmrrs r7rtz.BufferControl.create_content..get_lines'*1-77I"YK/I r9)rtrurv)rrZrrZrWrr)rload_history_if_not_yet_loadedrr]rrr layoutsearch_target_buffer_controlrdocument_for_searchrrrrrrr0rucursor_position_rowcursor_position_colcurrent_controlrwrrZtranslate_index_to_positioncomplete_stateminrvoriginal_document)r6r?rGrrsearch_control preview_nowssrrrtrrwmenu_rowmenu_colrs @r7rHzBufferControl.create_contents&  --/ 33$      ! B  B%%**  B I   9 9T A )  >5""B11'..33 ll "HH!AA eV );% R **,,,h.J.J  9   + +t 3484F4FD..0DM(!-555%+__%P%P!&"()98(L%"!&& &,__%P%P..--??OO&"( )98(L%)-%r9c|j}|j}tjj|k(rR|j rD|j |j }|j|j}|jj|j |}|jtjk(r|j||_y|jtj k(rl|j"t$j&k7rO|j(;t+|j|z dkDr |j-t.j0||_y|jtj2k(rt+|j|z dkDr3|j( |j-t.j0||_|j4xr$t7j6|j4z dk}t7j6|_|rq|jj9\}} |xj|z c_|j-t.j0|xj| |z z c_yt:Sy|j=r7|jtj2k(r|tj_yt:S)z1 Mouse handler for this control. Nr)selection_typer g333333?)rrr rr$rrsrrrrtranslate_row_col_to_index event_typer MOUSE_DOWNexit_selectionrv MOUSE_MOVEbuttonrNONEselection_stateabsstart_selectionr CHARACTERSMOUSE_UPrtimefind_boundaries_of_current_wordrKr) r6rLrrrrindex double_clickstartends r7rMzBufferControl.mouse_handler8sn'' 9   + +t 3,,!%!>!>xzz!J&77 CBB8::tT))^-F-FF))+-2F*rm **n.G.GG#**k.>.>>..6 6 6 >?!C..m>V>V.W-2F*ZW!++~/F/FF 611E9:Q>!119"22/V>V.W..#+=.$*)##%**n.E.EE 48   0&%r9cv|j}|xj|jjz c_yr3)rrvrget_cursor_down_positionr6bs r7rOzBufferControl.move_cursor_downs) KK QZZ@@BBr9cv|j}|xj|jjz c_yr3)rrvrget_cursor_up_positionrDs r7rQzBufferControl.move_cursor_ups) KK QZZ>>@@r9c|jS)zG When additional key bindings are given. Return these. rr5s r7rSzBufferControl.get_key_bindingss   r9c#K|jj|jj|jj|jjyw)z6 Return the Window invalidate events. N)ron_text_changedon_cursor_position_changedon_completions_changedon_suggestion_setr5s r7rUz#BufferControl.get_invalidate_eventssH kk)))kk444kk000kk+++sA"A$) NNTNFTNNFN)r Buffer | Nonerlist[Processor] | Nonerr]r Lexer | Nonerrrrrz>None | SearchBufferControl | Callable[[], SearchBufferControl]rwzCallable[[], int | None] | Nonerrrrcr)rWzSearchBufferControl | None)rWrN)rWrr_rYr\)rrrWr)rrr?rZrGrZrWr)F)r?rZrGrZrr]rWr0rarVrbrd)rerfrgrhrzrpropertyrrrrEr=rCrrrHrMrOrQrSrUr4r9r7r+r+s*!%3715"',"& 9=',/3,U,U1,U+/ ,U  ,U % ,U ,U K,U7,U%,U-,U\U    ! !  "  6   < J  J , J> >),>69> (>B?Dgg"%g7;g gRM^CA! ,r9r+cHeZdZdZ d dfd ZxZS)r,z :class:`.BufferControl` which is used for searching another :class:`.BufferControl`. :param ignore_case: Search case insensitive. cPt||||||t||_y)N)rrrrr)r)superrzrr)r6rrrrrrrs r7rzzSearchBufferControl.__init__s5 -)%  &1[%I"r9)NNNFNF) rrNrrOrrPrrrrcrr)rerfrgrhrz __classcell__)rs@r7r,r,sh!%37"',/3$)JJ1J J % J - J"JJr9r,)Erh __future__rr<abcrrtypingrrrr r "prompt_toolkit.application.currentr prompt_toolkit.bufferr prompt_toolkit.cacher prompt_toolkit.data_structuresrprompt_toolkit.documentrprompt_toolkit.filtersrrprompt_toolkit.formatted_textrrr#prompt_toolkit.formatted_text.utilsrrrprompt_toolkit.lexersrrprompt_toolkit.mouse_eventsrrrprompt_toolkit.searchrprompt_toolkit.selectionrprompt_toolkit.utilsr processorsr!r"r#r$r%r&r''prompt_toolkit.key_binding.key_bindingsr(r)r*__all__rZGetLinePrefixCallabler/r0r.r-rr+r,r4r9r7rjs# 'JJ6(,0,:  5OO-2++ !#s-=!=>I'IXll^W!9W!t9&,Z, q,Iq,h J-Jr9