23
23
from ..strip import Strip
24
24
from .._typing import Literal
25
25
26
- CursorType = Literal ["cell" , "row" , "column" ]
26
+ CursorType = Literal ["cell" , "row" , "column" , "none" ]
27
27
CELL : CursorType = "cell"
28
28
CellType = TypeVar ("CellType" )
29
29
@@ -490,8 +490,8 @@ def _render_row(
490
490
row_index : int ,
491
491
line_no : int ,
492
492
base_style : Style ,
493
- cursor_column : int = - 1 ,
494
- hover_column : int = - 1 ,
493
+ cursor_location : Coord ,
494
+ hover_location : Coord ,
495
495
) -> tuple [SegmentLines , SegmentLines ]:
496
496
"""Render a row in to lines for each cell.
497
497
@@ -504,7 +504,7 @@ def _render_row(
504
504
tuple[Lines, Lines]: Lines for fixed cells, and Lines for scrollable cells.
505
505
"""
506
506
507
- cache_key = (row_index , line_no , base_style , cursor_column , hover_column )
507
+ cache_key = (row_index , line_no , base_style , cursor_location , hover_location )
508
508
509
509
if cache_key in self ._row_render_cache :
510
510
return self ._row_render_cache [cache_key ]
@@ -534,17 +534,38 @@ def _render_row(
534
534
else :
535
535
row_style = base_style
536
536
537
- scrollable_row = [
538
- render_cell (
537
+ def should_highlight (
538
+ target_location : Coord ,
539
+ cell_location : Coord ,
540
+ cursor_type : CursorType ,
541
+ ) -> bool :
542
+ if cursor_type == "cell" :
543
+ return target_location == cell_location
544
+ elif cursor_type == "row" :
545
+ target_row , _ = target_location
546
+ cell_row , _ = cell_location
547
+ return target_row == cell_row
548
+ elif cursor_type == "column" :
549
+ _ , target_column = target_location
550
+ _ , cell_column = cell_location
551
+ return target_column == cell_column
552
+ else :
553
+ return False
554
+
555
+ cursor_type = self .cursor_type
556
+
557
+ scrollable_row = []
558
+ for column in self .columns :
559
+ cell_location = Coord (row_index , column .index )
560
+ cell_lines = render_cell (
539
561
row_index ,
540
562
column .index ,
541
563
row_style ,
542
564
column .render_width ,
543
- cursor = cursor_column == column . index ,
544
- hover = hover_column == column . index ,
565
+ cursor = should_highlight ( cursor_location , cell_location , cursor_type ) ,
566
+ hover = should_highlight ( hover_location , cell_location , cursor_type ) ,
545
567
)[line_no ]
546
- for column in self .columns
547
- ]
568
+ scrollable_row .append (cell_lines )
548
569
549
570
row_pair = (fixed_row , scrollable_row )
550
571
self ._row_render_cache [cache_key ] = row_pair
@@ -586,23 +607,24 @@ def _render_line(self, y: int, x1: int, x2: int, base_style: Style) -> Strip:
586
607
row_index , line_no = self ._get_offsets (y )
587
608
except LookupError :
588
609
return Strip .blank (width , base_style )
610
+
589
611
cursor_column = (
590
612
self .cursor_column
591
613
if (self .show_cursor and self .cursor_row == row_index )
592
614
else - 1
593
615
)
594
616
hover_column = self .hover_column if (self .hover_row == row_index ) else - 1
595
617
596
- cache_key = (y , x1 , x2 , width , cursor_column , hover_column , base_style )
618
+ cache_key = (y , x1 , x2 , width , self . cursor_cell , self . hover_cell , base_style )
597
619
if cache_key in self ._line_cache :
598
620
return self ._line_cache [cache_key ]
599
621
600
622
fixed , scrollable = self ._render_row (
601
623
row_index ,
602
624
line_no ,
603
625
base_style ,
604
- cursor_column = cursor_column ,
605
- hover_column = hover_column ,
626
+ cursor_location = self . cursor_cell ,
627
+ hover_location = self . hover_cell ,
606
628
)
607
629
fixed_width = sum (
608
630
column .render_width for column in self .columns [: self .fixed_columns ]
@@ -626,12 +648,10 @@ def render_line(self, y: int) -> Strip:
626
648
if self .show_header :
627
649
fixed_top_row_count += self .get_row_height (- 1 )
628
650
629
- style = self .rich_style
630
-
631
651
if y >= fixed_top_row_count :
632
652
y += scroll_y
633
653
634
- return self ._render_line (y , scroll_x , scroll_x + width , style )
654
+ return self ._render_line (y , scroll_x , scroll_x + width , self . rich_style )
635
655
636
656
def on_mouse_move (self , event : events .MouseMove ):
637
657
meta = event .style .meta
0 commit comments