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.
Make sure you are logged in and explorer mode is enabled.
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
escat any time to end your program.
onKeyPress(key)- this function fires whenever a key is pressed.
keyis a string representing the key that was pressed.
onMouseClick(event)- this function fires whenever the mouse is clicked. The
eventobject contains properties for the
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.
sleep(ms) - pauses execution (in the containing function only) for
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
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
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.
All color choices:
colors.bg.[colorname] - sets the terminal background color for subsequent outputs, e.g.
colors.reset - resets colors to terminal defaults.
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
cursor.gotoColumn(col) - move the cursor to the specified
col in the current row.
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.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.
async fileIO.readFile(fileName) - returns the file contents as a string
async fileIO.writeFile(fileName, content) - writes
content to file
async fileIO.appendFile(fileName, content) - appends
content to file
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!)