
    r!                         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 Z	 	 dd	Zd
 Z G d de      Zy)z!Prompt completion support module.    )absolute_import)division)unicode_literalsN)console_attr)rangec                     |  |z   S )zCreturns numerator/denominator rounded up if there is any remainder. )	numeratordenominators     3lib/googlecloudsdk/core/console/prompt_completer.py_IntegerCeilingDivider      s    :$	%%    c           
      X   fd}d }d }t        |       }	 ||	      \  }
}}||kD  r5|
dkD  r0| D ch c]  } |||
dz
         }	} ||	      \  }
}}||kD  r|
dkD  r0t        |	      }	|rAt        t        j                  j                  |	            }|	D cg c]  } |||
|||       }	}dg}d}||k  rh|}t        |      D ]<  }|t        |	      k\  r n,|j                         |j                  |	|          ||z  }> |j                  d       |dz  }||k  rh|S c c}w c c}w )	a2  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                     t        d | D              }t        t              |z   z        xs d}t        t        |       |      }|||fS )z+Returns the transpose dimensions for items.c              3   2   K   | ]  }t        |        y w)Nlen).0xs     r   	<genexpr>z<_TransposeListToRows.<locals>._Dimensions.<locals>.<genexpr>:   s     15a3q65s      )maxintr   r   )itemslongest_item_lencolumn_count	row_countpadwidths       r   _Dimensionsz)_TransposeListToRows.<locals>._Dimensions8   sO    1511uC+; ;<=BL%c%j,?I\944r   c                 2    t        |       |k  r| S | d| dz   S )z:Truncates and appends '*' if len(item) > longest_item_len.N*r   )itemr   s     r   _TrimAndAnnotatez._TransposeListToRows.<locals>._TrimAndAnnotate?   s(    
4y$$k!!"S((r   c                 l    t        |       }||kD  r| d| |z   | |   z   |z   | |dz   d z   } | ||z
  dz  z   S )zBHighlights the different part of the completion and left justfies.Nr    r   )r#   r   difference_indexboldnormallengths         r   
_Highlightz(_TransposeListToRows.<locals>._HighlightE   sk    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;   #   sq   *5)4 i.%.9%.@+L)F/!3@IJ	1a!1A!56	EJ0;E0B-lI 	F/!3 -% 
277//67 +-=tVL 
  V()IL< 	U	#oocooeL)*il ! OODNI 	I 
/1 Ks   D"D'c                 P    |D cg c]  }|j                  |       s| c}S c c}w )a  Returns 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   _PrefixMatchesr@   m   s)     &	>%f)=!%	>>	>s   ##c                   0    e Zd ZdZdZdZ	 	 ddZd Zd Zy)	PromptCompletera   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s t        j                  | _        t        j                         | _        | j                  j                         \  }}|	d}||kD  r|}|| _	        |	d}||kD  r|}|| _
        || _        y)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   r6   r   
term_widthterm_heights	            r   __init__zPromptCompleter.__init__   s     DLDM!szzDI))+DJ"jj446J}e		DK~f	+	DLDIr   c                 ^   d}g }g }| j                   r%| j                  j                  | j                          d}	 |}| j                  j	                         }|dddt
        j                  fv st        |      dk7  r| j                  j                  d       n|dt
        j                  fv r2|rm|j                          | j                  j                  d       |}n>|dk(  rdj                  |      }||k(  r.t        ||      }t        |      dkD  r| j                  ||       n|2t        | j                        r| j                         }n| j                  }|}t        ||      }dj                  |      }t        j                   j#                  |      }t        |      t        |      kD  rkt        ||      }| j                  j                  |t        |      d        t%        |      }n,|j'                  |       | j                  j                  |       dj                  |      S )	z=Reads and returns one line of user input with TAB complation.Nr.   r   z 	 )rH   rL   writerN   	GetRawKeyrB   
_CONTROL_Cr   _DELETEpopjoinr@   _DisplaycallablerI   r1   r2   r3   listr4   )rS   all_choicesmatchesresponsec
previous_cresponse_prefixcommon_prefixs           r   InputzPromptCompleter.Input   s   KGH||
iioodll#A j
**


 a	
tT4!;!;<	<A!		..//
,,.
))//'
"'9''(+?"?G<'\AMM/73
  & MMOk MMk' #?G<'GGH-/''..w7- #h-/ %]G<GIIOOM#h-.9:M*H 			k n 778r   c           	         t        || j                  | j                  | j                  | j                  j                  d      | j                  j                               }| j                  r|j                  | j                         |j                  |       | j                  j                  dj                  |             y)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   r6   r   r(   r)   r^   N)r;   rP   rQ   rR   rN   GetFontCoderH   r4   rL   r_   rd   )rS   r>   ri   r7   s       r   re   zPromptCompleter._Display   s     $t{{4<<TYYZZ###.tzz7M7M7OQH ||oodll#OOFIIOOBGGH%&r   )NNNN  )	__name__
__module____qualname____doc__ra   rb   rY   ro   re   r	   r   r   rB   rB   z   s*     *'HL<AF'r   rB   )rF   rG   rr   NN)rv   
__future__r   r   r   r1   rJ   googlecloudsdk.core.consoler   	six.movesr   r   r;   r@   objectrB   r	   r   r   <module>r{      sH     ( &  ' 	 
 4 &
 IM $GT
?{'f {'r   