
    AHjG                       U d dl mZ d dlmZ d dlmZmZmZmZ d dl	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mZmZ d dlmZ d dlmZ d dlmZm Z m!Z!m"Z" d dl#m$Z$m%Z% d dl&m'Z' d dl(m)Z) d dl*m+Z+ d dl,m-Z-m.Z.m/Z/ d dl0m1Z1 d dl2m3Z3 d dl4m5Z5 d dl6mZ erd dl7m8Z8 d dl9m:Z: d dl;m<Z< ee=ge5f   Z>de?d<   e
j                  j                  d       G d d             ZBy)    )annotations)	lru_cache)TYPE_CHECKINGCallableIterableSequenceN)SegmentStyle)TerminalTheme)log)DEFAULT_TERMINAL_THEME)get_boxrender_border_label
render_row)
active_app)_apply_opacity)apply_hatchline_pad	line_trim
make_blank)TRANSPARENTColor)DEBUG)Content)
LineFilter)RegionSizeSpacing)TextOpacity)Tint)Strip)	TypeAlias)
StylesBase)Widgetr#   RenderLineCallbackT)angularc                      e Zd ZdZddZddZddZddZddZddZ	dddd	e
f	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd
Z ed      	 	 	 	 	 	 dd       Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZy)StylesCacheu#  Responsible for rendering CSS Styles and keeping a cache of rendered lines.

    The render method applies border, outline, and padding set in the Styles object to widget content.

    The diagram below shows content (possibly from a Rich renderable) with padding and border. The
    labels A. B. and C. indicate the code path (see comments in render_line below) chosen to render
    the indicated lines.

    ```
    ┏━━━━━━━━━━━━━━━━━━━━━━┓◀── A. border
    ┃                      ┃◀┐
    ┃                      ┃ └─ B. border + padding +
    ┃   Lorem ipsum dolor  ┃◀┐         border
    ┃   sit amet,          ┃ │
    ┃   consectetur        ┃ └─ C. border + padding +
    ┃   adipiscing elit,   ┃     content + padding +
    ┃   sed do eiusmod     ┃           border
    ┃   tempor incididunt  ┃
    ┃                      ┃
    ┃                      ┃
    ┗━━━━━━━━━━━━━━━━━━━━━━┛
    ```
    c                >    i | _         t               | _        d| _        y )N   )_cacheset_dirty_lines_widthselfs    M/root/tools/cai/cai_env/lib/python3.12/site-packages/textual/_styles_cache.py__init__zStylesCache.__init__>   s    (*&)e    c              #  f   K   | j                   rd| j                   f d| j                  df y w)Ndirtywidthr+   )r.   r/   r0   s    r2   __rich_repr__zStylesCache.__rich_repr__C   s1     4,,,,t{{A%%s   /1c                    |r-|D ]'  }| j                   j                  |j                         ) y| j                          y)zAdd a dirty regions.N)r.   update
line_rangeclear)r1   regionsregions      r2   	set_dirtyzStylesCache.set_dirtyH   s9    ! <!!(():):;< JJLr4   c                    || j                   v S )zCheck if a given line is dirty (needs to be rendered again).

        Args:
            y: Y coordinate of line.

        Returns:
            True if line requires a render, False if can be cached.
        )r.   )r1   ys     r2   is_dirtyzStylesCache.is_dirtyP   s     D%%%%r4   c                l    | j                   j                          | j                  j                          y)z=Clear the styles cache (will cause the content to re-render).N)r,   r<   r.   r0   s    r2   r<   zStylesCache.clear[   s&     	!r4   c                   |j                   }|j                  }|j                  \  }}|j                  }| j	                  ||j
                  j                  |||j                  |j                         |dn|g|j                  |      |dn|g|j                  |      |j                  j                  |j                  ||j                  |j                  j                        }|j                   ri|j"                  }	|	j$                  rQ|	j&                  rEd|	j(                  v r7|j*                  }
|
r)|D cg c]  }|j-                  |	j.                  |
        }}|S c c}w )zRender the content for a widget.

        Args:
            widget: A widget.
            region: A region of the widget to render.

        Returns:
            Rendered lines.
        N)content_sizepaddingcropopacity
ansi_themez@click)_border_title_border_subtitlebackground_colorsstylesrenderr>   sizerender_lineget_line_filters_get_title_style_information_get_subtitle_style_informationcontent_regionrF   rH   apprI   
auto_linkshover_style_link_id_metametalink_style_hoverstyle_linkslink_id)r1   widgetrG   border_titleborder_subtitlebase_background
backgroundrM   stripsrW   r[   strips               r2   render_widgetzStylesCache.render_widgeta   sx    ++ 11&,&>&>#MM##%  '  !88I #*  $;;OL
  ..33NNNNzz,,7  
<  ,,K$$%% 0 00#)#:#: # &,! ))+*=*=?OPF 
 s   8#EN      ?c                n   |	|}	|
|j                   }
||j                  }|\  }}|| j                  k7  r| j                          || _        g }|j                  }| j
                  j                  }| j                  }|j                  D ]  } ||      s|| j                  vr# |||||	|
|||||||      }|| j                  |<   n| j                  |   }|D ]  }|j                  ||      } t        rKt        |D cg c]  }|j                  xs |j                    c}      rt        j                   d|d        ||        | j
                  j#                  |j                         |j$                  d|fk7  r.|j$                  \  }}|D cg c]  }|j'                  ||       }}|S c c}w c c}w )a  Render a widget content plus CSS styles.

        Args:
            styles: CSS Styles object.
            size: Size of widget.
            base_background: Background color beneath widget.
            background: Background color of widget.
            render_content_line: Callback to render content line.
            console: The console in use by the app.
            border_title: Optional tuple of (title, color, background, style).
            border_subtitle: Optional tuple of (subtitle, color, background, style).
            content_size: Size of content or None to assume full size.
            padding: Override padding from Styles, or None to use styles.padding.
            crop: Region to crop to.
            filters: Additional post-processing for the segments.
            opacity: Widget opacity.
            ansi_theme: Theme for ANSI colors.

        Returns:
            Rendered lines.
        z'Strip contains invalid empty Segments: .r   )rF   r>   r/   r<   appendr.   __contains__rP   r;   r,   apply_filterr   anycontroltextr   warningdifference_updatecolumn_spanrG   )r1   rM   rO   ra   rb   render_content_linefiltersr_   r`   rE   rF   rG   rH   rI   r7   _heightrc   	add_striprB   rP   rA   rd   filtersegmentx1x2s                             r2   rN   zStylesCache.render   s   J L?nnG<;;DwDKKJJLDK MM	$$11&& 	A{at{{2# #' # "'AA! ?**6:>? US'W__<=STKK"I%RS TUe7	: 	++DOO<5z)%%FB6<=UejjR(=F= T >s   ;"F-
F2i   c                8    t        ||z         t        |      fS )z&Get inner and outer background colors.)rb   r
   )clsra   rb   s      r2   get_inner_outerzStylesCache.get_inner_outer   s#     _z9:_-
 	
r4   c                ,	  D j                   }|\  }}|\  }}|\  }}}}j                  \  \  }}\  }}\  }}\  }}j                  \  \  }}\  } }!\  }"}#\  }$}%t        j                  D| j                        \  }&}'dDfd}(dfd})|r|dk(  s
|r||dz
  k(  r|dk(  }*|*r|n|j                        z   }+t        |+      },|*r|n|}-|dk7  }.|dk7  }/|*r|	n|
}0|0d}1nI|0\  }2}3}4}5z   }6|5t        |4j                  r|6|4z   nt        |3j                  r|6|3z   nt              z  }5|2|5f}1|1rt        |1|*|-|dz
  |&|'|,|.|/	      }7ng }7t        |-|&|'|,      }8|*rj                  nj                  }9t        |8|*rdnd   ||.|/|7|9      }:n|r||j                  k  s|r|||j                   z
  k\  r|&j"                  };t        |j                        z         }<t        ||&|'|<      d   d   }=t        |j                        z         }>t        ||&|'|>      d   d   }?|r|r|=t%        |dz
  |;      |?g}:n5|r|=t%        |dz
  |;      g}:n!|rt%        |dz
  |;      |?g}:nt%        ||;      g}: |(|:      }:n.||j                  z
  }@|@|k  r2 |||j                  z
        }:|:j'                  ||&j"                        }:n t)        j*                  ||&j"                        }:j,                  x}Ad	k7  rt/        j0                  |:A|      }: |(t3        |:|||&j"                              }:|s|r~t        |j                        z         }<t        ||&|'|<      d   d   }=t        |j                        z         }>t        ||&|'|>      d   d   }?|r
|r|=g|:|?}:n|r|=g|:}:ng |:|?}:|r|dk(  s
|"rS||dz
  k(  rK|dk(  r|n|#}Bt        |dk(  r|n|"|&|'t        Bz               }8t        |8|dk(  rdnd   ||$dk7  | dk7  d
      }:n|$s| r|t        |%z         }<t        |$|&|'|<      d   d   }=t        |!z         }>t        | |&|'|>      d   d   }?t5        t7        |:      |$dk7  | dk7        }:|$r
| r|=g|:|?}:n|$r|=g|:}:ng |:|?}:t)         |)|:      |      }C|CS )a  Render a styled line.

        Args:
            styles: Styles object.
            y: The y coordinate of the line (relative to widget screen offset).
            size: Size of the widget.
            content_size: Size of the content area.
            padding: Padding.
            base_background: Background color of widget beneath this line.
            background: Background color of widget.
            render_content_line: Callback to render a line of content.
            console: The console in use by the app.
            border_title: Optional tuple of (title, color, background, style).
            border_subtitle: Optional tuple of (subtitle, color, background, style).
            opacity: Opacity of line.

        Returns:
            A line of segments.
        c                    j                  d      r_j                  dk7  rPj                  \  }}|dk7  r<|j                  dkD  r- |z   j                  j                        }t	        | ||      S | S )z,Apply effects to segments inside the border.hatchnone r   )has_ruler   a
rich_colorr   )segments	charactercolorhatch_stylerb   
from_colorrM   s       r2   	line_postz*StylesCache.render_line.<locals>.line_post>  sp    w'FLLF,B#)<< 	5#!",#e+779N9N#K 'xKHHOr4   c                   	 t        j                         }|j                  }j
                  j                  r"t        j                  | j
                  |      } dk7  rt        |       } | S # t        $ r	 t        }Y ]w xY w)zPost process segments to apply opacity and tint.

            Args:
                segments: Iterable of segments.

            Returns:
                New list of segments
            rf   )
r   getrI   LookupErrorr   tintr   r!   process_segmentsr   )r   rU   rI   rb   ra   rH   rM   s      r2   postz%StylesCache.render_line.<locals>.postI  sy    4 nn& ^^
 {{}}00fkk:z #~)(OWMO  43
4s    A/ /B Br   r+   )
foreground N   rf    )r   Iterable[Segment]returnr   )gutterborderoutline	RichStyler   r|   multiply_alphar   r   r   r   r   border_title_alignborder_subtitle_alignr   topbottom
rich_styler   adjust_cell_lengthr"   blanktext_opacityr    r   r   r   list)Er1   rM   rA   rO   rE   rF   ra   rb   rr   r_   r`   rH   rI   r   r7   heightcontent_widthcontent_heightpad_top	pad_right
pad_bottompad_left
border_topborder_top_colorborder_rightborder_right_colorborder_bottomborder_bottom_colorborder_leftborder_left_coloroutline_topoutline_top_coloroutline_rightoutline_right_coloroutline_bottomoutline_bottom_coloroutline_leftoutline_left_colorinnerouterr   r   is_topborder_colorborder_color_as_styleborder_edge_typehas_left	has_rightborder_labelrender_labellabellabel_colorlabel_backgroundstylebase_label_backgroundlabel_segmentsbox_segmentslabel_alignmentlinebackground_rich_style
left_styleleftright_styleright	content_yr   outline_colorrd   r   sE    `    ``   `                                                        @r2   rP   zStylesCache.render_line  s   F v(4%~3:0J MM	
*Z).\-0]/,[+ NN	
,[+0]/2^1.\- ))
++OZHu			 	2 16}fqj!VF*$* 0CnW%&L %*\$B!-3z"b(H$*I+1<L##>J;{$4e(7*(D% ,-- /1AA( '== /<(  !&u~ !4 $AI)
" "$" %	L .4))9U9U  &Qa0D !fjj.1 66$)$4$4!*->-M-Mg-VVJ ;ujA!DQGD*-?-N-Nw-WWK L%DQGJE|j4IJERj4IJK"519.CDeL"5*?@AT?D FJJI>)*1vzz>:..}e>N>NO{{=%2B2BC & 3 33;"33D,
SXdHiAQAQRSDl".'66w? @
 {E5*EaHK#.(77@ A  eUKHKAN< /4//D  =4=D)T>5>D AF1
?12a-=QM" Av>=!@A	L !q&Qa0"#D ]?=O+OQJ<zB1EaHDO>Q,QSKM5%EaHKET$Z);]b=PQD+t+U+}t}%~u~d4j%(r4   )r   None)r   zrich.repr.Result)r=   r   r   r   )rA   intr   bool)r^   r%   rG   r   r   list[Strip])rM   r$   rO   r   ra   r   rb   r   rr   r&   rs   zSequence[LineFilter]r_   *tuple[Content, Color, Color, Style] | Noner`   r   rE   zSize | NonerF   zSpacing | NonerG   zRegion | NonerH   floatrI   r   r   r   )ra   r   rb   r   r   ztuple[Style, Style])rM   r$   rA   r   rO   r   rE   r   rF   r   ra   r   rb   r   rr   zCallable[[int], Strip]r_   r   r`   r   rH   r   rI   r   r   r"   )__name__
__module____qualname____doc__r3   r8   r?   rB   r<   re   r   rN   r   r|   rP   r   r4   r2   r)   r)   $   s   0
&
	&"<P %)"&"$:YY Y 	Y
 Y 0Y &Y AY DY "Y  Y Y Y "Y 
Yv t_
#
16
	
 
tt t 	t
 t t t t 4t At Dt t "t 
tr4   r)   )C
__future__r   	functoolsr   typingr   r   r   r   	rich.reprrichrich.segmentr	   
rich.styler   r   rich.terminal_themer   textualr   textual._ansi_themer   textual._borderr   r   r   textual._contextr   textual._opacityr   textual._segment_toolsr   r   r   r   textual.colorr   r   textual.constantsr   textual.contentr   textual.filterr   textual.geometryr   r   r    textual.renderables.text_opacityr    textual.renderables.tintr!   textual.stripr"   textual.styletyping_extensionsr#   textual.css.stylesr$   textual.widgetr%   r   r&   __annotations__reprautor)   r   r4   r2   <module>r      s    "  > >    ) -  6 D D ' + O O , # # % 2 2 8 )  +-% (# 6 I 6 S S Sr4   