
    K                     ,   S SK Jr  S SKJrJr  S SKJrJrJrJ	r	  S SK
Jr  S SKJr  S SKJr  SSKJrJrJrJr  SS	KJrJr  SS
KJr  SSKJr  SSKJrJr  S SKrSr  " S S\5      r!S r" " S S\5      r# " S S\5      r$ " S S\5      r% " S S\5      r&g)    )unicode_literals)zip_longestrange)HasCompletionsIsDone	Conditionto_cli_filter)MouseEventType)Token)
get_cwidth   )WindowHSplitConditionalContainerScrollOffsets)	UIControl	UIContent)LayoutDimension)ScrollbarMargin)PointCharN)CompletionsMenuMultiColumnCompletionsMenuc                   ^    \ rS rSrSrSrS rS rS rS r	S r
S	 rS
 rS rS rS rS rSrg)CompletionsMenuControl   a2  
Helper for drawing the complete menu to the screen.

:param scroll_offset: Number (integer) representing the preferred amount of
    completions to be displayed before and after the current one. When this
    is a very high number, the current completion will be shown in the
    middle most of the time.
   c                 B    [         R                  R                  U l        g N)r   MenuCompletionstokenselfs    .lib/third_party/prompt_toolkit/layout/menus.py__init__CompletionsMenuControl.__init__%   s    ZZ++
    c                     gNF r$   clis     r%   	has_focus CompletionsMenuControl.has_focus(       r(   c                     UR                   R                  nU(       a(  U R                  SU5      nU R                  SU5      nXE-   $ g)Ni  r   )current_buffercomplete_state_get_menu_width_get_menu_meta_width)r$   r-   max_available_widthr3   
menu_widthmenu_meta_widths         r%   preferred_width&CompletionsMenuControl.preferred_width+   sF    ++::--c>BJ"77^LO//r(   c                 h    UR                   R                  nU(       a  [        UR                  5      $ gNr   )r2   r3   lencurrent_completions)r$   r-   widthmax_available_height
wrap_linesr3   s         r%   preferred_height'CompletionsMenuControl.preferred_height5   s*    ++::~99::r(   c           	        ^ ^^^^	^
 UR                   R                  nU(       a  UR                  mUR                  mT R	                  X$5      m	T R                  UT	-
  U5      mT R                  U5      m
UUUU	U U
4S jn[        U[        ST=(       d    SS9[        T5      [        ST R                  5      S9$ [        5       $ )z-
Create a UIContent object for this control.
c                 x   > TU    nU T:H  nTR                  XT5      nT	(       a  UTR                  XT5      -  nU$ r   )_get_menu_item_tokens_get_menu_item_meta_tokens)
icis_current_completionresultcompletionsindexr8   r7   r$   	show_metas
       r%   get_line7CompletionsMenuControl.create_content.<locals>.get_lineJ   sH    N)*e%33AjYd==aXghhFr(   r   )xy )rO   cursor_position
line_countdefault_char)r2   r3   r>   complete_indexr4   r5   
_show_metar   r   r=   r   r"   )r$   r-   r?   heightr3   rO   rL   rM   r8   r7   rN   s   `     @@@@@r%   create_content%CompletionsMenuControl.create_content<   s     ++::(<<K"11E --eDJ"77
8JN[O7I  h-2Q%*1-E(+K(8*.sDJJ*?A A
 {r(   c                 :    [        S UR                   5       5      $ )zD
Return ``True`` if we need to show a column with meta information.
c              3   8   #    U  H  oR                   v   M     g 7fr   display_meta.0rI   s     r%   	<genexpr>4CompletionsMenuControl._show_meta.<locals>.<genexpr>^   s     N+Ma>>+M   )anyr>   r$   r3   s     r%   rX   !CompletionsMenuControl._show_metaZ   s     N>+M+MNNNr(   c           
      |    [        U[        U R                  [        S UR                   5       5      S-   5      5      $ )z&
Return the width of the main column.
c              3   L   #    U  H  n[        UR                  5      v   M     g 7fr   r   displayr`   s     r%   rb   9CompletionsMenuControl._get_menu_width.<locals>.<genexpr>d   s%      6@>q 7A6K6K>   "$   )minmax	MIN_WIDTHr>   r$   	max_widthr3   s      r%   r4   &CompletionsMenuControl._get_menu_width`   sI     9c$..# 6@*>>6@ 3@BC3D E F 	Fr(   c                     U R                  U5      (       a)  [        U[        S UR                   5       5      S-   5      $ g)z&
Return the width of the meta column.
c              3   L   #    U  H  n[        UR                  5      v   M     g 7fr   r   r_   r`   s     r%   rb   >CompletionsMenuControl._get_menu_meta_width.<locals>.<genexpr>l   s$      &D B1 '1&@&@ Brm   rn   r   )rX   ro   rp   r>   rr   s      r%   r5   +CompletionsMenuControl._get_menu_meta_widthg   sO     ??>**y# &D . B B&D #DFG#H I I r(   c                     U(       a!  U R                   R                  R                  nOU R                   R                  n[        UR                  US-
  5      u  pVSUS-
  U-
  -  nUSU< U< S34/$ Nrn   rS   r"   
CompletionCurrent
_trim_textrk   r$   
completionrJ   r?   r"   texttwpaddings           r%   rF   ,CompletionsMenuControl._get_menu_item_tokensq   sf     JJ))11EJJ))Ej00%!)<R(D'2344r(   c                     U(       a!  U R                   R                  R                  nOU R                   R                  n[        UR                  US-
  5      u  pVSUS-
  U-
  -  nUSU< U< S34/$ r{   )r"   Metar~   r   r_   r   s           r%   rG   1CompletionsMenuControl._get_menu_item_meta_tokens{   sb     JJOO++EJJOOEj55uqyAR(D'2344r(   c                 n   UR                   nUR                  [        R                  :X  a-  UR	                  UR
                  R                  5        SUl        gUR                  [        R                  :X  a  UR                  SSS9  gUR                  [        R                  :X  a  UR                  SSS9  gg)z.
Handle mouse events: clicking and scrolling.
N   Tcountdisable_wrap_around)r2   
event_typer
   MOUSE_UPgo_to_completionpositionrR   r3   SCROLL_DOWNcomplete_next	SCROLL_UPcomplete_previous)r$   r-   mouse_eventbs       r%   mouse_handler$CompletionsMenuControl.mouse_handler   s     !!^%<%<<{33556#A##~'A'AAOO!O>##~'?'??aTB @r(   )r"   N)__name__
__module____qualname____firstlineno____doc__rq   r&   r.   r9   rB   rZ   rX   r4   r5   rF   rG   r   __static_attributes__r+   r(   r%   r   r      sH     I,<OF55Cr(   r   c                     [        U 5      nX!:  ak  [        U 5      U:X  a%  U S[        SUS-
  5       S-   SU nU[        U5      4$ SnU  H  n[        X4-   5      US-
  ::  d  M  X4-  nM     US-  nU[        U5      4$ X4$ )zc
Trim the text to `max_width`, append dots when the text is too long.
Returns (text, width) tuple.
Nr   r   z... )r   r=   rp   )r   rs   r?   trimmed_textrI   s        r%   r   r      s    
 tE t9 !5#a1"56>
KL\!222
 Ll./9q=@ %L  E!L *\":;;{r(   c                   ,   ^  \ rS rSrSU 4S jjrSrU =r$ )r      c                    > [        U5      n[        U5      n[        [        U ]  [	        [        5       [        SS9[        SUS9[        X"S9[        US9/SS9[        5       [        5       ) -  U-  S	9  g )
N   ro   r   )ro   rp   )topbottom)display_arrowsT)contentr?   rY   scroll_offsetsright_marginsdont_extend_widthr   filter)r	   superr   r&   r   r   r   r   r   r   r   )r$   
max_heightscroll_offsetextra_filterr   	__class__s        r%   r&   CompletionsMenu.__init__   sx    $\2&~6ot-.0%!,&1*=,U.nMN"& "#vxi/,> 	. 	@r(   r+   )Nr   TF)r   r   r   r   r&   r   __classcell__r   s   @r%   r   r      s    @ @r(   r   c                   V    \ rS 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g) MultiColumnCompletionMenuControl   a>  
Completion menu that displays all the completions in several columns.
When there are more completions than space for them to be displayed, an
arrow is shown on the left or right side.

`min_rows` indicates how many rows will be available in any possible case.
When this is langer than one, in will try to use less columns and more
rows until this value is reached.
Be careful passing in a too big value, if less than the given amount of
rows are available, more columns would have been required, but
`preferred_width` doesn't know about that and reports a too small value.
This results in less completions displayed and additional scrolling.
(It's a limitation of how the layout engine currently works: first the
widths are calculated, then the heights.)

:param suggested_max_column_width: The suggested max width of a column.
    The column can still be bigger than this, but if there is place for two
    columns of this width, we will display two columns. This to avoid that
    if there is one very wide completion, that it doesn't significantly
    reduce the amount of columns.
r   c                    [        U[        5      (       a  US:  d   eXl        X l        [        R
                  R                  U l        SU l        SU l	        SU l
        SU l        0 U l        SU l        SU l        SU l        g )Nr   r   F)
isinstanceintmin_rowssuggested_max_column_widthr   r    r!   r"   scroll_rendered_rows_rendered_columns_total_columns_render_pos_to_completion_render_left_arrow_render_right_arrow_render_width)r$   r   r   s      r%   r&   )MultiColumnCompletionMenuControl.__init__   sx    (C((X]:: *D'ZZ++
  !")+&"'#( r(   c                     SU l         g r<   )r   r#   s    r%   reset&MultiColumnCompletionMenuControl.reset   s	    r(   c                     gr*   r+   r,   s     r%   r.   *MultiColumnCompletionMenuControl.has_focus   r0   r(   c           	      j   UR                   R                  nU R                  U5      n[        U[        R
                  " [        UR                  5      [        U R                  5      -  5      -  5      nXT:  a/  XRU R                  -
  :  a  XT-  nXT:  a  XRU R                  -
  :  a  M  XPR                  -   $ )zc
Preferred width: prefer to use at least min_rows, but otherwise as much
as possible horizontally.
)r2   r3   _get_column_widthr   mathceilr=   r>   floatr   _required_margin)r$   r-   r6   r3   column_widthrK   s         r%   r9   0MultiColumnCompletionMenuControl.preferred_width   s    
 ++::--n=\DIIc.2T2T.UX]^b^k^kXl.l$mmn
 #tG\G\1\(\"F #tG\G\1\(\----r(   c                    UR                   R                  nU R                  U5      n[        SX R                  -
  U-  5      n[        [        R                  " [        UR                  5      [        U5      -  5      5      $ )zN
Preferred height: as much as needed in order to display all the completions.
r   )r2   r3   r   rp   r   r   r   r   r=   r>   r   )r$   r-   r?   r@   rA   r3   r   column_counts           r%   rB   1MultiColumnCompletionMenuControl.preferred_height  sh     ++::--n=1u'<'<<MN499S!C!CDu\GZZ[\\r(   c           	      n  ^^^ UR                   R                  mU R                  T5      n0 U l        SS jnUU4S jnSnT(       Ga  [	        X'-
  U5      nX@R
                  :  a  XDU R
                  -  -  n[        SX R                  -
  U-  5      n[        U" UTR                  5      5      n	[        [        U	6 5      n
TR                  =(       d    SU-  n[	        U[        U R                  X-
  S-   5      5      U l        U R                  S:  nU R                  [        U
S   5      U-
  :  n/ m[        U
5       GH  u  p/ nU[        U
5      S-  :H  nU(       a  U[        R                   U(       a  SOS	4/-  n[        XR                  S SU 5       Ho  u  nmTbG  UU R#                  TU" T5      U5      -  n[%        U5       H  nTU R                  UU-  U-   U4'   M     MP  UU R&                  R(                  S	U-  4/-  nMq     UU R&                  R(                  S	4/-  nU(       a  U[        R                   U(       a  S
OS	4/-  nTR+                  U5        GM"     O/ nX0l        WU l        [        W	5      U l        WU l        WU l        XH-  U-   U-   S-   U l        U4S jn[9        U[        W
5      S9$ )z*
Create a UIContent object for this menu.
Nc                 6    [        U5      /U -  n[        USU06$ )z+grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx 	fillvalue)iterr   )niterabler   argss       r%   grouper@MultiColumnCompletionMenuControl.create_content.<locals>.grouper  s#    N#a'DT:::r(   c                 L   > TR                   SL=(       a    TTR                  :H  $ )zAReturns True when this completion is the currently selected one. N)rW   current_completion)r   rI   r3   s    r%   rJ   NMultiColumnCompletionMenuControl.create_content.<locals>.is_current_completion  s$    !00<gnFgFgAggr(   r   r   r   rn   <rS   >c                    > TU    $ r   r+   )rH   tokens_for_lines    r%   rO   AMultiColumnCompletionMenuControl.create_content.<locals>.get_lineh  s    "1%%r(   rO   rU   r   )r2   r3   r   r   ro   r   rp   r   listr>   ziprW   r   r=   	enumerater   	ScrollbarrF   r   r"   r}   appendr   r   r   r   r   r   r   )r$   r-   r?   rY   r   r   rJ   HORIZONTAL_MARGIN_REQUIREDvisible_columnscolumns_rows_selected_columnrender_left_arrowrender_right_arrow	row_indexrowtokens
middle_rowcolumn_indexrQ   rO   rI   r3   r   s                        @@@r%   rZ   /MultiColumnCompletionMenuControl.create_content  s    ++::--n=)+&	;
	h &'" uA<PL === $2Q2Q"QR!!e.C.C&C%TUOGFN,N,NOPHh(E  .<<AfLOos4;;@ade@e/fgDK $a!%s58}/N!N !O"+E"2	&#e*/9
 %
LMMF (1[[\1BCSO1T'UOL!}$"<"<Q@UVW@XZf"gg "'|!4AklD::L<<WZ[<[]f;gh "5 DJJ$9$93;M#N"OO (V DJJ113788 &
LMMF  &&v.7 #3< F$!0!(m"3#5 );>OORddghh	& (s5zBBr(   c                 @    [        S UR                   5       5      S-   $ )z"
Return the width of each column.
c              3   L   #    U  H  n[        UR                  5      v   M     g 7fr   rj   r`   s     r%   rb   EMultiColumnCompletionMenuControl._get_column_width.<locals>.<genexpr>q  s     U2TQ:aii((2Trm   r   )rp   r>   rf   s     r%   r   2MultiColumnCompletionMenuControl._get_column_widthm  s!     U.2T2TUUXYYYr(   c                     U(       a!  U R                   R                  R                  nOU R                   R                  n[        UR                  U5      u  pVSX6-
  S-
  -  nUSU< U< 34/$ )NrS   r   r|   r   s           r%   rF   6MultiColumnCompletionMenuControl._get_menu_item_tokenss  s`     JJ))11EJJ))Ej00%8a(41233r(   c                 n  ^ ^ UR                   mUU 4S jnUU 4S jnUR                  [        R                  :X  a  U" 5         gUR                  [        R                  :X  a  U" 5         gUR                  [        R
                  :X  a  UR                  R                  nUR                  R                  nUS:X  a  T R                  (       a  U" 5         ggUT R                  S-
  :X  a  T R                  (       a  U" 5         ggT R                  R                  XV45      nU(       a  TR                  U5        ggg)z 
Handle scoll and click events.
c                  v   > T R                  TR                  SS9  [        STR                  S-
  5      Tl        g )NTr   r   r   )r   r   rp   r   r   r$   s   r%   scroll_leftCMultiColumnCompletionMenuControl.mouse_handler.<locals>.scroll_left  s2    d&9&9tTaq1DKr(   c                     > T R                  TR                  SS9  [        TR                  TR                  -
  TR
                  S-   5      Tl        g )NTr   r   )r   r   ro   r   r   r   r  s   r%   scroll_rightDMultiColumnCompletionMenuControl.mouse_handler.<locals>.scroll_right  sA    OO$"5"54OPd11D4J4JJDKKZ[O\DKr(   r   r   N)r2   r   r
   r   r   r   r   rQ   rR   r   r   r   r   getapply_completion)	r$   r-   r   r  r
  rQ   rR   r   r   s	   `       @r%   r   .MultiColumnCompletionMenuControl.mouse_handler~  s     	2	] !!^%?%??N##~'?'??M##~'>'>>$$&&A$$&&A Av**M + d((1,,++ N ,
 ";;??G
&&z2 # ?r(   )r   r   r   r   r   r   r   r   r   r   r"   N)r      )r   r   r   r   r   r   r&   r   r.   r9   rB   rZ   r   rF   r   r   r+   r(   r%   r   r      sA    * ". ]ZCxZ	4&3r(   r   c                   0   ^  \ rS rSrSrSU 4S jjrSrU =r$ )r   i  z
Container that displays the completions in several columns.
When `show_meta` (a :class:`~prompt_toolkit.filters.CLIFilter`) evaluates
to True, it shows the meta information at the bottom.
c           
      2  > [        U5      n[        U5      n[        5       [        5       ) -  U-  n[        S 5      n[	        [        [        XS9[        SS9[        SS9S9US9n[	        [        [        5       S9X5-  U-  S9n[        [        U ]/  UU/5        g )	Nc                 b    [        S U R                  R                  R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7fr   r^   r`   s     r%   rb   HMultiColumnCompletionsMenu.__init__.<locals>.<lambda>.<locals>.<genexpr>  s     b,aqNN,ard   )re   r2   r3   r>   )r-   s    r%   <lambda>5MultiColumnCompletionsMenu.__init__.<locals>.<lambda>  s"    bC,>,>,M,M,a,abbr(   )r   r   r   r   r   )r   r?   rY   r   )r   )r	   r   r   r   r   r   r   r   _SelectedCompletionMetaControlr   r   r&   )
r$   r   r   rN   r   full_filterany_completion_has_metacompletions_windowmeta_windowr   s
            r%   r&   #MultiColumnCompletionsMenu.__init__  s    !),	$\2 %&&(2\A"+ -c #d 28%^%!,&1-	/
   +#A#CD*-DDF
 	($8:
 	r(   r+   )r   r  TT)r   r   r   r   r   r&   r   r   r   s   @r%   r   r     s    
 r(   r   c                   0    \ rS rSrSrS rS rS rS rSr	g)	r  i  z@
Control that shows the meta information of the selected token.
c                     UR                   R                  (       a5  UR                   R                  nS[        S UR                   5       5      -   $ g)a  
Report the width of the longest meta text as the preferred width of this control.

It could be that we use less width, but this way, we're sure that the
layout doesn't change when we select another completion (E.g. that
completions are suddenly shown in more or fewer columns.)
rn   c              3   L   #    U  H  n[        UR                  5      v   M     g 7fr   rw   r`   s     r%   rb   A_SelectedCompletionMetaControl.preferred_width.<locals>.<genexpr>  s     Y?X!:ann55?Xrm   r   )r2   r3   rp   r>   )r$   r-   r6   states       r%   r9   ._SelectedCompletionMetaControl.preferred_width  sC     ,,&&55EsYu?X?XYYYYr(   c                     g)Nr   r+   )r$   r-   r?   r@   rA   s        r%   rB   /_SelectedCompletionMetaControl.preferred_height  s    r(   c                 \   ^ U R                  U5      mU4S jn[        UT(       a  SS9$ SS9$ )Nc                    > T$ r   r+   )rH   r   s    r%   rO   ?_SelectedCompletionMetaControl.create_content.<locals>.get_line  s    Mr(   r   r   r   )_get_tokensr   )r$   r-   r?   rY   rO   r   s        @r%   rZ   -_SelectedCompletionMetaControl.create_content  s1    !!#&	 (FqJJJJr(   c                    [         R                  R                  R                  nUR                  R
                  nU(       aH  UR                  (       a7  UR                  R                  (       a  USUR                  R                  -  4/$ / $ )Nz %s )r   r    r!   MultiColumnMetar2   r3   r   r_   )r$   r-   r"   r!  s       r%   r(  *_SelectedCompletionMetaControl._get_tokens  se    

&&66""11U--%2J2J2W2WFU%=%=%J%JJKLL	r(   r+   N)
r   r   r   r   r   r9   rB   rZ   r(  r   r+   r(   r%   r  r    s    Kr(   r  )'
__future__r   	six.movesr   r   prompt_toolkit.filtersr   r   r   r	   prompt_toolkit.mouse_eventsr
   prompt_toolkit.tokenr   prompt_toolkit.utilsr   
containersr   r   r   r   controlsr   r   	dimensionr   marginsr   screenr   r   r   __all__r   r   r   r   r   r  r+   r(   r%   <module>r9     s    ' ( S S 6 & + K K * & $  CY CD8@* @&\3y \3~" "J$Y $r(   