o
    L                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
Z
G d	d
 d
eZG dd deZdd ZdddZdS )z2Stylized printing using ANSI codes utility module.    )absolute_import)division)unicode_literals)
properties)mappings)textNc                   @   s,   e Zd ZdZdd Zedd Zdd ZdS )	_StyleContextzGContains style information used in recursive parsingin TypedTextParser.c                 C   s   || _ || _d S Ncolorattrs)selfr   r    r   E/tmp/google-cloud-sdk/lib/googlecloudsdk/core/console/style/parser.py__init__   s   
z_StyleContext.__init__c                 C   s    |s| d g S | |j |jpg S r	   r
   )clstext_attributesr   r   r   FromTextAttributes#   s   
z _StyleContext.FromTextAttributesc                 C   s2   |s| S |j p	| j }t|dg | j }| ||S )Nr   )r   getattrr   	__class__)r   r   	new_color	new_attrsr   r   r   UpdateFromTextAttributes)   s
   z&_StyleContext.UpdateFromTextAttributesN)__name__
__module____qualname____doc__r   classmethodr   r   r   r   r   r   r      s    
r   c                   @   sB   e Zd ZdZdZdZdZdZdZdd Z	d	d
 Z
		dddZdS )TypedTextParserz%Logger used to styled text to stderr.z[mz38;5;{}z39;0    c                 C   s   || _ || _dS )am  Creates a styled logger used to print styled text to stdout.

    Args:
      style_mappings: (StyleMapping), A mapping from TextTypes to
        mappings.TextAttributes used to stylize the output. If the map does
        not contain a TextAttribute object, plain text will bef
        logged.
      style_enabled: (bool), whether logged text should be styled.
    N)style_mappingsstyle_enabled)r   r!   r"   r   r   r   r   =   s   

zTypedTextParser.__init__c           	         s   d}d}t t|dg t t|dg B }|r4|dtdd |D 7 }|dt fdd|D 7 }t|ddp?t|dd}|r\|rH|d7 }| j|j7 }|rW|d7 }| j7 }d	\}}|rj j|  j	 }|rt j|  j	 }||fS )
zCReturns the ANSI start and reset sequences for the text_attributes. r   ;c                 S   s   g | ]}t |jqS r   )six	text_typevalue.0attrr   r   r   
<listcomp>Q   s    z@TypedTextParser._GetAnsiSequenceForAttribute.<locals>.<listcomp>c                    s"   g | ]}t d |j j  qS )z%02x)r%   r&   r'   ATTR_OFFr(   r   r   r   r+   T   s    r   N)r#   r#   )
setr   joinsortedSET_FOREGROUNDformatr'   RESETCSISGR)	r   r   style_contextstyle_sequencereset_sequencer   r   begin_style	end_styler   r-   r   _GetAnsiSequenceForAttributeJ   s<   

z,TypedTextParser._GetAnsiSequenceForAttributeNTc                 C   s   t |tjr|S |o| j}g }| j|j }| ||\}}|r%||}nt	|}|j
D ] }	t |	tjrH|| j|	||d |rG|| q-||	 q-d|}
|r^|jr^|j|
}
|rhdj||
|d}
|
S )a4  Parses a TypedText object into plain and ansi-annotated unicode.

    The reason this returns both the plain and ansi-annotated strings is to
    support file logging.

    Args:
      typed_text: mappings.TypedText, typed text to be converted to unicode.
      style_context: _StyleContext, argument used for recursive calls
        to preserve text attributes and colors. Recursive calls are made when a
        TypedText object contains TypedText objects.
      stylize: bool, Whether or not to stylize the string.

    Returns:
      str, the parsed text.
    )r6   stylizer#   z{begin_style}{text}{end_style})r9   r   r:   )
isinstancer%   string_typesr"   r!   r&   r;   r   r   r   textsr   	TypedTextappendParseTypedTextToStringr/   
format_strr2   )r   
typed_textr6   r<   parsed_chunksr   r9   r:   new_style_contextchunkparsed_textr   r   r   rB   h   sD   





z&TypedTextParser.ParseTypedTextToString)NT)r   r   r   r   r4   r5   r1   r3   r,   r   r;   rB   r   r   r   r   r   2   s    r   c                  C   sJ   t jjj } t| t jjjjjkt jjj	 dkt jjj
 gr#dS dS )z)Returns true if colors should be enabled.neverFT)r   VALUEScoreinteractive_ux_styleGetanyInteractiveUXStylesOFFnameshow_structured_logsdisable_colorGetBool)styler   r   r   _ColorsEnabled   s   rV   Tc                 C   s$   t  }|o| } |pt }t|| S )z=Returns a typed text parser, creating a new one if necessary.)rV   r   GetStyleMappingsr   )enabledr!   
can_enabler   r   r   GetTypedTextParser   s   
rZ   )TN)r   
__future__r   r   r   googlecloudsdk.corer   !googlecloudsdk.core.console.styler   r   r%   objectr   r   rV   rZ   r   r   r   r   <module>   s   m