The standard librarySlide 59 of 86
59termThe standard library

The terminal

hive.term is the terminal, a line or a key at a time:

  • print(text) writes a Str to standard output exactly as it is, without ending the line — for a prompt, or a line built in pieces. println(text) adds the newline, as echo does for a Str. status(text) writes a line to standard error: what the program is doing, as against what it was asked for, so redirecting the answer never takes the progress with it.
  • read() waits for a line of input and answers without its newline. At the end of the input it answers with what was left, then "" for good, so an empty line and the end look alike.
  • readSecret() reads a line with the terminal's echo off, straight into a Secret<Str>, so it answers a Result<Secret<Str>, SecretError>. Interrupted at the prompt, it turns the echo back on and ends the program with status 130; with no terminal, it reads the line as it comes.
  • readSecretBypass() is the same read into ordinary memory, as bypass is to hide, so it answers a plain Secret<Str> and cannot fail.
  • raw(on) turns raw mode on or off: what is typed is neither shown nor held back for a whole line, and Ctrl+C is a key rather than the end of the program. However the program ends, a panic and exit included, the terminal is put back.
  • readKey() waits for one key and answers with a hive.term.Key: its name, the one hive.ui gives it — "a", "A", "Ctrl+C", "ArrowUp", "Enter" — and the text it types, "" for a key that edits or moves. In raw mode a paste is one key named Paste holding all of it, so a newline in it is never Enter.
  • size() is the window's width and height, in cells.
  • isTerminal() is whether standard error is a person rather than a pipe or a file.
  • args() is the command line, without the program's own name.
  • exit(code) ends the program there with that status, so a path ending in it counts as ended.

raw, readKey and size answer with a Result whose hive.term.TermError is NotATerminal, Closed once the input has ended, or Io.

A secret, read and then kept
proc main(): void {
	echo "API token:"
	typed := hive.term.readSecret()
	if typed is Result.Error(why) {
		echo "nowhere to keep it: {why.message}"
	} else if typed is Result.Ok(token) {
		// Nobody but this account can read the file, and that is true
		// before the first byte of the token is in it.
		if hive.file.writeSecret("./token.txt", token) is Result.Error(why) {
			echo "could not keep it: {why.message}"
		}
	}
}
Asking about the terminal itself
proc main(): void {
	echo hive.term.isTerminal()
	echo hive.term.args()
}
Keys, one at a time
proc main(): void {
	if hive.term.raw(true) is Result.Error(why) {
		echo "no keys to read here: {why.message}"
		return
	}
	hive.term.println("Type away; Escape ends it.")
	mut going := true
	for ; going; {
		if hive.term.readKey() is Result.Ok(key) {
			hive.term.println("{key.name} types [{key.text}]")
			going = key.name != "Escape"
		} else {
			going = false
		}
	}
}