o
    r!                     @   s   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	 Z		dddZdd ZG dd deZdS )z!Prompt completion support module.    )absolute_import)division)unicode_literalsN)console_attr)rangec                 C   s   |  |  S )zCreturns numerator/denominator rounded up if there is any remainder. )	numeratordenominatorr   r   I/tmp/google-cloud-sdk/lib/googlecloudsdk/core/console/prompt_completer.py_IntegerCeilingDivide   s   r   P   (     c                    s  fdd}dd dd  t | }||\}}	|	|kr;dkr;fdd	| D }||\}}	|	|kr;dks"t|}rVttj| fd
d|D }dg}
d}||	k r|}t|D ]}|t|kro n|
 |
||  ||	7 }qe|
d |d7 }||	k s_|
S )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                    sB   t dd | D }tt |  pd}tt| |}|||fS )z+Returns the transpose dimensions for items.c                 s   s    | ]}t |V  qd S )Nlen.0xr   r   r
   	<genexpr>:   s    z<_TransposeListToRows.<locals>._Dimensions.<locals>.<genexpr>   )maxintr   r   )itemslongest_item_lencolumn_count	row_count)padwidthr   r
   _Dimensions8   s   
z)_TransposeListToRows.<locals>._Dimensionsc                 S   s    t | |kr| S | d| d S )z:Truncates and appends '*' if len(item) > longest_item_len.N*r   )itemr   r   r   r
   _TrimAndAnnotate?   s   z._TransposeListToRows.<locals>._TrimAndAnnotatec                 S   sL   t | }||kr| d| | | |  | | |d d  } | || d  S )zBHighlights the different part of the completion and left justfies.Nr    r   )r    r   difference_indexboldnormallengthr   r   r
   
_HighlightE   s   z(_TransposeListToRows.<locals>._Highlight   c                    s   h | ]	} |d  qS )   r   r   )r!   r   r   r
   	<setcomp>R       z'_TransposeListToRows.<locals>.<setcomp>c                    s   g | ]
} |qS r   r   r   )r'   r$   r#   r   r%   r   r
   
<listcomp>Y   s    z(_TransposeListToRows.<locals>.<listcomp>
r   r   )setsortedr   ospathcommonprefixr   append)	all_itemsr   heightr   r$   r%   r   r   r   r   row_data	row_indexcolumn_index_r   )r'   r!   r$   r#   r   r%   r   r   r
   _TransposeListToRows#   s:   



r:   c                    s    fdd|D S )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.
  c                    s   g | ]	}|  r|qS r   )
startswithr   prefixr   r
   r,   w   r+   z"_PrefixMatches.<locals>.<listcomp>r   )r=   possible_matchesr   r<   r
   _PrefixMatchesm   s   
r?   c                   @   s6   e Zd ZdZdZdZ		dddZdd	 Zd
d ZdS )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
  Nr   c           	      C   sv   || _ || _|p
tj| _t | _| j \}}|du r$d}||kr$|}|| _	|du r3d}||kr3|}|| _
|| _dS )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.
    Nr   r   )_prompt_choicessysstderr_outr   ConsoleAttr_attrGetTermSize_width_height_pad)	selfpromptchoicesoutr   r5   r   
term_widthterm_heightr   r   r
   __init__   s    

zPromptCompleter.__init__c                 C   sn  d}g }g }| j r| j| j  d}	 |}| j }|dddtjfv s)t|dkr0| jd n|dtjfv rF|rE|	  | jd |}nk|dkrd	
|}||kret||}t|dkrd| || nL|du rvt| jrs|  }n| j}|}t||}d	
|}tj|}t|t|krt||}| j|t|d  t|}n|| | j| qd	
|S )
z=Reads and returns one line of user input with TAB complation.NTr-   r   z 	 )rC   rG   writerI   	GetRawKeyr@   
_CONTROL_Cr   _DELETEpopjoinr?   _DisplaycallablerD   r0   r1   r2   listr3   )rN   all_choicesmatchesresponsec
previous_cresponse_prefixcommon_prefixr   r   r
   Input   sT   









7zPromptCompleter.Inputc                 C   s\   t || j| j| j| jjdd| j d}| jr|| j || | j	d
| dS )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   r5   r   r$   r%   rX   N)r:   rK   rL   rM   rI   GetFontCoderC   r3   rG   rY   r^   )rN   r=   rc   r6   r   r   r
   r_      s   
zPromptCompleter._Display)NNNNr   )	__name__
__module____qualname____doc__r[   r\   rT   ri   r_   r   r   r   r
   r@   z   s    	
Cr@   )r   r   r   NN)rn   
__future__r   r   r   r0   rE   googlecloudsdk.core.consoler   	six.movesr   r   r:   r?   objectr@   r   r   r   r
   <module>   s   
J