From bcded3105b0b2451b14810242054d518a1db6c79 Mon Sep 17 00:00:00 2001 From: phg Date: Tue, 29 Jul 2025 17:03:18 +0200 Subject: [PATCH] Enhance DataTable population by adding sort indicators to column headers and clearing both rows and columns before repopulating --- src/hosts/main.py | 45 ++++++++++++++------------------------------- 1 file changed, 14 insertions(+), 31 deletions(-) diff --git a/src/hosts/main.py b/src/hosts/main.py index bf7f2c7..9d8bb97 100644 --- a/src/hosts/main.py +++ b/src/hosts/main.py @@ -151,19 +151,27 @@ class HostsManagerApp(App): def populate_entries_table(self) -> None: """Populate the left pane with hosts entries using DataTable.""" table = self.query_one("#entries-table", DataTable) - table.clear() + table.clear(columns=True) # Clear both rows and columns # Configure DataTable properties table.zebra_stripes = True table.cursor_type = "row" table.show_header = True - # Add columns only if they don't exist - if not table.columns: - table.add_columns("IP Address", "Canonical Hostname") + # Create column labels with sort indicators + ip_label = "IP Address" + hostname_label = "Canonical Hostname" - # Update column headers with sort indicators - self.update_column_headers() + # Add sort indicators + if self.sort_column == "ip": + arrow = "↑" if self.sort_ascending else "↓" + ip_label = f"{arrow} IP Address" + elif self.sort_column == "hostname": + arrow = "↑" if self.sort_ascending else "↓" + hostname_label = f"{arrow} Canonical Hostname" + + # Add columns with proper labels + table.add_columns(ip_label, hostname_label) # Add rows for entry in self.hosts_file.entries: @@ -276,31 +284,6 @@ class HostsManagerApp(App): # For now, just update the status with help info self.update_status("Help: ↑/↓ Navigate, r Reload, q Quit, h Help") - def update_column_headers(self) -> None: - """Update column headers with sort indicators.""" - table = self.query_one("#entries-table", DataTable) - if not table.columns or len(table.columns) < 2: - return - - # Get current column labels - ip_label = "IP Address" - hostname_label = "Canonical Hostname" - - # Add sort indicators - if self.sort_column == "ip": - arrow = "↑" if self.sort_ascending else "↓" - ip_label = f"{arrow} IP Address" - elif self.sort_column == "hostname": - arrow = "↑" if self.sort_ascending else "↓" - hostname_label = f"{arrow} Canonical Hostname" - - # Update column labels safely - try: - table.columns[0].label = ip_label - table.columns[1].label = hostname_label - except (IndexError, KeyError): - # If we can't update the labels, just continue - pass def action_sort_by_ip(self) -> None: """Sort entries by IP address, toggle ascending/descending."""