)L iz4ddlmZddlZddlZddlmZddlmZddlm Z ddl m Z ejdk(sJe s ddl Z dd lmZdd lmZmZmZdd lmZmZdd lmZmZmZmZmZdd lmZddlmZm Z ddl!m"Z"ddl#m$Z$ddl%m&Z&m'Z'ddl(m)Z)m*Z*m+Z+m,Z,m-Z-ddl.m/Z/ddl0m1Z1ddl2m3Z3gdZ4dZ5dZ6dZ7dZ8dZ9Gdde1Z:Gdde:Z;GddZ<Gd d!Z=Gd"d#Z>e d+d$Z?e d,d%Z@Gd&d'ZAGd(d)eAZBd-d*ZCy).) annotationsN)abstractmethod)get_running_loop)contextmanager)SPHINX_AUTODOC_RUNNINGwin32)windll)Arraybyrefpointer)DWORDHANDLE)CallableContextManagerIterableIteratorTextIOrun_in_executor_with_context)create_win32_eventwait_for_handles)KeyPress)Keys) MouseButtonMouseEventType) INPUT_RECORDKEY_EVENT_RECORDMOUSE_EVENT_RECORDSTD_INPUT_HANDLE EventTypes)REVERSE_ANSI_SEQUENCES)Input) Vt100Parser) Win32InputConsoleInputReaderraw_mode cooked_modeattach_win32_inputdetach_win32_inputic4eZdZdZddZeeddZy)_Win32InputBasez; Base class for `Win32Input` and `Win32PipeInput`. c"t|_yN) _Win32Handles win32_handlesselfs `/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/prompt_toolkit/input/win32.py__init__z_Win32InputBase.__init__As *_cyr0r3s r5handlez_Win32InputBase.handleDs r7NreturnNoner<r)__name__ __module__ __qualname____doc__r6propertyrr:r9r7r5r.r.<s(-  r7r.ceZdZdZddfd ZddZddZddZddZe ddZ ddZ dd Z dd Z dd Zdd Ze dd ZxZS)r&z< `Input` class that reads from the Windows console. ct|t|_||jrt |_yt |_yr0)superr6_is_win_vt100_input_enabled_use_virtual_terminal_inputVt100ConsoleInputReaderconsole_input_readerr')r4stdin __class__s r5r6zWin32Input.__init__Os; +F+H(  + +(?(AD %(:(tjjSr0)sysrKfilenor3s r5r`zWin32Input.filenozs yy!!r7cy)Nz win32-inputr9r3s r5typeahead_hashzWin32Input.typeahead_hashsr7c8|jjyr0)rJcloser3s r5rdzWin32Input.closes !!'')r7c.|jjSr0)rJr:r3s r5r:zWin32Input.handles((///r7r0)rKz TextIO | Noner<r=)rNCallable[[], None]r<ContextManager[None])r<rgr<list[KeyPress]r<bool)r<int)r<strr;r>)r?r@rArBr6rOrQrUrWrCrZr(r)r`rbrdr: __classcell__)rLs@r5r&r&Jsb =>(66 "*00r7r&c*eZdZdZidej dej dejdejdejdejdejd ejd ejd ejd ejd ej dej"dej$dej&dej(dej*idej,dej.dej0dej2dej4dej6dej8dej:dej<dej>dej@dejBdejDd ejFd!ejHd"ejJZ&id#ejNd$ejPd%ejRd&ejTd'ejVd(ejXd)ejZd*ej\d+ej^d,ej`d-ejbd.ejdd/ejfd0ejhd1ejjd2ejld3ejnejpejrejtejvejxd4Z=d5Z>d6Z?d7Z@d8ZAd9ZBdEdFd:ZCdGd;ZDdHd<ZEdId=ZFdJd>ZG dKd?ZHeIdLd@ZJeIdMdAZKdNdBZLdOdCZMyD)Pr'z| :param recognize_paste: When True, try to discover paste actions and turn the event into a BracketedPaste.      !"#$%&'(-.pqrstuv)wxyz{rr"r,cd|_||_|tjj r2t t jjt|_ ytjdtjtjz|_t tj |j|_ y)NCONIN$)_fdconrecognize_paster_rKisattyrr kernel32 GetStdHandler r:osopenO_RDWRO_BINARYmsvcrt get_osfhandle)r4rs r5r6zConsoleInputReader.__init__s| . 99    !=!=>N!OPDK''(BII ,CDDK !5!5dkk!BCDKr7c\|j tj|jyyz Close fdcon.Nrrrdr3s r5rdzConsoleInputReader.close! ;; " HHT[[ ! #r7c# Kd}td}t|z}|}t|jgdsytj j |jt||t|t|j||}|Dcgc]}|j|}}t|j|}|jr|j|rt|}|D]}g} |rt|j t"r,|j t"j$t"j&hvrq| j)|j* t-|}|rHt|j t"sD|j t"j$t"j&hvrq| r+t1t"j2dj5| ||y|Ed{ycc}w#t.$rd}YwxYw7w) Return a list of `KeyPress` instances. It won't return anything when there was nothing to read. (This function doesn't block.) http://msdn.microsoft.com/en-us/library/windows/desktop/ms684961(v=vs.85).aspx rtimeoutN)rrrr:r rReadConsoleInputWr rS _get_keys_insert_key_data_merge_paired_surrogatesr _is_pasteiter isinstancekeyrControlJControlMappenddatanext StopIterationrBracketedPastejoin) r4 max_countrTarrtype input_recordsall_keysrgenkrs r5rTzConsoleInputReader.reads Qx*   q9  )) KK/GDM t];<;CC3D))#.CC55h?@   DNN8$<x.C "155$/uu >>KK'! I "155$/uu >>"4#6#6 FF=G# &  9D()! ! sUBHG6.B0H G;*H,H4/H$ H0H 1 H; H HH  HcgSr0r9r3s r5rWzConsoleInputReader.flush_keys*s r7c|jr|St|jtr!t j |jd}nd}t |j|S)z@ Insert KeyPress data, for vt100 compatibility. r)rrrrr#getr)r4 key_pressrs r5rz#ConsoleInputReader._insert_key_data.sK >>  immT *)--immR@DD t,,r7c#Kt|jD]}||}|jtvst |j t|j}t |tr&|jr|j|Ed{xt |ts|j|Ed{y727 w)R Generator that yields `KeyPress` objects from the input records. N) rangevalue EventTyper!getattrEventrrKeyDown_event_to_key_pressesr _handle_mouse)r4rTriirevs r5rzConsoleInputReader._get_keys<s tzz" 6Aq!B ||z)RXXz",,'?@b"23 #99"===$67#11"555! 6>6s/0CAC B= C!C5B?6C?Cc#Kd}|D]}t|jt }|xrd|jcxkxrdknc}|xrd|jcxkxrdknc}|rM|rE|j|jzjddj d}t ||}n|d}|r|}||r|yyw)zm Combines consecutive KeyPresses with high and low surrogates into single characters Nuuuuz utf-16-le surrogatepass)rrrencodedecoder) key_pressesbuffered_high_surrogateris_textis_high_surrogateis_low_surrogatefullchars r5rz+ConsoleInputReader._merge_paired_surrogatesTs #' C$SWWd33G ' KH,K8,K &J8sww+J(+J &#144sww> _= , #8X6C11*.' *-' + . #) ) #sCCcd}d}|D]D}t|jts|dz }|jtjk(s@|dz }F|dk\xr|dk\S)a< Return `True` when we should consider this list of keys as a paste event. Pasted text on windows will be turned into a `Keys.BracketedPaste` event. (It's not 100% correct, but it is probably the best possible way to detect pasting of text and handle that correctly.) rr")rrrr)keys text_count newline_countrs r5rzConsoleInputReader._is_pasteusd   #AaeeT*a uu %"  # !5jAo5r7ch t|tr |jsJd}|j}|jj }|j dd}|dk(r<|j|jvrzt|j|jd}nV||jvr<|j|tjk(rd}t|j||}n t||}||jzs||jzrN||jzr>|r;tj tj"tj$tj&tj(tj*tj,tj.tj0tj2tj4tj6tj8tj:tj<tj>tj@tjBi }|jE|jF|jF|_#||jzs||jzr\|rYtj tjHtj$tjJtj(tjLtj,tjNtj0tjPtj4tjRtj8tjTtjVtjXtj<tjZtj@tj\i }|jE|jF|jF|_#||jzrz|rwtj^tj`tj tjbtj$tjdtj(tjftj,tjhtj0tjjtj4tjltj8tjntjVtjptj<tjrtj@tjti }|jE|jF|jF|_#||jzs||jzr+|r)|jvdk(rttjxd}||jzs||jzr;|r9|jFtjk(rttjzd|gS|r0||j|z}|rttjzd|gS|gSgS)zU For this `KEY_EVENT_RECORD`, return a list of `KeyPress` instances. Nzutf-8rr  )?rrrControlKeyStateuChar UnicodeCharrVirtualKeyCodekeycodesrmappingsrrLEFT_CTRL_PRESSEDRIGHT_CTRL_PRESSED SHIFT_PRESSEDLeftControlShiftLeftRightControlShiftRightUpControlShiftUpDownControlShiftDownHomeControlShiftHomeEndControlShiftEndInsertControlShiftInsertPageUpControlShiftPageUpPageDownControlShiftPageDownrr ControlLeft ControlRight ControlUp ControlDown ControlHome ControlEnd ControlInsertDelete ControlDelete ControlPageUpControlPageDownTabBackTab ShiftLeft ShiftRightShiftUp ShiftDown ShiftHomeShiftEnd ShiftInsert ShiftDelete ShiftPageUp ShiftPageDownr ControlSpaceEscapeLEFT_ALT_PRESSED)r4rresultcontrol_key_stateu_char ascii_charmapping meta_presseds r5rz(ConsoleInputReader._event_to_key_pressess "./BJJ>>"&..%%]]7O<  V   DMM1!$--0A0A"BBGT]]*==, ="$-- ";VD!&&1 "D$:$::$t'>'>>!D$6$66 400 D22,, 400 400$.. T44 T44 t88 'G!VZZ> DOO 4>> 4>>$-- T-- T-- T-- t11 G!VZZ'>> s"d//5F "D$:$::$t'>'>> dmm+T[["-v6 6 ,t/D/DDL b16::xIr7c|j}|j}d}tj}|tzr'|dkDrt j }nDt j}n3|tk(rtj}n|tk(rtj}|tzrt j}|&|dkDrt j}nt j}dj!|j"|j"t%|j&j(t%|j&j*g}t-t.j0|gS)zK Handle mouse events. Return a list of KeyPress instances. Nr;) EventFlags ButtonStaterNONE MOUSE_WHEELEDr SCROLL_UP SCROLL_DOWNFROM_LEFT_1ST_BUTTON_PRESSEDLEFTRIGHTMOST_BUTTON_PRESSEDRIGHT MOUSE_MOVED MOUSE_MOVE MOUSE_DOWNMOUSE_UPrrrm MousePositionXYrrWindowsMouseEvent)r4r event_flags button_state event_typebuttonrs r5rz ConsoleInputReader._handle_mousesmm ~~ ,0 )..  &a+55 +77 ;;$))!99$**  $'22J  a+66 ,44 xx   B$$&&'B$$&&'   //677r7N)T)rrkr<r=r;r<zIterable[KeyPress]rh)rrr<r)rTrrArray[INPUT_RECORD]r<Iterator[KeyPress])rrir<rN)rrir<rk)rrr<ri)rrr<ri)Nr?r@rArBrr,r+ControlAControlBControlCControlDControlEControlFControlGControlHControlIrControlKControlLrControlNControlOControlPControlQControlRControlSControlTControlUControlVControlWControlXControlYControlZControlBackslashControlSquareCloseControlCircumflexControlUnderscore Backspacerrrr r rrrrrrF1F2F3F4F5F6F7F8F9F10F11F12rr-RIGHT_ALT_PRESSEDrrrr6rdrTrWrr staticmethodrrrrr9r7r5r'r's """"" "  "  "  " " " " " " " " " " !"" #"$ %"& '"( )"* +", -". /"0 1"2 3"4 5"6 7"8 9": &&;"< ((="> ''?"@ ''A"B C"HJ DKK DMM DHH  DII  DII DGG DJJ DII DKK DKK TWW TWW TWW!" TWW#$ TWW%& TWW'( TWW)*WW WW XX XX XX3H8M D" < | -66*=6 60**@66*BH-8r7r'cDeZdZdZddZddZd dZd dZ d dZy) rIa Similar to `ConsoleInputReader`, but for usage when `ENABLE_VIRTUAL_TERMINAL_INPUT` is enabled. This assumes that Windows sends us the right vt100 escape sequences and we parse those with our vt100 parser. (Using this instead of `ConsoleInputReader` results in the "data" attribute from the `KeyPress` instances to be more correct in edge cases, because this responds to for instance the terminal being in application cursor keys mode.) cd_g_tfd_tj j r2ttjjt_ ytjdtjtj z_tt#j$j_ y)Nc:jj|Sr0)_bufferr)rr4s r5z2Vt100ConsoleInputReader.__init__..Qsdll11)<r7r)rr}r% _vt100_parserr_rKrrr rrr r:rrrrrrr3s`r5r6z Vt100ConsoleInputReader.__init__Ls ') ( <  99    !=!=>N!OPDK''(BII ,CDDK !5!5dkk!BCDKr7c\|j tj|jyyrrr3s r5rdzVt100ConsoleInputReader.close]rr7czd}td}t|z}|}t|jgdsgStj j |jt||t||j||D]}|jj||j}g|_ |S)rrrr) rrrr:r rrr rrfeedr})r4rrTrrkey_datar.s r5rTzVt100ConsoleInputReader.readbs Qx*   q9I )) KK/GDM t]; .H    # #H - .  r7c`|jj|j}g|_|S)zc Flush pending keys and return them. (Used for flushing the 'escape' key.) )rflushr})r4r.s r5rWz"Vt100ConsoleInputReader.flush_keyss-   "  r7c#:Kt|jD]}||}|jtvst |j t|j}t |tsS|js`|jj}|dk7s||yw)rrN) rrrr!rrrrrrr)r4rTrrrrr0s r5rz!Vt100ConsoleInputReader._get_keyss tzz" %Aq!B ||z)RXXz",,'?@b"23 XX11F'$  %s0B7B+ B8BBNr;rLrh)rTrrrMr<z Iterator[str]) r?r@rArBr6rdrTrWrr9r7r5rIrI?s: D"" #J %%*=% %r7rIc(eZdZdZddZddZddZy) r1a Utility to keep track of which handles are connectod to which callbacks. `add_win32_handle` starts a tiny event loop in another thread which waits for the Win32 handle to become ready. When this happens, the callback will be called in the current asyncio event loop using `call_soon_threadsafe`. `remove_win32_handle` will stop this tiny event loop. NOTE: We use this technique, so that we don't have to use the `ProactorEventLoop` on Windows and we can wait for things like stdin in a `SelectorEventLoop`. This is important, because our inputhook mechanism (used by IPython), only works with the `SelectorEventLoop`. c i|_i|_yr0)_handle_callbacks_remove_eventsr3s r5r6z_Win32Handles.__init__s@B24r7cj}| td|jt|j|<t |j |<dfd dfd ty)z7 Add a Win32 handle to the event loop. NInvalid handle.cT ty#twxYw)Nloopr)callbackrwaitsr5readyz-_Win32Handles.add_win32_handle..readys# > ,T=,T=s'ctg}|ur tjjyj yr0)rr r CloseHandlecall_soon_threadsafe)r.r:rr remove_events r5rz,_Win32Handles.add_win32_handle..waits?&|V&<=F%++L9))%0r7rr;)r ValueErrorremove_win32_handlerrrrr)r4r:r handle_valuerrrrs `` @@@@r5add_win32_handlez_Win32Handles.add_win32_handles}||  ./ /   (!/7|,*+ ,8L) > 1 1 %T5r7c.|jy |jj|j}tjj | |jj|jS#t $rY1wxYw#t $rYywxYw)zt Remove a Win32 handle from the event loop. Return either the registered handler or `None`. N)rrpopr rSetEventKeyErrorr)r4r:events r5rz!_Win32Handles.remove_win32_handles <<  ,''++FLL9E OO $ $U + ))--fll; ;      s#%A9$B9 BB BBNr;)r:rrrfr<r=)r:rr<zCallable[[], None] | None)r?r@rArBr6rrr9r7r5r1r1s 4)6Vr7r1c#\K|j}|j}|j td|j |}|j || d|j ||r|j ||yy#|j ||r|j ||wwxYww)z Context manager that makes this input active in the current event loop. :param input: :class:`~prompt_toolkit.input.Input` object. :param input_ready_callback: Called when the input is ready to read. Nrr2r:rrrr)inputrr2r:previous_callbacks r5r*r*s''M \\F ||*++&99&A""684F ))&1   * *63D E  ))&1   * *63D E sAB,B'B,(B))B,c#K|j}|j}|j td|j |} d|r|j ||yy#|r|j ||wwxYww)Nrr)rr2r:rs r5r+r+"s}''M \\F ||*++%99&AF   * *63D E    * *63D E sAA8AA8A55A8c>eZdZdZ d ddZd dZd dZd dZy) r(z :: with raw_mode(stdin): ''' the windows terminal is now in 'raw' mode. ''' The ``fileno`` attribute is ignored. This is to be compatible with the `raw_input` method of `.vt100_input`. Nctttjjt|_||_yr0)rr rrr r:r\)r4r`r\s r5r6zraw_mode.__init__>s(V__99:JKL 0P-r7ct}tjj|jt |||_|jyr0)rr rGetConsoleModer:r original_mode_patch)r4rs r5 __enter__zraw_mode.__enter__Ds7 &&t{{GM4JK* r7cd}d}d}|jj||z|zz}|jr |tz}tj j |j|yNr,rr")rrr\ENABLE_VIRTUAL_TERMINAL_INPUTr rSetConsoleModer:)r4ENABLE_ECHO_INPUTENABLE_LINE_INPUTENABLE_PROCESSED_INPUTnew_modes r5rzraw_mode._patchLsl""!'%%++  1 14J J/    0 0 5 5H&&t{{H=r7cltjj|j|jyr0)r rrr:r)r4as r5__exit__zraw_mode.__exit__[s &&t{{D4F4FGr7rY)r`z int | Noner\rkr<r=r;)robjectr<r=)r?r@rArBr6rrrr9r7r5r(r(3s?SXQ QKOQ Q  >Hr7r(ceZdZdZddZy)r)zx :: with cooked_mode(stdin): ''' The pseudo-terminal stdin is now used in cooked mode. ''' cd}d}d}tjj|j|jj ||z|zzyr)r rrr:rr)r4rrrs r5rzcooked_mode._patchhsQ""!'&& KK    $ $ #447MM O r7Nr;)r?r@rArBrr9r7r5r)r)`s   r7r)cttjjt}t d}tjj |t| tjj|t t}|dk(tjj||S#tjj||wxYw)z_ Returns True when we're running Windows and VT100 escape sequences are supported. rr") rr rrr rrr rr)hconsolerr.s r5rGrGus foo223CDEH!HM OO""8U=-AB@oo44 e9: {&&x?&&x?s "1B44"C)rr.rrfr<Iterator[None])rr.r<rrj)D __future__rrr_abcrasyncior contextlibrutilsrplatformrctypesr r r r ctypes.wintypesrrtypingrrrrrprompt_toolkit.eventlooprprompt_toolkit.eventloop.win32rr(prompt_toolkit.key_binding.key_processorrprompt_toolkit.keysrprompt_toolkit.mouse_eventsrrprompt_toolkit.win32_typesrrrr r!ansi_escape_sequencesr#baser$ vt100_parserr%__all__r<r>r@r9rr.r&r'rIr1r*r+r(r)rGr9r7r5rs7" $%* ||w(()GGAO=$C:%  #  !'  e  ?0?0Dp8p8f k%k%\UUpF F&8FFF8 F F *H*HZ ( *@r7