Class TermGrid

Represents the terminal as a 2D grid with 64 colors. Each cell has a foreground color fg, a background color bg and a unicode text character. Create an instance with the makeTermGrid factory function.

Typical usage might have a setup, a core function, and a shutdown.

Setup: the setup would call clear() to clean up the screen. Then attach an input event handler function with the onInput method. The handler should either be the core function or call the core function.

Core function: called for each input/other event. Example:

  • Interpret event
  • Apply business logic
  • Update grid with several calls to set() and/or text() methods
  • Call draw() to display the new grid state on the terminal

Shutdown: call reset() to return the terminal to normal.

A fg or bg color is a 6-bit RGB color from a 4x4x4 RGB color cube. A value in the range [0-63] inclusive. The bits in the number are RRGGBB. In other words, 2 bits per color component; in order red, green, then blue. For example if the color is 0b011011, then red is 1, green is 2 and blue is 3.

Hierarchy

  • TermGrid

Implements

Constructors

Methods

Constructors

  • For internal unit testing only. Use makeTermGrid factory function instead.

    Parameters

    • height: number
    • width: number
    • tty: ReadStream
    • printer: Printer

    Returns TermGrid

Methods

  • Set the background color of a cell in the grid. You must call draw() to see the change in the terminal.

    Parameters

    • y: number

      0-based row index into grid

    • x: number

      0-based column index into grid

    • bg: number

      6-bit RGB background color to set for the cell. Must be in range [0-63] inclusive.

    Returns void

  • Clears the screen with the current background color. Literally prints "\u001b[2J".

    Returns void

  • Draw the current state of the grid to the terminal.

    Returns void

  • Set the foreground color of a cell in the grid. You must call draw() to see the change in the terminal.

    Parameters

    • y: number

      0-based row index into grid

    • x: number

      0-based column index into grid

    • fg: number

      6-bit RGB foreground color to set for the cell. Must be in range [0-63] inclusive.

    Returns void

  • Whenever the terminal receives user keystrokes, calls handler passing in the utf-8 encoded string representing the key strokes

    Parameters

    • handler: ((data: string) => void)
        • (data: string): void
        • Parameters

          • data: string

          Returns void

    Returns void

  • Reset colors and re-enable the cursor. Literally prints "\u001b[0m\u001B[?25h"

    Returns void

  • Set a cell in the grid. You must call draw() to see the change in the terminal.

    Parameters

    • y: number

      0-based row index into grid

    • x: number

      0-based column index into grid

    • c: string

      character to set in cell

    • fg: number

      6-bit RGB foreground color to set for the cell. Must be in range [0-63] inclusive.

    • bg: number

      6-bit RGB background color to set for the cell. Must be in range [0-63] inclusive.

    Returns void

  • Set a sequence of cells of a row in the grid. Effects n cells where n is the length of text. You must call draw() to see the change in the terminal.

    Parameters

    • y: number

      0-based row index into grid

    • x: number

      0-based column index into grid

    • text: string

      text to write in row y starting in column x

    • fg: number

      6-bit foreground color to set to each cell for the text. Must be in range [0-63] inclusive.

    • bg: number

      6-bit background color to set to each cell. Must be in range [0-63] inclusive.

    Returns void

Generated using TypeDoc