First 3 of the 5 pilot apps, all built and screenshot-tested on hot_dallas: - banner (Python): text + font combo + scrolling output, 12/12 GUI-vs-CLI - bigfind (C++): top-N largest files table, exact CLI match - emptybig (C++): sparse-file finder, exact CLI match on fixture Top-level README indexes all 50 drawn apps; LICENSE is MIT (util_box itself is unlicensed; MIT matches lzply and lightgui). datecalc and certcheck are blocked pending execution-risk approval.
345 lines
13 KiB
Python
345 lines
13 KiB
Python
#!/usr/bin/env python3
|
|
# banner - util_boxgui: ASCII banner text with several built-in fonts.
|
|
# LightGUI (Python/XFrame) port of the util_box `banner` CLI utility
|
|
# (/root/fileutils/bin/banner): same fonts, same rendering, same
|
|
# inputs/outputs -- the font table and render() below are vendored
|
|
# verbatim from that script so the GUI can never drift from the CLI.
|
|
#
|
|
# Layout (640x480): text entry + font combo on top, scrolling output
|
|
# area below showing the rendered banner. Text is pre-filled with
|
|
# "Hello" and the first font (standard) is selected, so the banner is
|
|
# already rendered on launch. Typing in the entry or picking another
|
|
# font re-renders live.
|
|
#
|
|
# Run with:
|
|
# PYTHONPATH=/root/lg-review/lightgui_python python3 banner.py
|
|
#
|
|
# Modelled on 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 _HANDLERS. LightGUI draws the widgets with
|
|
# Cairo; it does not give them real GTK keyboard focus, so this app
|
|
# wires focus + typing itself: click the entry to focus it (blue
|
|
# outline), type to edit, BackSpace deletes; click the font combo to
|
|
# cycle to the next font.
|
|
|
|
import sys
|
|
|
|
import gi
|
|
|
|
gi.require_version("Gtk", "3.0")
|
|
gi.require_version("Gdk", "3.0")
|
|
|
|
from gi.repository import Gdk, Gtk # noqa: E402
|
|
from lightgui.xframe.core import create, parse_color # noqa: E402
|
|
|
|
FONTS = ["standard", "small", "big", "slant"]
|
|
DEFAULT_TEXT = "Hello"
|
|
|
|
# --- vendored from util_box `banner` (font table + render logic) -------
|
|
FONT = {
|
|
'A':(" ### ","# #","#####","# #","# #"),'B':("#### ","# #","#### ","# #","#### "),
|
|
'C':(" ####","# ","# ","# "," ####"),'D':("#### ","# #","# #","# #","#### "),
|
|
'E':("#####","# ","#### ","# ","#####"),'F':("#####","# ","#### ","# ","# "),
|
|
'G':(" ####","# ","# ##","# #"," ### "),'H':("# #","# #","#####","# #","# #"),
|
|
'I':("#####"," # "," # "," # ","#####"),'J':(" ###"," # "," # ","# # "," ### "),
|
|
'K':("# #","# # ","### ","# # ","# #"),'L':("# ","# ","# ","# ","#####"),
|
|
'M':("# #","## ##","# # #","# #","# #"),'N':("# #","## #","# # #","# ##","# #"),
|
|
'O':(" ### ","# #","# #","# #"," ### "),'P':("#### ","# #","#### ","# ","# "),
|
|
'Q':(" ### ","# #","# #","# # "," ## #"),'R':("#### ","# #","#### ","# # ","# #"),
|
|
'S':(" ####","# "," ### "," #","#### "),'T':("#####"," # "," # "," # "," # "),
|
|
'U':("# #","# #","# #","# #"," ### "),'V':("# #","# #","# #"," # # "," # "),
|
|
'W':("# #","# #","# # #","## ##","# #"),'X':("# #"," # # "," # "," # # ","# #"),
|
|
'Y':("# #"," # # "," # "," # "," # "),'Z':("#####"," # "," # "," # ","#####"),
|
|
'0':(" ### ","# ##","# # #","## #"," ### "),'1':(" # "," ## "," # "," # ","#####"),
|
|
'2':("#### "," #"," ### ","# ","#####"),'3':("#### "," #"," ### "," #","#### "),
|
|
'4':("# #","# #","#####"," #"," #"),'5':("#####","# ","#### "," #","#### "),
|
|
'6':(" ####","# ","#### ","# #"," ### "),'7':("#####"," #"," # "," # "," # "),
|
|
'8':(" ### ","# #"," ### ","# #"," ### "),'9':(" ### ","# #"," ####"," #"," ### "),
|
|
' ':(" "," "," "," "," "),'.':(" "," "," "," "," # "),
|
|
'!':(" # "," # "," # "," "," # "),'?':("#### "," #"," ## "," "," # "),
|
|
'-':(" "," ","#####"," "," "),'_':(" "," "," "," ","#####"),
|
|
"'":(" # "," # "," "," "," "),',':(" "," "," "," # "," # "),
|
|
':':(" "," # "," "," # "," "),
|
|
}
|
|
UNKNOWN = ("#####","# # #","#####","# # #","#####")
|
|
|
|
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)
|
|
# --- end vendored --------------------------------------------------------
|
|
|
|
|
|
def _current_font():
|
|
"""Font name currently shown in the combo box."""
|
|
return _FONT_COMBO.get_prop("text", FONTS[0]) or FONTS[0]
|
|
|
|
|
|
def _render():
|
|
"""Re-render the banner from the entry text + selected font."""
|
|
text = _TEXT_ENTRY.get_prop("text", "")
|
|
font = _current_font()
|
|
if font not in FONTS:
|
|
font = FONTS[0]
|
|
# The CLI refuses empty input (argparse error); the GUI shows an
|
|
# empty output area instead -- same visible result as the CLI
|
|
# printing five blank lines, minus the blank lines.
|
|
_OUTPUT.set_prop("text", render(text, font) if text else "")
|
|
|
|
|
|
def on_GtkEntry_001_OnClick(widget):
|
|
# Click focuses the entry so typing goes to it.
|
|
_FOCUSED[0] = widget
|
|
_AREA[0].queue_draw()
|
|
|
|
|
|
def on_GtkComboBoxText_001_OnClick(widget):
|
|
# Click cycles to the next font (LightGUI draws the combo; the
|
|
# drop-down list is not a live GTK popup, so cycling is the
|
|
# interaction) and re-renders live.
|
|
cur = _current_font()
|
|
nxt = FONTS[(FONTS.index(cur) + 1) % len(FONTS)] if cur in FONTS \
|
|
else FONTS[0]
|
|
widget.set_prop("text", nxt)
|
|
_FOCUSED[0] = None
|
|
_render()
|
|
_AREA[0].queue_draw()
|
|
|
|
|
|
def on_GtkTextView_001_OnClick(widget):
|
|
# Output area is read-only; clicking it just drops entry focus.
|
|
_FOCUSED[0] = None
|
|
_AREA[0].queue_draw()
|
|
|
|
|
|
# Event handlers, wired to the widgets in main():
|
|
_HANDLERS = {
|
|
("GtkEntry_001", "OnClick"): on_GtkEntry_001_OnClick,
|
|
("GtkComboBoxText_001", "OnClick"): on_GtkComboBoxText_001_OnClick,
|
|
("GtkTextView_001", "OnClick"): on_GtkTextView_001_OnClick,
|
|
}
|
|
|
|
|
|
# Design-time geometry + effective anchors per widget:
|
|
# name -> (top, left, right, bottom, x, y, width, height)
|
|
_ANCHORS = {
|
|
"GtkLabel_001": (True, True, False, False, 16, 12, 44, 30),
|
|
"GtkEntry_001": (True, True, True, False, 64, 12, 340, 30),
|
|
"GtkLabel_002": (True, False, True, False, 416, 12, 40, 30),
|
|
"GtkComboBoxText_001": (True, False, True, False, 460, 12, 164, 30),
|
|
"GtkTextView_001": (True, True, True, True, 16, 54, 608, 410),
|
|
}
|
|
|
|
|
|
def _apply_anchors(widgets, width, height):
|
|
"""Reposition anchored widgets for a drawing area of
|
|
*width* x *height* (Flag 1: minimal anchor layout).
|
|
Computed from the design geometry every time, so a
|
|
resize is idempotent -- no drift across resizes."""
|
|
design_w, design_h = _DESIGN_SIZE
|
|
dx = width - design_w
|
|
dy = height - design_h
|
|
for widget in widgets:
|
|
top, left, right, bottom, ox, oy, ow, oh = \
|
|
_ANCHORS[widget.name]
|
|
x, y, w, h = ox, oy, ow, oh
|
|
if right and not left:
|
|
x = ox + dx
|
|
elif right and left:
|
|
w = ow + dx
|
|
if bottom and not top:
|
|
y = oy + dy
|
|
elif bottom and top:
|
|
h = oh + dy
|
|
widget.move_to(x, y)
|
|
widget.resize_to(w, h)
|
|
|
|
|
|
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
|
|
|
|
|
|
_DESIGN_SIZE = (640, 480)
|
|
_BACKGROUND = ""
|
|
_WIDGETS = []
|
|
_LAST_SIZE = [None]
|
|
_PRESSED = [None] # widget that got the ButtonPress
|
|
_FOCUSED = [None] # entry widget receiving typed keys, or None
|
|
_AREA = [None] # the Gtk.DrawingArea, set in main()
|
|
_TEXT_ENTRY = None
|
|
_FONT_COMBO = None
|
|
_OUTPUT = None
|
|
|
|
|
|
def _hit_widget(widgets, x, y):
|
|
"""Topmost visible widget containing (*x*, *y*)."""
|
|
for widget in reversed(widgets):
|
|
if widget.get_prop("visible", "true") in ("false", "0"):
|
|
continue
|
|
if widget.contains(x, y):
|
|
return widget
|
|
return None
|
|
|
|
|
|
def _dispatch_to(widget, event):
|
|
"""Call the *event* handler of *widget* (already chosen)."""
|
|
if widget is None:
|
|
return
|
|
if widget.get_prop("enabled", "true") in ("false", "0"):
|
|
return
|
|
handler = _HANDLERS.get((widget.name, event))
|
|
if handler is not None:
|
|
handler(widget)
|
|
|
|
|
|
def _dispatch(widgets, x, y, event):
|
|
"""Call the *event* handler of the widget at (*x*, *y*),
|
|
if it has one wired."""
|
|
_dispatch_to(_hit_widget(widgets, x, y), event)
|
|
|
|
|
|
def _on_draw(area, cr):
|
|
bg = parse_color(_BACKGROUND, None)
|
|
if bg is None:
|
|
bg = (1.0, 1.0, 1.0)
|
|
cr.set_source_rgb(*bg)
|
|
cr.paint()
|
|
for widget in _WIDGETS:
|
|
widget.draw(cr)
|
|
# Focus outline around the entry receiving typed keys.
|
|
focused = _FOCUSED[0]
|
|
if focused is not None:
|
|
cr.save()
|
|
cr.set_source_rgb(0.2, 0.45, 0.85)
|
|
cr.set_line_width(2.0)
|
|
cr.rectangle(focused.x - 1, focused.y - 1,
|
|
focused.width + 2, focused.height + 2)
|
|
cr.stroke()
|
|
cr.restore()
|
|
return False
|
|
|
|
|
|
def _on_button_press(area, event):
|
|
if event.type == Gdk.EventType.DOUBLE_BUTTON_PRESS:
|
|
_dispatch(_WIDGETS, event.x, event.y, "OnDblClick")
|
|
elif event.type == Gdk.EventType.BUTTON_PRESS:
|
|
_dispatch(_WIDGETS, event.x, event.y, "OnMouseDown")
|
|
_dispatch(_WIDGETS, event.x, event.y, "OnClick")
|
|
# Task 4: remember the pressed widget; release goes there.
|
|
_PRESSED[0] = _hit_widget(_WIDGETS, event.x, event.y)
|
|
return False
|
|
|
|
|
|
def _on_button_release(area, event):
|
|
# Task 4: release goes to the widget that got the press,
|
|
# even if the pointer moved off it.
|
|
widget = _PRESSED[0]
|
|
_PRESSED[0] = None
|
|
_dispatch_to(widget, "OnMouseUp")
|
|
return False
|
|
|
|
|
|
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
|
|
|
|
|
|
def _on_area_resize(area, allocation):
|
|
size = (allocation.width, allocation.height)
|
|
if size == _LAST_SIZE[0]:
|
|
return False
|
|
_LAST_SIZE[0] = size
|
|
_apply_anchors(_WIDGETS, size[0], size[1])
|
|
area.queue_draw()
|
|
return False
|
|
|
|
|
|
def main():
|
|
global _TEXT_ENTRY, _FONT_COMBO, _OUTPUT
|
|
window = Gtk.Window(title="banner - util_boxgui")
|
|
window.set_default_size(640, 480)
|
|
window.connect("destroy", Gtk.main_quit)
|
|
window.connect("key-press-event", _on_key_press)
|
|
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL,
|
|
spacing=0)
|
|
window.add(vbox)
|
|
area = Gtk.DrawingArea()
|
|
area.set_hexpand(True)
|
|
area.set_vexpand(True)
|
|
area.add_events(Gdk.EventMask.BUTTON_PRESS_MASK |
|
|
Gdk.EventMask.BUTTON_RELEASE_MASK)
|
|
area.connect("draw", _on_draw)
|
|
area.connect("button-press-event", _on_button_press)
|
|
area.connect("button-release-event", _on_button_release)
|
|
area.connect("size-allocate", _on_area_resize)
|
|
vbox.pack_start(area, True, True, 0)
|
|
_AREA[0] = area
|
|
_WIDGETS.extend(_build_widgets())
|
|
by_name = {w.name: w for w in _WIDGETS}
|
|
_TEXT_ENTRY = by_name["GtkEntry_001"]
|
|
_FONT_COMBO = by_name["GtkComboBoxText_001"]
|
|
_OUTPUT = by_name["GtkTextView_001"]
|
|
window.show_all()
|
|
Gtk.main()
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|