
                            d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlmZ ddl	m
Z
 ddlmZ ddlZ G d	 d
e      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zy)z$The gcloud interactive key bindings.    )absolute_import)division)unicode_literalsN)browser)keys)managerc                   >    e Zd ZdZ	 	 d	dZd Zd
dZd
dZd Zd Z	y)_KeyBindinga  Key binding base info to keep registered bindings and toolbar in sync.

  Attributes:
    key: The keys.Key.* object.
    help_text: The UX help text.
    label: The short word label for the bottom toolbar.
    metavar: Display this value in GetLabel(markdown=True) instead of the real
      value.
    status: The bool => string toggle status map.
    toggle: The bool toggle state.
  Nc                 X    || _         || _        || _        || _        || _        || _        y N)key	help_textlabelmetavarstatustoggle)selfr   r   r   r   r   r   s          6lib/googlecloudsdk/command_lib/interactive/bindings.py__init__z_KeyBinding.__init__-   s,    DHDNDJDLDKDK    c                     t        j                  ddt        j                  | j                              j                  dd      S )z!Returns the binding display name.z
.*<(.*)>.*z\1zC-zctrl-)resubsix	text_typer   replace)r   s    r   GetNamez_KeyBinding.GetName6   s4    66,--)++274+ABr   c                 ~   | j                   | j                  yg }|r|j                  d       |j                  | j                                |j                  d       | j                   r8|j                  | j                          | j                  r|j                  d       |r|j                  d       | j                  rl|rB|j                  d       |j                  | j                  xs d       |j                  d       n(|j                  | j                  | j
                            dj                  |      S )zDReturns the key binding display label containing the name and value.N*:_STATE )r   r   appendr   r   r   join)r   markdownr   s      r   GetLabelz_KeyBinding.GetLabel;   s    zzdkk1Ell3	LL 	LLzzll4::	Sll3{{	ST\\,W-ST[[-.775>r   c                     | j                   sy| j                         }|rdj                  |      }| j                   j                  |      S )zReturns the key help text.Nz*{}*r   )r   r   format)r   r&   r   s      r   GetHelpz_KeyBinding.GetHelpS   s@    >>
,,.CMM#c>>  S ))r   c                      ~y)z Sets the toggle mode in the cli.N )r   clis     r   SetModez_KeyBinding.SetMode\   s    r   c                 ^    | j                    | _         | j                  |j                         y)zHandles a bound key event.N)r   r/   r.   r   events     r   Handlez_KeyBinding.Handle`   s    kk/DKLLr   )NNNNT)F)
__name__
__module____qualname____doc__r   r   r'   r+   r/   r3   r-   r   r   r
   r
       s/    
 ?C#'B
0*r   r
   c                   (     e Zd ZdZ fdZd Z xZS )_WebHelpKeyBindingzThe web help key binding.c                 2    t         t        |   |dd       y )Nzweb-helpzOpens a web browser tab/window to display the complete man page help for the current command. If there is no active web browser (running in *ssh*(1) for example), then command specific help or *man*(1) help is attempted.r   r   r   )superr9   r   r   r   	__class__s     r   r   z_WebHelpKeyBinding.__init__i   s#    	
d,*	 - 	r   c                     |j                   j                  j                  }t        j                  |j                   |j
                  |j                         y r   )r.   current_bufferdocumentr   OpenReferencePagetextcursor_position)r   r2   docs      r   r3   z_WebHelpKeyBinding.Handleu   s7    
))
"
"
+
+Ceii33F3FGr   r4   r5   r6   r7   r   r3   __classcell__r>   s   @r   r9   r9   f   s    !
Hr   r9   c                   (     e Zd ZdZ fdZd Z xZS )_ContextKeyBindingzset context key binding.c                 2    t         t        |   |dd       y )Ncontexta  Sets the context for command input, so you won't have to re-type common command prefixes at every prompt. The context is the command line from just after the prompt up to the cursor.
+
For example, if you are about to work with `gcloud compute` for a while, type *gcloud compute* and hit {key}. This will display *gcloud compute* at subsequent prompts until the context is changed.
+
Hit ctrl-c and {key} to clear the context, or edit a command line and/or move the cursor and hit {key} to set a different context.r;   )r<   rJ   r   r=   s     r   r   z_ContextKeyBinding.__init__}   s$    	
d,
O	 - r   c                     |j                   j                  j                  j                  |j                   j                  _        y r   )r.   r@   rA   text_before_cursorconfigrL   r1   s     r   r3   z_ContextKeyBinding.Handle   s-    		  ))<< 
IIr   rF   rH   s   @r   rJ   rJ   z   s     &>r   rJ   c                   $     e Zd ZdZd fd	Z xZS )_HelpKeyBindingzThe help key binding.c                 <    t         t        |   |d|dddd       y )NhelpOFFON)FTzHToggles the active help section, *ON* when enabled, *OFF* when disabled.)r   r   r   r   r   )r<   rQ   r   )r   r   r   r>   s      r   r   z_HelpKeyBinding.__init__   s,    	/4)e48 * r   Tr4   r5   r6   r7   r   rG   rH   s   @r   rQ   rQ      s    	 	r   rQ   c                   (     e Zd ZdZ fdZd Z xZS )_QuitKeyBindingzThe quit key binding.c                 2    t         t        |   |dd       y )NquitzExit.r;   )r<   rY   r   r=   s     r   r   z_QuitKeyBinding.__init__   s     	/4)	 * r   c                 0    ~t        j                  d       y )N   )sysexitr1   s     r   r3   z_QuitKeyBinding.Handle   s    HHQKr   rF   rH   s   @r   rY   rY      s    r   rY   c                   (     e Zd ZdZ fdZd Z xZS )_InterruptKeyBindingzmThe interrupt (ctrl-c) key binding.

  Catches control-C and clears the prompt input buffer and completer.
  c                 .    t         t        |   |       y Nr)   )r<   ra   r   r=   s     r   r   z_InterruptKeyBinding.__init__   s    	
. / r   c                     |j                   j                  j                          |j                   j                  j                          y r   )r.   r@   reset	completerr1   s     r   r3   z_InterruptKeyBinding.Handle   s.    	II""$	IIr   rF   rH   s   @r   ra   ra      s    

 r   ra   c                   "     e Zd ZdZ fdZ xZS )_StopKeyBindingzThe stop (^Z) key binding.

  This binding's sole purpose is to ignore ^Z and prevent it from echoing
  in the prompt window.
  c                 .    t         t        |   |       y rc   )r<   rh   r   r=   s     r   r   z_StopKeyBinding.__init__   s    	/4) * r   rW   rH   s   @r   rh   rh      s     r   rh   c                   $    e Zd ZdZddZd Zd Zy)KeyBindingsa  All key bindings.

  Attributes:
    bindings: The list of key bindings in left to right order.
    help_key: The help visibility key binding. True for ON, false for
      OFF.
    context_key: The command prefix context key that sets the context to the
      command substring from the beginning of the input line to the current
      cursor position.
    web_help_key: The browse key binding that pops up the full reference
      doc in a browser.
    quit_key: The key binding that exits the shell.
  c                 z   t        t        j                  j                  |      | _        t        t        j                  j                        | _        t        t        j                  j                        | _
        t        t        j                  j                        | _        t        t        j                  j                        | _        t#        t        j                  j$                        | _        | j                  | j                  | j                  | j                  | j                   | j&                  g| _        y)zEAssociates keys with handlers. Toggle states are reachable from here.)r   N)rQ   r   KeysF2help_keyrJ   F7context_keyr9   F8web_help_keyrY   F9quit_keyra   ControlCinterrupt_signalrh   ControlZstop_signalbindings)r   	help_modes     r   r   zKeyBindings.__init__   s    
 $DIILLCDM)$)),,7D*499<<8D#DIILL1DM01C1CDD&tyy'9'9:D 	DMr   c                     t        j                  dddd      }| j                  D ]9  } |j                  j	                  |j
                  d      |j                         ; |j                  S )zEMakes and returns a key binding registry populated with the bindings.T)enable_abort_and_exit_bindingsenable_system_bindingsenable_searchenable_auto_suggest_bindings)eager)r   KeyBindingManagerrz   registryadd_bindingr   r3   )r   mbindings      r   MakeRegistryzKeyBindings.MakeRegistry   s`    !!'+#%)		,A ==5ajjW[[5gnnE ! ::r   c                 H    | j                   D ]  }|j                  |        y)z+Initialize key binding defaults in the cli.N)rz   r/   )r   r.   r   s      r   
InitializezKeyBindings.Initialize  s    ==ooc !r   NrV   )r4   r5   r6   r7   r   r   r   r-   r   r   rk   rk      s    ,r   rk   )r7   
__future__r   r   r   r   r^   &googlecloudsdk.command_lib.interactiver   prompt_toolkitr   prompt_toolkit.key_bindingr   r   objectr
   r9   rJ   rQ   rY   ra   rh   rk   r-   r   r   <module>r      s     + &  ' 	 
 :  . 
C& CLH H(> >6k k " ;   
k 
5& 5r   