Standard Library: Console

Printing, prompting, and low-level console interaction

  1. clear
  2. error
  3. inspect
  4. pauseStdin
  5. print
  6. prompt
  7. rawKey
  8. write

Console.clear()

Clear the console.

clear() -- Clear the console

Console.error(value)

Identical to inspect, except that the result is sent to stderr rather than stdout.

Console.error("Hello")
Console.error("world")
Hello
world

Console.inspect(value)

Print the string representation of value to stdout, ignoring custom @show methods, followed by a newline, and return value. Include quotes if value is a string. Useful for debugging and logging.

Console.inspect("Hello")
Console.inspect("world")

Console.pauseStdin()

Temporary workaround for lack of JS promise cancellation.


Console.print(value)

Print the string representation of value to stdout, followed by a newline, and return value. Does not include quotes if value is a string. Useful for general-purpose printing.

print("Hello")
print("world")
Hello
world

Console.prompt(message)

Prompt the user for input, displaying the string message beforehand. User input is returned as a string.

prompt("Enter name: ")
Str.parse(prompt("Enter age: "))
Enter name: avery
"avery"
Enter age: 28
28

Console.rawKey()

Read a single keypress from the terminal in raw mode. Return an object with the following keypress event details:

  • str: the character, if printable, or none.
  • name: name of the key.
  • ctrl: is Ctrl pressed.
  • meta: is Meta pressed.
  • shift: is Shift pressed.
  • sequence: the raw input sequence.

Panics on Ctrl + C or Ctrl + D.

Console.rawKey() -- Read the next keypress

Sample outputs:

  • For Ctrl + A:

    {
      str: "\u0001",
      name: "a",
      ctrl: true,
      meta: false,
      shift: false,
      sequence: "\u0001",
    }
  • For Space:

    {
      str: " ",
      name: "space",
      ctrl: false,
      meta: false,
      shift: false,
      sequence: " ",
    }
  • For Left Arrow:

    {
      str: none,
      name: "left",
      ctrl: false,
      meta: false,
      shift: false,
      sequence: "\u001b[D",
    }

Console.write(string)

Print string to stdout without adding a trailing newline.

Console.write("Hello")
Console.write("world")
Helloworld