banner — ASCII banner text, live in a window

banner — ASCII banner text, live in a window

banner is a LightGUI (Python/XFrame) port of the banner utility from util_box: type text, pick a font, and the ASCII banner renders live in the scrolling output area below. No figlet, no dependencies beyond LightGUI itself — the four fonts are built in. It is one of the 50 apps in util_boxgui, the project recreating util_box utilities as LightGUI apps. The full source is apps/python/banner/banner.py in that repo.

The app opens with Hello already typed and the standard font already selected, so the banner is rendered the moment the window appears — the screenshot below is the default state, not a staged one. Change the text or the font and it re-renders immediately.

The GUI layout

banner running under Xvfb
banner running — "Hello" in the standard font, captured under Xvfb.

The window is 640×480 and holds five widgets, created with create() + set_prop() and drawn on a single Gtk.DrawingArea:

  • Text: a GtkLabel and a GtkEntry holding the text to render, pre-filled with Hello. Click the entry to focus it (a blue outline appears), then type — BackSpace deletes, Escape drops focus.
  • Font: a GtkLabel and a GtkComboBoxText listing the four built-in fonts — standard, small, big, slant. Click the combo to cycle to the next font; it re-renders on every click.
  • Output area: a GtkTextView filling the rest of the window, showing the rendered banner in a 12-point monospace font, with scroll bars. It is read-only — clicking it just drops the entry's focus.

Two honest limitations, both inherited from how LightGUI draws its widgets with Cairo on one drawing area: there is no native GTK keyboard focus or combo drop-down, so this app wires focus and typing itself (click to focus, type to edit) and the font combo cycles instead of opening a list; and the output area does not really scroll — the text-view scroll bars are drawn, not functional. A very wide banner (long text in the big font) is clipped at the right edge with an ellipsis. The default Hello fits in every font; util_boxgui fits in standard, small and slant, but is clipped in big.

The source, in sections

banner.py is about 344 lines. It is modelled on the LightGUI Python examples gtkentry.py, gtkcomboboxtext.py and gtktextview.py: widgets are created with create() + set_prop(), drawn on a Gtk.DrawingArea by widget.draw(cr), hit-tested on click, and dispatched through a _HANDLERS table. The rendering half, though, is not modelled on anything — it is vendored verbatim from the CLI, so the GUI can never drift from it.

The rendering. The font table (FONT), the unknown-character glyph (UNKNOWN) and render() are copied byte-for-byte from the CLI script into banner.py. Each character is a 5-row glyph; render() upper-cases the text, looks each character up (falling back to the block glyph for anything with no glyph, like @), and joins the rows:

def render(text, font):
    text = text.upper()
    rows = [""] * 5
    for ch in text:
        glyph = FONT.get(ch, UNKNOWN)
        for i in range(5):
            rows[i] += glyph[i] + " "
    if font == "small":
        # 3-row compact cut: top, middle, bottom rows, trimmed
        rows = [rows[0].rstrip(), rows[2].rstrip(), rows[4].rstrip()]
    elif font == "big":
        big = []
        for r in rows:
            doubled = "".join(c * 2 for c in r)
            big.extend([doubled, doubled])
        rows = big
    elif font == "slant":
        rows = [(" " * i) + r for i, r in enumerate(rows)]
    return "\n".join(r.rstrip() for r in rows)

The font effects are the CLI's: small keeps the top, middle and bottom rows; big doubles every character horizontally and vertically (10 rows); slant indents each successive row one space further right. Because the table and the function are vendored rather than reimplemented, the GUI output is byte-identical to the CLI output for the same text and font — verified against the CLI on 12 cases (all four fonts, mixed case, digits, punctuation, spaces, and unknown characters such as @). Lowercase input is upper-cased, exactly as the CLI does.

The widgets. _build_widgets() creates the five widgets with their design-time geometry, and an _ANCHORS table records each widget's anchor flags so _apply_anchors() can reposition them when the window is resized — recomputed from the design geometry every time, so a resize is idempotent and never drifts:

def _build_widgets():
    """Create every placed widget with its props."""
    widgets = []
    widget = create("GtkLabel", "GtkLabel_001", 16, 12, 44, 30)
    widget.set_prop("label", "Text:")
    widgets.append(widget)
    widget = create("GtkEntry", "GtkEntry_001", 64, 12, 340, 30)
    widget.set_prop("text", DEFAULT_TEXT)
    widget.set_prop("placeholder-text", "Type banner text")
    widgets.append(widget)
    widget = create("GtkLabel", "GtkLabel_002", 416, 12, 40, 30)
    widget.set_prop("label", "Font:")
    widgets.append(widget)
    widget = create("GtkComboBoxText", "GtkComboBoxText_001",
                    460, 12, 164, 30)
    widget.set_prop("items", "|".join(FONTS))
    widget.set_prop("text", FONTS[0])
    widgets.append(widget)
    widget = create("GtkTextView", "GtkTextView_001", 16, 54, 608, 410)
    widget.set_prop("editable", "false")
    widget.set_prop("scroll-bars", "3")
    widget.set_prop("font-name", "Monospace")
    widget.set_prop("font-size", "12")
    widget.set_prop("text", render(DEFAULT_TEXT, FONTS[0]))
    widgets.append(widget)
    return widgets

The output area is created already holding the rendered default banner — that is why the window opens populated. The combo's items are the four font names joined with |, and its text is the first font; the entry's placeholder text is a hint, not content.

Events. LightGUI draws the widgets with Cairo, so there is no native GTK focus to rely on — the app wires it itself. Clicking the entry focuses it (the draw handler paints a blue outline around the focused widget); typing goes to the focused entry through a window-level key-press handler, which appends printable characters, handles BackSpace and Delete, and re-renders on every keystroke. Clicking the font combo cycles to the next font and re-renders. Clicking the output area just drops focus. The dispatch is a small hit-test: the topmost visible widget containing the click gets the event through the _HANDLERS table, and a button release always goes to the widget that got the press, even if the pointer moved off it:

def _on_key_press(window, event):
    # Typing goes to the focused entry (LightGUI draws the entry
    # with Cairo, so there is no native GTK focus to rely on).
    entry = _FOCUSED[0]
    if entry is None:
        return False
    key = Gdk.keyval_name(event.keyval)
    text = entry.get_prop("text", "")
    if key in ("BackSpace", "Delete"):
        entry.set_prop("text", text[:-1])
    elif key == "Escape":
        _FOCUSED[0] = None
    elif event.string and event.string.isprintable():
        entry.set_prop("text", text + event.string)
    else:
        return False
    _render()
    _AREA[0].queue_draw()
    return True

main() is the standard LightGUI Python harness: a Gtk.Window titled banner - util_boxgui holding a Gtk.DrawingArea, with the draw, button-press, button-release and resize signals connected, and Gtk.main() running the loop.

Building and running it

There is nothing to build — it is pure Python. Run it with the LightGUI Python package on PYTHONPATH, under a real X server or under Xvfb (display :100 is the one used for this app). It needs python3-gi / gir1.2-gtk-3.0 and the LightGUI Python checkout:

cd apps/python/banner
PYTHONPATH=/root/lg-review/lightgui_python python3 banner.py

# Under Xvfb:
Xvfb :100 -screen 0 1024x768x24 &
DISPLAY=:100 PYTHONPATH=/root/lg-review/lightgui_python python3 banner.py

The PYTHONPATH above points at the LightGUI Python checkout on the build machine — on your own machine, point it at wherever you cloned LightGUI Python. The window title is banner - util_boxgui.

What the CLI version does differently

Same fonts, same rendering, same edge cases — and the differences are all about the medium. The CLI is banner [-f FONT] TEXT... | banner --list-fonts and prints to stdout; the GUI shows the banner in the output area and never writes to stdout. The CLI errors out on empty text (give text to render); the GUI shows an empty output area instead — the CLI's rendering of "" is five blank lines, and the GUI shows the same blank result without the blank lines. Characters with no glyph (for example @) render as the CLI's block glyph in both. And the interaction model is the GUI's own: the CLI takes its font from a flag, the GUI cycles it on click; the CLI prints once, the GUI re-renders on every keystroke.

See also

Read more

Instrumentation widgets — index

66 widgets in the Instrumentation category. Instrumentation * InstAnalogGauge — Instrument * InstBarMeter — Instrument * InstBeacon — Instrument * InstBezel — Container * InstCircuitBreaker — Toggle * InstControlPanel — Container * InstDigitalReadout — Instrument * InstEmergencyStop — Button * InstFlowMeter — Instrument * InstHorizontalTankLevel — Instrument * InstKeySwitch — Toggle * InstLampTestButton — Button * InstLedBlue — Instrument * InstLedDigits1 — Instrument * InstLed

By Muse

Stock widgets — index

287 widgets in the Stock category. Stock Market * IndADX — Indicator * IndADXR — Indicator * IndALMA — Indicator * IndAPO — Indicator * IndATR — Indicator * IndAcceleratorOscillator — Indicator * IndAccumulationDistributionLine — Indicator * IndAccumulativeSwingIndex — Indicator * IndAdaptiveCyberCycle — Indicator * IndAdaptiveRSI — Indicator * IndAlligatorJaw — Indicator * IndAlligatorLips — Indicator * IndAlligatorTeeth — Indicator * IndAnchoredVWAP — Indicator * IndAroonDown

By Muse