Skip to content

Commit

Permalink
refactor/don't rely on worker state changes to log color changes
Browse files Browse the repository at this point in the history
  • Loading branch information
joshlay committed Aug 15, 2023
1 parent 1291b79 commit 3e09d08
Showing 1 changed file with 12 additions and 23 deletions.
35 changes: 12 additions & 23 deletions tui_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
TabPane,
RichLog,
)
from textual.worker import Worker, WorkerState

# 'package' (script) meta
metadata = {
Expand All @@ -30,7 +29,7 @@
}

class QuitScreen(ModalScreen):
"""Screen with a dialog to quit."""
"""Screen with a dialog to quit. Shown when user presses keybind"""

def compose(self) -> ComposeResult:
yield Grid(
Expand All @@ -50,7 +49,7 @@ def on_button_pressed(self, event: Button.Pressed) -> None:


class TextualApp(App):
"""Textual 'app' for this utility"""
"""Defines the main Textual 'app'"""

# set the path to the stylesheet, relative is fine
CSS_PATH = 'style.css'
Expand Down Expand Up @@ -81,27 +80,24 @@ def __init__(self, *args, **kwargs):

@work(exclusive=True)
async def action_custom_dark(self) -> None:
"""An action to toggle dark mode.
Wraps 'action_toggle_dark' with our logging"""
"""An action to toggle dark mode. Wraps 'action_toggle_dark' with our logging"""
self.app.dark = not self.app.dark
await self.update_log(f'[bold]Dark mode: {self.dark}', notify=True)

def action_request_quit(self) -> None:
"""When the user presses the quit keybind, show the quit confirmation screen"""
self.push_screen('quit_screen')

def get_screenshot_name(self) -> str:
'''Using the current date and time, return a name for the requested screenshot'''
"""Using the current date and time, return a name for the requested screenshot"""
return f'screenshot_{datetime.now().isoformat().replace(":", "_")}.svg'

async def action_custom_screenshot(self, screen_dir: str = '/tmp') -> None:
"""Action that fires when the user presses 's' for a screenshot"""
# construct the screenshot elements: name (w/ ISO timestamp) + path
screen_name = self.get_screenshot_name()
# take the screenshot, recording the path for logging/notification
outpath = self.save_screenshot(path=screen_dir, filename=screen_name)
outpath = self.save_screenshot(path=screen_dir, filename=self.get_screenshot_name())
# construct the log/notification message, then show it
self.update_log(f"[bold]Screenshot saved: [green]'{outpath}'", notify=True)
await self.update_log(f"[bold]Screenshot saved: [green]'{outpath}'", notify=True)

def compose(self) -> ComposeResult:
"""Craft the main window/widgets"""
Expand All @@ -117,26 +113,19 @@ def compose(self) -> ComposeResult:
)
yield Footer()

def on_mount(self) -> None:
'''Fires when widget 'mounted', behaves like on-first-showing'''
self.update_log(f"Hello, {os.getlogin()} :)", notify=True)
async def on_mount(self) -> None:
"""Fires when widget 'mounted', behaves like on-first-showing"""
await self.update_log(f"Hello, {os.getlogin()} :)", notify=True)

def update_log(self, message: str, timestamp: bool = True, notify: bool = False) -> None:
'''Write to the main RichLog widget, optional timestamps'''
async def update_log(self, message: str, timestamp: bool = True, notify: bool = False) -> None:
"""Write to the main RichLog widget, optional timestamps and notifications"""
if timestamp:
self.text_log.write(datetime.now().strftime("%b %d %H:%M:%S") + ': ' + message)
else:
self.text_log.write(message)
if notify:
self.notify(message)

def on_worker_state_changed(self, event: Worker.StateChanged) -> None:
"""Called when the worker state changes."""
# log dark mode being toggled; when the worker event shows success
if event.worker.name == 'action_custom_dark':
if event.state == WorkerState.SUCCESS:
self.update_log(f'[bold]Dark mode: {self.dark}', notify=True)


if __name__ == "__main__":
app = TextualApp()
Expand Down

0 comments on commit 3e09d08

Please sign in to comment.