Module system.terminal

The terminal module defines functions to allow interacting with the terminal and screen, as well as handling user input.

Index

Data

colors

Constants for colors. This includes both normal and British spelling.

colours

getSize

Function

capture()

Captures input on the current stdin TTY, bringing the process to the front.

istty()

Returns whether the current stdio are linked to a TTY.

mktty()

Creates a new virtual TTY with the specified size. This can later be used in a call to stdin/stdout/stderr.

opengfx()

Opens the current output TTY in exclusive graphics mode, allowing direct manipulation of the pixels if available. Only one process may open the terminal at a time. Once opened, the screen will be cleared, and stdout will be sent to an off-screen buffer to be shown once the terminal is closed. The terminal will automatically be closed on process exit. This only works on CraftOS-PC.

openterm()

Opens the current output TTY in exclusive text mode, allowing direct manipulation of the screen buffer. Only one process may open the terminal at a time. Once opened, the screen will be cleared, and stdout will be sent to an off-screen buffer to be shown once the terminal is closed. The terminal will automatically be closed on process exit.

read()

Reads a number of characters from the standard input stream.

readline()

Reads a single line of text from the standard input stream.

readline2()

Reads a line of text from the standard input stream, allowing history and autocompletion.

release()

Releases a previously captured input on the current stdin TTY.

stderr()

Sets the standard error of the current process.

stdin()

Sets the standard input of the current process.

stdout()

Sets the standard output of the current process.

termctl()

Sets certain terminal control flags on the current TTY if available.

termsize()

Returns the current size of the TTY if available.

toEscape()

Converts a terminal.colors constant to an ANSI escape code.

write()

Writes text to the standard output stream.

writeerr()

Writes text to the standard error stream.

Class

GFXTerminal

The GFXTerminal type allows interfacing with the screen in exclusive graphics mode. It provides the same functions as CraftOS-PC does in mode 2, and can be used with minimal conversion.

Terminal

The Terminal type allows interfacing with the screen in exclusive text mode. It provides the same functions as CraftOS does (with some minor differences), and can be used with minimal conversion.

TTY

API Reference

system.terminal.colors: table

Constants for colors. This includes both normal and British spelling.

system.terminal.colours: table
system.terminal.toEscape(color: number, background?: boolean): (result: string)

Converts a terminal.colors constant to an ANSI escape code.

Parameters:
  • color (number) – The color to convert

  • background? (boolean) – Whether the escape should set the background (defaults to false)

Returns:

result (string) – The escape code generated for the color

system.terminal.write(...: any): unknown

Writes text to the standard output stream.

Parameters:

... (any) – The entries to write. Each one will be separated by tabs (\t).

system.terminal.writeerr(...: any): unknown

Writes text to the standard error stream.

Parameters:

... (any) – The entries to write. Each one will be separated by tabs (\t).

system.terminal.read(n: number): (result: string | nil)

Reads a number of characters from the standard input stream.

Parameters:

n (number) – The number of characters to read

Returns:

result (string | nil) – The text read, or nil if EOF was reached.

system.terminal.readline(): (result: string | nil)

Reads a single line of text from the standard input stream.

Returns:

result (string | nil) – The text read, or nil if EOF was reached.

system.terminal.readline2(
    history?: table,
    completion?: fun(partial: string): string[]
): (result: string | nil)

Reads a line of text from the standard input stream, allowing history and autocompletion.

arrow keys, with the first index being the most recent

to get completion options

Parameters:
  • history? (table) – A list of history items to scroll through with the

  • completion? (fun(partial: string): string[]) – A function to use

Returns:

result (string | nil) – The text read, or nil if EOF was reached.

system.terminal.termctl(
    flags?: {cbreak: boolean, delay: boolean, echo: boolean, keypad: boolean, nlcr: boolean, raw: boolean}
): (
    result: {cbreak: boolean, delay: boolean, echo: boolean, keypad: boolean, nlcr: boolean, raw: boolean} | nil
)

Sets certain terminal control flags on the current TTY if available.

Parameters:

flags? ({cbreak: boolean, delay: boolean, echo: boolean, keypad: boolean, nlcr: boolean, raw: boolean}) – The flags to set, or nil to just query.

Returns:

result ({cbreak: boolean, delay: boolean, echo: boolean, keypad: boolean, nlcr: boolean, raw: boolean} | nil) – The flags that are currently set on the TTY, or nil if no TTY is available.

system.terminal.openterm(): (term: system.terminal.Terminal | nil, err: string | nil)

Opens the current output TTY in exclusive text mode, allowing direct manipulation of the screen buffer. Only one process may open the terminal at a time. Once opened, the screen will be cleared, and stdout will be sent to an off-screen buffer to be shown once the terminal is closed. The terminal will automatically be closed on process exit.

Returns:
  • term (system.terminal.Terminal | nil) – A terminal object for the current TTY, or nil if the terminal could not be opened.

  • err (string | nil) – An error message describing why the terminal couldn’t be opened.

system.terminal.opengfx(): (
    term: system.terminal.GFXTerminal | nil,
    err: string | nil
)

Opens the current output TTY in exclusive graphics mode, allowing direct manipulation of the pixels if available. Only one process may open the terminal at a time. Once opened, the screen will be cleared, and stdout will be sent to an off-screen buffer to be shown once the terminal is closed. The terminal will automatically be closed on process exit. This only works on CraftOS-PC.

Returns:
  • term (system.terminal.GFXTerminal | nil) – A grahics terminal object for the current TTY, or nil if the terminal could not be opened.

  • err (string | nil) – An error message describing why the terminal couldn’t be opened.

system.terminal.mktty(width: number, height: number): (result: system.terminal.TTY)

Creates a new virtual TTY with the specified size. This can later be used in a call to stdin/stdout/stderr.

Parameters:
  • width (number) – The width of the new TTY.

  • height (number) – The height of the new TTY.

Returns:

result (system.terminal.TTY) – A new TTY object which is registered with the kernel. See the syscall docs for more info.

system.terminal.capture(): unknown

Captures input on the current stdin TTY, bringing the process to the front.

system.terminal.release(): unknown

Releases a previously captured input on the current stdin TTY.

system.terminal.stdin(handle: number | file* | system.terminal.TTY | nil): unknown

Sets the standard input of the current process.

either a physical TTY, a virtual TTY, a file, or nil.

Parameters:

handle (number | file* | system.terminal.TTY | nil) – The input handle to switch to, as

system.terminal.stdout(handle: number | file* | system.terminal.TTY | nil): unknown

Sets the standard output of the current process.

either a physical TTY, a virtual TTY, a file, or nil.

Parameters:

handle (number | file* | system.terminal.TTY | nil) – The output handle to switch to, as

system.terminal.stderr(handle: number | file* | system.terminal.TTY | nil): unknown

Sets the standard error of the current process.

either a physical TTY, a virtual TTY, a file, or nil.

Parameters:

handle (number | file* | system.terminal.TTY | nil) – The output handle to switch to, as

system.terminal.istty(): (result: boolean, result: boolean)

Returns whether the current stdio are linked to a TTY.

Returns:
  • result (boolean) – Whether the current stdin is linked to a TTY.

  • result – Whether the current stdout is linked to a TTY.

system.terminal.termsize(): (width: number | nil, height: number | nil)

Returns the current size of the TTY if available.

Returns:
  • width (number | nil) – The width of the screen, or nil if the current stdout is not a screen.

  • height (number | nil) – The height of the screen, or nil if the current stdout is not a screen.

system.terminal.getSize: function
class system.terminal.Terminal

The Terminal type allows interfacing with the screen in exclusive text mode. It provides the same functions as CraftOS does (with some minor differences), and can be used with minimal conversion.

staticmethod close()
staticmethod write(text: any)
staticmethod blit(text: any, fg: any, bg: any)
staticmethod clear()
staticmethod clearLine()
staticmethod getCursorPos()
staticmethod setCursorPos(x: any, y: any)
staticmethod isColor()
staticmethod getSize()
staticmethod scroll(lines: any)
staticmethod getTextColor()
staticmethod setTextColor(color: any)
staticmethod getBackgroundColor()
staticmethod setBackgroundColor(color: any)
staticmethod getPaletteColor(color: any)
staticmethod setPaletteColor(color: any, r: any, g: any, b: any)
staticmethod getLine(y: any)
class system.terminal.GFXTerminal

The GFXTerminal type allows interfacing with the screen in exclusive graphics mode. It provides the same functions as CraftOS-PC does in mode 2, and can be used with minimal conversion.

staticmethod close()
staticmethod getSize()
staticmethod clear()
staticmethod getPaletteColor(color: any)
staticmethod setPaletteColor(color: any, r: any, g: any, b: any)
staticmethod getPixel(x: any, y: any)
staticmethod setPixel(x: any, y: any, color: any)
staticmethod getPixels(x: any, y: any, width: any, height: any, asStr: any)
staticmethod drawPixels(x: any, y: any, data: any, width: any, height: any)
staticmethod getFrozen()
staticmethod setFrozen(frozen: any)
class system.terminal.TTY