refactor: Improve type hints and code organization in TUI

- Add ClassVar annotation to BINDINGS for proper type checking
- Move widget imports to module level for consistency
- Add type hints to event handler parameters
- Add return type annotation to run_tui function
- Organize .gitignore with proper section comments
- Resolves CodeRabbit nitpick suggestions
This commit is contained in:
Gnanasaikiran 2025-11-15 16:19:16 +05:30
parent 34d02a84c7
commit e86bce7280
2 changed files with 9 additions and 10 deletions

1
.gitignore vendored
View file

@ -197,6 +197,7 @@ SWE-bench_testsample/
# ChromaDB Data # ChromaDB Data
.chromadb_data/ .chromadb_data/
# TUI development artifacts
*.backup *.backup
create_tui_files.sh create_tui_files.sh
add_tui_command.py add_tui_command.py

View file

@ -2,11 +2,13 @@
Cognee TUI - Main Application Cognee TUI - Main Application
Text-based User Interface for managing Cognee knowledge graphs Text-based User Interface for managing Cognee knowledge graphs
""" """
from typing import ClassVar
from textual.app import App, ComposeResult from textual.app import App, ComposeResult
from textual.binding import Binding from textual.binding import Binding
from textual.widgets import Header, Footer from textual.widgets import Header, Footer, Static, Button
from textual.containers import VerticalScroll
from textual.screen import Screen from textual.screen import Screen
from textual.events import Key
from cognee.cli.tui.screens.home import HomeScreen from cognee.cli.tui.screens.home import HomeScreen
@ -57,12 +59,11 @@ class CogneeTUI(App):
TITLE = "Cognee TUI - Knowledge Graph Manager" TITLE = "Cognee TUI - Knowledge Graph Manager"
SUB_TITLE = "Navigate with arrow keys • Press ? for help" SUB_TITLE = "Navigate with arrow keys • Press ? for help"
BINDINGS = [ BINDINGS: ClassVar[list[Binding]] = [
Binding("q", "quit", "Quit", priority=True), Binding("q", "quit", "Quit", priority=True),
Binding("?", "help", "Help"), Binding("?", "help", "Help"),
Binding("d", "toggle_dark", "Toggle Dark Mode"), Binding("d", "toggle_dark", "Toggle Dark Mode"),
] ]
def on_mount(self) -> None: def on_mount(self) -> None:
"""Initialize the app with the home screen""" """Initialize the app with the home screen"""
self.push_screen(HomeScreen()) self.push_screen(HomeScreen())
@ -100,25 +101,22 @@ class HelpScreen(Screen):
self.help_text = help_text self.help_text = help_text
def compose(self) -> ComposeResult: def compose(self) -> ComposeResult:
from textual.widgets import Static, Button
from textual.containers import VerticalScroll
yield Header() yield Header()
with VerticalScroll(): with VerticalScroll():
yield Static(self.help_text, markup=False) yield Static(self.help_text, markup=False)
yield Button("Close (Esc)", id="close", variant="primary") yield Button("Close (Esc)", id="close", variant="primary")
yield Footer() yield Footer()
def on_button_pressed(self, event) -> None: def on_button_pressed(self, event: Button.Pressed) -> None:
if event.button.id == "close": if event.button.id == "close":
self.app.pop_screen() self.app.pop_screen()
def on_key(self, event) -> None: def on_key(self, event: Key) -> None:
if event.key == "escape": if event.key == "escape":
self.app.pop_screen() self.app.pop_screen()
def run_tui(mouse: bool = True): def run_tui(mouse: bool = True) -> None:
"""Entry point to run the TUI application """Entry point to run the TUI application