
    gfK                        d Z ddlZddlZddlmZmZ  ej                         dk(  rddlZnddlZd Z	 e	       Z
 edj                               Z G d dej                        Z G d	 d
ej                        Z G d dej                        Z G d dej                        Z G d dej                        Zd Zd Zd Zd Zd Zy)z3Sub-module providing sequence-formatting functions.    N)
CGA_COLORSX11_COLORNAMES_TO_RGBWindowsc                  *   t               } t        D ]O  }| j                  |       | j                  d|z          | j                  d|z          | j                  d|z          Q t        D ]'  }| j                  |       | j                  d|z          ) | 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_colors      j/var/dept/share/cheung/public_html/OutSchool/python/env/lib/python3.12/site-packages/blessed/formatters.py_make_colorsr      s     UF   -	

9

59$%

9y()

<)+,	- + &	

9

59$%& M    z,bold underline reverse blink italic standoutc                       e Zd ZdZddZd Zy)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'
    c                 b    t         j                  j                  | |      }||_        ||_        |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).
        )six	text_type__new___normal_name)clscapnormalnamenews        r   r   zParameterizingString.__new__@   s-     mm##C-	
r   c           	         	 t        j                  | j                  d      g| j                  d      }t	        || j
                        S # t        $ rC}|r;t        |d   t        j                        rt        d| j                  d|d|       d}~wt         j                  $ r,}dt        j                  |      vr t               cY d}~S d}~ww xY 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   zUnknown terminal capability, z, or, TypeError for arguments z: Nztparm() returned NULL)cursestparmencodedecodeFormattingStringr   	TypeError
isinstancer   string_typesr   errorr   NullCallableString)selfargsattrerrs       r   __call__zParameterizingString.__call__N   s    	( <<H 5==DDXND#D$,,77 		 
47C,<,<=.2jj$EF F
 || 	( 'cmmC.@@%''	(s*   A	A 	C>BC)!C
CCN z<not specified>__name__
__module____qualname____doc__r   r/    r   r   r   r   3   s    
#(r   r   c                       e Zd ZdZddZd Zy)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'
    c                     t        |t              sJ |       t        |d         s
J |d          t        j                  j                  | |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   r   	_fmt_argsr   r   )r   fmt_pairr   r   r   s        r   r   z!ParameterizingProxyString.__new__   sl     (E*4H4*$1hqk1$mm##C!5 	
r   c                 b    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%   formatr>   r   r+   r,   s     r   r/   z"ParameterizingProxyString.__call__   s0      ^T^^T-B C $. 	.r   Nr0   r2   r7   r   r   r9   r9   t   s    .&.r   r9   c                       e Zd ZdZddZd Zy)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'
    c                 T    t         j                  j                  | |      }||_        |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   )r   sequencer   r   s       r   r   zFormattingString.__new__   s&     mm##C2
r   c                    t        |      D ]o  \  }}t        |t        j                        r!dj	                  d t        j                  D              }t        d|d|d|dt        |      j                         d}| r^| j                  rR| j                  }| j                  | z   }|D cg c],  }|j	                  |j                  | j                              . }}| dj	                  |      z   |z   S c c}w )z
        Return ``text`` joined by ``sequence`` and ``normal``.

        :raises TypeError: Not a string type
        :rtype: str
        :returns: Arguments wrapped in sequence and normal
        z, c              3   4   K   | ]  }|j                     y wN)r3   ).0_types     r   	<genexpr>z,FormattingString.__call__.<locals>.<genexpr>   s     *Xe5>>*Xs   z)TypeError for FormattingString argument, z, at position z: expected type z, got r1   )
	enumerater'   r   r(   joinr&   typer3   r   split)r+   r,   idxucs_partexpected_typespostfix_refreshs          r   r/   zFormattingString.__call__   s     't_ 	:MCh(8(89!%*XsGWGW*X!X !)#~ $X 7 79: :	: DLLllG||d*H$(*  MM(..">? *D * chhtn$w..*s   -1C7N)r1   r2   r7   r   r   r%   r%      s    $
/r   r%   c                   "    e Zd ZdZd Zd Zd Zy)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                 T    t         j                  j                  | |      }||_        |S )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   r   	_callable)r   directtargetr   s       r   r   zFormattingOtherString.__new__   s&     mm##C0
r   c                 v    t         j                  j                  t         j                  |       | j                  fS rH   rX   )r+   s    r   __getnewargs__z$FormattingOtherString.__getnewargs__  s&    }}$$S]]D94>>IIr   c                 (    |r | j                   | S | S )zReturn ``text`` by ``target``.)rY   rB   s     r   r/   zFormattingOtherString.__call__  s    (,~t~~t$6$6r   N)r3   r4   r5   r6   r   r]   r/   r7   r   r   rV   rV      s    "
J7r   rV   c                       e Zd ZdZd Zd Zy)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                 B    t         j                  j                  | d      S )zClass constructor.r1   )r   r   r   )r   s    r   r   zNullCallableString.__new__  s    }}$$S#..r   c                 b    |rt        |d   t              r
t               S dj                  |      S )a=  
        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   r1   )r'   intr*   rM   rB   s     r   r/   zNullCallableString.__call__   s-     z$q'3/ &''xx~r   Nr2   r7   r   r   r*   r*     s    /r   r*   c           	          t        t         fddD                     }t        dd f j                  |      t        dd f j                  |      dt        dd	 f j                  |      t        d
d f j                  |      t        dd f j                  |      t        dd f j                  |      dddd}|j	                  |i       j	                  |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   X   K   | ]!  }j                   j                  |      r| # y wrH   )kind
startswith)rI   _kindterms     r   rK   z#get_proxy_string.<locals>.<genexpr>G  s*      :E!YY11%8   :s   '*)screenansiz[{0}Gc                      | d   dz   fS Nr   r;   r7   args    r   <lambda>z"get_proxy_string.<locals>.<lambda>M      SVaZM r   z[{0}dc                      | d   dz   fS rl   r7   rm   s    r   ro   z"get_proxy_string.<locals>.<lambda>O  rp   r   )hpavpaz[?25lc                       yNr7   r7   rm   s    r   ro   z"get_proxy_string.<locals>.<lambda>U      r   z[?25hc                       yru   r7   rm   s    r   ro   z"get_proxy_string.<locals>.<lambda>W  rv   r   c                      | d   dz   fS rl   r7   rm   s    r   ro   z"get_proxy_string.<locals>.<lambda>Y  rp   r   c                      | d   dz   fS rl   r7   rm   s    r   ro   z"get_proxy_string.<locals>.<lambda>[  rp   r   z[sz[u)civiscnormrr   rs   scrcN)nextiterr9   r   get)rh   r-   	term_kind_proxy_tables   `   r   get_proxy_stringr   :  s     T :-@ : :;?AI
 -9:DKKO,9:DKKO	
 //dD./dD,9:DKKO,9:DKKO
L. Ir*..tT::r   c                     g }g d}| j                  d      D ]-  }|r|d   |v r|dxx   d|z   z  cc<   |j                  |       / |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_)rO   append)compoundmerged_segsmergeable_prefixessegments       r   split_compoundr   c  s^     K6>>#& (;r?.@@OsW},Ow'	(
 r   c                     | j                   syt        j                  | j                  j	                  ||            }|dS |j                  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
    r1   r    )does_stylingr!   tigetstr_sugarr   r$   )rh   r-   vals      r   resolve_capabilityr   z  sG     
//$++//$5
6C +373::h#77r   c                    | j                   dk(  r
t               S d|v r| j                  n| j                  }|j	                  dd      d   }|t
        v r_d|v rdnd}|j	                  dd      d   }d|j                         } |t        t        |      |z         }t        || j                        S |t        v s	J d	|f       t        |   }| j                   d
k  r+ | | j                  |       }t        || j                        S d|v rdnd}| j                   dk(  sJ d|z   dz   }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   r   r;   r   r      COLOR_z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_downconvertrA   )	rh   colorvga_color_cap
base_coloroffsetr-   fmt_attrrgbfgbg_seqs	            r   resolve_colorr     sh   " !!## 05~T++++  c1%b)JZ  5(a\\#q)"-
'--/1 !6!?@$++66.. ':1' '.


+C # !5!5!5s!;<$++66 DH  G+++("%66HOHOOS14;;??r   c                     |t         v rt         |      S |t        v r"t         |      }t	        | j
                        S t        |      }t        d |D              r0 fd|D        }t	        dj                  |       j
                        S t         |      }|s*t          j                  j                  ||            }||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              3   @   K   | ]  }|t         v xs |t        v   y wrH   )COLORSCOMPOUNDABLES)rI   fmts     r   rK   z$resolve_attribute.<locals>.<genexpr>  s!     
IsC6M1SM11
Is   c              3   6   K   | ]  }t        |        y wrH   )resolve_attribute)rI   r   rh   s     r   rK   z$resolve_attribute.<locals>.<genexpr>  s     Is'c2Is   r1   )r   r   r   r   r%   r   r   allrM   r   r   r   r   )rh   r-   rE   
formatters
resolutiontparm_capseqproxys   `      r   r   r     s      v~T4(( }%dD1$++66
  %J

Ij
IIIjI
 4dkkBB &dD1L !!%t!<>Ldkk4@@r   )r6   platformr   blessed.colorspacer   r   systemjinxedr!   r   r   r
   rO   r   r   r   r9   r%   rV   r*   r   r   r   r   r   r7   r   r   <module>r      s    9   A 8??	!2 
 BHHJK>(3== >(B8. 8.v;/s}} ;/|$7CMM $7N# #L&;R.8&2@j.Ar   