Enhance DataTable population by adding sort indicators to column headers and clearing both rows and columns before repopulating

This commit is contained in:
Philip Henning 2025-07-29 17:03:18 +02:00
parent 15a3b6230f
commit bcded3105b

View file

@ -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."""