Skip to content

Commit

Permalink
✨ BannerApp wrapper class
Browse files Browse the repository at this point in the history
  • Loading branch information
haliphax committed Oct 16, 2023
1 parent 39be186 commit 2021e48
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 49 deletions.
56 changes: 7 additions & 49 deletions userland/scripts/oneliners.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
"""Oneliners script"""

# stdlib
from math import floor

# 3rd party
from rich.text import Text
from textual import events
from textual.validation import Length
from textual.widgets import Input, Label, ListItem, ListView

# api
from xthulu.resources import Resources
from xthulu.ssh.console.app import XthuluApp
from xthulu.ssh.console.banner_app import BannerApp
from xthulu.ssh.console.art import load_art
from xthulu.ssh.context import SSHContext

# local
from userland.models import Oneliner

BANNER_PADDING = 10
"""Required space left over to display banner art"""

LIMIT = 200
"""Total number of oneliners to load"""


class OnlinersApp(XthuluApp):
class OnlinersApp(BannerApp):

"""Oneliners Textual app"""

Expand Down Expand Up @@ -56,12 +48,6 @@ class OnlinersApp(XthuluApp):
"""
"""Stylesheet"""

artwork: list[str]
"""Lines from pre-loaded artwork file"""

banner: Label
"""Artwork banner widget"""

error_message: Label
"""Error message widget"""

Expand All @@ -71,37 +57,17 @@ class OnlinersApp(XthuluApp):
def __init__(
self,
context: SSHContext,
oneliners: list[Oneliner],
artwork: list[str],
oneliners: list[Oneliner],
**kwargs,
):
self.artwork = artwork
self.oneliners = oneliners
super().__init__(context, **kwargs)
super().__init__(context, artwork, **kwargs)
self.bind("escape", "quit")

def _update_banner(self):
padded = []
pad_left = " " * floor(self.context.term.width / 2 - 40)

for line in self.artwork:
padded += [pad_left, line]

text = Text.from_ansi(
"".join(padded), overflow="crop", no_wrap=True, end=""
)
self.banner.update(text)

def compose(self):
# banner
self.banner = Label(markup=False)

if self.console.height < len(self.artwork) + BANNER_PADDING:
self.banner.display = False
else:
self._update_banner()

yield self.banner
for widget in super().compose():
yield widget

# oneliners
list = ListView(
Expand Down Expand Up @@ -166,14 +132,6 @@ async def on_input_submitted(self, event: Input.Submitted) -> None:

self.exit()

def on_resize(self, event: events.Resize) -> None:
if event.size.height < len(self.artwork) + BANNER_PADDING:
self.banner.update("")
self.banner.display = False
else:
self._update_banner()
self.banner.display = True


async def main(cx: SSHContext):
cx.term.set_window_title("oneliners")
Expand All @@ -190,5 +148,5 @@ async def main(cx: SSHContext):
)

artwork = await load_art("userland/artwork/oneliners.ans", "amiga")
app = OnlinersApp(cx, oneliners, artwork)
app = OnlinersApp(cx, artwork, oneliners)
await app.run_async()
63 changes: 63 additions & 0 deletions xthulu/ssh/console/banner_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""Textual application wrapper with display banner"""

# stdlib
from math import floor

# 3rd party
from rich.text import Text
from textual import events
from textual.widgets import Label

# local
from ..context import SSHContext
from .app import XthuluApp

BANNER_PADDING = 10
"""Required space left over to display banner art"""


class BannerApp(XthuluApp):

"""Textual app with banner display"""

artwork: list[str]
"""Lines from pre-loaded artwork file"""

banner: Label
"""Artwork banner widget"""

def __init__(self, context: SSHContext, artwork: list[str], **kwargs):
self.artwork = artwork
super().__init__(context=context, **kwargs)
self.run_worker(self._watch_for_resize, exclusive=True)

def _update_banner(self):
padded = []
pad_left = " " * floor(self.context.term.width / 2 - 40)

for line in self.artwork:
padded += [pad_left, line]

text = Text.from_ansi(
"".join(padded), overflow="crop", no_wrap=True, end=""
)
self.banner.update(text)

def compose(self):
# banner
self.banner = Label(markup=False)

if self.console.height < len(self.artwork) + BANNER_PADDING:
self.banner.display = False
else:
self._update_banner()

yield self.banner

def on_resize(self, event: events.Resize) -> None:
if event.size.height < len(self.artwork) + BANNER_PADDING:
self.banner.update("")
self.banner.display = False
else:
self._update_banner()
self.banner.display = True

0 comments on commit 2021e48

Please sign in to comment.