
    AHj,                    &   d dl mZ d dlZd dlmZmZmZmZ d dl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 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  erd dl!m"Z"  ejF                  d      jH                  Z% G d ded      Z&y)    )annotationsN)TYPE_CHECKINGIterableOptionalSequencecell_len)HighlighterReprHighlighter)Style)Text)work)
line_split)LRUCache)Size)var)
ScrollView)	Selection)Strip)Selfz[ -]c                      e Zd ZU dZdZdZ eee      d      Z	de
d<   	  ed      Zde
d<   	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd	Zedd
       Zed d       Zd! fdZd"dZed#d       Zed$d       Z ed      d%d       Zd!dZ	 d&	 	 	 	 	 d'dZ	 d&	 	 	 	 	 d(dZ	 d&	 	 	 	 	 d)dZd*dZd+dZd,dZd-dZd.dZd/dZ d0d1 fdZ! xZ"S )2LogzA widget to log text.Tz
    Log {
        background: $surface;
        color: $text;
        overflow: scroll;
        &:focus {
            background-tint: $foreground 5%;
        }
    }
    Nzvar[int | None]	max_linesz	var[bool]auto_scrollc                    t         |   ||||       || _        	 || _        || _        g | _        d| _        d| _        t        d      | _	        t               | _        	 d| _        y)a  Create a Log widget.

        Args:
            highlight: Enable highlighting.
            max_lines: Maximum number of lines to display.
            auto_scroll: Scroll to end on new lines.
            name: The name of the text log.
            id: The ID of the text log in the DOM.
            classes: The CSS classes of the text log.
            disabled: Whether the text log is disabled or not.
        )nameidclassesdisabledr   i   N)super__init__	highlightr   r   _lines_width_updatesr   _render_line_cacher   highlighter_clear_y)	selfr"   r   r   r   r   r   r   	__class__s	           L/root/tools/cai/cai_env/lib/python3.12/site-packages/textual/widgets/_log.pyr!   zLog.__init__/   sj    * 	dr7XN"""&!#8@(7(9E    c                     y)NT r)   s    r+   allow_selectzLog.allow_selectQ   s    r,   c                    | j                   S )zThe raw lines in the Log.

        Note that this attribute is read only.
        Changing the lines will not update the Log's contents.

        )r#   r/   s    r+   linesz	Log.linesU   s     {{r,   c                V    t         |           | j                  j                          y)z%Called by Textual when styles update.N)r    notify_style_updater&   clear)r)   r*   s    r+   r4   zLog.notify_style_update_   s     #%%%'r,   c                    || j                   k(  rAt        || j                        | _        t        | j                  | j                        | _        yy)zUpdate the virtual size width.

        Args:
            updates: A counter of updates.
            size: Maximum size of new lines.
        N)r%   maxr$   r   
line_countvirtual_size)r)   updatessizes      r+   _update_maximum_widthzLog._update_maximum_widthd   s>     dmm#dDKK0DK $T[[$// BD $r,   c                l    | j                   r(t        | j                         | j                   d   dk(  z
  S y)zNumber of lines of content. r   )r#   lenr/   s    r+   r8   zLog.line_counto   s/     ;;t{{#t{{2"'<==r,   c                6    t        d|j                               S )zProcess a line before it is rendered to remove control codes.

        Args:
            line: A string.

        Returns:
            New string with no control codes.
        u   �)_sub_escape
expandtabs)clslines     r+   _process_linezLog._process_linev   s     5$//"344r,   )threadc                    |rH| j                   t        fd|D              }| j                  j                  | j                  ||       yy)zA thread worker to update the width in the background.

        Args:
            updates: The update index at the time of invocation.
            lines: Lines that were added.
        c              3  @   K   | ]  }t         |              y wNr   ).0rE   rF   s     r+   	<genexpr>z#Log._update_size.<locals>.<genexpr>   s     MtXmD&9:Ms   N)rF   r7   appcall_from_threadr<   )r)   r:   r2   
max_lengthrF   s       @r+   _update_sizezLog._update_size   sD      ..MMuMMJHH%%d&@&@':V r,   c                \   | j                   yt        | j                        | j                   z
  }|dkD  rs| j                  }|j	                         D ci c]  }||kD  s	||z
  ||    }}|j                          |j                         D ]
  \  }}|||<    | j                  d|= yyc c}w )z/Prune lines if there are more than the maximum.Nr   )r   r@   r#   r&   keysr5   items)r)   remove_lines_cacheyupdated_cacherE   s         r+   _prune_max_lineszLog._prune_max_lines   s    >>!4;;'$..8!,,F 6<[[]01a,FVL &)+M  LLN(..0 !4 q	!M\M* s   
B)B)c           
        | j                   }|r| j                  s| j                  j                  d       t        |      D ]  \  }}| j                  dxx   |z  cc<   t	        | j
                  t        | j                  | j                  d                     | _        | j                  t        | j                        dz
         |s| j                  j                  d        t        | j
                  | j                        | _        | j                  2t        | j                        | j                  kD  r| j                          || j                  n|}|r| j!                  ddd       | S )zWrite to the log.

        Args:
            data: Data to write.
            scroll_end: Scroll to the end after writing, or `None` to use `self.auto_scroll`.

        Returns:
            The `Log` instance.
        r?   r>      FTanimate	immediatex_axis)is_vertical_scroll_endr#   appendr   r7   r$   r	   rF   refresh_linesr@   r   r8   r9   r   rX   r   
scroll_end)r)   datarb   r_   rE   endingr   s          r+   writez	Log.write   s%    "&!<!<;;""2& *4 0 +fB4'!KK$*<*<T[[_*M!N ""3t{{#3a#78KK&&r*+ !%T[[$// BD>>%#dkk*:T^^*K!!#*4*<d&&*OOET%OHr,   c                ,    | j                  |g|       | S )zWrite content on a new line.

        Args:
            line: String to write to the log.
            scroll_end: Scroll to the end after writing, or `None` to use `self.auto_scroll`.

        Returns:
            The `Log` instance.
        )write_lines)r)   rE   rb   s      r+   
write_linezLog.write_line   s     	$,r,   c                   | j                   }|| j                  n|}g }|D ]!  }|j                  |j                                # t	        | j
                        }| j
                  j                  |       | j                  2t	        | j
                        | j                  kD  r| j                          t        | j                  t	        | j
                              | _
        | j                  | j                  |       | j                  |t	        |             |r$| j                  s|r| j                  ddd       | S | j!                          | S )zWrite an iterable of lines.

        Args:
            lines: An iterable of strings to write.
            scroll_end: Scroll to the end after writing, or `None` to use `self.auto_scroll`.

        Returns:
            The `Log` instance.
        FTr[   )r_   r   extend
splitlinesr@   r#   r   rX   r   r$   r9   rP   r%   ra   is_vertical_scrollbar_grabbedrb   refresh)r)   r2   rb   r_   r   	new_linesrE   
start_lines           r+   rg   zLog.write_lines   s    "&!<!<*4*<d&&*	 	0DT__./	0%
9%>>%#dkk*:T^^*K!!# c$++.>?$--3:s9~666&OOET%OH  LLNr,   c                    | j                   j                          d| _        | j                  j                          | xj                  dz  c_        t        dd      | _        d| _        | S )zIClear the Log.

        Returns:
            The `Log` instance.
        r   rZ   )r#   r5   r$   r&   r%   r   r9   r(   r/   s    r+   r5   z	Log.clear   sU     	%%' AJr,   c                ^    dj                  | j                        }|j                  |      dfS )zGet the text under the selection.

        Args:
            selection: Selection information.

        Returns:
            Tuple of extracted text and ending (typically "
" or " "), or `None` if no text could be extracted.
        
)joinr#   extract)r)   	selectiontexts      r+   get_selectionzLog.get_selection	  s,     yy%  &,,r,   c                X    | j                   j                          | j                          y rJ   )r&   r5   rm   )r)   ru   s     r+   selection_updatedzLog.selection_updated  s    %%'r,   c                x    | j                   \  }}| j                  ||z   || j                  j                        }|S )zRender a line of content.

        Args:
            y: Y Coordinate of line.

        Returns:
            A rendered line.
        )scroll_offset_render_liner;   width)r)   rV   scroll_xscroll_ystrips        r+   render_linezLog.render_line  s9     "//(!!(Q,$))//Jr,   c                
   | j                   }|t        | j                        k\  rt        j                  ||      S | j                  ||      }|j                  J |j                  |||z   |      }|j                  ||      }|S )zRender a line into a cropped strip.

        Args:
            y: Y offset of line.
            scroll_x: Current horizontal scroll.
            width: Width of the widget.

        Returns:
            A Strip suitable for rendering.
        )	
rich_styler@   r#   r   blank_render_line_strip_cell_lengthcrop_extendapply_offsets)r)   rV   r~   r}   r   rE   s         r+   r|   zLog._render_line&  s     __
DKK  ;;uj11&&q*5  ,,,(U*:JG!!(A.r,   c                j   | j                   }|| j                  v r|| j                  |   S | j                  | j                  |         }t	        |d      }|j                  |       | j                  r| j                  |      }|c|j                  || j                  z
        x}C|\  }}|dk(  rt        |      }| j                  j                  d      }	|j                  |	||       t        |j                  | j                  j                         t#        |            }
||
| j                  |<   |
S )zRender a line into a Strip.

        Args:
            y: Y offset of line.
            rich_style: Rich style of line.

        Returns:
            An uncropped Strip.
        T)no_wrapr>   zscreen--selection)text_selectionr&   rF   r#   r   stylizer"   r'   get_spanr(   r@   screenget_component_rich_styler   renderrM   consoler	   )r)   rV   r   ru   _line	line_textselect_spanstartendselection_stylerE   s              r+   r   zLog._render_line_strip;  s%    ''	'''I,=**1--""4;;q>2-	*%>>((3I (11!dmm2CDDQ(
s"9i.C"&++"F"F'# !!/5#>Y%%dhh&6&67%I )-D##A&r,   c                    t        |||z         D ]  }| j                  j                  |        t        |   ||       y)zRefresh one or more lines.

        Args:
            y_start: First line to refresh.
            line_count: Total number of lines to refresh.
        )r8   N)ranger&   discardr    ra   )r)   y_startr8   rV   r*   s       r+   ra   zLog.refresh_linesa  sF     w* 45 	/A##++A.	/g*=r,   )FNTNNNF)r"   boolr   z
int | Noner   r   r   
str | Noner   r   r   r   r   r   returnNone)r   r   )r   zSequence[str])r   r   )r:   intr;   r   r   r   )r   r   )rE   strr   r   )r:   r   r2   z	list[str]r   r   rJ   )rc   r   rb   bool | Noner   r   )rE   r   rb   r   r   r   )r2   zIterable[str]rb   r   r   r   )r   r   )ru   r   r   ztuple[str, str] | None)ru   zSelection | Noner   r   )rV   r   r   r   )rV   r   r~   r   r}   r   r   r   )rV   r   r   r   r   r   )rZ   )r   r   r8   r   r   r   )#__name__
__module____qualname____doc__ALLOW_SELECTDEFAULT_CSSr   r   r   r   __annotations__r   r!   propertyr0   r2   r4   r<   r8   classmethodrF   r   rP   rX   re   rh   rg   r5   rw   ry   r   r|   r   ra   __classcell__)r*   s   @r+   r   r      s   L	K "4Xc]!3D!9I9) YK&,   $ "     	 
         
 D    (
	C   	5 	5 

W 
W+, #'""  " 
	"N #'   
	( #'""  " 
	"H
-*$L	> 	>r,   r   T)	can_focus)'
__future__r   retypingr   r   r   r   
rich.cellsr	   rich.highlighterr
   r   
rich.styler   	rich.textr   textualr   textual._line_splitr   textual.cacher   textual.geometryr   textual.reactiver   textual.scroll_viewr   textual.selectionr   textual.stripr   typing_extensionsr   compilesubrB   r   r.   r,   r+   <module>r      sb    " 	 > >  9    * " !   * ' &bjj*+//P>* P>r,   