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 aStrto standard output exactly as it is, without ending the line — for a prompt, or a line built in pieces.println(text)adds the newline, asechodoes for aStr.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 aSecret<Str>, so it answers aResult<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, asbypassis tohide, so it answers a plainSecret<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 andexitincluded, the terminal is put back.readKey()waits for one key and answers with ahive.term.Key: itsname, the onehive.uigives it —"a","A","Ctrl+C","ArrowUp","Enter"— and thetextit types,""for a key that edits or moves. In raw mode a paste is one key namedPasteholding all of it, so a newline in it is neverEnter.size()is the window'swidthandheight, 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()
}You can edit this program and run it here.Tab indents, Escape leaves the program, and Ctrl+Enter runs it.
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
}
}
}Output
Run a program to see what it prints here.