code / tomo

Lines41.3K C23.7K Markdown9.7K YAML5.0K Tomo2.3K
7 others 763
Python231 Shell230 make212 INI47 Text21 SVG16 Lua6
(206 lines)

% 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

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 % API
3 # Builtins
4 ## USE_COLOR
6 ```tomo
7 USE_COLOR : Bool
8 ```
10 Whether or not the console prefers ANSI color escape sequences in the output.
12 ## ask
14 ```tomo
15 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 | Default
23 ---------|------|-------------|---------
24 prompt | `Text` | The text to print as a prompt before getting the input. | -
25 bold | `Bool` | Whether or not to print make the prompt appear bold on a console. | `yes`
26 force_tty | `Bool` | Whether or not to force the use of /dev/tty. | `yes`
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:**
32 ```tomo
33 assert ask("What's your name? ") == "Arthur Dent"
35 ```
36 ## at_cleanup
38 ```tomo
39 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 | Default
47 ---------|------|-------------|---------
48 fn | `func()` | A function to run at cleanup time. | -
50 **Return:** Nothing.
53 **Example:**
54 ```tomo
55 at_cleanup(func()
56 _ := (/tmp/file.txt).remove(ignore_missing=yes)
59 ```
60 ## exit
62 ```tomo
63 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 | Default
69 ---------|------|-------------|---------
70 message | `Text?` | If nonempty, this message will be printed (with a newline) before exiting. | `none`
71 status | `Int32` | The status code that the program with exit with. | `Int32(1)`
73 **Return:** This function never returns.
76 **Example:**
77 ```tomo
78 exit("Goodbye forever!", Int32(1))
80 ```
81 ## fail
83 ```tomo
84 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 | Default
90 ---------|------|-------------|---------
91 message | `Text` | The error message to print. | -
93 **Return:** Nothing, aborts the program.
96 **Example:**
97 ```tomo
98 fail("Oh no!")
100 ```
101 ## getenv
103 ```tomo
104 getenv : func(name: Text -> Text?)
105 ```
107 Gets an environment variable.
109 Argument | Type | Description | Default
110 ---------|------|-------------|---------
111 name | `Text` | The name of the environment variable to get. | -
113 **Return:** If set, the environment variable's value, otherwise, `none`.
116 **Example:**
117 ```tomo
118 assert getenv("TERM") == "xterm-256color"
119 assert getenv("not_a_variable") == none
121 ```
122 ## print
124 ```tomo
125 print : func(text: Text, newline: Bool = yes -> Void)
126 ```
128 Prints a message to the console (alias for say()).
130 Argument | Type | Description | Default
131 ---------|------|-------------|---------
132 text | `Text` | The text to print. | -
133 newline | `Bool` | Whether or not to print a newline after the text. | `yes`
135 **Return:** Nothing.
138 **Example:**
139 ```tomo
140 print("Hello ", newline=no)
141 print("world!")
143 ```
144 ## say
146 ```tomo
147 say : func(text: Text, newline: Bool = yes -> Void)
148 ```
150 Prints a message to the console.
152 Argument | Type | Description | Default
153 ---------|------|-------------|---------
154 text | `Text` | The text to print. | -
155 newline | `Bool` | Whether or not to print a newline after the text. | `yes`
157 **Return:** Nothing.
160 **Example:**
161 ```tomo
162 say("Hello ", newline=no)
163 say("world!")
165 ```
166 ## setenv
168 ```tomo
169 setenv : func(name: Text, value: Text? -> Void)
170 ```
172 Sets an environment variable.
174 Argument | Type | Description | Default
175 ---------|------|-------------|---------
176 name | `Text` | The name of the environment variable to set. | -
177 value | `Text?` | The new value of the environment variable. If `none`, then the environment variable will be unset. | -
179 **Return:** Nothing.
182 **Example:**
183 ```tomo
184 setenv("FOOBAR", "xyz")
186 ```
187 ## sleep
189 ```tomo
190 sleep : func(seconds: Num -> Void)
191 ```
193 Pause execution for a given number of seconds.
195 Argument | Type | Description | Default
196 ---------|------|-------------|---------
197 seconds | `Num` | How many seconds to sleep for. | -
199 **Return:** Nothing.
202 **Example:**
203 ```tomo
204 sleep(1.5)
206 ```