Terminal Kit

repository·master·Indexed 25 days ago

https://github.com/cronvel/terminal-kit

A full-featured Node.js library for creating interactive terminal applications. It provides low-level color and style control (supporting 256 and 24-bit colors), mouse handling, and high-level UI components such as menus, progress bars, and input fields. It features a Document Model for complex interfaces with overlapping widgets and event dispatching, as well as an Inline Mode for traditional sequential output. The library includes advanced screen buffering with 32-bit composition and image loading and does not depend on ncurses.

Tokens
27K
Snippets
20
Records
164
Agent score
85%

What's inside terminal-kit

  1. Overview of Terminal Kit

    master
    Terminal Kit is a comprehensive Node.js library for building terminal applications. It provides features ranging from basic color and style manipulation to complex UI components like input fields, progress bars, and menus. It supports 256 colors, 24-bit colors (if supported by the terminal), mouse handling, and advanced screen buffering with 32-bit composition and image loading. Notably, it does NOT depend on ncurses.
  2. Use high-level Terminal methods for advanced UI features

    master
    The Terminal instance provides high-level methods to build feature-rich terminal applications. You can use these methods to handle keyboard inputs, manage the terminal color palette, create menus, capture text input (similar to readline), display progress bars, or implement special visual effects.
  3. Understand the Document Model vs Inline Mode

    master

    Terminal Kit offers two distinct ways to build interfaces:

    1. Document Model: Uses a portion or the entirety of the terminal area as a 'document' (similar to a webpage). Multiple widgets are present at specific screen locations and are all active simultaneously. This model supports overlapping widgets (like DropDownMenu), independent redraw conditions, keyboard focus management, event dispatching, widget cycling, and full mouse support. It is backed by ScreenBuffer.

    2. Inline Mode: The traditional Terminal Kit approach where widgets are instantiated one at a time, line after line.

    Note: Some widgets exist in both modes but may have different feature sets during the transition period where Document Model widgets are being made compatible with Inline Mode.

  4. Understand the TextBuffer concept

    master

    A TextBuffer is a specialized buffer designed for holding text content, making it suitable for implementing text areas or text editors. It is more flexible than a raw screenBuffer for text-centric tasks.

    Every TextBuffer is backed by a screenBuffer (its dst or destination). Internally, it manages three distinct buffers:

    1. raw text buffer: Holds the actual lines of text.
    2. attr buffer: Holds attributes such as colors and styles.
    3. misc buffer: Holds userland data for application-specific use.
  5. Understand the Container class

    master

    The Container class is the base class for all document model objects that possess their own screenBuffer (such as a Document instance).

    A container provides an inputDst (input destination) which serves as the outputDst (output destination) for all its children. This creates a drawing area for children. By using a rectangular viewport for clipping, the inputDst can be larger than the actual terminal display, allowing for features like virtual enlargement, scrolling, and overflow management.

  6. Understand the ScreenBuffer concept

    master

    A ScreenBuffer is a rectangular area containing cell data. Each cell in the buffer consists of:

    • A character
    • An 8-bit foreground color
    • An 8-bit background color
    • A style (bit flags: bold, dim, italic, underline, blink, inverse, hidden, strike)
    • A blending mask (bit flags for foreground, background, character, and style transparency)

    There are two types of ScreenBuffer based on their write-destination:

    1. Writing directly to the terminal.
    2. Writing to another ScreenBuffer (layering).

    Best Practice: For complex UIs with many moving elements, create one large ScreenBuffer that maps the entire terminal, then create smaller ScreenBuffer instances for specific UI components (widgets, sprites, or moving areas) that write into the main terminal buffer.

  7. Optimize terminal writes with ScreenBuffer delta updates

    master
    When writing a ScreenBuffer directly to the terminal, you can optimize performance by using the delta option in the .draw() method. This prevents the CPU-intensive task of refreshing the entire screen by only writing cells that have changed. The ScreenBuffer will automatically minimize the number of terminal escape sequences required to produce the new frame.
  8. Use Regular Markup for Text Styling

    master

    Terminal-kit supports text styling via markup sequences starting with a caret ^. Regular markup uses a single character following the caret to apply colors or text attributes. These sequences can be inserted directly into text content passed to most Terminal-kit functions.

    Attributes

    • ^-: Dim
    • ^+: Bold
    • ^_: Underline
    • ^/: Italic
    • ^!: Inverse

    Foreground Colors

    • ^k: Black
    • ^r: Red
    • ^g: Green
    • ^y: Yellow/Brown
    • ^b: Blue
    • ^m: Magenta
    • ^c: Cyan
    • ^w: White
    • ^K: Gray
    • ^R: Bright Red
    • ^G: Bright Green
    • ^Y: Bright Yellow
    • ^B: Bright Blue
    • ^M: Bright Magenta
    • ^C: Bright Cyan
    • ^W: Bright White

    Special Sequences

    • ^^: Output a literal caret ^
    • ^:: Reset style (default color/background/attributes)
    • ^ : Reset style and output a space
    • ^;: Special style reset (also resets attributes forced by a document-model's widget)
    • ^#: Shift mode (the next markup sequence will apply to the background instead of the foreground)
    "This is ^Ggreen^ and this is ^Rred^ !"
  9. Use chainable low-level methods on Terminal instances

    master

    The Terminal instance provides low-level methods for controlling colors, styles, and cursor positioning. These methods are chainable and support three primary usage patterns:

    1. Toggle Mode: Passing a boolean turns a feature on or off for subsequent output.
      • term.red(true): Turns on red for all following output.
      • term.red(false): Disables red and returns to default.
    2. Direct Output: Passing a string as an argument turns the feature on, outputs the string, and then turns the feature off.
      • term.red('Hello') is equivalent to term.red(true); term('Hello'); term.red(false);.
    3. Style Mixing: Chain multiple properties together to combine styles and colors.

    Note: Some methods like term.reset() perform an action and cannot be toggled off.

  10. Access the default terminal instance

    master

    To use the library, you can require the main package and access the .terminal property. This instance (term) is an instanceof termkit.Terminal and is configured to match your current terminal environment.

    By convention in this documentation:

    • termkit refers to require('terminal-kit')
    • term refers to termkit.terminal