1313)
1414from .layout .base import Rect
1515from .options_stub import Options
16- from .rgb import Color , alpha_blend , color_from_int
16+ from .rgb import Color , alpha_blend , color_as_sgr , color_from_int , to_color
1717from .utils import color_as_int , log_error
1818from .window import calculate_gl_geometry
1919
@@ -61,6 +61,35 @@ def compile_template(template: str) -> Any:
6161 report_template_failure (template , str (e ))
6262
6363
64+ class ColorFormatter :
65+
66+ def __init__ (self , which : str ):
67+ self .which = which
68+
69+ def __getattr__ (self , name : str ) -> str :
70+ q = name
71+ if q == 'default' :
72+ ans = '9'
73+ else :
74+ if name .startswith ('_' ):
75+ q = '#' + name [1 :]
76+ c = to_color (q )
77+ if c is None :
78+ raise AttributeError (f'{ name } is not a valid color' )
79+ ans = '8' + color_as_sgr (c )
80+ return f'\x1b [{ self .which } { ans } m'
81+
82+
83+ class Formatter :
84+ reset = '\x1b [0m'
85+ fg = ColorFormatter ('3' )
86+ bg = ColorFormatter ('4' )
87+ bold = '\x1b [1m'
88+ nobold = '\x1b [22m'
89+ italic = '\x1b [3m'
90+ noitalic = '\x1b [23m'
91+
92+
6493def draw_title (draw_data : DrawData , screen : Screen , tab : TabBarData , index : int ) -> None :
6594 if tab .needs_attention and draw_data .bell_on_tab :
6695 fg = screen .cursor .fg
@@ -81,13 +110,22 @@ def draw_title(draw_data: DrawData, screen: Screen, tab: TabBarData, index: int)
81110 'index' : index ,
82111 'layout_name' : tab .layout_name ,
83112 'num_windows' : tab .num_windows ,
84- 'title' : tab .title
113+ 'title' : tab .title ,
114+ 'fmt' : Formatter ,
85115 }
86116 title = eval (compile_template (template ), {'__builtins__' : {}}, eval_locals )
87117 except Exception as e :
88118 report_template_failure (template , str (e ))
89119 title = tab .title
90- screen .draw (title )
120+ if '\x1b ' in title :
121+ import re
122+ for x in re .split ('(\x1b \\ [[^m]*m)' , title ):
123+ if x .startswith ('\x1b ' ) and x .endswith ('m' ):
124+ screen .apply_sgr (x [2 :- 1 ])
125+ else :
126+ screen .draw (x )
127+ else :
128+ screen .draw (title )
91129
92130
93131def draw_tab_with_separator (draw_data : DrawData , screen : Screen , tab : TabBarData , before : int , max_title_length : int , index : int , is_last : bool ) -> int :
0 commit comments