
    L                         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
e      Z G d de      Zd ZddZy)z2Stylized printing using ANSI codes utility module.    )absolute_import)division)unicode_literals)
properties)mappings)textNc                   ,    e Zd ZdZd Zed        Zd Zy)_StyleContextzGContains style information used in recursive parsingin TypedTextParser.c                      || _         || _        y Ncolorattrs)selfr   r   s      /lib/googlecloudsdk/core/console/style/parser.py__init__z_StyleContext.__init__   s    DJDJ    c                 Z    |s	 | d g       S  | |j                   |j                  xs g       S r   r   )clstext_attributess     r   FromTextAttributesz _StyleContext.FromTextAttributes#   s/    r]$$o&;&;&ArBBr   c                     |s| S |j                   xs | j                   }t        |dg       | j                  z   }| j                  ||      S )Nr   )r   getattrr   	__class__)r   r   	new_color	new_attrss       r   UpdateFromTextAttributesz&_StyleContext.UpdateFromTextAttributes)   sF    k%%3I"5

BI>>)Y//r   N)__name__
__module____qualname____doc__r   classmethodr   r    r   r   r
   r
      s%    O C C
0r   r
   c                   <    e Zd ZdZdZdZdZdZdZd Z	d Z
	 	 dd
Zy	)TypedTextParserz%Logger used to styled text to stderr.z[mz38;5;{}z39;0    c                      || _         || _        y)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*   s      r   r   zTypedTextParser.__init__=   s     )D&Dr   c                    d}d}t        t        |dg             t        t        |dg             z  }|r|dj                  t        |D cg c]!  }t	        j
                  |j                        # c}            z  }|dj                  t        |D cg c]1  }t	        j
                  d|j                  | j                  z   z        3 c}            z  }t        |dd      xs t        |dd      }|rE|r|dz  }|| j                  j                  |j                        z  }|r|dz  }|| j                  z  }d\  }}	|r| j                  |z   | j                  z   }|r| j                  |z   | j                  z   }	||	fS c c}w c c}w )zCReturns the ANSI start and reset sequences for the text_attributes. r   ;z%02xr   N)r,   r,   )setr   joinsortedsix	text_typevalueATTR_OFFSET_FOREGROUNDformatRESETCSISGR)
r   r   style_contextstyle_sequencereset_sequencer   attrr   begin_style	end_styles
             r   _GetAnsiSequenceForAttributez,TypedTextParser._GetAnsiSequenceForAttributeJ   s   NN45'2678E05)705#--


#)7 "8 9 9n
$ mmFdjj4==&@AB  n
 _gt4 3]GT2 
	#++225;;??n	#

"n#KHH~-8k((^+dhh6i	!!+)7s   	&E<6FNc                    t        |t        j                        r|S |xr | j                  }g }| j                  |j
                     }| j                  ||      \  }}|r|j                  |      }nt        j                  |      }|j                  D ]e  }	t        |	t        j                        r8|j                  | j                  |	||             |sC|j                  |       U|j                  |	       g dj                  |      }
|r'|j                   r|j                   j#                  |
      }
|rdj#                  ||
|      }
|
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.
    )r:   stylizer,   z{begin_style}{text}{end_style})r>   r   r?   )
isinstancer1   string_typesr*   r)   r2   r@   r   r
   r   textsr   	TypedTextappendParseTypedTextToStringr/   
format_strr6   )r   
typed_textr:   rB   parsed_chunksr   r>   r?   new_style_contextchunkparsed_texts              r   rH   z&TypedTextParser.ParseTypedTextToStringh   sM   " *c../,$,,GM))**>*>?O!>>(K '@@
 (::?K!!	E4>>	*T88+ 9  	 


{
+U# " ''-(K?55#..55kBk4;;! < k r   )NT)r   r   r    r!   r8   r9   r5   r7   r4   r   r@   rH   r#   r   r   r%   r%   2   s8    -##.
% ('"< >B%)4r   r%   c                     t         j                  j                  j                  j	                         } t        | t         j                  j                  j                  j                  j                  k(  t         j                  j                  j                  j	                         dk7  t         j                  j                  j                  j                         g      ryy)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)styles    r   _ColorsEnabledr]      s    



 
 
5
5
9
9
;%z  %%99==BBB115577B**224
6 7 	r   c                 j    t               }|xr | } |xs t        j                         }t        ||       S )z=Returns a typed text parser, creating a new one if necessary.)r]   r   GetStyleMappingsr%   )enabledr)   
can_enables      r   GetTypedTextParserrb      s4    *"7'!@X%>%>%@.		11r   )TN)r!   
__future__r   r   r   googlecloudsdk.corer   !googlecloudsdk.core.console.styler   r   r1   objectr
   r%   r]   rb   r#   r   r   <module>rg      sD    9 &  ' * 6 2 
0F 0,jf jZ2r   