
    +                        S r SSKJr  SSKrSSKrSSKrSSKrSSKrSSKrSSK	r	SSK
r
SSKrSSKrSSKJr  SSKJrJr  SSKJr  SSKJr  SS	KJrJr  SS
KJr  SSKJr  SSKJr  SSKJ r   SSK!J"r"J#r#  SSK$J%r%  SSK$J&r&  SSK'J(r(  SSK)J*r*  SSK+J,r,  SSK-J.r.  SSK/J0r0J1r1  SSK2J3r3  SSK4J5r5  SSKJ6r6  Sr7 " S S\85      r9 " S S\5      r: " S S\85      r; " S  S!\85      r< " S" S#\5      r=g)$z2
The main `CommandLineInterface` class and logic.
    )unicode_literalsN)Popen   )ApplicationAbortAction)Buffer)BufferMapping)CompleteEventget_common_complete_suffix)SEARCH_BUFFER)	EventLoop)EventLoopCallbacks)	Condition)
StdinInputInput)InputProcessor)KeyPress)Registry)ViState)Keys)Output)Rendererprint_tokens)SearchState)Event)AcceptAction)r   CommandLineInterfacec                   H   \ rS rSrSrS1S jr\S 5       r\S 5       r\S 5       r	S2S jr
   S3S	 jr\S
 5       r\S 5       rS rS rS r\S 5       r\S 5       rS2S jr\S 5       r\S 5       rS r\rS rS rS rS4S jrS5S jr \R<                  " \R@                  " S5      5          S7S jr#S r$S r%\$r&\%r'S r(S  r)S2S! jr*S2S" jr+S# r,S8S$ jr-S4S% jr.\S& 5       r/\S' 5       r0\S( 5       r1S) r2\S* 5       r3S+ r4S, r5S2S- jr6S9S. jr7S/ r8S0r9g! \! a    S6S jr" Ntf = f):r   /   a  
Wrapper around all the other classes, tying everything together.

Typical usage::

    application = Application(...)
    cli = CommandLineInterface(application, eventloop)
    result = cli.run()
    print(result)

:param application: :class:`~prompt_toolkit.application.Application` instance.
:param eventloop: The :class:`~prompt_toolkit.eventloop.base.EventLoop` to
                  be used when `run` is called. The easiest way to create
                  an eventloop is by calling
                  :meth:`~prompt_toolkit.shortcuts.create_eventloop`.
:param input: :class:`~prompt_toolkit.input.Input` instance.
:param output: :class:`~prompt_toolkit.output.Output` instance. (Probably
               Vt100_Output or Win32Output.)
Nc                    [        U[        5      (       d   e[        U[        5      (       d   S5       eUb  [        U[        5      (       d   eUb  [        U[        5      (       d   eSSKJn  Xl        X l        SU l	        U=(       d    U" 5       U l
        U=(       d    [        [        R                  5      U l        [        UR                  [         5      (       d   eUR                  U l        UR"                  U l        SU l        ['        5       U l        [+        U R                  R,                  U R                  UR.                  UR0                  S9U l        SU l        SU l        SU l        [;        UR<                  [>        R@                  " U 5      5      U l!        0 U l"        S U l#        U R                  RI                  5        H  u  pgU RK                  Xg5        M     [M        XRN                  5      U l'        [M        XRP                  5      U l(        [M        XRR                  5      U l)        [M        XRT                  5      U l*        [M        XRV                  5      U l+        [M        XRX                  5      U l,        [M        XRZ                  5      U l-        [M        XR\                  5      U l.        U R_                  5         U =RP                  U R                  RP                  -  sl(        U RP                  Ra                  5         g )Nz!Passing an eventloop is required.r   )create_outputF)use_alternate_screenmouse_supportr   )1
isinstancer   r   r   r   	shortcutsr!   application	eventloop_is_runningoutputr   sysstdininputbuffersr	   editing_modequoted_insertr   vi_stater   styler"   r#   rendererrender_countermax_render_postpone_time_invalidatedr   key_bindings_registryweakrefrefinput_processor_async_completers_sub_cliitems
add_bufferr   on_buffer_changedon_initializeon_input_timeouton_invalidate	on_renderon_reseton_starton_stopresetfire)selfr&   r'   r,   r)   r!   namebs           +lib/third_party/prompt_toolkit/interface.py__init__CommandLineInterface.__init__C   sd   +{3333)Y//T1TT/~FF!;!;;;}
5% 8 888,&"  /3j3
 +--}===="** (44 #  	 !""KK!,!A!A%33	5   )*% "  .k.O.OQXQ\Q\]aQbc!#  ||))+GDOOD$ , "'t-J-J!K"4)B)BC %d,H,H I"4)B)BCt%:%:;d$8$89d$8$89T#6#67 	

d..<<<!    c                 .    U R                   R                  $ N)r&   layoutrH   s    rK   rQ   CommandLineInterface.layout   s    &&&rN   c                 .    U R                   R                  $ rP   )r&   	clipboardrR   s    rK   rU   CommandLineInterface.clipboard   s    )))rN   c                 .    U R                   R                  $ rP   )r&   pre_run_callablesrR   s    rK   rX   &CommandLineInterface.pre_run_callables   s    111rN   c                 x  ^ ^^^ [        T[        5      (       d   eTT R                  U'   U(       a  T R                  R                  U5        T R	                  T5      mT R                  T5      mTT R                  U'   UUU4S jnT=R                  U" 5       -  sl        U 4S jnT=R                  U-  sl        g)z
Insert a new buffer.
c                     > UUU4S jn U $ )z
Wrapper around the asynchronous completer and auto suggestion, that
ensures that it's only called while typing if the
`complete_while_typing` filter is enabled.
c                    > TR                   (       a  TR                  5       (       a  T" 5         TR                  (       a  T" 5         g g rP   )	completercomplete_while_typingauto_suggest)_auto_suggest_functionbuffercompleter_functions    rK   on_text_insertYCommandLineInterface.add_buffer.<locals>.create_on_insert_handler.<locals>.on_text_insert   s6    ##(D(D(F(F&( &&)+ 'rN    )rd   ra   rb   rc   s    rK   create_on_insert_handlerACommandLineInterface.add_buffer.<locals>.create_on_insert_handler   s    , "!rN   c                 :   > TR                   R                  5         g)z
When the text in a buffer changes.
(A paste event is also a change, but not an insert. So we don't
want to do autocompletions in this case, but we want to propagate
the on_buffer_changed event.)
N)r>   rG   )r`   rH   s    rK   buffer_changed7CommandLineInterface.add_buffer.<locals>.buffer_changed   s     ""'')rN   N)	r$   r   r-   focus_create_auto_suggest_function_create_async_completerr:   rd   on_text_changed)rH   rI   rb   rl   rg   rj   ra   rc   s   ` `   @@rK   r=   CommandLineInterface.add_buffer   s     &&))))#TLLt$ !% B B6 J!99&A'9t$	"" 	!9!;;	* 	.0rN   c           	          U=(       d    U R                   nU R                  R                  U5      nU(       a  U" UUU[        SS9S9  gg)zz
Start asynchronous autocompletion of this buffer.
(This will do nothing if a previous completion was still in progress.)
T)completion_requested)select_firstselect_lastinsert_common_partcomplete_eventN)current_buffer_namer:   getr
   )rH   buffer_namers   rt   ru   rv   r]   s          rK   start_completion%CommandLineInterface.start_completion   sK     "=T%=%=**..{;	<"-);%2%MO rN   c                 8    U R                   R                  U 5      $ )z9
The name of the current  :class:`.Buffer`. (Or `None`.)
)r-   current_namerR   s    rK   rw   (CommandLineInterface.current_buffer_name   s    
 ||((..rN   c                 8    U R                   R                  U 5      $ )z
The currently focussed :class:`~.Buffer`.

(This returns a dummy :class:`.Buffer` when none of the actual buffers
has the focus. In this case, it's really not practical to check for
`None` values or catch exceptions every time.)
)r-   currentrR   s    rK   current_buffer#CommandLineInterface.current_buffer   s     ||##D))rN   c                 :    U R                   R                  X5        g)z:
Focus the buffer with the given name on the focus stack.
N)r-   rl   rH   ry   s     rK   rl   CommandLineInterface.focus   s     	4-rN   c                 :    U R                   R                  X5        g)z
Push to the focus stack.
N)r-   
push_focusr   s     rK   r   CommandLineInterface.push_focus   s     	2rN   c                 :    U R                   R                  U 5        g)z
Pop from the focus stack.
N)r-   	pop_focusrR   s    rK   r   CommandLineInterface.pop_focus   s     	t$rN   c                     U R                   R                  5       nUb!  [        U[        R                  5      (       d   eU$ )zy
Return the current title to be displayed in the terminal.
When this in `None`, the terminal title remains the original.
)r&   	get_titler$   six	text_type)rH   results     rK   terminal_title#CommandLineInterface.terminal_title   s:     !!++- ~FCMM!B!BBBrN   c                 (    U R                   [        :H  $ )z
True when we are searching.
)rw   r   rR   s    rK   is_searching!CommandLineInterface.is_searching  s    
 ''=88rN   c                 j  ^  ST l         ST l        ST l        T R                  R	                  5         T R
                  R	                  5         T R                  R	                  5         T R                  R	                  5         [        [        U 4S j5      S9T l
        T R                  R                  5         g)a?  
Reset everything, for reading the next input.

:param reset_current_buffer: XXX: not used anymore. The reason for
    having this option in the past was when this CommandLineInterface
    is run multiple times, that we could reset the buffer content from
    the previous run. This is now handled in the AcceptAction.
FNc                     > T R                   $ rP   )is_ignoring_caserR   s   rK   <lambda>,CommandLineInterface.reset.<locals>.<lambda>,  s    dF[F[rN   )ignore_case)
_exit_flag_abort_flag_return_valuer2   rF   r9   rQ   r0   r   r   search_staterC   rG   )rH   reset_current_buffers   ` rK   rF   CommandLineInterface.reset  s       !""$ (I>[4\] 	rN   c                 8    U R                   R                  U 5      $ )z True when we are in paste mode. )r&   
paste_moderR   s    rK   in_paste_mode"CommandLineInterface.in_paste_mode1  s     **400rN   c                 8    U R                   R                  U 5      $ )z&True when we currently ignore casing. )r&   r   rR   s    rK   r   %CommandLineInterface.is_ignoring_case6  s     ++D11rN   c                 2  ^  T R                   (       a  gST l         T R                  R                  5         T R                  bV  U 4S jnT R                  (       a#  [
        R
                  " 5       T R                  -   nOSnT R                  R                  XS9  gg)zG
Thread safe way of sending a repaint trigger to the input event loop.
NTc                  4   > ST l         T R                  5         g NF)r5   _redrawrR   s   rK   redraw/CommandLineInterface.invalidate.<locals>.redrawK  s    $)!rN   _max_postpone_until)r5   rA   rG   r'   r4   timecall_from_executor)rH   r   r   s   `  rK   
invalidateCommandLineInterface.invalidate;  s      $D 	!>>% ,,&*iikD4Q4Q&Q#&*#NN-- . A &rN   c                     U R                   (       al  U R                  c^  U =R                  S-  sl        U R                  R	                  X R
                  U R                  S9  U R                  R                  5         ggg)z
Render the command line again. (Not thread safe!) (From other threads,
or if unsure, use :meth:`.CommandLineInterface.invalidate`.)
Nr   )is_done)	r(   r;   r3   r2   renderrQ   r   rB   rG   rR   s    rK   r   CommandLineInterface._redraw^  s`      51$MM  {{DLL I NN! !6rN   c                     U R                   R                  SSS9  U R                   R                  5         U R                  5         g)z
When the window size changes, we erase the current output and request
again the cursor position. When the CPR answer arrives, the output is
drawn again.
F)leave_alternate_screenerase_titleN)r2   erase request_absolute_cursor_positionr   rR   s    rK   
_on_resizeCommandLineInterface._on_resizek  s4     	5eL668rN   c                     U R                   R                  5        H)  u  pXR                  ;   d  M  X R                  U   l        M+     g rP   )_next_buffer_indexesr<   r-   working_index)rH   buffindexs      rK   _load_next_buffer_indexes.CommandLineInterface._load_next_buffer_indexesw  s6    44::<KD||#38T"0 =rN   c                 r    U(       a  U" 5         U R                    H
  nU" 5         M     U R                   SS2	 g)zCalled during `run`. N)rX   )rH   pre_runcs      rK   _pre_runCommandLineInterface._pre_run|  s/    I ''AC (""1%rN   c                    Ub  [        U5      (       d   e SU l        U R                  R                  5         U R	                  5         U R                  U5        U R                  R                  5          U R                  R                  5         U R                  5         U R                  R                  U R                  U R                  5       5        SSS5        U R                  (       d  SU l        U R                  5         U R                  R	                  5         U R                   R                  5         SU l        U R#                  5       $ ! , (       d  f       N= f! U R                  (       d  SU l        U R                  5         U R                  R	                  5         U R                   R                  5         SU l        f = f)a	  
Read input from the command line.
This runs the eventloop until a return value has been set.

:param reset_current_buffer: XXX: Not used anymore.
:param pre_run: Callable that is called right after the reset has taken
    place. This allows custom initialisation.
NTF)callabler(   rD   rG   rF   r   r,   raw_moder2   r   r   r'   runcreate_eventloop_callbacksr   r   rE   return_valuerH   r   r   s      rK   r   CommandLineInterface.run  s;    (7"3"333	%#DMM JJL MM'" $$&>>@""4::t/N/N/PQ	 ' <<"&MM!LL$D   ""+ '& <<"&MM!LL$Ds&   AE 3AEE 
EE A%GaQ  
        def run_async(self, reset_current_buffer=True, pre_run=None):
            """
            Same as `run`, but this returns a coroutine.

            This is only available on Python >3.3, with asyncio.
            """
            # Inline import, because it slows down startup when asyncio is not
            # needed.
            import asyncio

            @asyncio.coroutine
            def run():
                assert pre_run is None or callable(pre_run)

                try:
                    self._is_running = True

                    self.on_start.fire()
                    self.reset()

                    # Call pre_run.
                    self._pre_run(pre_run)

                    with self.input.raw_mode():
                        self.renderer.request_absolute_cursor_position()
                        self._redraw()

                        yield from self.eventloop.run_as_coroutine(
                                self.input, self.create_eventloop_callbacks())

                    return self.return_value()
                finally:
                    if not self.is_done:
                        self._exit_flag = True
                        self._redraw()

                    self.renderer.reset()
                    self.on_stop.fire()
                    self._is_running = False

            return run()
        c                     [         e)zd
Same as `run`, but this returns a coroutine.

This is only available on Python >3.3, with asyncio.
)NotImplementedErrorr   s      rK   	run_asyncCommandLineInterface.run_async  s
     &%rN   c                   ^ ^^^^^ [        T[        5      (       d   eTb  [        T5      (       d   eT R                  b  [	        S5      eT(       d  T R
                  R                  5         UUUUU U4S jn[        T[        T U5      T R                  T R                  S9mSTl        TR                  5         TT l        g)ax  
Run a sub :class:`~prompt_toolkit.application.Application`.

This will suspend the main application and display the sub application
until that one returns a value. The value is returned by calling
`done_callback` with the result.

The sub application will share the same I/O of the main application.
That means, it uses the same input and output channels and it shares
the same event loop.

.. note:: Technically, it gets another Eventloop instance, but that is
    only a proxy to our main event loop. The reason is that calling
    'stop' --which returns the result of an application when it's
    done-- is handled differently.
Nz(Another sub application started already.c                  x  > TR                  5         T(       d  TR                  (       a  TR                  R                  5         TR                  R	                  5         STl        S Tl        T (       d*  TR                  R                  5         TR                  5         T(       a  T" TR                  5       5        g g r   )	r   erase_when_doner2   r   rF   r(   r;   r   r   )_from_application_generatorr&   done_callbackr   rH   sub_clis   rK   done6CommandLineInterface.run_sub_application.<locals>.done	  s     OO+"="=  &&(""$"'G DM />>@ g2245 rN   )r&   r'   r,   r)   T)r$   r   r   r;   RuntimeErrorr2   r   r   _SubApplicationEventLoopr,   r)   r(   r   )rH   r&   r   r   r   r   r   s   ````` @rK   run_sub_application(CommandLineInterface.run_sub_application  s    & +{3333$(?(???==$IJJ +MM!	6 	6, '#.tT:**;;	 
 #rN   c                    U R                   R                  nSU l        U R                  5         U[        R
                  :X  a  S nU R                  U5        gU[        R                  :X  aE  U R                  5         U R                  R                  5         U R                  R                  5         gU[        R                  :X  a  U R                  S5        gg)z,
Set exit. When Control-D has been pressed.
Tc                      [        5       erP   )EOFErrorrf   rN   rK   	eof_error,CommandLineInterface.exit.<locals>.eof_error2  s
    j rN   N)r&   on_exitr   r   r   RAISE_EXCEPTION_set_return_callableRETRYrF   r2   r   r   RETURN_NONEset_return_value)rH   r   r   s      rK   exitCommandLineInterface.exit)  s     ""**k111!%%i0)))JJLMM::<%%'///!!$' 0rN   c                    U R                   R                  nSU l        U R                  5         U[        R
                  :X  a  S nU R                  U5        gU[        R                  :X  aE  U R                  5         U R                  R                  5         U R                  R                  5         gU[        R                  :X  a  U R                  S5        gg)z-
Set abort. When Control-C has been pressed.
Tc                      [        5       erP   )KeyboardInterruptrf   rN   rK   keyboard_interrupt6CommandLineInterface.abort.<locals>.keyboard_interruptG  s    '))rN   N)r&   on_abortr   r   r   r   r   r   rF   r2   r   r   r   r   )rH   r   r   s      rK   abortCommandLineInterface.abort>  s     ##,,{222*%%&89***JJLMM::<%%'000!!$' 1rN   c                 P   ^ U R                  U4S j5        U R                  5         g)zY
Set a return value. The eventloop can retrieve the result it by calling
`return_value`.
c                     > T $ rP   rf   )documents   rK   r   7CommandLineInterface.set_return_value.<locals>.<lambda>\  s    (rN   N)r   r   )rH   r   s    `rK   r   %CommandLineInterface.set_return_valueW  s    
 	!!"23rN   c                     [        U5      (       d   eXl        U R                  (       a  U R                  R                  5         g g rP   )r   r   r'   stop)rH   values     rK   r   )CommandLineInterface._set_return_callable_  s2    ">>NN! rN   c                    U(       a2  SU l         U R                  5         U R                  R                  5         OU R                  R	                  5         SU l         U R
                  R                  5          U" 5       nSSS5        U R                  R                  5         U R                  R                  5         U R                  5         W$ ! , (       d  f       NT= f)a  
Run function on the terminal above the prompt.

What this does is first hiding the prompt, then running this callable
(which can safely output to the terminal), and then again rendering the
prompt which causes the output of this function to scroll above the
prompt.

:param func: The callable to execute.
:param render_cli_done: When True, render the interface in the
        'Done' state first, then execute the function. If False,
        erase the interface first.

:returns: the result of `func`.
TN)r   r   r2   rF   r   r,   cooked_moder   )rH   funcrender_cli_doner   s       rK   run_in_terminal$CommandLineInterface.run_in_terminalf  s    " !%DLLNMM!MM!! ZZ##%VF & 	668 &%s   5C
Cc                 @  ^ ^^^ U(       a2  ST l         T R                  5         T R                  R                  5         OT R                  R	                  5         ST l         U" 5       m[        T[        R                  5      (       d   eSUUU U4S jjmU 4S jmT" 5         g)a	  
EXPERIMENTAL
Like `run_in_terminal`, but takes a generator that can yield Application instances.

Example:

    def f():
        yield Application1(...)
        print('...')
        yield Application2(...)
    cli.run_in_terminal_async(f)

The values which are yielded by the given coroutine are supposed to be
`Application` instances that run in the current CLI, all other code is
supposed to be CPU bound, so except for yielding the applications,
there should not be any user interaction or I/O in the given function.
TNc                    >  TR                   R                  5          TR                  U 5      nSSS5        [        W[        5      (       d   eTR                  UTSS9  g! , (       d  f       N7= f! [         a
    T" 5          g  T" 5         e = f)z#Execute next step of the coroutine.NT)r   r   )r,   r  sendr$   r   r   StopIteration)
send_valuer   r   grH   	step_nexts     rK   r  ACommandLineInterface.run_application_generator.<locals>.step_next  s    KZZ++-VVJ/F . "&+6666((yEI ) K .-  s'   A1 A A1  
A.*A1 1B	Bc                     > T R                   R                  5         T R                   R                  5         T R                  5         g rP   )r2   rF   r   r   rR   s   rK   r   <CommandLineInterface.run_application_generator.<locals>.done  s,    MM!MM::<LLNrN   rP   )r   r   r2   rF   r   r$   typesGeneratorType)rH   	coroutiner  r   r  r  s   `  @@@rK   run_application_generator.CommandLineInterface.run_application_generator  s{    & !%DLLNMM!MM!! K!U001111	K 	K"	 	rN   c                 H   ^ ^^ U 4S jmUU U4S jnT R                  U5        g)z
Run system command (While hiding the prompt. When finished, all the
output will scroll above the prompt.)

:param command: Shell command to be executed.
c                     > SSK Jn   [        5       nUR                  [        R
                  5      UR                  [        R                  5      S 5       5       nU " SUS9nTR                  U5        g)z
Create a sub application to wait for the enter key press.
This has two advantages over using 'input'/'raw_input':
- This will share the same input/output I/O.
- This doesn't block the event loop.
r   )create_prompt_applicationc                 :    U R                   R                  S 5        g rP   )clir   )events    rK   r`   JCommandLineInterface.run_system_command.<locals>.wait_for_enter.<locals>._  s     		**40rN   zPress ENTER to continue...)messager6   N)r%   r  r   add_bindingr   ControlJControlMr   )r  registryr`   r&   rH   s       rK   wait_for_enter?CommandLineInterface.run_system_command.<locals>.wait_for_enter  si     =zH!!$--0!!$--01 1 11 44&.0K $$[1rN   c                  n  >  TR                   R                  5       n  TR
                  R                  5       n[        TSXS9nUR                  5         T" 5         g ! [         a!    [        R                  R                  5       n  Nhf = f! [         a!    [        R                  R                  5       n N{f = f)NT)shellr+   stdout)	r,   filenoAttributeErrorr*   r+   r)   r&  r   wait)input_fd	output_fdpcommandrH   r"  s      rK   r   4CommandLineInterface.run_system_command.<locals>.run  s    .::,,.0 KK..0	 gT$8AFFH  " .99++-. " 0JJ--/	0s"   A B	 (BB	(B43B4N)r  )rH   r-  r   r"  s   `` @rK   run_system_command'CommandLineInterface.run_system_command  s    	2*	* 	S!rN   c                 `   ^ [        [        S5      (       a  U4S jnU R                  U5        gg)z
(Not thread safe -- to be called from inside the key bindings.)
Suspend process.

:param suspend_group: When true, suspend the whole process group.
    (This is the default, and probably what you want.)
SIGTSTPc                     > T (       a&  [         R                  " S[        R                  5        g [         R                  " [         R                  " 5       [        R                  5        g )Nr   )oskillsignalr2  getpid)suspend_groups   rK   r   7CommandLineInterface.suspend_to_background.<locals>.run  s2     !GGAv~~.GGBIIK8rN   N)hasattrr6  r  )rH   r8  r   s    ` rK   suspend_to_background*CommandLineInterface.suspend_to_background  s,     69%%	9   % &rN   c                 j    [        U R                  X=(       d    U R                  R                  5        g)z
Print a list of (Token, text) tuples to the output.
(When the UI is running, this method has to be called through
`run_in_terminal`, otherwise it will destroy the UI.)

:param style: Style class to use. Defaults to the active style in the CLI.
N)r   r)   r&   r1   )rH   tokensr1   s      rK   r   !CommandLineInterface.print_tokens  s#     	T[[&*I43C3C3I3IJrN   c                     U R                   $ )z*
``True`` when the exit flag as been set.
)r   rR   s    rK   
is_exitingCommandLineInterface.is_exiting  s    
 rN   c                     U R                   $ )z+
``True`` when the abort flag as been set.
)r   rR   s    rK   is_aborting CommandLineInterface.is_aborting  s    
 rN   c                     U R                   SL$ )z,
``True`` when a return value has been set.
Nr   rR   s    rK   is_returning!CommandLineInterface.is_returning&  s    
 !!--rN   c                 F    U R                   (       a  U R                  5       $ g)zD
Get the return value. Not that this method can throw an exception.
NrG  rR   s    rK   r   !CommandLineInterface.return_value-  s!     %%'' rN   c                 f    U R                   =(       d    U R                  =(       d    U R                  $ rP   )rA  rD  rH  rR   s    rK   r   CommandLineInterface.is_done6  s"    G$"2"2Gd6G6GGrN   c                 8   ^ ^^^^ S/mS m  SUUUUU 4S jjmT$ )zR
Create function for asynchronous autocompletion.
(Autocomplete in other thread.)
Fc                 l    U R                   nU[        U5      UR                  -   S nX1R                  :H  $ )zj
Return `True` if applying this completion doesn't have any effect.
(When it doesn't insert any new text.
N)text_before_cursorlenstart_positiontext)r   
completionrP  replaced_texts       rK   completion_does_nothingMCommandLineInterface._create_async_completer.<locals>.completion_does_nothingA  s?    
 "*!<!<.&'**C*CCDFM OO33rN   c           
        >^ ^^^^ TR                   mT=(       d	    [        SS9mTS   (       a  g TR                  (       d  TR                  (       d  g STS'   UUUUU	UUU UU
4
S jnT
R                  R                  U5        g )NT)text_insertedr   c            
         >
^ [        TR                  R                  TT5      5      mUUUUUUUU	U
U4
S jn TR                  (       a  TR                  R	                  U 5        g g )Nc                    >
 STS'   [        T5      S:X  a  T" T	TS   5      (       a  TSS2	 TR                  T	R                  :X  a  TR                  T	R                  :X  a  TR                  (       d  Sn SnT
(       av  [	        T	T5      nU(       aR  TR                  U5        [        T5      S:  a/  T Vs/ s H  nUR                  [        U5      5      PM     snTSS& OSn O[        T5      S:X  a  SnU (       a  TR                  TT=(       d    UTS9  TR                  5         gTR                  (       d  T" 5         ggs  snf )z
Set the new complete_state in a safe way. Don't replace an
existing complete_state if we had one. (The user could have
pressed 'Tab' in the meantime. Also don't set it if the text
was changed in the meantime.
Fr   r   NT)completionsgo_to_first
go_to_last)	rQ  rS  cursor_positioncomplete_stater   insert_textnew_completion_from_positionset_completionsr   )rc  select_first_anywaycommon_partr   async_completerrb   complete_thread_runningrV  r\  r   ru   rs   rt   rH   s       rK   callbackdCommandLineInterface._create_async_completer.<locals>.async_completer.<locals>.run.<locals>.callback^  s9    27+A. K(A-3Hk!nMM'N {{hmm3"22h6N6NN & 5 5*..3+ .*DX{*[K* & 2 2; ?#&{#3a#7 2=6>1<A )*(F(Fs;GW(X1<6>KN 7<O $'{#3q#8:>$7*"22,7,8,O<O+6 3 8 )#22') 3+6>s   3$D>)listr]   get_completionsr'   r   )rh  r\  rf  rb   rv   rg  rV  r   ru   rs   rt   rH   s    @rK   r   RCommandLineInterface._create_async_completer.<locals>.async_completer.<locals>.run[  sK    "6#3#3#C#CHn#]^8* 8*t >>NN55h? "rN   )r   r
   r`  r]   r'   run_in_executor)rs   rt   ru   rv   r   r   rf  rb   rg  rV  rH   s   ```` @rK   rf  ECommandLineInterface._create_async_completer.<locals>.async_completerK  sl    H+P}4/PN 'q) $$F,<,< *.#A&>@ >@@ NN**3/rN   )FFFNrf   )rH   rb   rf  rg  rV  s   ``@@@rK   rn   ,CommandLineInterface._create_async_completer:  s0    
 $)'	4 =BEIP	0 P	0b rN   c                 &   ^ ^^^ S/mUUU U4S jmT$ )zR
Create function for asynchronous auto suggestion.
(AutoSuggest in other thread.)
Fc                     >^ TR                   mTS   (       a  g TR                  (       d  TR                  (       d  g STS'   UUUUU4S jn TR                  R	                  U 5        g )Nr   Tc                     >^ TR                   R                  TTT5      mUUUUUU4S jn TR                  (       a  TR                  R                  U 5        g g )Nc                     > STS'   TR                   TR                   :X  a2  TR                  TR                  :X  a  TTl        TR                  5         g T " 5         g )NFr   )rS  r_  
suggestionr   )async_suggestorrb   r   rH   suggest_thread_runningrt  s   rK   rh  jCommandLineInterface._create_auto_suggest_function.<locals>.async_suggestor.<locals>.run.<locals>.callback  sP    05*1- {{hmm3"22h6N6NN -7)) ()rN   )r_   get_suggestionr'   r   )rh  rt  ru  rb   r   rH   rv  s    @rK   r   XCommandLineInterface._create_auto_suggest_function.<locals>.async_suggestor.<locals>.run  sG    #00??fhW
* * >>NN55h? "rN   )r   rt  r_   r'   rm  )r   r   ru  rb   rH   rv  s    @rK   ru  KCommandLineInterface._create_auto_suggest_function.<locals>.async_suggestor  s\    H &a(   (;(; )-"1%@ @( NN**3/rN   rf   )rH   rb   ru  rv  s   ``@@rK   rm   2CommandLineInterface._create_auto_suggest_function  s     
 #("	0 "	0F rN   c                     [        XS9$ )aC  
Create an :class:`_StdoutProxy` class which can be used as a patch for
`sys.stdout`. Writing to this proxy will make sure that the text
appears above the prompt, and that it doesn't destroy the output from
the renderer.

:param raw: (`bool`) When True, vt100 terminal escape sequences are not
            removed/escaped.
raw)_StdoutProxy)rH   r~  s     rK   stdout_proxy!CommandLineInterface.stdout_proxy  s     D**rN   c                 0    [        U R                  US9X#S9$ )a  
Return a context manager that will replace ``sys.stdout`` with a proxy
that makes sure that all printed text will appear above the prompt, and
that it doesn't destroy the output from the renderer.

:param patch_stdout: Replace `sys.stdout`.
:param patch_stderr: Replace `sys.stderr`.
r}  )patch_stdoutpatch_stderr)_PatchStdoutContextr  )rH   r~  r  r  s       rK   patch_stdout_context)CommandLineInterface.patch_stdout_context  s'     ##&%B 	BrN   c                     [        U 5      $ rP   )_InterfaceEventLoopCallbacksrR   s    rK   r   /CommandLineInterface.create_eventloop_callbacks  s    +D11rN   )r   r:   r   r5   r(   r   r;   r&   r-   r.   r'   r,   r9   r4   r>   r?   r@   rA   rB   rC   rD   rE   r)   r/   r3   r2   r   r0   )NNNF)NFFFNrP   )FN)TN)NFF)T)FTT):__name__
__module____qualname____firstlineno____doc__rL   propertyrQ   rU   rX   r=   rz   rw   r   rl   r   r   r   r   rF   r   r   r   request_redrawr   r   r   r   r   r   exec_textwrapdedentSyntaxErrorr   r   r   r   set_exit	set_abortr   r   r  r  r/  r;  r   rA  rD  rH  r   r   rn   rm   r  r  r   __static_attributes__rf   rN   rK   r   r   /   s   &J"X ' ' * * 2 2-1^ ?D?D(,O  / / * *.3% 
 
 9 9> 1 1 2 2AB  N"
9
&*#X7& 			(// *# * *	j TY8=< |(*(, HI""H7r1"f&0K       . .( H HbH*X
+B2E  &	&&s   
"D D! D!r   c                   @    \ rS rSrSrS r\S 5       rS rS r	S r
Srg	)
r  i  zY
Callbacks on the :class:`.CommandLineInterface` object, to which an
eventloop can talk.
c                 >    [        U[        5      (       d   eXl        g rP   )r$   r   r  rH   r  s     rK   rL   %_InterfaceEventLoopCallbacks.__init__  s    #34444rN   c                 ~    U R                   nUR                  (       a  UR                  nUR                  (       a  M  U$ )z+
Return the active `CommandLineInterface`.
)r  r;   r  s     rK   _active_cli(_InterfaceEventLoopCallbacks._active_cli  s1    
 hh ll,,C lll 
rN   c                 8    U R                   R                  5         g)z:
Report terminal size change. This will trigger a redraw.
N)r  r   rR   s    rK   terminal_size_changed2_InterfaceEventLoopCallbacks.terminal_size_changed  s     	##%rN   c                 P    U R                   nUR                  R                  5         g rP   )r  r@   rG   r  s     rK   input_timeout*_InterfaceEventLoopCallbacks.input_timeout  s    !!#rN   c                     [        U[        5      (       d   eU R                  nUR                  (       d6  UR                  R                  U5        UR                  R                  5         gg)z/
Feed a key press to the CommandLineInterface.
N)r$   r   r  r   r9   feedprocess_keys)rH   	key_pressr  s      rK   feed_key%_InterfaceEventLoopCallbacks.feed_key  sV     )X....
 {{$$Y/,,. rN   )r  N)r  r  r  r  r  rL   r  r  r  r  r  r  rf   rN   rK   r  r    s/     
 
&$/rN   r  c                   *    \ rS rSrSS jrS rS rSrg)r  i  c                 (    Xl         X l        X0l        g rP   )
new_stdoutr  r  )rH   r  r  r  s       rK   rL   _PatchStdoutContext.__init__  s    $((rN   c                     [         R                  U l        [         R                  U l        U R
                  (       a  U R                  [         l        U R                  (       a  U R                  [         l        g g rP   )r*   r&  original_stdoutstderroriginal_stderrr  r  r  rR   s    rK   	__enter___PatchStdoutContext.__enter__  sG    "zz"zzCJCJ rN   c                     U R                   (       a  U R                  [        l        U R                  (       a  U R
                  [        l        g g rP   )r  r  r*   r&  r  r  r  )rH   akws      rK   __exit___PatchStdoutContext.__exit__%  s3    --CJ--CJ rN   )r  r  r  r  r  N)TT)r  r  r  r  rL   r  r  r  rf   rN   rK   r  r    s    )
).rN   r  c                   @    \ rS rSrSrSS jrS rS rS rS r	S r
S	rg
)r  i-  zN
Proxy for stdout, as returned by
:class:`CommandLineInterface.stdout_proxy`.
c                 6   [        U[        5      (       d   e[        U[        5      (       d   e[        R                  " 5       U l        Xl        X l        / U l        [        R                  R                  U l        [        R                  R                  U l        g rP   )r$   r   bool	threadingRLock_lock_cli_raw_bufferr*   
__stdout__errorsencoding)rH   r  r~  s      rK   rL   _StdoutProxy.__init__2  sj    #34444#t$$$$__&
		nn++//rN   c                     U R                   R                  (       aQ  [        R                  " U R                   R                  U5      nU R                   R
                  R                  U5        g U" 5         g rP   )r  r(   	functoolspartialr  r'   r   )rH   r  r  s      rK   _do_StdoutProxy._do>  sH    99  '//		0I0I4POII22?CFrN   c                    ^ ^ SU;   aF  UR                  SS5      u  p#T R                  US/-   mU/T l        U U4S jnT R                  U5        gT R                  R                  U5        g)a  
        Note: print()-statements cause to multiple write calls.
              (write('line') and write('
')). Of course we don't want to call
              `run_in_terminal` for every individual call, because that's too
              expensive, and as long as the newline hasn't been written, the
              text itself is again overwritter by the rendering of the input
              command line. Therefor, we have a little buffer which holds the
              text until a newline is written to stdout.
        
r   c                     > T H`  n TR                   (       a'  TR                  R                  R                  U 5        M;  TR                  R                  R	                  U 5        Mb     g rP   )r  r  r)   	write_rawwrite)srH   to_writes    rK   r    _StdoutProxy._write.<locals>.runV  sD    !Ayy		((2215		((..q1	 "rN   N)rsplitr  r  append)rH   databeforeafterr   r  s   `    @rK   _write_StdoutProxy._writeE  sa     4< !KKa0MF||vtn4H!7DL2 HHSM LL%rN   c                 r    U R                      U R                  U5        S S S 5        g ! , (       d  f       g = frP   )r  r  )rH   r  s     rK   r  _StdoutProxy.writea  s    ZZKK ZZs   (
6c                 4   ^  U 4S jnT R                  U5        g )Nc                  <  > TR                    H`  n TR                  (       a'  TR                  R                  R	                  U 5        M;  TR                  R                  R                  U 5        Mb     / Tl         TR                  R                  R                  5         g rP   )r  r  r  r)   r  r  flush)r  rH   s    rK   r    _StdoutProxy._flush.<locals>.runf  sg    \\99II$$..q1II$$**1-	 "
 DLII""$rN   )r  )rH   r   s   ` rK   _flush_StdoutProxy._flushe  s    	% 	rN   c                 p    U R                      U R                  5         SSS5        g! , (       d  f       g= f)z
Flush buffered output.
N)r  r  rR   s    rK   r  _StdoutProxy.flushp  s     ZZKKM ZZs   '
5)r  r  r  r  r  r  Nr  )r  r  r  r  r  rL   r  r  r  r  r  r  rf   rN   rK   r  r  -  s%    
0&8	rN   r  c                   F    \ rS rSrSrS rS rS rS rSS jr	S	 r
S
 rSrg)r   ix  a  
Eventloop used by sub applications.

A sub application is an `Application` that is "spawned" by a parent
application. The parent application is suspended temporarily and the sub
application is displayed instead.

It doesn't need it's own event loop. The `EventLoopCallbacks` from the
parent application are redirected to the sub application. So if the event
loop that is run by the parent application detects input, the callbacks
will make sure that it's forwarded to the sub application.

When the sub application has a return value set, it will terminate
by calling the `stop` method of this event loop. This is used to
transfer control back to the parent application.
c                 n    [        U[        5      (       d   e[        U5      (       d   eXl        X l        g rP   )r$   r   r   r  stop_callback)rH   r  r  s      rK   rL   !_SubApplicationEventLoop.__init__  s2    #34444&&&&*rN   c                 $    U R                  5         g rP   )r  rR   s    rK   r   _SubApplicationEventLoop.stop  s    rN   c                     g rP   rf   rR   s    rK   close_SubApplicationEventLoop.close  s    rN   c                 N    U R                   R                  R                  U5        g rP   )r  r'   rm  )rH   rh  s     rK   rm  (_SubApplicationEventLoop.run_in_executor  s    **84rN   Nc                 J    U R                   R                  R                  XS9  g )Nr   )r  r'   r   )rH   rh  r   s      rK   r   +_SubApplicationEventLoop.call_from_executor  s"    -- 	. 	?rN   c                 N    U R                   R                  R                  X5        g rP   )r  r'   
add_reader)rH   fdrh  s      rK   r  #_SubApplicationEventLoop.add_reader  s    %%b3rN   c                 N    U R                   R                  R                  U5        g rP   )r  r'   remove_reader)rH   r  s     rK   r  &_SubApplicationEventLoop.remove_reader  s    ((,rN   )r  r  rP   )r  r  r  r  r  rL   r   r  rm  r   r  r  r  rf   rN   rK   r   r   x  s*     +5?4-rN   r   )>r  
__future__r   r  r4  r6  r   r*   r  r  r   r  r7   
subprocessr   r&   r   r   rb   r   buffer_mappingr	   rT  r
   r   enumsr   eventloop.baser   eventloop.callbacksr   filtersr   r,   r   r   key_binding.input_processorr   r   key_binding.registryr   key_binding.vi_stater   keysr   r)   r   r2   r   r   r   r   utilsr   r   __all__objectr   r  r  r  r   rf   rN   rK   <module>r	     s    (  	  
 
       1  ) A   % 3  $ 7 1 * )   , %  !u26 u2p,/#5 ,/^.& ..H6 HV)-y )-rN   