piermesh/src/ui.py

130 lines
3.5 KiB
Python

from textual.app import App, ComposeResult
from textual.widgets import Log, Label, Footer, Header, ProgressBar
from textual.binding import Binding
from textual.containers import Horizontal, Vertical
import sys, os
global nodeOb
nodeOb = None
class TUI(App):
"""
TUI for PierMesh
`🔗 Source <https://git.utopic.work/PierMesh/piermesh/src/branch/main/src/ui.py>`_
Attributes
----------
visibleLogo: bool
Whether the logo is visible or not, used in toggling visibility
nodeOb: Node
Reference to the Node running the PierMesh service
done: bool
Whether the TUI has been killed
"""
visibleLogo = True
nodeOb = None
done = False
CSS_PATH = "ui.tcss"
BINDINGS = [
Binding(key="q", action="quitFull", description="Quit the app", show=True),
Binding(
key="f",
action="toggleFullscreen",
description="Full screen the logs",
show=True,
),
]
def action_toggleFullscreen(self):
"""
Toggle fullscreen logs by either collapsing width or setting it to it's original size
"""
if self.visibleLogo:
self.query_one("#logo").styles.width = 0
else:
self.query_one("#logo").styles.width = "50%"
self.visibleLogo = not self.visibleLogo
def action_quitFull(self):
"""
Kill the whole stack by setting self to done and terminating the thread. We check in run.monitor later and kill the rest of the stack then with psutil
See Also
--------
run.monitor
"""
self.done = True
# sys.exit("Terminating TUI...")
def compose(self):
"""
Build the TUI
"""
ascii = ""
with open("piermesh-mini.ascii", "r") as f:
ascii = f.read()
"""
Load the ascii art for display on the left label
"""
yield Header(icon="P")
yield Label(ascii, classes="largeLabel", name="logo", id="logo")
yield Vertical(
Log(auto_scroll=True, classes="baseLog"),
Label("CPU usage:", name="cpul", id="cpul"),
ProgressBar(show_eta=False, show_percentage=True),
Label("MEM usage: ", name="meml", id="meml"),
)
yield Footer()
def do_write_line(self, logLine: str):
"""
Write line to the logs panel
Parameters
----------
logLine: str
Line to log
"""
log = self.query_one(Log)
log.write_line(logLine)
def do_set_cpu_percent(self, percent: float):
"""
Set CPU percent in the label and progress bar
Parameters
----------
percent: float
Percent of the cpu PierMesh is using
"""
self.query_one("#cpul").update("CPU usage: {0} %".format(str(percent)))
pbar = self.query_one(ProgressBar)
pbar.progress = percent
def do_set_mem(self, memmb: float):
"""
Set memory usage label in the ui
Parameters
----------
memmb: float
Memory usage of PierMesh in megabytes
"""
self.query_one("#meml").update("MEM usage: {0} mB".format(str(memmb)))
def on_mount(self):
"""
Called at set up, configures the title and the progess bar
"""
self.title = "PierMesh TUI"
self.query_one(ProgressBar).update(total=100)
self.query_one(ProgressBar).update(progress=0)
if __name__ == "__main__":
app = TUI()
app.run()