From d6a7eec735ce504064886e9793516309315085db Mon Sep 17 00:00:00 2001 From: "Waylon S. Walker" Date: Tue, 24 Jan 2023 08:35:39 -0600 Subject: [PATCH] add edit command --- textual_tutorial/tui.py | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/textual_tutorial/tui.py b/textual_tutorial/tui.py index 5fcb40d..2c060bb 100644 --- a/textual_tutorial/tui.py +++ b/textual_tutorial/tui.py @@ -1,14 +1,16 @@ +import os from pathlib import Path -from textual.app import App, ComposeResult -from textual.css.query import NoMatches -from textual.widgets import Header, Footer - -from textual.containers import Container -from textual.widgets import Button, Header, Footer, Static - -from textual.reactive import reactive +import subprocess +import tempfile from time import monotonic +from textual.app import App, ComposeResult +from textual.containers import Container +from textual.css.query import NoMatches +from textual.reactive import reactive +from textual.widgets import Button, Footer, Header, Static +import tomlkit + class TimeDisplay(Static): """A widget to display elapsed time.""" @@ -86,6 +88,7 @@ class StopwatchApp(App): ("j", "next", "Next"), ("k", "prev", "Prev"), ("space", "toggle", "Toggle"), + ("e", "edit", "Edit"), ] def on_mount(self): @@ -112,6 +115,16 @@ class StopwatchApp(App): def action_prev(self): self.activate(-1) + def action_edit(self): + prompt = {"prompt": "this is the prompt"} + editor = os.environ.get("EDITOR", "vim") + file = tempfile.NamedTemporaryFile(prefix="lockhart", suffix=".toml") + file.write(tomlkit.dumps(prompt).encode()) + file.seek(0) + proc = subprocess.Popen([editor, file.name]) + proc.wait() + self.refresh() + def action_toggle(self): try: active = self.query_one("Stopwatch.active") @@ -170,10 +183,11 @@ class StopwatchApp(App): def tui(): - from textual.features import parse_features import os import sys + from textual.features import parse_features + dev = "--dev" in sys.argv features = set(parse_features(os.environ.get("TEXTUAL", ""))) if dev: