
    r!                         S 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 r  SS	 jrS
 r " S S\5      rg)z!Prompt completion support module.    )absolute_import)division)unicode_literalsN)console_attr)rangec                     U * U-  * $ )zCreturns numerator/denominator rounded up if there is any remainder. )	numeratordenominators     3lib/googlecloudsdk/core/console/prompt_completer.py_IntegerCeilingDivider      s    :$	%%    c           
      v  ^^ UU4S jnS nS n[        U 5      n	U" U	5      u  pnX:  a8  U
S:  a2  U  Vs1 s H  o" XS-
  5      iM     n	nU" U	5      u  pnX:  a  U
S:  a  M2  [        U	5      n	U(       aB  [        [        R                  R                  U	5      5      nU	 Vs/ s H  nU" XXU5      PM     n	nS/nSnUU:  am  Un[        U5       H>  nU[        U	5      :  a    O.UR                  T5        UR                  U	U   5        UU-  nM@     UR                  S5        US-  nUU:  a  Mm  U$ s  snf s  snf )	a  Returns padded newline terminated column-wise list for items.

Used by PromptCompleter to pretty print the possible completions for TAB-TAB.

Args:
  all_items: [str], The ordered list of all items to transpose.
  width: int, The total display width in characters.
  height: int, The total display height in lines.
  pad: str, String inserted before each column.
  bold: str, The bold font highlight control sequence.
  normal: str, The normal font highlight control sequence.

Returns:
  [str], A padded newline terminated list of colum-wise rows for the ordered
  items list.  The return value is a single list, not a list of row lists.
  Convert the return value to a printable string by ''.join(return_value).
  The first "row" is preceded by a newline and all rows start with the pad.
c                    > [        S U  5       5      n[        T[        T5      U-   -  5      =(       d    Sn[        [        U 5      U5      nXU4$ )z+Returns the transpose dimensions for items.c              3   8   #    U  H  n[        U5      v   M     g 7f)Nlen).0xs     r   	<genexpr><_TransposeListToRows.<locals>._Dimensions.<locals>.<genexpr>:   s     15a3q665s      )maxintr   r   )itemslongest_item_lencolumn_count	row_countpadwidths       r   _Dimensions)_TransposeListToRows.<locals>._Dimensions8   sM    1511uC+; ;<=BL%c%j,?I944r   c                 4    [        U 5      U::  a  U $ U SU S-   $ )z:Truncates and appends '*' if len(item) > longest_item_len.N*r   )itemr   s     r   _TrimAndAnnotate._TransposeListToRows.<locals>._TrimAndAnnotate?   s(    
4y$$k!!"S((r   c                 f    [        U 5      nXR:  a  U SU U-   X   -   U-   XS-   S -   n XU-
  S-  -   $ )zBHighlights the different part of the completion and left justfies.Nr    r   )r%   r   difference_indexboldnormallengths         r   
_Highlight(_TransposeListToRows.<locals>._HighlightE   s_    YF $$%,$%'-.A%&'(d f,333r         
r   r   )setsortedr   ospathcommonprefixr   append)	all_itemsr    heightr   r+   r,   r!   r&   r.   r   r   r   r   r   r*   row_data	row_indexcolumn_index_s    ` `               r   _TransposeListToRowsr?   #   sa   *5)4 i.%.9%.@+)/!3@IJ	1aA!56	EJ0;E0B-I 	/!3 -% 
277//67 -=VL 
  V()IL< 	U	#oocooeL)*il ! OODNI 	I 
/1 Ks   D1!D6c                 `    U Vs/ s H  o"R                  U 5      (       d  M  UPM     sn$ s  snf )zReturns the subset of possible_matches that start with prefix.

Args:
  prefix: str, The prefix to match.
  possible_matches: [str], The list of possible matching strings.

Returns:
  [str], The subset of possible_matches that start with prefix.
)
startswith)prefixpossible_matchesr   s      r   _PrefixMatchesrD   m   s'     &	>%f)=!%	>>	>s   ++c                   :    \ rS rSrSrSrSr  S
S jrS rS r	S	r
g)PromptCompleterz   a  Prompt + input + completion.

Yes, this is a roll-your own implementation.
Yes, readline is that bad:
  linux: is unaware of the prompt even though it overrise raw_input()
  macos: different implementation than linux, and more brokener
  windows: didn't even try to implement
Nc                    Xl         X l        U=(       d    [        R                  U l        [
        R                  " 5       U l        U R                  R                  5       u  pxUc	  SnXG:  a  UnX@l	        Uc	  SnXX:  a  UnXPl
        X`l        g)a  Constructor.

Args:
  prompt: str or None, The prompt string.
  choices: callable or list, A callable with no arguments that returns the
    list of all choices, or the list of choices.
  out: stream, The output stream, sys.stderr by default.
  width: int, The total display width in characters.
  height: int, The total display height in lines.
  pad: str, String inserted before each column.
NP   (   )_prompt_choicessysstderr_outr   ConsoleAttr_attrGetTermSize_width_height_pad)	selfpromptchoicesoutr    r:   r   
term_widthterm_heights	            r   __init__PromptCompleter.__init__   s|     LM!szzDI))+DJ"jj446J}e		K~f		LIr   c                    Sn/ n/ nU R                   (       a%  U R                  R                  U R                   5        Sn UnU R                  R	                  5       nUSSS[
        R                  4;   d  [        U5      S:w  a  U R                  R                  S5        GOUS[
        R                  4;   a6  U(       a-  UR                  5         U R                  R                  S5        UnGOBUS:X  Ga  SR                  U5      nXT:X  a,  [        Xb5      n[        U5      S:  a  U R                  Xb5        OUc7  [        U R                  5      (       a  U R                  5       nOU R                  nUn[        Xb5      nSR                  U5      n[        R                   R#                  U5      n[        U5      [        U5      :  a=  [        Xr5      nU R                  R                  U[        U5      S 5        [%        U5      nO,UR'                  U5        U R                  R                  U5        GM  SR                  U5      $ )	z=Reads and returns one line of user input with TAB complation.Nr2   r   z 	 )rM   rQ   writerS   	GetRawKeyrF   
_CONTROL_Cr   _DELETEpopjoinrD   _DisplaycallablerN   r5   r6   r7   listr8   )rX   all_choicesmatchesresponsec
previous_cresponse_prefixcommon_prefixs           r   InputPromptCompleter.Input   s   KGH||
iioodll#A j
**


 a	
tT4!;!;<	<A!		..//
,,.
))//'
"'9''(+?"?<'\AMM/3
  && MMOk MMk' #?<'GGH-/''..w7- #h-/ %]<GIIOOM#h-.9:M*H 			k n 778r   c           
         [        X R                  U R                  U R                  U R                  R                  SS9U R                  R                  5       S9nU R                  (       a  UR                  U R                  5        UR                  U5        U R                  R                  SR                  U5      5        g)zDisplays the possible completions and redraws the prompt and response.

Args:
  prefix: str, The current response.
  matches: [str], The list of strings that start with prefix.
T)r+   )r    r:   r   r+   r,   rd   N)r?   rU   rV   rW   rS   GetFontCoderM   r8   rQ   re   rj   )rX   rB   ro   r;   s       r   rk   PromptCompleter._Display   s     ${{4<<TYYZZ###.tzz7M7M7OQH ||oodll#OOFIIOOBGGH%&r   )rS   rN   rV   rQ   rW   rM   rU   )NNNN  )__name__
__module____qualname____firstlineno____doc__rg   rh   r^   ru   rk   __static_attributes__r	   r   r   rF   rF   z   s*     *'HL<AF'r   rF   )rK   rL   rz   NN)r   
__future__r   r   r   r5   rO   googlecloudsdk.core.consoler   	six.movesr   r   r?   rD   objectrF   r	   r   r   <module>r      sH     ( &  ' 	 
 4 &
 IM $GT
?{'f {'r   