aboutsummaryrefslogtreecommitdiff
path: root/api
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-11-23 14:19:04 -0500
committerBruce Hill <bruce@bruce-hill.com>2025-11-23 14:22:22 -0500
commit2a24b0a3fc3c4986572ae2c4ea0e8e387497a7f6 (patch)
tree539d7bb993b8b9aca1935b3afb21ba0a6cba241c /api
parent8700224e98f95807d896d214380796e6f80678d0 (diff)
Add `at_cleanup()` function
Diffstat (limited to 'api')
-rw-r--r--api/api.md26
-rw-r--r--api/builtins.md26
-rw-r--r--api/builtins.yaml28
3 files changed, 77 insertions, 3 deletions
diff --git a/api/api.md b/api/api.md
index 02ad054e..5c9dc9c4 100644
--- a/api/api.md
+++ b/api/api.md
@@ -33,10 +33,34 @@ force_tty | `Bool` | Whether or not to force the use of /dev/tty. | `yes`
assert ask("What's your name? ") == "Arthur Dent"
```
+## at_cleanup
+
+```tomo
+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:**
+```tomo
+at_cleanup(func()
+ (/tmp/file.txt).remove(ignore_missing=yes)
+)
+
+```
## exit
```tomo
-exit : func(message: Text? = none, status: Int32 = Int32(1) -> Void)
+exit : func(message: Text? = none, status: Int32 = Int32(1) -> Abort)
```
Exits the program with a given status and optionally prints a message.
diff --git a/api/builtins.md b/api/builtins.md
index 0b06a41b..6d042741 100644
--- a/api/builtins.md
+++ b/api/builtins.md
@@ -33,10 +33,34 @@ force_tty | `Bool` | Whether or not to force the use of /dev/tty. | `yes`
assert ask("What's your name? ") == "Arthur Dent"
```
+## at_cleanup
+
+```tomo
+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:**
+```tomo
+at_cleanup(func()
+ (/tmp/file.txt).remove(ignore_missing=yes)
+)
+
+```
## exit
```tomo
-exit : func(message: Text? = none, status: Int32 = Int32(1) -> Void)
+exit : func(message: Text? = none, status: Int32 = Int32(1) -> Abort)
```
Exits the program with a given status and optionally prints a message.
diff --git a/api/builtins.yaml b/api/builtins.yaml
index 2eae5340..764a1bd4 100644
--- a/api/builtins.yaml
+++ b/api/builtins.yaml
@@ -38,7 +38,7 @@ exit:
description: >
Exits the program with a given status and optionally prints a message.
return:
- type: 'Void'
+ type: 'Abort'
description: >
This function never returns.
args:
@@ -56,6 +56,32 @@ exit:
example: |
exit(status=1, "Goodbye forever!")
+at_cleanup:
+ short: register a cleanup function
+ description: >
+ 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.
+ note: >
+ 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.
+ args:
+ fn:
+ type: 'func()'
+ description: >
+ A function to run at cleanup time.
+ return:
+ type: 'Void'
+ description: >
+ Nothing.
+ example: |
+ at_cleanup(func()
+ (/tmp/file.txt).remove(ignore_missing=yes)
+ )
+
getenv:
short: get an environment variable
description: >