Skip to main content

A robust curses wrapper for creating Text User Interfaces (TUIs).

Project description

console-window

A robust and feature-rich wrapper for the standard Python curses library, simplifying the creation of complex, full-screen Text User Interface (TUI) applications.

This package abstracts away the complexities of managing curses pads, dynamic screen resizing, custom scroll/pick synchronization, and keyboard handling, allowing developers to focus purely on content.

Installation

Install the package via pip:

pip install console-window

Core Features (The "Secret Sauce")

The wrapper handles several advanced terminal interaction patterns automatically:

  • Padded Content Management: Uses separate head and body pads, allowing for a static header area while the main content area can be scrolled independently.
  • Seamless Resizing: Automatically detects terminal size changes (KEY_RESIZE) and recalculates layout dimensions, with robust error handling to prevent common curses.error crashes during resizing.
  • Scroll & Pick Synchronization: The Window class manages scroll position and a separate pick position (highlighted row) simultaneously.
    • When using pick mode, scrolling automatically tracks the highlighted element to keep it visible in .pick_pos.
    • To use that value, you must separately be able to associate .pick_pos to its value by, say, keeping an array of values for every added line to the body.
  • Intuitive Navigation: Provides built-in handling for common navigation keys (j/k, UP/DOWN, Page Up/Down, H/M/L for home/middle/last viewable line, etc.).
  • Dynamic Options Management (OptionSpinner): The companion class simplifies creation of application settings, allowing users to toggle options or input strings using single keypresses (e.g., p to toggle pick mode).
  • Blocking Popups: Includes fully implemented, blocking pop-up methods for user input (.answer()) and alerts (.alert()), handling the necessary temporary screen takeover and cursor management.

Minimal Working Example

This example demonstrates setting up the window, using the OptionSpinner for control, and running the main render/prompt loop.

import curses
from console_window import ConsoleWindow, ConsoleWindowOpts, OptionSpinner

def main_app_loop(stdscr):
    # 1. Setup Options Manager
    spin = OptionSpinner()
    opts = spin.default_obj  # Access options via this namespace

    # Add features controlled by keypresses
    spin.add_key('help_mode', '? - toggle help screen', vals=[False, True])
    spin.add_key('pick_mode', 'p - toggle pick mode', vals=[False, True])
    spin.add_key('pick_size', 's - #rows in pick', vals=[1, 2, 3])
    spin.add_key('name', 'n - select name', prompt='Provide Your Name:')
    spin.add_key('quit', 'q,Q - quit the app', category='action', keys={ord('Q'), ord('q')})

    # 2. Initialize Window with ConsoleWindowOpts
    win_opts = ConsoleWindowOpts(
        head_line=True,
        keys=spin.keys,
        min_cols_rows=(60, 20),
        single_cell_scroll_indicator=True
    )
    win = ConsoleWindow(opts=win_opts)
    opts.name = "[hit 'n' to enter name]"
    loop_count = 0

    while True:
        loop_count += 1
        
        # 3. Add Content
        if opts.help_mode:
            win.set_pick_mode(False)
            spin.show_help_nav_keys(win)
            spin.show_help_body(win)
        else:
            win.set_pick_mode(opts.pick_mode, opts.pick_size)
            win.add_header(f'TUI App Header | Loop: {loop_count} | Name: "{opts.name}"')
            
            # Add body content (scrollable)
            for idx in range(100):
                win.add_body(f'Main Item {idx+1}')

        # 4. Render and Prompt for Input
        win.render()
        key = win.prompt(seconds=0.5)  # Wait for half a second or a keypress

        # 5. Handle Keys (App-specific logic)
        if key is not None:
            # Check if OptionSpinner can handle the key (p, s, n, ?)
            spin.do_key(key, win)
            
            if opts.quit:
                opts.quit = False
                break  # Exit the loop
        
        win.clear()

if __name__ == '__main__':
    try:
        # Note: Window.__init__ starts curses and registers atexit cleanup
        curses.wrapper(main_app_loop)
    except KeyboardInterrupt:
        pass

License

MIT

Projects Using console-window

For more extensive examples, you can look at some projects using console-window:

  • efibootdude
    • simple wrapper for efibootmgr
    • one of the smaller projects
    • demonstrates a context sensitive available keys lines (i.e., first line) where the actions depend on current line.
  • dwipe
    • disk cleaner with parallel jobs and stamps wiped partitions
  • pmemstat
    • system/process memory monitor with rollups and hooks for zRAM
  • memfo
    • viewer of /proc/meminfo that shows historical values
  • vappman
    • convenince wrapper to mostly hide CLI of AppMan, a life-cycle manager for 2000+ AppImages
  • my-snaps
    • special purpose BTRFS snap manager

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

console_window-1.1.3.tar.gz (24.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

console_window-1.1.3-py3-none-any.whl (21.2 kB view details)

Uploaded Python 3

File details

Details for the file console_window-1.1.3.tar.gz.

File metadata

  • Download URL: console_window-1.1.3.tar.gz
  • Upload date:
  • Size: 24.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.31.0

File hashes

Hashes for console_window-1.1.3.tar.gz
Algorithm Hash digest
SHA256 2826482022c1a3ca9e3d1e2cefdf32756d659e605af3961285941465f70a961c
MD5 5fe1c3700ecb5a41cf033aaacaf03963
BLAKE2b-256 d88bc513cc230172e6454b51309c18d7c55bce5236e35090cc6178bfb3dab6d6

See more details on using hashes here.

File details

Details for the file console_window-1.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for console_window-1.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c3c53b4c04e1a2948a99d85375e9fecfd5c65dc5cfd867cfaa9ebe3bec58880e
MD5 07ad5d65e81c43c91619d9bfdf130ea4
BLAKE2b-256 3bcda9836356c49c16754c0da994670ad9f164a1730393fafe0be98bb0198111

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page