Module userland.scripts.oneliners

Oneliners script

Global variables

var LIMIT

Total number of oneliners to load

Functions

async def main(cx: SSHContext) ‑> None
Expand source code
async def main(cx: SSHContext) -> None:
    cx.console.set_window_title("oneliners")
    await OnlinersApp(
        cx,
        art_path=path.join("userland", "artwork", "oneliners.ans"),
        art_encoding="amiga",
    ).run_async()

Classes

class OnlinersApp (context: SSHContext,
**kwargs)
Expand source code
class OnlinersApp(BannerApp):
    """Oneliners Textual app"""

    CSS = """
        $accent: ansi_red;
        $error: ansi_bright_red;

        Label {
            width: 100%;
        }

        ListItem {
            background: $primary-background;
        }

        ListItem.even {
            background: $secondary-background;
        }

        ListView:focus ListItem.--highlight {
            background: $accent;
        }

        #err {
            background: $error;
            color: black;
        }
    """
    """Stylesheet"""

    def __init__(self, context: SSHContext, **kwargs):
        super().__init__(context, **kwargs)
        self.bind("escape", "quit")

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

        # oneliners
        lv = ListView()
        lv.styles.scrollbar_background = "black"
        lv.styles.scrollbar_color = "ansi_yellow"
        lv.styles.scrollbar_color_active = "white"
        lv.styles.scrollbar_color_hover = "ansi_bright_yellow"
        yield lv

        # error message
        err = Label(id="err")
        err.display = False
        yield err

        # input
        input_widget = Input(
            max_length=Oneliner.MAX_LENGTH,
            placeholder="Enter a oneliner or press ESC",
        )
        input_widget.focus()
        yield input_widget

    async def on_input_submitted(self, event: Input.Submitted) -> None:
        val = event.input.value.strip()

        if val != "":
            async with db_session() as db:
                db.add(Oneliner(message=val, user_id=self.context.user.id))
                await db.commit()

        self.exit()

    async def on_mount(self) -> None:
        recent = (
            select(Oneliner.id)
            .order_by(col(Oneliner.id).desc())
            .limit(LIMIT)
            .alias("recent")
            .select()
        )

        async with db_session() as db:
            oneliners = (
                await db.exec(
                    select(Oneliner).where(col(Oneliner.id).in_(recent))
                )
            ).all()

        lv = self.query_one(ListView)

        for idx, o in enumerate(oneliners):
            lv.mount(
                ListItem(Label(o.message), classes="even" if idx % 2 else "")
            )

        lv.index = len(oneliners) - 1
        lv.scroll_end(animate=False)

Oneliners Textual app

Create an instance of an app.

Args

driver_class
Driver class or None to auto-detect. This will be used by some Textual tools.
css_path
Path to CSS or None to use the CSS_PATH class variable. To load multiple CSS files, pass a list of strings or paths which will be loaded in order.
watch_css
Reload CSS if the files changed. This is set automatically if you are using textual run with the dev switch.
ansi_color
Allow ANSI colors if True, or convert ANSI colors to to RGB if False.

Raises

CssPathError
When the supplied CSS path(s) are an unexpected type.

Ancestors

  • BannerApp
  • XthuluApp
  • textual.app.App
  • typing.Generic
  • textual.dom.DOMNode
  • textual.message_pump.MessagePump

Class variables

var CSS

Stylesheet

Methods

async def on_input_submitted(self, event: textual.widgets._input.Input.Submitted) ‑> None
Expand source code
async def on_input_submitted(self, event: Input.Submitted) -> None:
    val = event.input.value.strip()

    if val != "":
        async with db_session() as db:
            db.add(Oneliner(message=val, user_id=self.context.user.id))
            await db.commit()

    self.exit()
async def on_mount(self) ‑> None
Expand source code
async def on_mount(self) -> None:
    recent = (
        select(Oneliner.id)
        .order_by(col(Oneliner.id).desc())
        .limit(LIMIT)
        .alias("recent")
        .select()
    )

    async with db_session() as db:
        oneliners = (
            await db.exec(
                select(Oneliner).where(col(Oneliner.id).in_(recent))
            )
        ).all()

    lv = self.query_one(ListView)

    for idx, o in enumerate(oneliners):
        lv.mount(
            ListItem(Label(o.message), classes="even" if idx % 2 else "")
        )

    lv.index = len(oneliners) - 1
    lv.scroll_end(animate=False)

Inherited members