Skip to main content

Curses based boxes, menus, loggers

Project description

libcurses

Framework and tools for multi-threaded, curses(3)-based, terminal applications.

  • Write to screen from multiple threads.

    • Use libcurses.wrapper instead of curses.wrapper.
    • Use libcurses.getkey instead of curses.getch.
    • Use libcurses.getline instead of curses.getstr.
    • Preserve the cursor with context manager libcurses.preserve_cursor.
  • Register callbacks with register_fkey to handle function-keys pressed during getkey and getline processing.

  • Register callbacks with add_mouse_handler to handle mouse events during getkey and getline processing.

  • Manage a logger destination, LogSink, to write to a curses window.

  • A Grid framework.

class Grid

Grid of windows.

A rectangular collection of windows with shared (collapsed) borders that resize the windows to either side (syncronized shrink/expand) when moused upon.

    +-------+---+------+    example `Grid`, 9 windows.
    |       |   |      |
    +-------+---+------+
    |           |      |
    +------+----+------+
    |      |           |
    +------+--+--------+
    |         |        |
    +---------+--------+

Drag and drop an interior border to resize the windows on either side.

Double-click an interior border to enter Resize Mode: * scroll-wheel and arrow-keys move the border, and * click anywhere, Enter and Esc to exit Resize Mode.

Grids also provide a wrapper around curses.newwin that takes positioning parameters that describe the spatial-relationship to other windows on the screen, instead of (y,x) coordinates:

      +--------+                 +--------+
      |        |                 |  ^     |
      |        |<------ left2r --|  |     |
      |        |                 |  |     |
      |<---------------- left ---|  |     |
      |        |                 |  |     |
      +--------+                 +--|-----+
         |  |                       |  ^
bottom2t |  | bottom            top |  | top2b
         v  |                       |  |
      +-----|--+                 +--------+
      |     |  |                 |        |
      |     |  |-- right ---------------->|
      |     |  |                 |        |
      |     |  |-- right2l ----->|        |
      |     v  |                 |        |
      +--------+                 +--------+

For example, this 3x13 grid with three 3x5 boxes may be described at least three different ways:

        +---+---+---+
        | a | b | c |
        +---+---+---+

grid = Grid(curses.newwin(3, 13))

1)  a = grid.box('a', 3, 5)
    b = grid.box('b', 3, 5, left2r=a)
    c = grid.box('c', 3, 5, left2r=b)

2)  c = grid.box('c', 3, 5, right=grid)
    b = grid.box('b', 3, 5, right=c)
    a = grid.box('a', 3, 5, right=b)

3)  a = grid.box('a', 3, 5, left=grid)
    c = grid.box('c', 3, 5, right=grid)
    b = grid.box('b', 3, 0, left2r=a, right=c)

If two endpoints are given (such as 3b), the length will be calculated to fill the gap between the endpoints.

class LogSink

Logger sink to curses window.

The LogSink class provides a logger destination that writes log messages to a curses window, and methods that control various logging features.

class MouseEvent

Wrap curses.getmouse with additional, convenience-properties.

MouseEvent encapsulates the results of curses.getmouse,

x               x-coordinate.
y               y-coordinate.
bstate          bitmask describing the type of event.

and provides these additional properties:

button          button number (1-5).
nclicks         number of clicks (1-3).
is_pressed      True if button is pressed.
is_released     True if button was just released.
is_alt          True if Alt key is held.
is_ctrl         True if Ctrl key is held.
is_shift        True if Shift key is held.
is_moving       True if mouse is moving.

method add_mouse_handler

Call func with args when mouse event happens at (y, x).

method clear_mouse_handlers

Remove all mouse handlers.

function get_colormap

Return map of loguru-level-name to curses-color/attr.

Call after creating all custom levels with logger.level(). Map is build once and cached; repeated calls return same map.

function getkey

Read and return a character from window.

Args: win: curses window to read from. no_mouse: ignore mouse events (for internal use).

Return: -1 when no-input in no-delay mode, or None on end of file, or >=0 int character read.

function getline

Read and return a line of input from window.

A line is terminated with CR, LF or KEY_ENTER. Backspace deletes the previous character. NAK (ctrl-U) kills the line. Mouse events are handled.

function preserve_cursor

Context manager to save and restore the cursor.

function register_fkey

Register func to be called when key is pressed.

Args: func: callable, to be called on receipt of key. key: the key to be captured, e.g., curses.KEY_F1, or zero (0) for all keys.

func is appended to a list for the key; pass func=None to remove list of funcs for key from registry.

function wrapper

Use instead of curses.wrapper.

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

rlane_libcurses-1.0.10.tar.gz (52.1 kB view details)

Uploaded Source

Built Distribution

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

rlane_libcurses-1.0.10-py3-none-any.whl (25.1 kB view details)

Uploaded Python 3

File details

Details for the file rlane_libcurses-1.0.10.tar.gz.

File metadata

  • Download URL: rlane_libcurses-1.0.10.tar.gz
  • Upload date:
  • Size: 52.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.13.0

File hashes

Hashes for rlane_libcurses-1.0.10.tar.gz
Algorithm Hash digest
SHA256 f14d37b8cecf94625709aeee351d334090c7bd26b4ed0bbd9fb57ba2cb3031e1
MD5 3238e334b8462e385ecb18236b51b1df
BLAKE2b-256 27e36c49de251ba7f83c936d1e27731e3fd33d2cfd7d0028bdeb3e3c8083bab2

See more details on using hashes here.

File details

Details for the file rlane_libcurses-1.0.10-py3-none-any.whl.

File metadata

File hashes

Hashes for rlane_libcurses-1.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 0df9af5148382ee76cda3518305d5127ce344675e22c8f3012bd45e704b25a9f
MD5 df2cb43c9d89651ab7034fa92aa8f0b8
BLAKE2b-256 c70a1cc5e4856acfc39174dd8d42611845a43341b7ba0927af494b0f6f87ad02

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