
    =                        S r SSKJr  SSKJr  SSKJr  SSKJr  SSKrSSK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
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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J)r)  SSKJ*r*  SSKJ+r+  SSK,J-r.   " S S \)R^                  5      r0 " S! S"\.Rb                  5      r2S(S# jr3S$ r4 " S% S&\55      r6S)S' jr7g)*z#The gcloud interactive application.    )absolute_import)division)print_function)unicode_literalsN)cli_tree)bindings)bindings_vi)	completer)coshell)debug)layout)parser)style)generate_cli_trees)config)
properties)named_configs)application)auto_suggest)buffer)document)enums)filters)history)	interface)	shortcuts)token)
processorsc                   P   ^  \ rS rSrSr   SU 4S jjrSS jrSU 4S jjrSrU =r	$ )	CLI8   a  Extends the prompt CLI object to include our state.

Attributes:
  command_count: Command line serial number, incremented on ctrl-c and Run.
  completer: The interactive completer object.
  config: The interactive shell config object.
  coshell: The shell coprocess object.
  debug: The debugging object.
  parser: The interactive parser object.
  root: The root of the static CLI tree that contains all commands, flags,
    positionals and help doc snippets.
c
                    > [         [        U ]  UUU	S9  SU l        X`l        Xl        X l        X0l        XPl        X@l	        g )N)r   	eventloopoutputr   )
superr    __init__command_countr
   r   r   r   r   root)selfr   r   r   r(   interactive_parserinteractive_completerr   r#   r$   	__class__s             9lib/googlecloudsdk/command_lib/interactive/application.pyr&   CLI.__init__F   sJ     
#t   D*NKLJ$KI    c                    U(       a  U R                   R                  5         U R                  R                  U5        U(       aE  U R                   R                  SSS9  U R                   R	                  5         U R                  5         gg)a  Runs the command line in text, optionally in an alternate screen.

This should use an alternate screen but I haven't found the incantations
to get that working. Currently alternate_screen=True clears the default
screen so full screen commands, like editors and man or help, have a clean
slate. Otherwise they may overwrite previous output and end up with a
garbled mess. The downside is that on return the default screen is
clobbered. Not too bad right now because this is only used as a fallback
when the real web browser is inaccessible (for example when running in ssh).

Args:
  text: The command line string to run.
  alternate_screen: Send output to an alternate screen and restore the
    original screen when done.
F)leave_alternate_screenerase_titleN)renderereraser   Run request_absolute_cursor_position_redraw)r)   textalternate_screens      r-   r5   CLI.RunU   sa      
mmLLT
mmEJ
mm446
lln r/   c                    >^ [         [        U ]  UTU5        SU4S jjnTR                  5       (       a>  TR                  U:w  a-  U R
                  U   Tl        TR                  Tl        UTl        ggg)z?MONKEYPATCH! Calls the async completer on delete before cursor.c                 F   > TR                  U S9nTR                  5         U$ )N)count)patch_real_delete_before_cursorpatch_completer_function)r=   deletedbufs     r-   DeleteBeforeCursor*CLI.add_buffer.<locals>.DeleteBeforeCursorr   s)    33%3@g 
""$nr/   N)   )r%   r    
add_buffercomplete_while_typingdelete_before_cursor_async_completersr?   r>   )r)   namerA   focusrB   r,   s     `  r-   rE   CLI.add_buffern   sn    	#tc51 	!!##  $66%)%;%;D%Ac",/,D,Dc)!3c 	7 	$r/   )r'   r
   r   r   r   r   r(   )	NNNNNNNNN)F)
__name__
__module____qualname____firstlineno____doc__r&   r5   rE   __static_attributes____classcell__)r,   s   @r-   r    r    8   s)     BF>B8<24 4r/   r    c                   (    \ rS rSrSr\S 5       rSrg)Context   z"Input processor that adds context.c                   ^  T R                   (       d[  UR                  (       dJ  ST l         T R                  R                  [        R
                  " T R                  R                  5      5        [        R                  " UU 4S jS9$ )NTc                 B   > [        TR                  R                  5      $ N)lenr   context)iclis    r-   <lambda>.Context.apply_transformation.<locals>.<lambda>   s    C

0B0B,Cr/   )display_to_source)
context_was_setr8   current_bufferset_documentr   Documentr   rZ   	pt_layoutTransformation)r\   doclinenosource_to_displaytokenss   `    r-   apply_transformationContext.apply_transformation   s^    sxx c	%%h&7&7

8J8J&KL##"CE Er/    N)rL   rM   rN   rO   rP   staticmethodrj   rQ   rl   r/   r-   rT   rT      s    *E Er/   rT   c                    U(       aa  SnSnU  H  nUc  M  US-  nU[        U5      -  nM     U(       d  / $ US:  a  X-
  US-
  -  nUS:  a  SnOSnX-
  XdS-
  -  -
  nUS:  a  US-  nOSnSn/ nU  H  nUc  M  UR                  [        R                  R                  R
                  U45        UR                  [        R                  R                  R                  SU-  45        US-  nUS:X  d  M  US-  nM     USS $ )z3Returns labels as left- and right-justified tokens.r   NrD       )rY   appendr   TokenToolbarHelp	Separator)	labelswidthjustify
used_widthlabel_countlabelseparator_widthseparator_remainderri   s	            r-   _GetJustifiedTokensr      s2   JK	QkCJj	  i	q+qAo	1	o 	_a@@ Q o O&e}
MM5;;&&++U34
MM5;;&&00#2GHI1ao  
r/   c                    [         R                  " SSSSSSSSS	./S
9nSU[        R                  '   X[        R                     S'   S HT  n[         R                  " U5      nSU[        R                  '   SU[        R
                  '   X[        R                     U'   MV     S HA  n[         R                  " U5      nSU[        R                  '   X[        R                     U'   MC     g)z0Adds keywords and builtins to the CLI tree root.exitzExit the interactive shell.0zThe exit status.status?FSTATUS)defaultdescriptionrI   nargsrequiredvalue)commandr   positionals)
!{doelifelseifthentimeuntilwhileT)breakcasecontinuedoneesacfiN)r   Noder   LOOKUP_IS_GROUPLOOKUP_COMMANDSLOOKUP_IS_SPECIAL)r(   noderI   s      r-   _AddCliTreeKeywordsAndBuiltinsr      s    
 
/ /	
$ "'$v)-vv&!d==D#(D		 %)D	!	!")-		 &! Bd==D#(D		 )-		 & Br/   c                   X    \ rS rSrSrSS jrS rS rS rS r	S	 r
S
 rS rS rS rSrg)Application   aR  The CLI application.

Attributes:
  args: The parsed command line arguments.
  config: The interactive shell config object.
  coshell: The shell coprocess object.
  debug: The debugging object.
  key_bindings: The key_bindings object holding the key binding list and
    toggle states.
  key_bindings_registry: The key bindings registry.
Nc                    X l         Xl        X0l        X@l        [        R
                  " 5       U l        U R                  R                  5       U l        [        R                  " SSS9U l        [        U R                  5        [        R                  " S5      n[        R                  " S5      nXV) -  n[        R                   R#                  [$        R&                  " 5       R(                  S5      n[        R                  " S5      n[*        R,                  " U R                  UR.                  UR0                  S9n	[2        R4                  " UUU	UUR0                  UR6                  S9n
[8        R:                  " UUU[<        R>                  " U5      S U
UR@                  (       a  [B        RD                  " 5       OS [8        RF                  RH                  S9U l%        [M        UUUU R                  U	U
U RO                  UUS9[        RP                  " 5       [        RR                  " 5       S	9	U l*        U RT                  U
l*        U R                  RW                  U RT                  5        [X        RZ                  " U R                  5        g )
NT)ignore_out_of_datewarn_on_exceptionsFshell_history)rZ   hidden)r   r   r*   argsr   manpage_generator)enable_history_searchrF   is_multiliner   	validatorr
   r   accept_action)r   	multiline)	r   r   r   r(   r*   r+   r   r#   r$   ).r   r   r   r   r   KeyBindingskey_bindingsMakeRegistrykey_bindings_registryr   LoadAllr(   r   r   to_simple_filterospathjoincore_configPathsglobal_config_dirr   ParserrZ   r   r
   InteractiveCliCompleterr   	pt_bufferBuffer
pt_historyFileHistorysuggestr   AutoSuggestFromHistoryAcceptActionRETURN_DOCUMENTdefault_bufferr    _CreatePromptApplicationcreate_eventloopcreate_outputr\   
Initializer	   LoadViBindings)r)   r   r   r   r   rF   r   history_filer   r*   r+   s              r-   r&   Application.__init__   s   ILKJ ,,.D!%!2!2!?!?!AD #**D:DI
 #499-
 &66t<%66u=3377<< 1 1 3 E E /1L**51I  		}} &==-}} 224 $**33&&|4' #99;-1,,<<
D YY-311<E 2 G,,.&&(DH !% 	  *t99:r/   c                 t  ^ ^ [         R                  " [        R                  " U[	        5       /T R
                  T R                  ST R                  SSS[        R                  " U4S j5      [        R                  " U 4S j5      SS9T R                  SSST R                  SS[        R                  " 5       S9	$ )z#Creates a shell prompt Application.NFc                    > T" 5       $ rX   rl   )r\   r   s    r-   r]   6Application._CreatePromptApplication.<locals>.<lambda>N  s    IKr/   c                 D   > TR                   R                  R                  $ rX   )r   help_keytoggle)_r)   s    r-   r]   r   P  s    $++44;;r/   T)r   extra_input_processorsget_bottom_status_tokensget_bottom_toolbar_tokensget_continuation_tokensget_debug_tokensget_prompt_tokensis_passwordlexerr   	show_help
wrap_lines)	r   r   	clipboarderase_when_done	get_titler   mouse_supportreverse_vi_search_directionr   )pt_applicationr   r   CreatePromptLayoutrT   _GetBottomStatusTokens_GetBottomToolbarTokens_GetDebugTokensr   	Conditionr   r   interactive_styleGetDocumentStyle)r)   r   r   s   ` `r-   r   $Application._CreatePromptApplication@  s     %%(($+I;%)%@%@&*&B&B$(!11"''(?@'';=
 """88$(002/ r/   c                    U R                   R                  (       a  gU R                  R                  S5      (       d#  [        R
                  " 5       R                  5         [        R                  R                  R                  R                  5       =(       d    Sn[        R                  R                  R                  R                  5       =(       d    SnX4$ )z-Returns the current (project, account) tuple.)mezmyself@iprojectz<NO PROJECT SET>z<NO ACCOUNT SET>)r   	obfuscater   IsSpecifiedr   ActivePropertiesFile
Invalidater   VALUEScorer   Getaccount)r)   r   r   s      r-   _GetProjectAndAccount!Application._GetProjectAndAccount]  s    {{99  ++((*557$$,,002H6HG$$,,002H6HGr/   c                     U R                  5       u  p#[        SU-   SU-   /UR                  R                  UR                  R                  5       R                  S9$ )z@Returns the bottom status tokens based on the key binding state.zProject:zAccount:ry   rx   )r   r   r   justify_bottom_linesr$   get_sizecolumns)r)   r\   r   r   s       r-   r   "Application._GetBottomStatusTokensg  sU    113G	g	zG34

//jj!!#++- -r/   c                    U R                   R                   Vs/ s H  o"R                  5       PM     nnUR                  R                  (       d4  U R                  5       u  pEUR                  U5        UR                  U5        [        UUR                  R                  UR                  R                  5       R                  S9$ s  snf )zAReturns the bottom toolbar tokens based on the key binding state.r   )r   r   GetLabelr   bottom_status_liner   rr   r   r   r$   r   r   )r)   r\   bindingri   r   r   s         r-   r   #Application._GetBottomToolbarTokenso  s    040A0A0J0JK0JW 0JFK::((335gmmGmmG

//jj!!#++- - Ls   Cc                     UR                   R                  5        Vs/ s H"  n[        R                  R                  US-   4PM$     sn$ s  snf )zReturns the debug frame tokens.rp   )r   contentsr   rs   Text)r)   r\   cs      r-   r   Application._GetDebugTokens{  s<    141C1C1EF1EAU[[q3w'1EFFFs   )A	c                     U R                   R                  R                  (       + U R                   l        U R                   R	                  5       nU(       a  UR
                  $ S$ )z%Prompts and returns one command line.N)r\   r   rZ   r`   runr8   )r)   rf   s     r-   PromptApplication.Prompt  s?    #'88??#:#::DHH
((,,.C388$$r/   c                     U R                   R                  S:X  a*  [        R                  R                  U R
                  l        g[        R                  R                  U R
                  l        g)z+Called when coshell modes may have changed.emacsN)r   	edit_moder   EditingModeEMACSr\   editing_modeVI)r)   s    r-   SetModesApplication.SetModes  sD    ||(#//55dhh#//22dhhr/   c                     U R                   =R                  S-  sl        U R                  R                  U5      nUS:  a
  [	        5         U$ )z;Runs the command(s) in text and waits for them to complete.rD      )r\   r'   r   r5   print)r)   r8   r   s      r-   r5   Application.Run  s<    HHa\\d#F|gMr/   c                 8   U R                   R                  U R                  5          U R                  5       nUc  gU R	                  U5        M(  ! [
         a     U R                   R                  (       d   g N+[         a     N6[        R                   a     gf = f)z(Loops Prompt-Run until ^D exit, or quit.N)
r   SetModesCallbackr  r  r5   EOFError
ignore_eofKeyboardInterruptinteractive_coshellCoshellExitError)r)   r8   s     r-   LoopApplication.Loop  s    LL!!$--0
{{}<
   ||&&
 '  11 s"   A A %B8	BBB)	r   r\   r   r   r   r   r   r   r(   )NNNN)rL   rM   rN   rO   rP   r&   r   r   r   r   r   r  r  r5   r$  rQ   rl   r/   r-   r   r      s<    
M;^:-
-G%3r/   r   c                     [         R                  " 5       n [        U UU[        R                  " 5       S9R                  5         UR                  5       n[        R                  " U5        g! UR                  5       nf = f)z!The interactive application loop.)r   r   r   r   N)	r"  Coshellr   interactive_debugDebugr$  Closesysr   )r   r   r   r   s       r-   mainr,    sa    '')'%%'	
 df]]_F((6 ]]_Fs   -A+ +A=)P   T)NN)8rP   
__future__r   r   r   r   r   r+  googlecloudsdk.callioper   &googlecloudsdk.command_lib.interactiver   r	   r
   r   r"  r   r(  r   r   r   r   googlecloudsdk.command_lib.metar   googlecloudsdk.corer   r   r   "googlecloudsdk.core.configurationsr   prompt_toolkitr   r   r   r   r   r   r   r   r   r   r   r   r   prompt_toolkit.layoutr   rd   CommandLineInterfacer    	ProcessorrT   r   r   objectr   r,  rl   r/   r-   <module>r9     s     * &  % ' 	 
 , ; > < Q M 9 9 M > 5 * < 8 ' . #   " 0 $ $   9I4)
(
( I4X	Ei!! 	E*Z$.NB& BJr/   