o
    PhK                     @   s   d Z ddlZddlmZmZ ddlmZmZ e dkr!ddl	Z
nddl
Z
dd Ze Zed 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G dd deZdd Zdd Zdd Zdd Zdd ZdS )z3Sub-module providing sequence-formatting functions.    N)TextType
StringType)
CGA_COLORSX11_COLORNAMES_TO_RGBWindowsc                  C   sj   t  } tD ]}| | | d|  | d|  | d|  qtD ]}| | | d|  q$| S )zt
    Return set of valid colors and their derivatives.

    :rtype: set
    :returns: Color names with prefixes
    on_bright_
on_bright_)setr   addr   )colors	cga_color	vga_color r   M/var/www/html/env_mimamsha/lib/python3.10/site-packages/blessed/formatters.py_make_colors   s   

r   z,bold underline reverse blink italic standoutc                   @   "   e Zd ZdZd	ddZdd ZdS )
ParameterizingStringa:  
    A Unicode string which can be called as a parameterizing termcap.

    For example::

        >>> from blessed import Terminal
        >>> term = Terminal()
        >>> color = ParameterizingString(term.color, term.normal, 'color')
        >>> color(9)('color #9')
        u'\x1b[91mcolor #9\x1b(B\x1b[m'
     <not specified>c                 C   s   t | |}||_||_|S )a  
        Class constructor accepting 3 positional arguments.

        :arg str cap: parameterized string suitable for curses.tparm()
        :arg str normal: terminating sequence for this capability (optional).
        :arg str name: name of this terminal capability (optional).
        )r   __new___normal_name)clscapnormalnamenewr   r   r   r   >   s   	zParameterizingString.__new__c              
   G   s   zt j| dg|R  d}t|| jW S  ty6 } z|r1t|d tr1td| j	||f  d}~w t j
yR } zdt|vrE t W  Y d}~S d}~ww )a3  
        Returning :class:`FormattingString` instance for given parameters.

        Return evaluated terminal capability (self), receiving arguments
        ``*args``, followed by the terminating sequence (self.normal) into
        a :class:`FormattingString` capable of being called.

        :raises TypeError: Mismatch between capability and arguments
        :raises curses.error: :func:`curses.tparm` raised an exception
        :rtype: :class:`FormattingString` or :class:`NullCallableString`
        :returns: Callable string for given parameters
        latin1r   zCUnknown terminal capability, %r, or, TypeError for arguments %r: %sNztparm() returned NULL)cursestparmencodedecodeFormattingStringr   	TypeError
isinstancer   r   errorr   NullCallableString)selfargsattrerrr   r   r   __call__L   s$   
zParameterizingString.__call__Nr   r   __name__
__module____qualname____doc__r   r,   r   r   r   r   r   1   s    
r   c                   @   r   )
ParameterizingProxyStringa+  
    A Unicode string which can be called to proxy missing termcap entries.

    This class supports the function :func:`get_proxy_string`, and mirrors
    the behavior of :class:`ParameterizingString`, except that instead of
    a capability name, receives a format string, and callable to filter the
    given positional ``*args`` of :meth:`ParameterizingProxyString.__call__`
    into a terminal sequence.

    For example::

        >>> from blessed import Terminal
        >>> term = Terminal('screen')
        >>> hpa = ParameterizingString(term.hpa, term.normal, 'hpa')
        >>> hpa(9)
        u''
        >>> fmt = u'\x1b[{0}G'
        >>> fmt_arg = lambda *arg: (arg[0] + 1,)
        >>> hpa = ParameterizingProxyString((fmt, fmt_arg), term.normal, 'hpa')
        >>> hpa(9)
        u'\x1b[10G'
    r   r   c                 C   sT   t |ts	J |t|d sJ |d t| |d }|d |_||_||_|S )a  
        Class constructor accepting 4 positional arguments.

        :arg tuple fmt_pair: Two element tuple containing:
            - format string suitable for displaying terminal sequences
            - callable suitable for receiving  __call__ arguments for formatting string
        :arg str normal: terminating sequence for this capability (optional).
        :arg str name: name of this terminal capability (optional).
           r   )r%   tuplecallabler   r   	_fmt_argsr   r   )r   fmt_pairr   r   r   r   r   r   r      s   
z!ParameterizingProxyString.__new__c                 G   s   t | j| j|  | jS )a  
        Returning :class:`FormattingString` instance for given parameters.

        Arguments are determined by the capability.  For example, ``hpa``
        (move_x) receives only a single integer, whereas ``cup`` (move)
        receives two integers.  See documentation in terminfo(5) for the
        given capability.

        :rtype: FormattingString
        :returns: Callable string for given parameters
        )r#   formatr7   r   r(   r)   r   r   r   r,      s   z"ParameterizingProxyString.__call__Nr-   r.   r   r   r   r   r3   r   s    
r3   c                   @   s"   e Zd ZdZdddZdd ZdS )	r#   af  
    A Unicode string which doubles as a callable.

    This is used for terminal attributes, so that it may be used both
    directly, or as a callable.  When used directly, it simply emits
    the given terminal sequence.  When used as a callable, it wraps the
    given (string) argument with the 2nd argument used by the class
    constructor::

        >>> from blessed import Terminal
        >>> term = Terminal()
        >>> style = FormattingString(term.bright_blue, term.normal)
        >>> print(repr(style))
        u'\x1b[94m'
        >>> style('Big Blue')
        u'\x1b[94mBig Blue\x1b(B\x1b[m'
    r   c                 C      t | |}||_|S )z
        Class constructor accepting 2 positional arguments.

        :arg str sequence: terminal attribute sequence.
        :arg str normal: terminating sequence for this attribute (optional).
        )r   r   r   )r   sequencer   r   r   r   r   r         zFormattingString.__new__c                    s|   t |D ]\}}t|tstd||tjt|jf qd}r5jr5j}j   fdd|D }d| | S )z
        Return ``text`` joined by ``sequence`` and ``normal``.

        :raises TypeError: Not a string type
        :rtype: str
        :returns: Arguments wrapped in sequence and normal
        zUTypeError for FormattingString argument, %r, at position %s: expected type %s, got %sr   c                    s   g | ]}  |jqS r   )joinsplitr   ).0ucs_part_refreshr(   r   r   
<listcomp>   s    z-FormattingString.__call__.<locals>.<listcomp>)	enumerater%   r   r$   r/   typer   r>   )r(   r)   idxrA   postfixr   rB   r   r,      s    


zFormattingString.__call__N)r   r.   r   r   r   r   r#      s    
r#   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	FormattingOtherStringa  
    A Unicode string which doubles as a callable for another sequence when called.

    This is used for the :meth:`~.Terminal.move_up`, ``down``, ``left``, and ``right()``
    family of functions::

        >>> from blessed import Terminal
        >>> term = Terminal()
        >>> move_right = FormattingOtherString(term.cuf1, term.cuf)
        >>> print(repr(move_right))
        u'\x1b[C'
        >>> print(repr(move_right(666)))
        u'\x1b[666C'
        >>> print(repr(move_right()))
        u'\x1b[C'
    c                 C   r;   )z
        Class constructor accepting 2 positional arguments.

        :arg str direct: capability name for direct formatting, eg ``('x' + term.right)``.
        :arg str target: capability name for callable, eg ``('x' + term.right(99))``.
        r   r   	_callable)r   directtargetr   r   r   r   r      r=   zFormattingOtherString.__new__c                 C   s   t t | | jfS NrJ   )r(   r   r   r   __getnewargs__     z$FormattingOtherString.__getnewargs__c                 G   s   |r| j | S | S )zReturn ``text`` by ``target``.)rK   r:   r   r   r   r,     rP   zFormattingOtherString.__call__N)r/   r0   r1   r2   r   rO   r,   r   r   r   r   rI      s
    rI   c                   @   s    e Zd ZdZdd Zdd ZdS )r'   z
    A dummy callable Unicode alternative to :class:`FormattingString`.

    This is used for colors on terminals that do not support colors, it is just a basic form of
    unicode that may also act as a callable.
    c                 C   s   t | dS )zClass constructor.r   )r   r   )r   r   r   r   r     s   zNullCallableString.__new__c                 G   s"   |r	t |d trt S d|S )a6  
        Allow empty string to be callable, returning given string, if any.

        When called with an int as the first arg, return an empty Unicode. An int is a good hint
        that I am a :class:`ParameterizingString`, as there are only about half a dozen string-
        returning capabilities listed in terminfo(5) which accept non-int arguments, they are seldom
        used.

        When called with a non-int as the first arg (no no args at all), return the first arg,
        acting in place of :class:`FormattingString` without any attributes.
        r   r   )r%   intr'   r>   r:   r   r   r   r,     s   	
zNullCallableString.__call__Nr.   r   r   r   r   r'     s    r'   c                    s   t t fdddD  }tddd f j|tddd f j|d	td
dd f j|tddd f j|tddd f j|tddd f j|dddd}||i |dS )a  
    Proxy and return callable string for proxied attributes.

    :arg Terminal term: :class:`~.Terminal` instance.
    :arg str attr: terminal capability name that may be proxied.
    :rtype: None or :class:`ParameterizingProxyString`.
    :returns: :class:`ParameterizingProxyString` for some attributes
        of some terminal types that support it, where the terminfo(5)
        database would otherwise come up empty, such as ``move_x``
        attribute for ``term.kind`` of ``screen``.  Otherwise, None.
    c                 3   s     | ]} j |r|V  qd S rN   )kind
startswith)r@   _kindtermr   r   	<genexpr>B  s    
z#get_proxy_string.<locals>.<genexpr>)screenansiz[{0}Gc                  W      | d d fS Nr   r4   r   argr   r   r   <lambda>H      z"get_proxy_string.<locals>.<lambda>z[{0}dc                  W   rZ   r[   r   r\   r   r   r   r^   J  r_   )hpavpaz[?25lc                  W      dS Nr   r   r\   r   r   r   r^   P      z[?25hc                  W   rb   rc   r   r\   r   r   r   r^   R  rd   c                  W   rZ   r[   r   r\   r   r   r   r^   T  r_   c                  W   rZ   r[   r   r\   r   r   r   r^   V  r_   z[sz[u)civiscnormr`   ra   scrcN)nextiterr3   r   get)rV   r*   	term_kind_proxy_tabler   rU   r   get_proxy_string5  s6   
rn   c                 C   sP   g }g d}|  dD ]}|r |d |v r |d  d| 7  < q|| q|S )aN  
    Split compound formating string into segments.

    >>> split_compound('bold_underline_bright_blue_on_red')
    ['bold', 'underline', 'bright_blue', 'on_red']

    :arg str compound: a string that may contain compounds, separated by
        underline (``_``).
    :rtype: list
    :returns: List of formating string segments
    )onbright	on_bright_)r?   append)compoundmerged_segsmergeable_prefixessegmentr   r   r   split_compound^  s   ry   c                 C   s4   | j sdS t| j||}|du rdS |dS )ak  
    Resolve a raw terminal capability using :func:`tigetstr`.

    :arg Terminal term: :class:`~.Terminal` instance.
    :arg str attr: terminal capability name.
    :returns: string of the given terminal capability named by ``attr``,
       which may be empty (u'') if not found or not supported by the
       given :attr:`~.Terminal.kind`.
    :rtype: str
    r   Nr   )does_stylingr   tigetstr_sugarrk   r"   )rV   r*   valr   r   r   resolve_capabilityu  s   r~   c           	      C   s  | j dkrt S d|v r| jn| j}|ddd }|tv rDd|v r$dnd}|ddd }d| f }|tt|| }t	|| j
S |tv sNJ d	|ft| }| j d
krd|| j| }t	|| j
S d|v rjdnd}| j dkssJ d| d }t	|j| | j
S )au  
    Resolve a simple color name to a callable capability.

    This function supports :func:`resolve_attribute`.

    :arg Terminal term: :class:`~.Terminal` instance.
    :arg str color: any string found in set :const:`COLORS`.
    :returns: a string class instance which emits the terminal sequence
        for the given color, and may be used as a callable to wrap the
        given string with such sequence.
    :returns: :class:`NullCallableString` when
        :attr:`~.Terminal.number_of_colors` is 0,
        otherwise :class:`FormattingString`.
    :rtype: :class:`NullCallableString` or :class:`FormattingString`
    r   r   rr   r4   rs   r      zCOLOR_%szcolor not known   4838i   z[z;2;{0};{1};{2}m)number_of_colorsr'   _background_color_foreground_colorrsplitr   uppergetattrr   r#   r   r   rgb_downconvertr9   )	rV   colorvga_color_cap
base_coloroffsetr*   fmt_attrrgbfgbg_seqr   r   r   resolve_color  s.   


r   c                    s   |t v r	t |S |tv rt |}t| jS t|}tdd |D r7 fdd|D }td| jS t |}|sNt	  j
||}|durN|S t| j|S )a  
    Resolve a terminal attribute name into a capability class.

    :arg Terminal term: :class:`~.Terminal` instance.
    :arg str attr: Sugary, ordinary, or compound formatted terminal
        capability, such as "red_on_white", "normal", "red", or
        "bold_on_black".
    :returns: a string class instance which emits the terminal sequence
        for the given terminal capability, or may be used as a callable to
        wrap the given string with such sequence.
    :returns: :class:`NullCallableString` when
        :attr:`~.Terminal.number_of_colors` is 0,
        otherwise :class:`FormattingString`.
    :rtype: :class:`NullCallableString` or :class:`FormattingString`
    c                 s   s     | ]}|t v p|tv V  qd S rN   )COLORSCOMPOUNDABLESr@   fmtr   r   r   rW     s    z$resolve_attribute.<locals>.<genexpr>c                 3   s    | ]}t  |V  qd S rN   )resolve_attributer   rU   r   r   rW     s    r   N)r   r   r   r~   r#   r   ry   allr>   rn   r|   rk   r   )rV   r*   r<   
formatters
resolutiontparm_capseqproxyr   rU   r   r     s"   


r   )r2   platformblessed._compatr   r   blessed.colorspacer   r   systemjinxedr   r   r   r
   r?   r   r   r3   r#   rI   r'   rn   ry   r~   r   r   r   r   r   r   <module>   s(    
A;<'%)5