% API
Builtins
USE_COLOR
USE_COLOR : Bool
Whether or not the console prefers ANSI color escape sequences in the output.
ask
ask : func(prompt: Text, bold: Bool = yes, force_tty: Bool = yes -> Text?)
Gets a line of user input text with a prompt.
When a program is receiving input from a pipe or writing its output to a pipe, this flag (which is enabled by default) forces the program to write the prompt to /dev/tty and read the input from /dev/tty, which circumvents the pipe. This means that foo | ./tomo your-program | baz will still show a visible prompt and read user input, despite the pipes. Setting this flag to no will mean that the prompt is written to stdout and input is read from stdin, even if those are pipes.
| Argument | Type | Description | Default |
|---|---|---|---|
| prompt | Text |
The text to print as a prompt before getting the input. | - |
| bold | Bool |
Whether or not to print make the prompt appear bold on a console. | yes |
| force_tty | Bool |
Whether or not to force the use of /dev/tty. | yes |
Return: A line of user input text without a trailing newline, or empty text if something went wrong (e.g. the user hit Ctrl-D).
Example:
assert ask("What's your name? ") == "Arthur Dent"
at_cleanup
at_cleanup : func(fn: func() -> Void)
Register a function that runs at cleanup time for Tomo programs. Cleanup time happens when a program exits (see atexit() in C), or immediately before printing error messages in a call to fail(). This allows for terminal cleanup so error messages can be visible as the program shuts down.
Use this API very carefully, because errors that occur during cleanup functions may make it extremely hard to figure out what's going on. Cleanup functions should be designed to not error under any circumstances.
| Argument | Type | Description | Default |
|---|---|---|---|
| fn | func() |
A function to run at cleanup time. | - |
Return: Nothing.
Example:
at_cleanup(func()
_ := (/tmp/file.txt).remove(ignore_missing=yes)
)
exit
exit : func(message: Text? = none, status: Int32 = Int32(1) -> Abort)
Exits the program with a given status and optionally prints a message.
| Argument | Type | Description | Default |
|---|---|---|---|
| message | Text? |
If nonempty, this message will be printed (with a newline) before exiting. | none |
| status | Int32 |
The status code that the program with exit with. | Int32(1) |
Return: This function never returns.
Example:
exit("Goodbye forever!", Int32(1))
fail
fail : func(message: Text -> Abort)
Prints a message to the console, aborts the program, and prints a stack trace.
| Argument | Type | Description | Default |
|---|---|---|---|
| message | Text |
The error message to print. | - |
Return: Nothing, aborts the program.
Example:
fail("Oh no!")
getenv
getenv : func(name: Text -> Text?)
Gets an environment variable.
| Argument | Type | Description | Default |
|---|---|---|---|
| name | Text |
The name of the environment variable to get. | - |
Return: If set, the environment variable's value, otherwise, none.
Example:
assert getenv("TERM") == "xterm-256color"
assert getenv("not_a_variable") == none
print : func(text: Text, newline: Bool = yes -> Void)
Prints a message to the console (alias for say()).
| Argument | Type | Description | Default |
|---|---|---|---|
| text | Text |
The text to print. | - |
| newline | Bool |
Whether or not to print a newline after the text. | yes |
Return: Nothing.
Example:
print("Hello ", newline=no)
print("world!")
say
say : func(text: Text, newline: Bool = yes -> Void)
Prints a message to the console.
| Argument | Type | Description | Default |
|---|---|---|---|
| text | Text |
The text to print. | - |
| newline | Bool |
Whether or not to print a newline after the text. | yes |
Return: Nothing.
Example:
say("Hello ", newline=no)
say("world!")
setenv
setenv : func(name: Text, value: Text? -> Void)
Sets an environment variable.
| Argument | Type | Description | Default |
|---|---|---|---|
| name | Text |
The name of the environment variable to set. | - |
| value | Text? |
The new value of the environment variable. If none, then the environment variable will be unset. |
- |
Return: Nothing.
Example:
setenv("FOOBAR", "xyz")
sleep
sleep : func(seconds: Num -> Void)
Pause execution for a given number of seconds.
| Argument | Type | Description | Default |
|---|---|---|---|
| seconds | Num |
How many seconds to sleep for. | - |
Return: Nothing.
Example:
sleep(1.5)
1 % API3 # Builtins4 ## USE_COLOR7 USE_COLOR : Bool8 ```10 Whether or not the console prefers ANSI color escape sequences in the output.12 ## ask15 ask : func(prompt: Text, bold: Bool = yes, force_tty: Bool = yes -> Text?)16 ```18 Gets a line of user input text with a prompt.20 When a program is receiving input from a pipe or writing its output to a pipe, this flag (which is enabled by default) forces the program to write the prompt to `/dev/tty` and read the input from `/dev/tty`, which circumvents the pipe. This means that `foo | ./tomo your-program | baz` will still show a visible prompt and read user input, despite the pipes. Setting this flag to `no` will mean that the prompt is written to `stdout` and input is read from `stdin`, even if those are pipes.22 Argument | Type | Description | Default23 ---------|------|-------------|---------28 **Return:** A line of user input text without a trailing newline, or empty text if something went wrong (e.g. the user hit `Ctrl-D`).31 **Example:**33 assert ask("What's your name? ") == "Arthur Dent"35 ```36 ## at_cleanup39 at_cleanup : func(fn: func() -> Void)40 ```42 Register a function that runs at cleanup time for Tomo programs. Cleanup time happens when a program exits (see `atexit()` in C), or immediately before printing error messages in a call to `fail()`. This allows for terminal cleanup so error messages can be visible as the program shuts down.44 Use this API very carefully, because errors that occur during cleanup functions may make it extremely hard to figure out what's going on. Cleanup functions should be designed to not error under any circumstances.46 Argument | Type | Description | Default47 ---------|------|-------------|---------53 **Example:**55 at_cleanup(func()56 _ := (/tmp/file.txt).remove(ignore_missing=yes)57 )59 ```60 ## exit63 exit : func(message: Text? = none, status: Int32 = Int32(1) -> Abort)64 ```66 Exits the program with a given status and optionally prints a message.68 Argument | Type | Description | Default69 ---------|------|-------------|---------70 message | `Text?` | If nonempty, this message will be printed (with a newline) before exiting. | `none`76 **Example:**78 exit("Goodbye forever!", Int32(1))80 ```81 ## fail84 fail : func(message: Text -> Abort)85 ```87 Prints a message to the console, aborts the program, and prints a stack trace.89 Argument | Type | Description | Default90 ---------|------|-------------|---------96 **Example:**98 fail("Oh no!")100 ```101 ## getenv104 getenv : func(name: Text -> Text?)105 ```107 Gets an environment variable.109 Argument | Type | Description | Default110 ---------|------|-------------|---------116 **Example:**118 assert getenv("TERM") == "xterm-256color"119 assert getenv("not_a_variable") == none121 ```122 ## print125 print : func(text: Text, newline: Bool = yes -> Void)126 ```128 Prints a message to the console (alias for say()).130 Argument | Type | Description | Default131 ---------|------|-------------|---------138 **Example:**140 print("Hello ", newline=no)141 print("world!")143 ```144 ## say147 say : func(text: Text, newline: Bool = yes -> Void)148 ```150 Prints a message to the console.152 Argument | Type | Description | Default153 ---------|------|-------------|---------160 **Example:**162 say("Hello ", newline=no)163 say("world!")165 ```166 ## setenv169 setenv : func(name: Text, value: Text? -> Void)170 ```172 Sets an environment variable.174 Argument | Type | Description | Default175 ---------|------|-------------|---------177 value | `Text?` | The new value of the environment variable. If `none`, then the environment variable will be unset. | -182 **Example:**184 setenv("FOOBAR", "xyz")186 ```187 ## sleep190 sleep : func(seconds: Num -> Void)191 ```193 Pause execution for a given number of seconds.195 Argument | Type | Description | Default196 ---------|------|-------------|---------202 **Example:**204 sleep(1.5)206 ```