22
33from math import ceil
44import logging
5- from typing import Iterable
65
6+
7+ from rich .color import Color
8+ from rich .style import Style
79from rich .console import Console , ConsoleOptions , RenderResult , RenderableType
810from rich .segment import Segment
911from rich .style import Style
@@ -17,7 +19,7 @@ def __init__(
1719 lines : list [list [Segment ]],
1820 height : int ,
1921 virtual_height : int ,
20- position : int ,
22+ position : float ,
2123 overlay : bool = False ,
2224 ) -> None :
2325 self .lines = lines
@@ -31,6 +33,7 @@ def __rich_console__(
3133 ) -> RenderResult :
3234 bar = render_bar (
3335 size = self .height ,
36+ window_size = len (self .lines ),
3437 virtual_size = self .virtual_height ,
3538 position = self .position ,
3639 )
@@ -46,56 +49,48 @@ def render_bar(
4649 virtual_size : float = 50 ,
4750 window_size : float = 20 ,
4851 position : float = 0 ,
49- bar_style : Style | None = None ,
50- back_style : Style | None = None ,
52+ back_color : str = "#555555" ,
53+ bar_color : str = "bright_magenta" ,
5154 ascii_only : bool = False ,
5255 vertical : bool = True ,
5356) -> list [Segment ]:
54- if vertical :
55- if ascii_only :
56- solid = "|"
57- half_start = "|"
58- half_end = "|"
59- else :
60- solid = "┃"
61- half_start = "╻"
62- half_end = "╹"
63- else :
64- if ascii_only :
65- solid = "-"
66- half_start = "-"
67- half_end = "-"
68- else :
69- solid = "━"
70- half_start = "╺"
71- half_end = "╸"
72-
73- _bar_style = bar_style or Style .parse ("bright_magenta" )
74- _back_style = back_style or Style .parse ("#555555" )
75-
76- _Segment = Segment
7757
78- start_bar_segment = _Segment (half_start , _bar_style )
79- end_bar_segment = _Segment (half_end , _bar_style )
80- bar_segment = _Segment (solid , _bar_style )
58+ if ascii_only :
59+ bars = ["|" , "|" , "|" , "|" , "|" , "|" , "|" , "|" , "|" ]
60+ else :
61+ bars = [" " , "▁" , "▂" , "▃" , "▄" , "▅" , "▆" , "▇" , "█" ]
8162
82- start_back_segment = _Segment (half_end , _bar_style )
83- end_back_segment = _Segment (half_end , _back_style )
84- back_segment = _Segment (solid , _back_style )
63+ back = Color .parse (back_color )
64+ bar = Color .parse (bar_color )
8565
86- segments = [back_segment ] * size
66+ _Segment = Segment
67+ _Style = Style
68+ segments = [_Segment (" " , _Style (bgcolor = back ))] * int (size )
8769
8870 step_size = virtual_size / size
8971
90- start = position / step_size
91- # end = (position + window_size) / step_size
92- end = start + window_size / step_size
72+ start = int (position / step_size * 8 )
73+ end = start + int (window_size / step_size * 8 )
74+
75+ start_index , start_bar = divmod (start , 8 )
76+ end_index , end_bar = divmod (end , 8 )
9377
94- start_index = int (start )
95- end_index = start_index + ceil (window_size / step_size )
96- bar_height = end_index - start_index
78+ if start_index == end_index :
79+ if start_index < len (segments ):
80+ segments [start_index ] = _Segment (" " , _Style (bgcolor = bar ))
81+ else :
82+ segments [start_index :end_index ] = [_Segment (" " , _Style (bgcolor = bar ))] * (
83+ end_index - start_index
84+ )
9785
98- segments [start_index :end_index ] = [bar_segment ] * bar_height
86+ if start_index < len (segments ):
87+ segments [start_index ] = _Segment (
88+ bars [7 - start_bar ], _Style (bgcolor = back , color = bar )
89+ )
90+ if end_index < len (segments ):
91+ segments [end_index ] = _Segment (
92+ bars [7 - end_bar ], _Style (color = back , bgcolor = bar )
93+ )
9994
10095 return segments
10196
@@ -107,12 +102,12 @@ def render_bar(
107102 console = Console ()
108103
109104 bar = render_bar (
110- size = 20 ,
105+ size = 10 ,
111106 virtual_size = 100 ,
112- window_size = 50 ,
113- position = 0 ,
114- vertical = False ,
107+ window_size = 20 ,
108+ position = 80 ,
109+ vertical = True ,
115110 ascii_only = False ,
116111 )
117112
118- console .print (Segments (bar , new_lines = False ))
113+ console .print (Segments (bar , new_lines = True ))
0 commit comments