Skip to content

Commit 9f6c7c4

Browse files
committed
docs: Update functions
1 parent 98ce0d1 commit 9f6c7c4

File tree

5 files changed

+29
-19
lines changed

5 files changed

+29
-19
lines changed

README.md

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,47 @@ Core functions for any Bash program
44

55
## Summary
66

7-
The following are functions available for use. See [api.md](./docs/api.md) for more details
8-
9-
### `init`
10-
11-
- `core.util.init`
12-
13-
Initialize global variables used by other functions
14-
15-
Add and remove traps. With these, multiple packages can add or remove traps handlers for signals
7+
The following is a brief overview of the available functions. See [api.md](./docs/api.md) for more details
168

179
### `trap`
1810

11+
Add or remove multiple functions at a time to any set of signals. Without these, it is impossible to trap a signal without erasing a previous one
12+
1913
- `core.trap_add`
2014
- `core.trap_remove`
2115

2216
### `shopt`
2317

18+
Enable or disable a shell option. Enabling a shell option adds it to a hidden stack. When that shell option is no longer needed, it should be removed by popping it from the stack
19+
2420
- `core.shopt_push`
2521
- `core.shopt_pop`
2622

2723
### `err`
2824

29-
I suppose it can look redundant (compared to `if ! fn; then :; fi`), but it can help make errors a bit more safe in larger applications, since you don't have to worry about a caller forgetting to `if ! fn` or `fn ||` (and terminating the script if `set -e`). It also makes it easier to communicate specific error codes and helps separate between calculated / expected errors and unexpected errors (fatal / faults)
25+
It can look redundant (compared to `if ! fn; then :; fi`) to define error functions, but it can help make errors a bit more safe in larger applications, since you don't have to worry about a caller forgetting to `if ! fn` or `fn ||` (and terminating the script if `set -e`). It also makes it easier to communicate specific error codes and helps separate between calculated / expected errors and unexpected errors / faults
3026

3127
- `core.err_set`
3228
- `core.err_clear`
3329
- `core.err_exists`
3430

3531
### `stacktrace`
3632

37-
Prints the stack trace. Recommended to use with `core.trap_add`
33+
Prints the stack trace. It is recommended to use this with `core.trap_add` (see [example](./docs/api.md#corestacktraceprint))
3834

3935
- `core.stacktrace_print`
4036

37+
This is what it may look like
38+
39+
```txt
40+
Stacktrace:
41+
in core.stacktrace_print (/storage/ur/storage_home/Docs/Programming/repos/Groups/Bash/bash-core/.hidden/test.sh:0)
42+
in err_handler (/storage/ur/storage_home/Docs/Programming/repos/Groups/Bash/bash-core/.hidden/test.sh:36)
43+
in fn3 (/storage/ur/storage_home/Docs/Programming/repos/Groups/Bash/bash-core/.hidden/test.sh:48)
44+
in fn2 (/storage/ur/storage_home/Docs/Programming/repos/Groups/Bash/bash-core/.hidden/test.sh:53)
45+
in fn (/storage/ur/storage_home/Docs/Programming/repos/Groups/Bash/bash-core/.hidden/test.sh:57)
46+
```
47+
4148
## Installation
4249

4350
Use [Basalt](https://github.com/hyperupcall/basalt), a Bash package manager, to add this project as a dependency

docs/api.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ Core functions for any Bash program
1818

1919
### core.init()
2020

21-
Initiates global variables used by other functions
22-
@deprecated
21+
(DEPRECATED) Initiates global variables used by other functions. Deprecated as this function is called automatically
2322

2423
_Function has no arguments._
2524

pkg/src/public/bash-core.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
# @name bash-core
44
# @description Core functions for any Bash program
55

6-
# @description Initiates global variables used by other functions
7-
# @deprecated
6+
# @description (DEPRECATED) Initiates global variables used by other functions. Deprecated as this function is called automatically
87
# @noargs
98
core.init() {
109
core.util.init

pkg/src/util/util.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# shellcheck shell=bash
22

3+
# @internal
34
core.util.init() {
45
if [ ${___global_bash_core_has_init__+x} ]; then
56
return
@@ -10,6 +11,7 @@ core.util.init() {
1011
declare -ga ___global_shopt_stack___=()
1112
}
1213

14+
# @internal
1315
core.util.trap_handler_common() {
1416
local signal_spec="$1"
1517

tests/trap.bats

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
load './util/init.sh'
44

5-
# Note that this and similar functions only test for array appending, not
6-
# actual execution of the functions on the signal. There seems to be a limitation
5+
# Note that the array appending of these core.trap-* functions are tested. The actual
6+
# execution of the functions on the signal are bit tested. There seems to be a limitation
77
# of Bats that prevents this from working
88

9-
# The '${___global_trap_table___[nokey]}' is there to ensure that the
10-
# ___global_trap_table___ is an actual associative array. If '___global_trap_table___' is not an associative array, the index with 'nokey' still returns the value of the variable (no error will be thrown). These were origianlly done when the 'core.init' function was not called within these tests
9+
# Additionally, the '${___global_trap_table___[nokey]}' is there to ensure that the
10+
# ___global_trap_table___ is an associative array. If that variable is not an associative
11+
# array, indexing with 'nokey' still yields the value of the (string) variable (no error
12+
# will be thrown). Now that 'core.init' is automatically called when the array is not
13+
# defined (to be an associative array), these checks are unecessary (yet, they still exist)
1114

1215
@test "core.trap_add fails when function is not supplied" {
1316
run core.trap_add '' 'USR1'

0 commit comments

Comments
 (0)