Skip to content

Instantly share code, notes, and snippets.

@vsajip
Created December 18, 2022 12:37
Show Gist options
  • Save vsajip/75787e466cd53210d2c6e6a4a8c8e76c to your computer and use it in GitHub Desktop.
Save vsajip/75787e466cd53210d2c6e6a4a8c8e76c to your computer and use it in GitHub Desktop.
from __future__ import annotations
import asyncio
import datetime
import sys
import time
from textual.app import App, ComposeResult
from textual.containers import Container, Horizontal, Vertical
from textual.widgets import DataTable, Footer, Label
class TestApp(App):
BINDINGS = [
('ctrl+t', 'test', 'Test action'),
]
CSS = '''
.hfr {
height: 1fr;
}
.wfr {
width: 1fr;
}
'''
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.loglabel = Label(id='logs', classes='wfr')
self.vertpane = Vertical(self.loglabel, id='list', classes='wfr hfr')
self.footer = Footer()
self.logs = []
def log_message(self, message):
now = datetime.datetime.now().isoformat()
self.logs.append(f'{now} {message}')
s = '\n'.join(self.logs)
self.loglabel.update(s)
def compose(self) -> ComposeResult:
yield Horizontal(self.vertpane)
yield self.footer
def perform_operation(self):
self.log_message('Starting operation ...')
def do_op():
time.sleep(5)
self.log_message('Operation finished!')
method = 'timer'
if len(sys.argv) > 1:
method = sys.argv[1]
if method == 'timer':
self.set_timer(0.001, do_op)
elif method == 'later':
self.call_later(do_op)
elif method == 'refresh':
self.call_after_refresh(do_op)
else:
raise ValueError(f'Unsupported method: {method}')
def action_test(self):
self.perform_operation()
TestApp().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment