$ show learnbyexample/TUI-apps
This repository is a collection of over a dozen Terminal User Interface (TUI) applications built with the Textual Python framework. The apps serve educational and recreational purposes: interactive exercises for learning CLI tools (awk, sed, grep), Python regular expressions, Python programming fundamentals, and two original grid-based games (Connect Square, Square Tic Tac Toe). Each app is self-contained, with a similar architectural pattern: an App subclass defines widgets, binds keys, and handles user input. The exercise apps execute user commands via subprocess and compare output to expected results; the regex playground and Python exercises evaluate Python expressions in a sandbox; the games implement AI opponents using weight-based heuristics. The code is clean, well-structured, and consistently styled, though the exercise apps share a large amount of boilerplate. The repository is actively maintained (1015 stars, 0 open issues, recent commits) and demonstrates solid craftsmanship, particularly in the game AI algorithms and custom Textual widget subclasses.
The LLM identified 2 files worth highlighting for reuse:
The `SmartTextArea` class (lines ~18-40) extends Textual's TextArea with smart indentation: on Enter, it replicates the previous line's indentation and adds an extra level after block-starting keywords (def, for, if, etc.). It also supports duplicate line (Ctrl+D) and open new line (Ctrl+O). This component is self-contained, provides real UX improvement, and could be extracted and reused in any Textual app that needs a code editor.
The `generate_winning_moves` method (lines ~36-68) and the `update_weights` / `ai_response` / `choose_ai_move` methods (lines ~146-200) implement a non-trivial AI for a Connect Four variant where a win can be a line of 4 or a square of 4 cells. The algorithm precomputes all possible winning lines and squares, assigns weights to each empty cell based on current board sums, and selects moves with priority (first win, then block, then weight-based). The square generation logic (including rotated and skewed squares) is a reusable pattern for any grid-based game needing all possible 4-cell squares.