
    L                         S r SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJ	r	  SSK
r
 " S	 S
\5      r " S S\5      rS rSS jrg)z2Stylized printing using ANSI codes utility module.    )absolute_import)division)unicode_literals)
properties)mappings)textNc                   4    \ rS rSrSrS r\S 5       rS rSr	g)_StyleContext   zGContains style information used in recursive parsingin TypedTextParser.c                     Xl         X l        g Ncolorattrs)selfr   r   s      /lib/googlecloudsdk/core/console/style/parser.py__init___StyleContext.__init__   s    JJ    c                 n    U(       d	  U " S / 5      $ U " UR                   UR                  =(       d    / 5      $ r   r   )clstext_attributess     r   FromTextAttributes _StyleContext.FromTextAttributes#   s/    r]$$o&;&;&ArBBr   c                     U(       d  U $ UR                   =(       d    U R                   n[        US/ 5      U R                  -   nU R                  X#5      $ )Nr   )r   getattrr   	__class__)r   r   	new_color	new_attrss       r   UpdateFromTextAttributes&_StyleContext.UpdateFromTextAttributes)   sD    k%%3I"5

BI>>)//r   )r   r   N)
__name__
__module____qualname____firstlineno____doc__r   classmethodr   r    __static_attributes__ r   r   r
   r
      s%    O C C
0r   r
   c                   F    \ rS rSrSrSrSrSrSrSr	S r
S	 r  SS jrSrg
)TypedTextParser2   z%Logger used to styled text to stderr.z[mz38;5;{}z39;0    c                     Xl         X l        g)aQ  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   r0   r1   s      r   r   TypedTextParser.__init__=   s     )&r   c                 Z   SnSn[        [        US/ 5      5      [        [        US/ 5      5      -  nU(       a  USR                  [        U Vs/ s H#  n[        R
                  " UR                  5      PM%     sn5      5      -  nUSR                  [        U Vs/ s H3  n[        R
                  " SUR                  U R                  -   -  5      PM5     sn5      5      -  n[        USS5      =(       d    [        USS5      nU(       aM  U(       a  US-  nX0R                  R                  UR                  5      -  nU(       a  US-  nX@R                  -  nSu  pU(       a  U R                  U-   U R                  -   nU(       a  U R                  U-   U R                  -   n	X4$ s  snf s  snf )zCReturns the ANSI start and reset sequences for the text_attributes. r   ;z%02xr   N)r4   r4   )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   _GetAnsiSequenceForAttribute,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   *F#:F(Nc           	         [        U[        R                  5      (       a  U$ U=(       a    U R                  n/ nU R                  UR
                     nU R                  XR5      u  pgU(       a  UR                  U5      nO[        R                  U5      nUR                   Ho  n	[        U	[        R                  5      (       a<  UR                  U R                  U	UUS95        U(       a  UR                  U5        M\  M^  UR                  U	5        Mq     SR                  U5      n
U(       a,  UR                   (       a  UR                   R#                  U
5      n
U(       a  SR#                  UU
US9n
U
$ )a  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.
)rB   stylizer4   z{begin_style}{text}{end_style})rF   r   rG   )
isinstancer9   string_typesr1   r0   r:   rH   r    r
   r   textsr   	TypedTextappendParseTypedTextToStringr7   
format_strr>   )r   
typed_textrB   rK   parsed_chunksr   rF   rG   new_style_contextchunkparsed_texts              r   rQ   &TypedTextParser.ParseTypedTextToStringh   sV   " *c..//,$,,GM))**>*>?O!>>(K '@@
 (::?K!!	E4>>	*	*T88+ 9  	 


{
+  	U# " ''-(K?55#..55kBk4;;! < k r   )r1   r0   )NT)r"   r#   r$   r%   r&   r@   rA   r=   r?   r<   r   rH   rQ   r(   r)   r   r   r+   r+   2   s8    -##.
% ('"< >B%)4r   r+   c                     [         R                  R                  R                  R	                  5       n [        U [         R                  R                  R                  R                  R                  :H  [         R                  R                  R                  R	                  5       S:g  [         R                  R                  R                  R                  5       /5      (       a  gg)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   _ColorsEnabledrg      s    



 
 
5
5
9
9
;%z  %%99==BBB115577B**224
6 7 7 	r   c                 ~    [        5       nU=(       a    U n U=(       d    [        R                  " 5       n[        X5      $ )z=Returns a typed text parser, creating a new one if necessary.)rg   r   GetStyleMappingsr+   )enabledr0   
can_enables      r   GetTypedTextParserrl      s2    *"7'!@X%>%>%@.		11r   )TN)r&   
__future__r   r   r   googlecloudsdk.corer   !googlecloudsdk.core.console.styler   r   r9   objectr
   r+   rg   rl   r)   r   r   <module>rq      sD    9 &  ' * 6 2 
0F 0,jf jZ2r   