
    %                         d dl mZ d dlmZmZ d dlmZ d dlZd dlZdZ	 G d d eee
            Z G d d	e      Z G d
 de      Zy)    )unicode_literals)ABCMetaabstractmethod)with_metaclassN)FileHistoryHistoryInMemoryHistoryc                   Z    e Zd ZdZed        Zed        Zed        Zed        Zd Z	e	Z
y)r   z%
    Base ``History`` interface.
    c                      y)z Append string to history. N selfstrings     )lib/third_party/prompt_toolkit/history.pyappendzHistory.append           c                      y)zH Return one item of the history. It should be accessible like a `list`. Nr   r   keys     r   __getitem__zHistory.__getitem__   r   r   c                      y)z? Iterate through all the items of the history. Cronologically. Nr   r   s    r   __iter__zHistory.__iter__   r   r   c                      y)z$ Return the length of the history.  Nr   r   s    r   __len__zHistory.__len__   r   r   c                      y)z
        Never evaluate to False, even when the history is empty.
        (Python calls __len__ if __bool__ is not implemented.)
        This is mainly to allow lazy evaluation::

            x = history or InMemoryHistory()
        Tr   r   s    r   __bool__zHistory.__bool__#   s     r   N)__name__
__module____qualname____doc__r   r   r   r   r   r   __nonzero__r   r   r   r   r      sg     & & S S J J / / Kr   r   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)r	   zM
    :class:`.History` class that keeps a list of all strings in memory.
    c                     g | _         y Nstringsr   s    r   __init__zInMemoryHistory.__init__4   s	    r   c                 :    | j                   j                  |       y r&   )r(   r   r   s     r   r   zInMemoryHistory.append7   s    F#r   c                      | j                   |   S r&   r'   r   s     r   r   zInMemoryHistory.__getitem__:       ||C  r   c                 ,    t        | j                        S r&   iterr(   r   s    r   r   zInMemoryHistory.__iter__=       DLL!!r   c                 ,    t        | j                        S r&   lenr(   r   s    r   r   zInMemoryHistory.__len__@       4<<  r   N)	r   r    r!   r"   r)   r   r   r   r   r   r   r   r	   r	   0   s     $!"!r   r	   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	r   zD
    :class:`.History` class that stores all strings in a file.
    c                 @    g | _         || _        | j                          y r&   )r(   filename_load)r   r7   s     r   r)   zFileHistory.__init__H   s     

r   c                 ^    g  fd}t         j                  j                   j                        rnt	         j                  d      5 }|D ]B  }|j                  d      }|j                  d      rj                  |dd         : |        g D  |        d d d        y y # 1 sw Y   y xY w)Nc                  j    r0dj                        d d } j                  j                  |        y y )N )joinr(   r   )r   linesr   s    r   addzFileHistory._load.<locals>.addQ   s1    ,##F+	 r   rbutf-8+   )ospathexistsr7   opendecode
startswithr   )r   r?   fliner>   s   `   @r   r8   zFileHistory._loadN   s    	, 77>>$--(dmmT*aD;;w/Ds+T!"X. "   +* )**s   
AB##B,c                 .   | j                   j                  |       t        | j                  d      5 fd} |dt        j                  j                         z         |j                  d      D ]  } |d|z          	 d d d        y # 1 sw Y   y xY w)Nabc                 F    j                  | j                  d             y )NrA   )writeencode)trJ   s    r   rO   z!FileHistory.append.<locals>.writej   s    )*r   z
# %s

z+%s
)r(   r   rG   r7   datetimenowsplit)r   r   rO   rK   rJ   s       @r   r   zFileHistory.appende   su    F# $--&!+ *x0044667T*gn% + '&&s   ABBc                      | j                   |   S r&   r'   r   s     r   r   zFileHistory.__getitem__q   r,   r   c                 ,    t        | j                        S r&   r.   r   s    r   r   zFileHistory.__iter__t   r0   r   c                 ,    t        | j                        S r&   r2   r   s    r   r   zFileHistory.__len__w   r4   r   N)
r   r    r!   r"   r)   r8   r   r   r   r   r   r   r   r   r   D   s%    .
&!"!r   r   )
__future__r   abcr   r   sixr   rS   rD   __all__objectr   r	   r   r   r   r   <module>r^      sG    ' '   	nWf- B!g !(4!' 4!r   