Enhance DataTable row styling by using Rich Text for active and inactive entries

This commit is contained in:
Philip Henning 2025-07-29 17:06:29 +02:00
parent bcded3105b
commit 9cd2b5c3fe

View file

@ -9,6 +9,7 @@ from textual.containers import Horizontal, Vertical
from textual.widgets import Header, Footer, Static, DataTable
from textual.binding import Binding
from textual.reactive import reactive
from rich.text import Text
from .core.parser import HostsParser
from .core.models import HostsFile
@ -73,6 +74,8 @@ class HostsManagerApp(App):
#entries-table .datatable--odd-row {
background: $surface;
}
/* DataTable row styling - colors are now handled via Rich Text objects */
"""
BINDINGS = [
@ -180,10 +183,15 @@ class HostsManagerApp(App):
# Add row with styling based on active status
if entry.is_active:
table.add_row(entry.ip_address, canonical_hostname)
# Active entries in green
ip_text = Text(entry.ip_address, style="green")
hostname_text = Text(canonical_hostname, style="green")
table.add_row(ip_text, hostname_text)
else:
# For inactive entries, we'll style them differently
table.add_row(f"# {entry.ip_address}", canonical_hostname)
# Inactive entries in yellow/orange with # prefix and italic
ip_text = Text(f"# {entry.ip_address}", style="yellow italic")
hostname_text = Text(canonical_hostname, style="yellow italic")
table.add_row(ip_text, hostname_text)
def restore_cursor_position(self, previous_entry) -> None:
"""Restore cursor position after reload, maintaining selection if possible."""