22import gc
33import pathlib
44import time
5- from typing import ClassVar
5+ from typing import Any , ClassVar , Literal
66
77import click
88import polars as pl
2929)
3030from dt_browser .bookmarks import Bookmarks
3131from dt_browser .column_selector import ColumnSelector
32- from dt_browser .custom_table import CustomTable , polars_list_to_string
32+ from dt_browser .custom_table import CustomTable , _color_name , polars_list_to_string
3333from dt_browser .filter_box import FilterBox
3434from dt_browser .save_df_modal import SaveModal
3535from dt_browser .suggestor import ColumnNameSuggestor
@@ -72,32 +72,32 @@ class TableWithBookmarks(CustomTable):
7272 def __init__ (self , * args , bookmarks : Bookmarks , ** kwargs ):
7373 super ().__init__ (* args , ** kwargs )
7474 self ._bookmarks = bookmarks
75- self ._bookmark_highlight : Style | None = None
76- self ._search_highlight : Style | None = None
75+ self ._bookmark_highlight : Style = Style . null ()
76+ self ._search_highlight : Style = Style . null ()
7777
7878 def on_mount (self ):
7979 self ._bookmark_highlight = self .get_component_rich_style ("datatable--row-bookmark" )
8080 self ._search_highlight = self .get_component_rich_style ("datatable--row-search-result" )
8181
82- def _get_sel_col_bg_color (self , struct : pl . Struct ) :
82+ def _get_sel_col_bg_color (self , struct : dict [ str , Any ]) -> str :
8383 if self .active_search_queue and struct [INDEX_COL ] in self .active_search_queue :
84- return self ._search_highlight .bgcolor . name
84+ return _color_name ( self ._search_highlight .bgcolor )
8585 if self ._bookmarks .has_bookmarks and struct [INDEX_COL ] in self ._bookmarks .meta_dt [INDEX_COL ]:
86- return self ._bookmark_highlight .bgcolor . name
86+ return _color_name ( self ._bookmark_highlight .bgcolor )
8787 return super ()._get_sel_col_bg_color (struct )
8888
8989 def _get_row_bg_color_expr (self , cursor_row_idx : int ) -> pl .Expr :
9090 tmp = super ()._get_row_bg_color_expr (cursor_row_idx )
9191 if self .active_search_queue :
9292 tmp = (
9393 pl .when (pl .col (INDEX_COL ).is_in (self .active_search_queue ))
94- .then (pl .lit (self ._search_highlight .bgcolor . name ))
94+ .then (pl .lit (_color_name ( self ._search_highlight .bgcolor ) ))
9595 .otherwise (tmp )
9696 )
9797 if self ._bookmarks .has_bookmarks :
9898 tmp = (
9999 pl .when (pl .col (INDEX_COL ).is_in (self ._bookmarks .meta_dt [INDEX_COL ]))
100- .then (pl .lit (self ._bookmark_highlight .bgcolor . name ))
100+ .then (pl .lit (_color_name ( self ._bookmark_highlight .bgcolor ) ))
101101 .otherwise (tmp )
102102 )
103103 return tmp
@@ -108,7 +108,8 @@ def _get_row_bg_color_expr(self, cursor_row_idx: int) -> pl.Expr:
108108
109109def _guess_timestamp_cols (df : pl .DataFrame ):
110110 date_range = pl .Series (values = [datetime .date (2001 , 1 , 1 ), datetime .date (2042 , 1 , 1 )])
111- converts = [(x ,) + tuple (date_range .dt .epoch (x )) for x in ("s" , "ms" , "us" , "ns" )]
111+ epoch_units : tuple [Literal ["s" , "ms" , "us" , "ns" ], ...] = ("s" , "ms" , "us" , "ns" )
112+ converts = [(x ,) + tuple (date_range .dt .epoch (x )) for x in epoch_units ]
112113
113114 for col , dtype in df .schema .items ():
114115 if dtype .is_integer ():
@@ -342,7 +343,7 @@ class DtBrowser(Widget): # pylint: disable=too-many-public-methods,too-many-ins
342343 active_search : reactive [str | None ] = reactive (None )
343344 # active_dt: reactive[pl.DataFrame] = reactive(pl.DataFrame(), init=False, always_update=True)
344345
345- def __init__ (self , table_name : str , source_file_or_table : pathlib .Path | pl .DataFrame ):
346+ def __init__ (self , table_name : str | pathlib . Path , source_file_or_table : pathlib .Path | pl .DataFrame ):
346347 super ().__init__ ()
347348 bt = (
348349 from_file_path (source_file_or_table )
@@ -463,7 +464,7 @@ async def watch_active_search(self, goto: bool = True):
463464 if goto :
464465 # Find the nearest index to the current cursor
465466 coord = self .query_one ("#main_table" , CustomTable ).cursor_coordinate .row
466- next_row = next ((i for i , x in enumerate (self .active_search_queue ) if x > coord ), None )
467+ next_row = next ((i for i , x in enumerate (self .active_search_queue ) if x > coord ), 0 )
467468 self .active_search_idx = next_row - 1
468469 self .action_iter_search (True )
469470 except Exception as e :
@@ -472,12 +473,14 @@ async def watch_active_search(self, goto: bool = True):
472473 foot .search_pending = False
473474
474475 def action_iter_search (self , forward : bool ):
476+ if self .active_search_queue is None or self .active_search_idx is None :
477+ return
475478 table = self .query_one ("#main_table" , CustomTable )
476479 coord = table .cursor_coordinate
477480 self .active_search_idx = min (
478481 max (self .active_search_idx + (1 if forward else - 1 ), 0 ), len (self .active_search_queue ) - 1
479482 )
480- if self . active_search_idx >= 0 and self .active_search_idx < len (self .active_search_queue ):
483+ if 0 <= self .active_search_idx < len (self .active_search_queue ):
481484 next_idex = self .active_search_queue [self .active_search_idx ]
482485 ys = next_idex
483486 xs = table .scroll_x
@@ -638,7 +641,7 @@ async def handle_cell_highlight(self, event: CustomTable.CellHighlighted):
638641 @on (CustomTable .CellSelected , selector = "#main_table" )
639642 def handle_cell_select (self , event : CustomTable .CellSelected ):
640643 if self ._select_interest :
641- self .query_one (self ._select_interest , ReceivesTableSelect ).on_table_select (event .value )
644+ self .query_one (self ._select_interest , ReceivesTableSelect ).on_table_select (event .value ) # type: ignore[type-abstract]
642645 self ._select_interest = None
643646
644647 @on (Bookmarks .BookmarkSelected )
@@ -800,7 +803,7 @@ def compose(self) -> ComposeResult:
800803
801804
802805class DtBrowserApp (App ): # pylint: disable=too-many-public-methods,too-many-instance-attributes
803- def __init__ (self , table_name : str , source_file_or_table : pathlib .Path | pl .DataFrame ):
806+ def __init__ (self , table_name : str | pathlib . Path , source_file_or_table : pathlib .Path | pl .DataFrame ):
804807 super ().__init__ ()
805808 self ._table_name = table_name
806809 self ._source = source_file_or_table
0 commit comments