Archived
1
1
Fork 0

improve log toggle keybind/stylesheet ref

This commit is contained in:
Josh Lay 2023-04-23 16:09:38 -05:00
parent c6a2fd4f65
commit 02664b5b9a
Signed by: jlay
GPG key ID: B265E45CACAD108A

View file

@ -13,7 +13,7 @@ import sys
from typing import Tuple, Optional from typing import Tuple, Optional
import pkg_resources import pkg_resources
# from textual import events from textual.binding import Binding
from textual.app import App, ComposeResult from textual.app import App, ComposeResult
from textual.containers import Container, Horizontal from textual.containers import Container, Horizontal
from textual.reactive import reactive from textual.reactive import reactive
@ -94,8 +94,6 @@ def format_frequency(frequency_hz) -> str:
class LogScreen(Screen): class LogScreen(Screen):
"""Creates a screen for the logging widget""" """Creates a screen for the logging widget"""
BINDINGS = [("l", "app.pop_screen", "Show/hide logs")]
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.text_log = TextLog(highlight=True, markup=True) self.text_log = TextLog(highlight=True, markup=True)
@ -127,23 +125,23 @@ class GPUStatsWidget(Static):
class GPUStats(App): class GPUStats(App):
"""Textual-based tool to show AMDGPU statistics.""" """Textual-based tool to show AMDGPU statistics."""
# determine the real path of the script, to load the stylesheet # apply stylesheet
CSS_PATH = pkg_resources.resource_filename('amdgpu_stats', 'amdgpu_stats.css') CSS_PATH = 'amdgpu_stats.css'
# initialize log screen # initialize log screen
SCREENS = {"logs": LogScreen()} SCREENS = {"logs": LogScreen()}
# setup keybinds # setup keybinds
# Binding("l", "push_screen('logs')", "Toggle logs", priority=True),
BINDINGS = [ BINDINGS = [
("c", "toggle_dark", "Toggle colors"), Binding("c", "toggle_dark", "Toggle colors", priority=True),
("l", "push_screen('logs')", "Show/hide logs"), Binding("l", "toggle_log", "Toggle logs", priority=True),
("q", "quit_app", "Quit"), Binding("q", "quit_app", "Quit", priority=True)
] ]
def compose(self) -> ComposeResult: def compose(self) -> ComposeResult:
"""Create child widgets for the app.""" """Create child widgets for the app."""
yield Header() yield Header()
yield Footer()
yield Container(GPUStatsWidget()) yield Container(GPUStatsWidget())
self.update_log("[bold green]App started, logging begin!") self.update_log("[bold green]App started, logging begin!")
self.update_log("[bold italic]Information sources:[/]") self.update_log("[bold italic]Information sources:[/]")
@ -151,6 +149,7 @@ class GPUStats(App):
self.update_log(f'[bold] {metric}:[/] {source}') self.update_log(f'[bold] {metric}:[/] {source}')
for metric, source in TEMP_FILES.items(): for metric, source in TEMP_FILES.items():
self.update_log(f'[bold] {metric} temperature:[/] {source}') self.update_log(f'[bold] {metric} temperature:[/] {source}')
yield Footer()
def action_toggle_dark(self) -> None: def action_toggle_dark(self) -> None:
"""An action to toggle dark mode.""" """An action to toggle dark mode."""
@ -163,6 +162,13 @@ class GPUStats(App):
self.update_log(f"[bold]{message}") self.update_log(f"[bold]{message}")
self.exit(message) self.exit(message)
def action_toggle_log(self) -> None:
"""Toggle between the main screen and the LogScreen."""
if isinstance(self.screen, LogScreen):
self.pop_screen()
else:
self.push_screen("logs")
def update_log(self, message: str) -> None: def update_log(self, message: str) -> None:
"""Update the TextLog widget with a new message.""" """Update the TextLog widget with a new message."""
log_screen = self.SCREENS["logs"] log_screen = self.SCREENS["logs"]