a
    ñ»j+  ã                   @   sþ   d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dlm	Z	m
Z
 ddlmZ G dd„ deƒZG dd	„ d	eƒZed
krúeƒ Zeƒ Zeƒ ZejZe edƒ¡ e edƒ¡ e edƒ¡ e edƒ¡ e edƒ¡ e edƒ¡ e edƒ¡ dS )é    N)Úlog)Ú
ensure_strÚensure_unicodeé   )Úlineobjc                   @   s   e Zd ZdS )ÚEscapeHistoryN)Ú__name__Ú
__module__Ú__qualname__© r   r   úDE:\soccer\ml_env\Lib\site-packages\pyreadline3/lineeditor/history.pyr      s   r   c                   @   sÈ   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	e
eeƒZe
ee	ƒZdd„ Zd*dd„Zd+dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd,d d!„Zd-d"d#„Zd$d%„ Zd&d'„ Zd(d)„ ZdS ).ÚLineHistoryc                 C   s:   g | _ d| _d| _tj tdƒ¡| _d | _d| _	d| _
d S )Néd   r   z
~/.historyÚ )ÚhistoryÚ_history_lengthÚ_history_cursorÚosÚpathÚ
expanduserr   Úhistory_filenameÚlastcommandÚqueryÚlast_search_for©Úselfr   r   r   Ú__init__   s    zLineHistory.__init__c                 C   s   t | jƒ}td| ƒ |S )zÅReturn the number of lines currently in the history.
        (This is different from get_history_length(), which returns
        the maximum number of lines that will be written to a history file.)zget_current_history_length:%d)Úlenr   r   ©r   Úvaluer   r   r   Úget_current_history_length!   s    
z&LineHistory.get_current_history_lengthc                 C   s   | j }td| ƒ |S )ziReturn the desired length of the history file. Negative values imply
        unlimited history file size.zget_history_length:%d)r   r   r   r   r   r   Úget_history_length)   s    zLineHistory.get_history_lengthc                 C   s&   | j |d  }td||f ƒ | ¡ S )zKReturn the current contents of history item at index (starts with index 1).r   z"get_history_item: index:%d item:%r)r   r   Úget_line_text)r   ÚindexÚitemr   r   r   Úget_history_item0   s    zLineHistory.get_history_itemc                 C   s   t d| j|f ƒ || _d S )Nz!set_history_length: old:%d new:%d)r   r   r   r   r   r   Úset_history_length6   s    zLineHistory.set_history_lengthc                 C   s   | j }td| ƒ |S )Nzget_history_cursor:%d)r   r   r   r   r   r   Úget_history_cursor:   s    zLineHistory.get_history_cursorc                 C   s   t d| j|f ƒ || _d S )Nz!set_history_cursor: old:%d new:%d)r   r   r   r   r   r   Úset_history_cursor?   s    zLineHistory.set_history_cursorc                 C   s   g | j dd…< d| _dS )zClear readline history.Nr   )r   Úhistory_cursorr   r   r   r   Úclear_historyF   s    zLineHistory.clear_historyNc                 C   s„   |du r| j }zRtj|ddd��.}|D ]}|  t | ¡ ¡¡ q&W d  ƒ n1 sT0    Y  W n ty~   g | _d| _	Y n0 dS )zLoad a readline history file.NÚrtÚreplace©Úerrorsr   )
r   ÚioÚopenÚadd_historyr   ÚReadLineTextBufferÚrstripÚIOErrorr   r)   )r   ÚfilenameÚfdÚliner   r   r   Úread_history_fileK   s    8zLineHistory.read_history_filec                 C   sh   |du r| j }tj|ddd��6}| tdd„ | j| j d… D ƒƒ¡ W d  ƒ n1 sZ0    Y  dS )zSave a readline history file.NZwtr,   r-   c                 s   s   | ]}|  ¡ d  V  qdS )Ú
N)r"   )Ú.0r7   r   r   r   Ú	<genexpr>]   s   ÿz1LineHistory.write_history_file.<locals>.<genexpr>)r   r/   r0   Ú
writelinesÚtupler   Úhistory_length)r   r5   Úfpr   r   r   Úwrite_history_fileW   s    þÿzLineHistory.write_history_filec                 C   sh   t |ƒ}t|dƒst |¡}| ¡ s&n2t| jƒdkrL| jd  ¡ | ¡ krLn| j |¡ t| jƒ| _dS )zFAppend a line to the history buffer, as if it was the last line typed.r"   r   éÿÿÿÿN)	r   Úhasattrr   r2   r"   r   r   Úappendr)   )r   r7   r   r   r   r1   c   s    

ÿþzLineHistory.add_historyc                 C   sZ   | j t| jƒkr | j | ¡ ¡ | j dkrV|  j d8  _ | | j| j   ¡ ¡ tj|_	dS )zBMove back through the history list, fetching the previous command.r   r   N)
r)   r   r   rC   ÚcopyÚset_liner"   r   Z	EndOfLineÚpoint©r   Úcurrentr   r   r   Úprevious_historys   s    
zLineHistory.previous_historyc                 C   s<   | j t| jƒd k r8|  j d7  _ | | j| j   ¡ ¡ dS )zAMove forward through the history list, fetching the next command.r   N)r)   r   r   rE   r"   rG   r   r   r   Únext_history~   s    zLineHistory.next_historyc                 C   s$   d| _ t| jƒdkr | jd | _dS )z&Move to the first line in the history.r   N)r)   r   r   Zl_bufferr   r   r   r   Úbeginning_of_history„   s    z LineHistory.beginning_of_historyc                 C   s$   t | jƒ| _| | jd  ¡ ¡ dS )zUMove to the end of the input history, i.e., the line currently
        being entered.rA   N)r   r   r)   rE   r"   rG   r   r   r   Úend_of_historyŠ   s    zLineHistory.end_of_historyc                 C   sØ   |d u r| j }|}t d¡}tt| jƒƒ|dd… D ]\}}||v r4|} qNq4| j|krš|dkrš|d8 }tt| jƒƒ|dd… D ]\}}||v r€|} qšq€| jr°| j|  ¡ }nd}|| _ || _td|| j |f ƒ |S )Nr   r   rA   r   z/reverse_search_history: old:%d new:%d result:%r)	r)   r   r2   ÚlistÚ	enumerater   r   r"   r   )r   Ú	searchforÚstartposZorigposÚresultÚidxr7   r   r   r   Úreverse_search_history�   s2    
  
ÿÿz"LineHistory.reverse_search_historyc                 C   sØ   |d u r"t | jtd|  ¡ d ƒƒ}t d¡}tt| jƒƒ|d … D ]\}}||v rB|} q\qB| j	|kr®||  ¡ d k r®|d7 }tt| jƒƒ|d … D ]\}}||v r”|} q®q”| jrÄ| j|  
¡ }nd}|| _|| _	|S )Nr   r   r   )Úminr)   Úmaxr    r   r2   rM   rN   r   r   r"   )r   rO   rP   rQ   rR   r7   r   r   r   Úforward_search_history±   s0    ÿ
ÿþz"LineHistory.forward_search_historyc                 C   sÚ  �z¼| j | jkr6| j | jkr6d |d|j…  ¡ ¡| _t| jdƒ}| j| }|dk r\|dksr|dkræ|t	| j
ƒk ræ| j
| }| js¤|| _tj|t	| ¡ ƒd�}|W S | ¡  | j¡rÜ|| ¡ krÜ|| _tj||jd�}|W S ||7 }qLt	| j
ƒdkrönª|t	| j
ƒk�r*| j�s*t	| j
ƒ| _tjddd�W S | j
tt|t	| j
ƒd ƒdƒ  ¡  | j¡�rŽ| j�rŽtj| j
tt|t	| j
ƒd ƒdƒ |jd�W S tj||jd�W S tj| jtt	| jƒ|jƒd�W S  t�yÔ   ‚ Y n0 d S )Nr   r   )rF   r   )r   Úhistory_search_forwardÚhistory_search_backwardÚjoinrF   r"   r   rU   r)   r   r   r   r2   Ú
startswithrT   Ú
IndexError)r   Ú	directionÚpartialZhcstartZhcÚhrQ   r   r   r   Ú_searchÓ   s\    
ÿ
þ
ÿÿ

ÿ
$þÿüþÿzLineHistory._searchc                 C   s   |   d|¡S )zËSearch forward through the history for the string of characters
        between the start of the current line and the point. This is a
        non-incremental search. By default, this command is unbound.r   ©r_   ©r   r]   r   r   r   rW     s    z"LineHistory.history_search_forwardc                 C   s   |   d|¡S )zÌSearch backward through the history for the string of characters
        between the start of the current line and the point. This is a
        non-incremental search. By default, this command is unbound.rA   r`   ra   r   r   r   rX   	  s    z#LineHistory.history_search_backward)N)N)N)N)r   r	   r
   r   r    r!   r%   r&   r'   r(   Úpropertyr>   r)   r*   r8   r@   r1   rI   rJ   rK   rL   rS   rV   r_   rW   rX   r   r   r   r   r      s,   





!
"0r   Ú__main__ZaaaaZaabaZaacaZakcaÚbbbZako)ÚreÚoperatorÚstringÚsysr   r/   Zpyreadline3.loggerr   Zpyreadline3.unicode_helperr   r   r   r   Ú	Exceptionr   Úobjectr   r   ÚqÚrÚsr2   ZRLr1   r   r   r   r   Ú<module>
   s$   0 |