o
    ¹e\+  ã                   @   s  d dl mZmZmZ d dlZd dlZd dlmZ d dlm	Z	m
Z
 dejv r+ejd Znd dl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 dS )é    )Úabsolute_importÚprint_functionÚunicode_literalsN)Úlog)Ú
ensure_strÚensure_unicodeÚpyreadline3é   )Úlineobjc                   @   s   e Zd ZdS )ÚEscapeHistoryN)Ú__name__Ú
__module__Ú__qualname__© r   r   úND:\Projects\ConvertPro\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   ó   | 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_length0   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_item7   s   zLineHistory.get_history_itemc                 C   ó   t d| j|f ƒ || _d S )Nz!set_history_length: old:%d new:%d)r   r   r"   r   r   r   Úset_history_length=   ó   
zLineHistory.set_history_lengthc                 C   r%   )Nzget_history_cursor:%d)r   r   r"   r   r   r   Úget_history_cursorA   s   zLineHistory.get_history_cursorc                 C   r+   )Nz!set_history_cursor: old:%d new:%d)r   r   r"   r   r   r   Úset_history_cursorF   r-   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_historyM   s   
zLineHistory.clear_historyNc                 C   sb   |du r| j }zt|dddD ]}|  t t| ¡ ƒ¡¡ qW dS  ty0   g | _d| _	Y dS w )zLoad a readline history file.NÚrzutf-8)Úencodingr   )
r   ÚopenÚadd_historyr
   ÚReadLineTextBufferr   ÚrstripÚIOErrorr   r0   )r   ÚfilenameÚliner   r   r   Úread_history_fileR   s    ÿÿÿÿþzLineHistory.read_history_filec                 C   s^   |du r| j }t|dƒ}| j| j d… D ]}| t| ¡ ƒ¡ | d d¡¡ q| ¡  dS )zSave a readline history file.NÚwbÚ
Úascii)	r   r4   r   Úhistory_lengthÚwriter   r'   ÚencodeÚclose)r   r9   Úfpr:   r   r   r   Úwrite_history_file`   s   
zLineHistory.write_history_filec                 C   sh   t |ƒ}t|dƒst |¡}| ¡ snt| jƒdkr&| jd  ¡ | ¡ kr&n| 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
   r6   r'   r!   r   Úappendr0   )r   r:   r   r   r   r5   j   s   

$zLineHistory.add_historyc                 C   s^   | j t| jƒkr| j | ¡ ¡ | j dkr-|  j d8  _ | | j| j   ¡ ¡ tj|_	dS dS )zCMove back through the history list, fetching the previous command. r   r	   N)
r0   r!   r   rG   ÚcopyÚset_liner'   r
   Z	EndOfLineÚpoint©r   Úcurrentr   r   r   Úprevious_historyw   s   
ýzLineHistory.previous_historyc                 C   s@   | j t| jƒd k r|  j d7  _ | | j| j   ¡ ¡ dS dS )zBMove forward through the history list, fetching the next command. r	   N)r0   r!   r   rI   r'   rK   r   r   r   Únext_history‚   s   þzLineHistory.next_historyc                 C   s(   d| _ t| jƒdkr| jd | _dS dS )z&Move to the first line in the history.r   N)r0   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.rE   N)r!   r   r0   rI   r'   rK   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 r&|} nq| j|krM|dkrM|d8 }tt| jƒƒ|dd… D ]\}}||v rL|} nq@| jrX| j|  ¡ }nd}|| _ || _td|| j |f ƒ |S )Nr   r   rE   r	   z/reverse_search_history: old:%d new:%d result:%r)	r0   r
   r6   ÚlistÚ	enumerater   r   r'   r   )r   Ú	searchforÚstartposZorigposÚresultÚidxr:   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 r-|} nq!| j	|krW||  ¡ d k rW|d7 }tt| jƒƒ|d … D ]\}}||v rV|} nqJ| jrb| j|  
¡ }nd}|| _|| _	|S )Nr   r	   r   )Úminr0   Úmaxr$   r
   r6   rQ   rR   r   r   r'   )r   rS   rT   rU   rV   r:   r   r   r   Úforward_search_history³   s4   ÿÿ
þ
þz"LineHistory.forward_search_historyc                 C   sê  zì| j | jkr| j | jkrd |d|j…  ¡ ¡| _t| jdƒ}| j| }|dk r-|dks8|dkr„|t	| j
ƒk r„| j
| }| jsQ|| _tj|t	| ¡ ƒd}|W S | ¡  | j¡rm|| ¡ krm|| _tj||jd}|W S ||7 }|dk ry|dks8|dkr„|t	| j
ƒk s8t	| j
ƒdkrŒnQ|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ô   ‚ w )Nr   r   )rJ   r	   )r   Úhistory_search_forwardÚhistory_search_backwardÚjoinrJ   r'   r   rY   r0   r!   r   r
   r6   Ú
startswithrX   Ú
IndexError)r   Ú	directionÚpartialZhcstartZhcÚhrU   r   r   r   Ú_searchÓ   sh   
ÿ
ÿóÿÿÿ
ÿÿÿ
ÿÿÿzLineHistory._searchc                 C   ó   |   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	   ©rc   ©r   ra   r   r   r   r[   ü   s   z"LineHistory.history_search_forwardc                 C   rd   )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.rE   re   rf   r   r   r   r\     s   z#LineHistory.history_search_backward)N)r   r   r   r    r$   r&   r*   r,   r.   r/   Úpropertyr?   r0   r1   r;   rD   r5   rM   rN   rO   rP   rW   rZ   rc   r[   r\   r   r   r   r   r      s.    







 )r   Ú__main__ZaaaaZaabaZaacaZakcaÚbbbZako)Ú
__future__r   r   r   r   ÚsysZpyreadline3.loggerr   Zpyreadline3.unicode_helperr   r   Úmodulesr   r   r
   Ú	Exceptionr   Úobjectr   r   Úqr2   Úsr6   ZRLr5   r   r   r   r   Ú<module>   s2   
 nõ