
    BNeQZ                       d Z ddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
 ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZ ddlmZ ddlmZ ddlmZ ddl m!Z! ddl"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z, ddl-m.Z.m/Z/m0Z0 ddl1m2Z3 ddl4m5Z5 ddl6m7Z7m8Z8 ddl9m:Z:m;Z;m<Z< ddl=m>Z> ddl?m@Z@ ddlAmBZB ddlCmDZDmEZE ddlFmGZH ddlFmIZI ddlJmKZK ddlLmMZM erddlNmOZO d ZPd!d"gZQe!ZRd#ZS G d$ d%          ZTdDd,ZU G d- d!          ZVdEd/ZWdFd1ZX G d2 d3e7          ZY G d4 d5e7          ZZ G d6 d7e:          Z[ G d8 d9          Z\dGd;Z]dGd<Z^dHdAZ_ G dB d"          Z`dCS )Iz
Utility to easily select lines from the history and execute them again.

`create_history_application` creates an `Application` instance that runs will
run as a sub application of the Repl/PythonInput.
    )annotations)partial)TYPE_CHECKINGCallableListOptionalSet)Application)get_app)Buffer)Document)DEFAULT_BUFFER)	Condition	has_focus)StyleAndTextTuples)fragment_list_to_text)History)KeyBindings)KeyPressEvent)
ConditionalContainer	ContainerFloatFloatContainerHSplitScrollOffsetsVSplitWindowWindowAlignWindowRenderInfo)BufferControlFormattedTextControl	UIContent)	Dimension)Layout)MarginScrollbarMargin)	ProcessorTransformationTransformationInput)PygmentsLexer)
MouseEventFrame)
ArgToolbarSearchToolbar)Python3Lexer)RstLexer)get_inputmode_fragments   )if_mousedown)PythonInputi  HistoryLayoutPythonHistorya  
This interface is meant to select multiple lines from the
history and execute them together.

Typical usage
-------------

1. Move the ``cursor up`` in the history pane, until the
   cursor is on the first desired line.
2. Hold down the ``space bar``, or press it multiple
   times. Each time it will select one line and move to
   the next one. Each selected line will appear on the
   right side.
3. When all the required lines are displayed on the right
   side, press ``Enter``. This will go back to the Python
   REPL and show these lines as the current input. They
   can still be edited from there.

Key bindings
------------

Many Emacs and Vi navigation key bindings should work.
Press ``F4`` to switch between Emacs and Vi mode.

Additional bindings:

- ``Space``: Select or delect a line.
- ``Tab``: Move the focus between the history and input
  pane. (Alternative: ``Ctrl-W``)
- ``Ctrl-C``: Cancel. Ignore the result and go back to
  the REPL. (Alternatives: ``q`` and ``Control-G``.)
- ``Enter``: Accept the result and go back to the REPL.
- ``F1``: Show/hide help. Press ``Enter`` to quit this
  help message.

Further, remember that searching works like in Emacs
(using ``Ctrl-R``) or Vi (using ``/``).
c                  .    e Zd ZdZdZdZdZdZdZdZ	dZ
d	S )
BORDERzBox drawing characters.u   ━u   ┃u   ┏u   ┓u   ┗u   ┛u   │N)__name__
__module____qualname____doc__
HORIZONTALVERTICALTOP_LEFT	TOP_RIGHTBOTTOM_LEFTBOTTOM_RIGHTLIGHT_VERTICAL     /home/feoh/.cache/pypoetry/virtualenvs/veilid-python-play-bIboZq2W-py3.11/lib/python3.11/site-packages/ptpython/history_browser.pyr9   r9   l   s7        JHHIKLNNNrF   r9   titlestrbodyr   returnr-   c                $    t          ||           S )z
    Return the layout for a pop-up window. It consists of a title bar showing
    the `title` text, and a body layout. The window is surrounded by borders.
    )rJ   rH   r,   rH   rJ   s     rG   _create_popup_windowrN   w   s    
 d%((((rF   c                      e Zd ZdZddZdS )	r6   zS
    Create and return a `Container` instance for the history
    application.
    historyr7   rK   Nonec                   t                      }t          |j        t          t                              | _        t          dt          | j        t          d          gt          dd                              }t          |j
        t          |j                  gt          t                    	          | _        t          |j        t          t                    |j        d
          | _        t          | j        dt%          |          gt          dd                    }t'          t          t)          t*                    t,          j        d          t1          t3          |t          t5          j        d          t8          j        d          t          | j        dt=          |          gt          dd                    g          t?          dddtA          |tC          |j                                      g          tE                      |t          t)          tG          tH          |                    d          g          | _%        tM          | j%        |          | _'        d S )N)bufferlexerzHistory HelpT)display_arrows   )topbottom)contentright_marginsscroll_offsetsrM   )rS   input_processorsrT   )rS   rT   search_buffer_controlpreview_searchF)rY   
wrap_linesleft_marginsr[   class:status-toolbar)rY   alignstyler3   zclass:separator)widthcharrc   <      )rY   filter)rd   rW   rX   rY   )rY   floats)rP   )rY   rc   )(r/   r    help_bufferr*   r1   help_buffer_controlrN   r   r&   r   default_bufferGrayExistingTexthistory_mappingPythonLexerdefault_buffer_controlhistory_buffercontrolhistory_buffer_controlHistoryMarginr   r!   _get_top_toolbar_fragmentsr   CENTERr   r   Dexactr9   rD   ResultMarginr   r   r   r.   r   _get_bottom_toolbar_fragmentsroot_containerr$   layout)selfrP   search_toolbarhelp_windowhistory_windows        rG   __init__zHistoryLayout.__init__   s   &#0&mH.E.E$
 $
 $
  + 0.dCCCD,1===  
 
 
 '4).w/FGGH,,'
 '
 '
# '4),,"0"8	'
 '
 '
#  /'001(Qq999	
 
 
 % 01KLL%,0  
 " +"&'gajj%+%:&7   #(,(C+0.:7.C.C-D/<1/M/M/M	   * "$ !#$$8(3'01D'E'E% % %	  )     D 0 =wOOO  1	  W13
 3
j T0.AArF   NrP   r7   rK   rQ   )r:   r;   r<   r=   r   rE   rF   rG   r6   r6      s>         
YB YB YB YB YB YBrF   r   c                     dgS )N)zclass:status-bar.titlez%History browser - Insert from historyrE   rE   rF   rG   ru   ru      s    OPPrF   rP   c                      j         }t          d fd            }t          d fd            }dgt          |          z   ddd	d
d|fdd|fddd
d|fdd|fg	z   S )Nmouse_eventr+   rK   rQ   c                &    t                     d S N_toggle_helpr   rP   s    rG   f1z)_get_bottom_toolbar_fragments.<locals>.f1   s    WrF   c                &    t                     d S r   _select_other_windowr   s    rG   tabz*_get_bottom_toolbar_fragments.<locals>.tab   s    W%%%%%rF   )ra    )class:status-toolbar.keyz[Space])ra   z Toggle r   z[Tab]ra   z Focus )r   z[Enter])ra   z Accept z[F1]z Help )r   r+   rK   rQ   )python_inputr4   r2   )rP   r   r   r   s   `   rG   rz   rz      s    'L     \ & & & & & \& 
''
!,
/
/	0 *30'#6#Y430'4#Xr2

	
rF   c                  *    e Zd ZdZddZdd
ZddZdS )rt   z`
    Margin for the history buffer.
    This displays a green bar for the selected entries.
    rP   r7   rK   rQ   c                6    |j         | _         |j        | _        d S r   )rq   rn   r}   rP   s     rG   r   zHistoryMargin.__init__  s    %4&6rF   get_ui_contentCallable[[], UIContent]intc                    dS NrV   rE   r}   r   s     rG   	get_widthzHistoryMargin.get_width      qrF   window_render_infor   rd   heightr   c                N   | j         j        }| j        j        }| j        j        }|j        }|j        }g }	t          |          D ]`}
|                    |
          }||v rd}nd}||v rd}nd}||k    r|dz   }|		                    ||f           |		                    d           a|	S )N*r   class:history-line,selectedzclass:history-linez,current 
)
rq   documentrn   lines_starting_new_entriesselected_linescursor_position_rowvisible_line_to_input_linerangegetappend)r}   r   rd   r   r   r   r   current_linenor   resultyline_numberre   ts                 rG   create_marginzHistoryMargin.create_margin  s     &/%)%9%T"-<!5%7%R"%'v 	& 	&A488;;K 888n,,1(n,,
NMM1d)$$$MM*%%%%rF   Nr   r   r   rK   r   r   r   rd   r   r   r   rK   r   )r:   r;   r<   r=   r   r   r   rE   rF   rG   rt   rt     sZ         
7 7 7 7   " " " " " "rF   rt   c                  2    e Zd ZdZddZdd
ZddZddZdS )ry   z4
    The margin to be shown in the result pane.
    rP   r7   rK   rQ   c                6    |j         | _         |j        | _        d S r   )rn   rq   r   s     rG   r   zResultMargin.__init__8  s    &6%4rF   r   r   r   c                    dS r   rE   r   s     rG   r   zResultMargin.get_width<  r   rF   r   r   rd   r   r   c                h   | j         j        }|j        }| j        j        }|j        }g }t          |          D ]y}	|                    |	          }
|
&|
|k     s |
|t          | j        j	                  z   k    rd}n|
|k    rd}nd}|
                    |df           |
                    d           z|S )Nr   z#class:history-line,selected,currentr   r   r   )rq   r   r   rn   result_line_offsetr   r   r   lenr   r   )r}   r   rd   r   r   r   offsetr   r   r   r   r   s               rG   r   zResultMargin.create_margin?  s     &/!5 3 	 &8%R"%'v 	& 	&A488;;K #''&3t/C/R+S+S"SSS..91MM1c(###MM*%%%%rF   r   r   c                    |j         S r   )r   )r}   r   s     rG   invalidation_hashzResultMargin.invalidation_hash`  s    ++rF   Nr   r   r   )r   r   rK   r   )r:   r;   r<   r=   r   r   r   r   rE   rF   rG   ry   ry   3  so         5 5 5 5      B, , , , , ,rF   ry   c                  "    e Zd ZdZddZdd
ZdS )rm   zK
    Turn the existing input, before and after the inserted code gray.
    rn   HistoryMappingrK   rQ   c                t    || _         t          |j        j                                                  | _        d S r   )rn   r   original_documenttext_before_cursor
splitlines_lines_before)r}   rn   s     rG   r   zGrayExistingText.__init__i  s7    . -@KKMM
 
rF   transformation_inputr)   r(   c                    |j         }|j        }|| j        k     s%|| j        t          | j        j                  z   k    r"t          |          }t          d|fg          S t          |          S )Nzclass:history.existing-input)	fragments)linenor   r   r   rn   r   r   r(   )r}   r   r   r   texts        rG   apply_transformationz%GrayExistingText.apply_transformationo  s     &,(2	D&&&&D4F /J
 J
 5
 +
 +
 )33D!.Ld-S,TUUUU!I6666rF   N)rn   r   rK   rQ   )r   r)   rK   r(   )r:   r;   r<   r=   r   r   rE   rF   rG   rm   rm   d  sF         
 
 
 
7 7 7 7 7 7rF   rm   c                  ,    e Zd ZdZdd
ZdddZddZdS )r   zO
    Keep a list of all the lines from the history and the selected lines.
    rP   r7   python_historyr   r   r   rK   rQ   c                   || _         || _        || _        t                      | _        t                      | _        |                                }g }t          t          |                    t           d          D ]X\  }}| j        
                    t          |                     |                                D ]}|                    |           Yt          |          t          k    rdt          z  |d<   || _        d                    |          | _        | j        j        r| j        j        dz   | _        d S d| _        d S )Nz0# *** History has been truncated to %s lines ***r   r   r3   )rP   r   r   setr   r   get_stringslist	enumerateHISTORY_COUNTaddr   r   r   history_linesjoinconcatenated_historyr   r   r   )	r}   rP   r   r   history_stringsr   entry_nrentrylines	            rG   r   zHistoryMapping.__init__  sL    ,!2*-%%'(+ )4466#%#Io$>$>??P 	+ 	+OHe+//M0B0BCCC((** + +$$T****+ -//B]R ! +$(IIm$<$<! !4 	(&*&<&PST&TD###&'D###rF   N
cursor_pos
int | Nonec                   g }| j         j        r|                    | j         j                   t          | j                  D ]"}|                    | j        |                    #| j         j        r|                    | j         j                   d                    |          }|"|t          |          k    rt          |          }t          ||          S )zP
        Create a `Document` instance that contains the resulting text.
        r   )
r   r   r   sortedr   r   text_after_cursorr   r   r   )r}   r   linesline_nor   s        rG   get_new_documentzHistoryMapping.get_new_document  s      !4 	DLL/BCCC d122 	6 	6GLL+G45555 !3 	CLL/ABBB yy!j3t99&<&<TJj)))rF   c                |    | j         j        }|                    |                     |j                  d           d S )NT)bypass_readonly)rP   rl   set_documentr   cursor_position)r}   bs     rG   update_default_bufferz$HistoryMapping.update_default_buffer  s:    L'	t,,Q->??QUVVVVVrF   )rP   r7   r   r   r   r   rK   rQ   r   )r   r   rK   r   )rK   rQ   )r:   r;   r<   r=   r   r   r   rE   rF   rG   r   r   ~  sf         #( #( #( #(J* * * * *0W W W W W WrF   r   rQ   c                    | j         j        }| j        j        j        |k    r | j        j                                         dS || j        j        _        dS zDisplay/hide help.N)history_layoutrk   appr|   current_controlfocus_previous)rP   rk   s     rG   r   r     sR    !0D{)-@@@))+++++-@***rF   c                    | j         j        }| j        j        }|| j        k    r| j        j        |_        dS || j        k    r| j        j        |_        dS dS )z'Toggle focus between left/right window.N)	r   current_bufferr   r|   rq   rp   r   rl   rs   )rP   r   r|   s      rG   r   r     sc    [/N#*F///!(!7!N	71	1	1!(!7!N 
2	1rF   r   r5   rn   r   c                    t                      }|j        } |dt           j                            d! fd            } |dt          t                               |dt          t                               |d	t          t                              d!fd
                                    }t           j                  }t           j                  t           j                  z  } |d|           |d|d           |d|          d! fd                                    } |d          d!fd            } |d          d! fd            } |d|           |d|           |d|           |d|          d!d                                                } |d|           |d|           |d|           |d|          d!d                                                } |d|          d! fd            }t          fd          } |d|          d!d             }|S )"z
    Key bindings.
    r   )rh   eventErK   rQ   c                   | j         }|j        j        }j        sdS |j        v r/j                            |                                            nj                            |                                            j        }t          j                  
                    |          j        z   }|j                            |d          |_        |j                            |dz   d          |_        dS )z@
        Space: select/deselect line from history pane.
        Nr   r3   )r   r   r   r   r   remover   r   rl   r   indexr   translate_row_col_to_indexr   )r   r   r   rl   default_linenorP   rn   s        rG   _zcreate_key_bindings.<locals>._  s	   
  *0, 	Fo444*11':::113333 *..w77711333 %3N566<<WEE!45 
 'BB>STUU * JAA'A+qQQrF   deletezc-hc                   | j         }|j        j        j        z
  }|dk    r\	 t	          j                  |         }j                            |           n# t          $ r Y nw xY w                                 dS dS )z7
        Space: remove line from default pane.
        r   N)	r   r   r   r   r   r   r   
IndexErrorr   )r   r   r   history_linenorn   s       rG   r   zcreate_key_bindings.<locals>._
  s      *0?3UUa<<F!'(F!G!G!P  .55nEEEE    
 1133333 <s   A 
A&%A&r   zc-xT)rh   eagerzc-wc                &    t                     dS )zSelect other window.Nr   r   rP   s    rG   r   zcreate_key_bindings.<locals>._#  s     	W%%%%%rF   f4c                "    j          _         dS )zSwitch between Emacs/Vi mode.N)vi_mode)r   r   s    rG   r   zcreate_key_bindings.<locals>._+  s     $0#77rF   r   c                &    t                     dS r   r   r  s    rG   r   zcreate_key_bindings.<locals>._0  s     	WrF   enterzc-czc-gescapec                B    | j         j                                         dS )zLeave help.N)r   r|   r   r   s    rG   r   zcreate_key_bindings.<locals>._5  s!     		'')))))rF   qf3c                <    | j                             d           dS )zCancel and go back.Nr   )r   exitr	  s    rG   r   zcreate_key_bindings.<locals>._=  s      		d#####rF   c                R    | j                             j        j                   dS )zAccept input.r  N)r   r  rl   r   r  s    rG   r   zcreate_key_bindings.<locals>._E  s'     		g49:::::rF   c                      j         S r   )enable_system_bindingsr   s   rG   <lambda>z%create_key_bindings.<locals>.<lambda>J  s
    |/R rF   zc-zc                8    | j                                          dS )zSuspend to background.N)r   suspend_to_backgroundr	  s    rG   r   zcreate_key_bindings.<locals>._L  s     		'')))))rF   )r   r   rK   rQ   )r   r   r   rq   r   rj   rl   r   )	rP   r   rn   bindingshandler   help_focussedmain_buffer_focussedr  s	   ```      rG   create_key_bindingsr    s    }}H\FVC	'"899::: R  R  R  R  R  R ;: RD VC	.11222VHY~66777VE)N334444 4 4 4 4 54 87 324" g122M$W%;<<y@ @  VE.///VE.d;;;VE.///& & & & & 0/ <; 0/& VD\\8 8 8 8 8 \8 VD\\     \ VGM***VE-(((VE-(((VH]+++* * * ,+ )( )( +** VC,---VD-...VE.///VE.///$ $ $ 0/ 0/ /. .-$ VG0111; ; ; ; ; 21; ''R'R'R'RSSVE0111* * * 21* OrF   c                  &    e Zd ZddZdd
ZddZdS )r7   r   r5   r   r   rK   rQ   c                     _         t           j        |          }| _        t	          |j                  }t	          |j        |j        |                                z             }d fd}t          | j
        |d           _        t          t          |                                 j        d	           _        t          t	          t           d
          d           _        t%                      _        t)           j        j        dj        t/          fd          t1           |                     _        dS )z
        Create an `Application` for the history screen.
        This has to be run as a sub application of `python_input`.

        When this application runs and returns, it returns the selected lines.
        )r   rS   r   rK   boolc                `    t                                          j        j                   dS )Nr  F)r   r  rl   r   )rS   r}   s    rG   accept_handlerz.PythonHistory.__init__.<locals>.accept_handlerh  s&    IINN$"5":N;;;5rF   T)r   on_cursor_position_changedr  	read_only)namer   r   r!  r   )r   r!  c                      j         S r   )enable_mouse_supportr  s   rG   r  z(PythonHistory.__init__.<locals>.<lambda>  s
    L,M rF   )r|   full_screenrc   mouse_supportkey_bindingsN)rS   r   rK   r  )r   r   rP   rn   r   r   r   r   get_start_of_line_positionr   _history_buffer_pos_changedrq   r   r   _default_buffer_pos_changedrl   	HELP_TEXTrj   r6   r   r
   r|   _current_styler   r  r   )r}   r   r   rn   r   r  s   ``    rG   r   zPythonHistory.__init__U  si    )(|/CEVWW.O@AAM$411334
 
 
	 	 	 	 	 	 %'+'G)	
 
 
 %$5577'+'G	
 
 
 "8Iq+A+ATRRR+D11%0&--#$M$M$M$MNN,T<QQ&
 &
 &
rF   r   r   c                @   | j         j        | j        k    r	 | j        j        j        | j        j        z
  }|dk     rt          t          | j        j	                  |         }| j
        j                            |d          | j
        _        dS # t          $ r Y dS w xY wdS )zWWhen the cursor changes in the default buffer. Synchronize with
        history buffer.r   N)r   r   rl   r   r   rn   r   r   r   r   rq   r   r   )r}   r   r   r   s       rG   r*  z)PythonHistory._default_buffer_pos_changed  s     8"d&999'0D*=> 
 Q;;$$!'(<(K!L!LW!U
 '0KK&  #333     :9s   A
B 
BBc                <   | j         j        | j        k    r| j        j        j        }|| j        j        v rgt          | j        j                                      |          | j        j	        z   }| j
        j                            |d          | j
        _        dS dS dS )z;When the cursor changes in the history buffer. Synchronize.r   N)r   r   rq   r   r   rn   r   r   r   r   rl   r   r   )r}   r   r   r   s       rG   r)  z)PythonHistory._history_buffer_pos_changed  s     8"d&999)2FG$.===4/>??EEgNN*=>  '0KK&  #333 :9 >=rF   N)r   r5   r   r   rK   rQ   )r   r   rK   rQ   )r:   r;   r<   r   r*  r)  rE   rF   rG   r7   r7   T  sQ        /
 /
 /
 /
b   0     rF   N)rH   rI   rJ   r   rK   r-   )rK   r   )rP   r7   rK   r   r   )rP   r7   r   r5   rn   r   rK   r   )ar=   
__future__r   	functoolsr   typingr   r   r   r   r	   prompt_toolkit.applicationr
   "prompt_toolkit.application.currentr   prompt_toolkit.bufferr   prompt_toolkit.documentr   prompt_toolkit.enumsr   prompt_toolkit.filtersr   r   "prompt_toolkit.formatted_text.baser   #prompt_toolkit.formatted_text.utilsr   prompt_toolkit.historyr   prompt_toolkit.key_bindingr   (prompt_toolkit.key_binding.key_processorr    prompt_toolkit.layout.containersr   r   r   r   r   r   r   r   r   r   prompt_toolkit.layout.controlsr    r!   r"   prompt_toolkit.layout.dimensionr#   rw   prompt_toolkit.layout.layoutr$   prompt_toolkit.layout.marginsr%   r&    prompt_toolkit.layout.processorsr'   r(   r)   prompt_toolkit.lexersr*   prompt_toolkit.mouse_eventsr+   prompt_toolkit.widgetsr-   prompt_toolkit.widgets.toolbarsr.   r/   pygments.lexersr0   ro   r1   ptpython.layoutr2   utilsr4   r   r5   r   __all__r   r+  r9   rN   r6   ru   rz   rt   ry   rm   r   r   r   r  r7   rE   rF   rG   <module>rK     s    # " " " " "       ? ? ? ? ? ? ? ? ? ? ? ? ? ? 2 2 2 2 2 2 6 6 6 6 6 6 ( ( ( ( ( ( , , , , , , / / / / / / 7 7 7 7 7 7 7 7 A A A A A A E E E E E E * * * * * * 2 2 2 2 2 2 B B B B B B                                
 ; : : : : : / / / / / / A A A A A A A A         
 0 / / / / / 2 2 2 2 2 2 ( ( ( ( ( ( E E E E E E E E 7 7 7 7 7 7 $ $ $ $ $ $ 3 3 3 3 3 3       *))))))O
,%	P       ) ) ) )_B _B _B _B _B _B _B _BDQ Q Q Q   8/ / / / /F / / /d., ., ., ., .,6 ., ., .,b7 7 7 7 7y 7 7 74EW EW EW EW EW EW EW EWPA A A A	O 	O 	O 	Ou u u upZ Z Z Z Z Z Z Z Z ZrF   