A representation of a virtual "terminal" where we write out the character and style information as we parse through the ANSI encoded stream.

Important: this is not a full-fledged Stream class. You can write to it multiple times, and it will update appropriately, but it does zero buffering or input validation, so writing a partial ANSI code sequence will result in mochibake in the output.

The virtual terminal is an infinitely high and wide screen, with no scrollback buffer. So, when scrollDown(n) is called (either explicitly, or with a \x1b[<n>S ANSI code), n lines are removed from the top of the "screen". When scrollUp(n) is called (explicitly or via a \x1b[<n>T ANSI code), n empty lines are added to the top of the screen; the lines lost to a previous scrollDown action are not restored when scrolling up.

Also, actions that move the cursor down or to the right, which would on a normal physical terminal be limited to the height/width of the terminal, are unbounded. For example, on an actual terminal, echo $'\x1b[1000Bhello' will print "hello" at the bottom of the screen (unless your screen happens to be more than 1000 lines high); in this virtual terminal, it will create 1000 empty lines.

Most of the methods (other than toString() of course) return this, allowing for things like this:

console.log(
new Terminal()
.setStyle({ color: '#ff0000' })
.write('hello, ')
.down(1)
.setStyle({ inverse: true })
.write('world!').ansi
)

Constructors

Accessors

  • get ansi(): string
  • Output the results of the style and character buffers as ANSI styled text. This effectively normalizes all color codes to explicit RGB values and resolves all cursor motions and other control codes, and styling runs of identical styles with a single code.

    Returns string

  • get blocks(): Block[]
  • An array of Block objects each representing a string of text with a given style.

    Returns Block[]

  • get text(): string[]
  • the raw unstyled text of each line

    Returns string[]

Methods

  • Delete all printed data from the screen.

    Note that this is used for both \x1b[2J and \x1b[3J, because there is no scrollback buffer in this virtual terminal.

    Returns Terminal

  • Move to the n-th column (1-indexed), limited by the left-most column.

    Parameters

    • n: number

    Returns Terminal

  • Move to the 1-indexed row and column specified, limited by the top and left sides of the screen.

    Parameters

    • row: number
    • col: number

    Returns Terminal

  • Move to the start of the n-th previous line, stopping at the top of the screen.

    Parameters

    • n: number

    Returns Terminal

  • Remove n lines from the start of the buffer, effectively moving the cursor down as a result.

    Parameters

    • n: number

    Returns Terminal

  • Prepend n empty lines at the start of the buffer, effectively moving the cursor up as a result.

    Parameters

    • n: number

    Returns Terminal

  • Set the style that the terminal will use for text writes.

    If a string, must be a valid \x1b[...m ANSI style code.

    The styles provided will be appended onto the current style in use, just as they would be by a real terminal if the relevant ANSI code is encountered.

    Parameters

    Returns Terminal

  • Called when an OSC code of \x1b]0;...\x1b is encountered.

    Sets the title attribute on the root <pre> in html output, and creates a corresponding OSC title sequence in the ansi output.

    Parameters

    • s: string

    Returns Terminal

  • Output the results of the style and character buffers as a <pre> tag with <span> elements setting effective styles.

    Returns string

  • Parse the ANSI encoded string provided, updating the internal character and style buffers appropriately.

    Parameters

    • input: string

    Returns Terminal