B
    't\$                 @   s   d 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mZ ddlmZ d	d
dddgZG dd	 d	eeeZG dd
 d
eZG dd deZG dd deZG dd deZdS )zQ
Margin implementations for a :class:`~prompt_toolkit.layout.containers.Window`.
    )unicode_literals)ABCMetaabstractmethod)with_metaclass)range)	to_filter)fragment_list_to_textto_formatted_text)
get_cwidthMarginNumberedMarginScrollbarMarginConditionalMarginPromptMarginc               @   s(   e Zd ZdZedd Zedd ZdS )r   z&
    Base interface for a margin.
    c             C   s   dS )a  
        Return the width that this margin is going to consume.

        :param get_ui_content: Callable that asks the user control to create
            a :class:`.UIContent` instance. This can be used for instance to
            obtain the number of lines.
        r    )selfget_ui_contentr   r   <lib/python3.7/site-packages/prompt_toolkit/layout/margins.py	get_width   s    	zMargin.get_widthc             C   s   g S )a  
        Creates a margin.
        This should return a list of (style_str, text) tuples.

        :param window_render_info:
            :class:`~prompt_toolkit.layout.containers.WindowRenderInfo`
            instance, generated after rendering and copying the visible part of
            the :class:`~prompt_toolkit.layout.controls.UIControl` into the
            :class:`~prompt_toolkit.layout.containers.Window`.
        :param width: The width that's available for this margin. (As reported
            by :meth:`.get_width`.)
        :param height: The height that's available for this margin. (The height
            of the :class:`~prompt_toolkit.layout.containers.Window`.)
        r   )r   window_render_infowidthheightr   r   r   create_margin&   s    zMargin.create_marginN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r      s   c               @   s*   e Zd ZdZd
ddZdd Zdd Zd	S )r   a  
    Margin that displays the line numbers.

    :param relative: Number relative to the cursor position. Similar to the Vi
                     'relativenumber' option.
    :param display_tildes: Display tildes after the end of the document, just
        like Vi does.
    Fc             C   s   t || _t || _d S )N)r   relativedisplay_tildes)r   r   r   r   r   r   __init__B   s    
zNumberedMargin.__init__c             C   s   | j }tdtd| d S )N   z%s   )
line_countmaxlen)r   r   r"   r   r   r   r   F   s    zNumberedMargin.get_widthc             C   s   |   }d}d}|jjj}g }d }	xt|jD ]\}
}||	kr|d krHnr||kr|rl||d|d  f q||d|d  |f n0|rt|| d }||d|d  |f |}	|d q.W | 	 rx |
|j
k r|d |
d7 }
qW |S )Nzclass:line-numberz%class:line-number,current-line-numberz%ir!   z%i ) 
)zclass:tildez~
)r   
ui_contentZcursor_positiony	enumeratedisplayed_linesappendrjustabsr   window_height)r   r   r   r   r   ZstyleZstyle_currentZcurrent_linenoresultZlast_linenor(   linenor   r   r   r   J   s0    

zNumberedMargin.create_marginN)FF)r   r   r   r   r   r   r   r   r   r   r   r   9   s   
c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	r   zJ
    Wrapper around other :class:`.Margin` classes to show/hide them.
    c             C   s"   t |tst|| _t|| _d S )N)
isinstancer   AssertionErrormarginr   filter)r   r3   r4   r   r   r   r   z   s    zConditionalMargin.__init__c             C   s   |   r| j|S dS d S )Nr   )r4   r3   r   )r   r'   r   r   r   r      s    zConditionalMargin.get_widthc             C   s$   |r|   r| j|||S g S d S )N)r4   r3   r   )r   r   r   r   r   r   r   r      s    zConditionalMargin.create_marginN)r   r   r   r   r   r   r   r   r   r   r   r   v   s   c               @   s*   e Zd ZdZdddZdd Zd	d
 ZdS )r   zc
    Margin displaying a scrollbar.

    :param display_arrows: Display scroll up/down arrows.
    F^vc             C   s   t || _|| _|| _d S )N)r   display_arrowsup_arrow_symboldown_arrow_symbol)r   r7   r8   r9   r   r   r   r      s    
zScrollbarMargin.__init__c             C   s   dS )Nr!   r   )r   r'   r   r   r   r      s    zScrollbarMargin.get_widthc                sR  |j }|j}|  }|r |d8 }yHt|jt| }|jt| }tt|t	d||  t|| W n t
k
r|   g S X  fdd}	g }
|r|
d| jfdg d}d}d	}d
}xzt|D ]n}|	|r|	|d s|
|df n|
|df n,|	|d r|
|df n|
|df |
d qW |rJ|
d| jf |
S d S )N   r!   c                s   |   ko  kS   S )z1 True if we should display a button on this row. r   )row)scrollbar_heightscrollbar_topr   r   is_scroll_button   s    z7ScrollbarMargin.create_margin.<locals>.is_scroll_buttonzclass:scrollbar.arrow)zclass:scrollbarr&   zclass:scrollbar.backgroundz*class:scrollbar.background,scrollbar.startzclass:scrollbar.buttonz$class:scrollbar.button,scrollbar.end )r%   r&   )content_heightr.   r7   r$   r*   floatZvertical_scrollintminr#   ZeroDivisionErrorextendr8   r   r+   r9   )r   r   r   r   r@   r.   r7   Zfraction_visibleZfraction_abover>   r/   Zscrollbar_backgroundZscrollbar_background_startZscrollbar_buttonZscrollbar_button_endir   )r<   r=   r   r      sD    zScrollbarMargin.create_marginN)Fr5   r6   )r   r   r   r   r   r   r   r   r   r   r   r      s   
c               @   s*   e Zd ZdZd	ddZdd Zdd ZdS )
r   a  
    [Deprecated]

    Create margin that displays a prompt.
    This can display one prompt at the first line, and a continuation prompt
    (e.g, just dots) on all the following lines.

    This `PromptMargin` implementation has been largely superseded in favor of
    the `get_line_prefix` attribute of `Window`. The reason is that a margin is
    always a fixed width, while `get_line_prefix` can return a variable width
    prefix in front of every line, making it more powerful, especially for line
    continuations.

    :param get_prompt: Callable returns formatted text or a list of
        `(style_str, type)` tuples to be shown as the prompt at the first line.
    :param get_continuation: Callable that takes three inputs. The width (int),
        line_number (int), and is_soft_wrap (bool). It should return formatted
        text or a list of `(style_str, type)` tuples for the next lines of the
        input.
    Nc             C   s0   t |st|d ks t |s t|| _|| _d S )N)callabler2   
get_promptget_continuation)r   rH   rI   r   r   r   r      s    zPromptMargin.__init__c             C   s   t |  }t|S )z" Width to report to the `Window`. )r   rH   r
   )r   r'   textr   r   r   r      s    zPromptMargin.get_widthc          	   C   sh   | j }g }|t|   |rdd }x>|jdd  D ],}|d |t|||||k |}q4W |S )Nr!   )r%   r&   )rI   rE   r	   rH   r*   r+   )r   r   r   r   rI   r/   Zlast_yr(   r   r   r   r      s    
zPromptMargin.create_margin)N)r   r   r   r   r   r   r   r   r   r   r   r      s   
N)r   Z
__future__r   abcr   r   Zsixr   Z	six.movesr   Zprompt_toolkit.filtersr   Zprompt_toolkit.formatted_textr   r	   Zprompt_toolkit.utilsr
   __all__objectr   r   r   r   r   r   r   r   r   <module>   s"   "=G