← Back to all posts
3
Node.js Terminal Apps Library

Terminal Apps Library

Link to repl: https://repl.it/@timmy_i_chen/Terminal-Apps-Library-v2

I made a library, inspired by p5.js, for terminal apps using Node.js and repl.it's Repl Run.

Requirements

Make sure you are logged in and explorer mode is enabled.

Usage

All edits will probably go in the app.js file. There are four main functions to edit:

  • setup() - this function is run once at the beginning of your program.
  • loop() - this function is continually run at an interval of your choosing. You can press esc at any time to end your program.
  • onKeyPress(key) - this function fires whenever a key is pressed. key is a string representing the key that was pressed.
  • onMouseClick(event) - this function fires whenever the mouse is clicked. The event object contains properties for the row, column, and scroll (direction the mouse was scrolling in, if any).

When running, be sure to run using repl run. This will be fixed later as we roll xterm out to more languages.

Examples

API Docs

Misc

sleep(ms) - pauses execution (in the containing function only) for ms ms

print(value) - prints value at the cursor location without a newline. Converts value to a string.

printDebug(output [, atRow, length]) - printing for debugging purposes. Prints output at atRow rows down, clearing length lines underneath as well. Objects are pretty printed, otherwise the value is converted to a string.

randomInt(low, high) - generates and returns a random integer between low and high inclusive.

Colors

Color values are ANSI escape sequences that set color. These are usually printed with a string.

colors.[colorname] - sets the terminal foreground (text) color for subsequent outputs. e.g. colors.black, colors.cyan.

All color choices: black, red, green, yellow, blue, magenta, cyan, white

colors.bg.[colorname] - sets the terminal background color for subsequent outputs, e.g. colors.bg.magenta

colors.reset - resets colors to terminal defaults.

Cursor

Even those all of these represent ANSI escape sequences, they do not need to be printed. Just calling the function will execute it.

cursor.goto(row, col) - moves the cursor to row, col.

cursor.gotoColumn(col) - move the cursor to the specified col in the current row.

cursor.down(amt) / cursor.up(amt) / cursor.left(amt) / cursor.right(amt) - moves the cursor down/up/left/right byamt`

cursor.save() - saves the position of the cursor

cursor.restore() - moves the cursor back to its last saved position

cursor.hide() - hides the cursor

cursor.show() - shows the cursor (if hidden)

Screen

screen.clear() - clears the screen

screen.reset() - clears the screen and moves the cursor back to (0, 0)

screen.getWidth() - returns the number of columns on the screen (only works after resizing)

screen.getHeight - returns the number of rows on the screen (only works after resizing)

screen.clearLineToEnd() - clears content starting from the character ending at the end of the current row.

FileIO

async fileIO.readFile(fileName) - returns the file contents as a string

async fileIO.writeFile(fileName, content) - writes content to file fileName.

async fileIO.appendFile(fileName, content) - appends content to file fileName.

Closing

Hope you enjoyed it! Please feel free to fork and modify. If you make something cool with it, I'd love to see it - share it here or make it in another post (but tag me!)