
    
8d&                     F    d dl mZ d dlmZ ddlmZ  G d de          ZdS )    )QtGui)Bool   )ConsoleWidgetc                        e Zd ZdZ edd          Z fdZ fdZd Zd Z	dd
Z
ddZddZd Zd Zd Zd Z xZS )HistoryConsoleWidgetz A ConsoleWidget that keeps a history of the commands that have been
        executed and provides a readline-esque interface to this history.
    FT)configc                 r     t                      j        |i | g | _        i | _        d| _        d| _        d S )Nr    )super__init___history_history_edits_history_index_history_prefix)selfargskw	__class__s      @lib/python3.11/site-packages/qtconsole/history_console_widget.pyr   zHistoryConsoleWidget.__init__   sF    $%"%%%  !    c                 <   || j         n|}t                                          |||           |rj|                                }|r2| j        r| j        d         |k    r| j                            |           i | _        t          | j                  | _        dS dS )z% Reimplemented to the store history. N)	input_bufferr   
do_executerstripr   appendr   lenr   )r   sourcecompleteindenthistoryr   s        r   r   zHistoryConsoleWidget.do_execute&   s    '-~$##668V444 	5 nn&&G . .r1Bg1M1M$$W--- #%D #&dm"4"4D	5 	5r   c                    |                                  }|                                                                 |                                k    r |                                 r|sdS |                                 }| j        }t          |t          | j                            }| j        d|         |d|         k    rt          | j	                  | _
        |                                 }|                                }|                    t          j        j                   |                                |k    }| j
        t          | j	                  k    s;| j        dk    r0|r.|                     | j
                  d|         |d|         k    s|d|         | _        |                     | j        |            |                                  }	| j        r9|	                    t          j        j        t          | j                             n$|	                    t          j        j                   |                     |	           dS dS )zj Called when the up key is pressed. Returns whether to continue
            processing the event.
        FNr   	as_prefixnT)_get_prompt_cursor_get_cursorblockNumber_history_locked_get_input_buffer_cursor_posr   minr   r   r   r   positionmovePositionr   QTextCursor
EndOfBlock_get_edited_historyhistory_previousRight_set_cursor)
r   shift_modifierprompt_cursorposr   r'   ccurrent_posat_eolcursors
             r   _up_pressedz HistoryConsoleWidget._up_pressed=   sH    //11))++}/H/H/J/JJJ##%% n u 3355C,L CT12233A $RaR(L!,<<<&)$-&8&8#   ""A**,,KNN5,7888jjllk1F"c$-&8&888)R//F/--d.ABB4C4HLY]Z]Y]L^^^'3DSD'9$ !!$"62@.@ " B B B ,,..F# B##E$5$;&)$*>&?&? $ A A A A ##E$5$@AAAV$$$5tr   c                    |                                  }|                                                                 |                                k    r|                                 r|sdS |                     | j        |           }| j        rc|ra|                                 }|                    t          j	        j
        t          | j                             |                     |           dS dS )zl Called when the down key is pressed. Returns whether to continue
            processing the event.
        Fr$   r&   T)_get_end_cursorr)   r*   r+   history_nextr   r(   r/   r   r0   r4   r   r5   )r   r6   
end_cursorreplacedr<   s        r   _down_pressedz"HistoryConsoleWidget._down_pressedu   s    ))++
))++z/E/E/G/GGG##%% n u (()=:H6H ) J JH # ) )0022##E$5$;&)$*>&?&? $ A A A  (((5tr   r   c                    | j         }d}|dk    rL|dz  }|                     |          }|| j        k    r,|r|                    |          s|s||v rd}n|dk    L|r"|                                  || _         || _        |S )a   If possible, set the input buffer to a previous history item.

        Parameters
        ----------
        substring : str, optional
            If specified, search for an item with this substring.
        as_prefix : bool, optional
            If True, the substring must match at the beginning (default).

        Returns
        -------
        Whether the input buffer was changed.
        Fr   r   T)r   r2   r   
startswith_store_editsr   	substringr%   indexreplacer"   s         r   r3   z%HistoryConsoleWidget.history_previous   s     #aiiQJE..u55G$+++ g00;; !&/7&:&: aii  	("'D 'Dr   c                 L   | j         }d}|t          | j                  k     r^|dz  }|                     |          }|| j        k    r>|r|                    |          s|s||v rd}n|t          | j                  k     ^|r"|                                  || _         || _        |S )a   If possible, set the input buffer to a subsequent history item.

        Parameters
        ----------
        substring : str, optional
            If specified, search for an item with this substring.
        as_prefix : bool, optional
            If True, the substring must match at the beginning (default).

        Returns
        -------
        Whether the input buffer was changed.
        Fr   T)r   r   r   r2   r   rE   rF   rG   s         r   r@   z!HistoryConsoleWidget.history_next   s     #c$-((((QJE..u55G$+++ g00;; !&/7&:&: c$-((((  	("'D 'Dr   
   c                 "    | j         | d         S )z Get the local history list.

        Parameters
        ----------
        n : int
            The (maximum) number of history items to get.
        N)r   )r   r'   s     r   history_tailz!HistoryConsoleWidget.history_tail   s     }aRSS!!r   c                     | j         op|                     | j                  | j        k    oM|                                                                 |                                                                 k    S )z5 Returns whether history movement is locked.
        )history_lockr2   r   r   r(   r*   r?   )r   s    r   r+   z$HistoryConsoleWidget._history_locked   su     ! 7))$*=>>"#7 ((**6688%%''33556	8r   c                     || j         v r| j         |         S |t          | j                  k    rt                      S | j        |         S )zB Retrieves a history item, possibly with temporary edits.
        )r   r   r   str)r   rI   s     r   r2   z(HistoryConsoleWidget._get_edited_history   sK     D'''&u--c$-((((55L}U##r   c                 n    t          |          | _        i | _        t          | j                  | _        dS )zG Replace the current history with a sequence of history items.
        N)listr   r   r   r   )r   r"   s     r   _set_historyz!HistoryConsoleWidget._set_history   s1     W !$-00r   c                     | j         }| j        t          | j                  k    s| j        | j                 |k    r|| j        | j        <   dS dS )zE If there are edits to the current input buffer, store them.
        N)r   r   r   r   r   )r   currents     r   rF   z!HistoryConsoleWidget._store_edits   sW     ##dm"4"444d12g==7>D 3444 >=r   )r   T)rL   )__name__
__module____qualname____doc__r   rP   r   r   r=   rC   r3   r@   rN   r+   r2   rU   rF   __classcell__)r   s   @r   r   r   
   s         4d+++L" " " " "5 5 5 5 5.6 6 6p  @   B   B" " " "8 8 8$ $ $1 1 1? ? ? ? ? ? ?r   r   N)qtpyr   	traitletsr   console_widgetr   r    r   r   <module>ra      s{                ) ) ) ) ) )z? z? z? z? z?= z? z? z? z? z?r   