Skip to content

Commit 0d1d64d

Browse files
committed
feat: Add core.print_ functions
1 parent a2d8a2f commit 0d1d64d

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

docs/api.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Core functions for any Bash program
1515
* [core.err_clear()](#coreerr_clear)
1616
* [core.err_exists()](#coreerr_exists)
1717
* [core.stacktrace_print()](#corestacktrace_print)
18+
* [core.print_error()](#coreprint_error)
19+
* [core.print_warn()](#coreprint_warn)
20+
* [core.print_info()](#coreprint_info)
1821
* [core.should_output_color()](#coreshould_output_color)
1922
* [core.get_package_info()](#coreget_package_info)
2023

@@ -145,6 +148,30 @@ core.trap_add 'err_handler' ERR
145148

146149
_Function has no arguments._
147150

151+
### core.print_error()
152+
153+
Print an error message to standard error
154+
155+
#### Arguments
156+
157+
* **$1** (string): message
158+
159+
### core.print_warn()
160+
161+
Print a warning message to standard error
162+
163+
#### Arguments
164+
165+
* **$1** (string): message
166+
167+
### core.print_info()
168+
169+
Print an informative message to standard output
170+
171+
#### Arguments
172+
173+
* **$1** (string): message
174+
148175
### core.should_output_color()
149176

150177
Determine if color should be printed. Note that this doesn't

pkg/src/public/bash-core.sh

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,30 @@ core.stacktrace_print() {
309309
fi
310310
} >&2
311311

312+
# @description Print an error message to standard error
313+
# @arg $1 string message
314+
core.print_error() {
315+
local msg="$1"
316+
317+
printf '%s\n' "Error: ${FUNCNAME[1]}${msg:+": "}$msg" >&2
318+
}
319+
320+
# @description Print a warning message to standard error
321+
# @arg $1 string message
322+
core.print_warn() {
323+
local msg="$1"
324+
325+
printf '%s\n' "Warn: ${FUNCNAME[1]}${msg:+": "}$msg" >&2
326+
}
327+
328+
# @description Print an informative message to standard output
329+
# @arg $1 string message
330+
core.print_info() {
331+
local msg="$1"
332+
333+
printf '%s\n' "Info: ${FUNCNAME[1]}${msg:+": "}$msg"
334+
}
335+
312336
# @description Determine if color should be printed. Note that this doesn't
313337
# use tput because simple environment variable checking heuristics suffice
314338
core.should_output_color() {
@@ -353,7 +377,7 @@ core.get_package_info() {
353377
local toml_file="$basalt_package_dir/basalt.toml"
354378

355379
if [ ! -f "$toml_file" ]; then
356-
printf '%s\n' "Error: core.get_package_data: File '$toml_file' could not be found"
380+
printf '%s\n' "Error: core.get_package_info: File '$toml_file' could not be found"
357381
fi
358382

359383
local regex="^[ \t]*${key_name}[ \t]*=[ \t]*['\"](.*)['\"]"

tests/print.bats

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env bats
2+
3+
load './util/init.sh'
4+
5+
# The following functions are defined so the names of the function
6+
# at the callsite can be tested. Otherwise, the function names are
7+
# automatically generated by Bats (and are more difficult to test)
8+
errorfn() { "$@"; }
9+
warnfn() { "$@"; }
10+
infofn() { "$@"; }
11+
12+
@test "core.print_error works" {
13+
run errorfn core.print_error
14+
assert_success
15+
assert_output 'Error: errorfn'
16+
17+
run errorfn core.print_error 'Something'
18+
assert_success
19+
assert_output 'Error: errorfn: Something'
20+
}
21+
22+
@test "core.print_warn works" {
23+
run warnfn core.print_warn
24+
assert_success
25+
assert_output 'Warn: warnfn'
26+
27+
run warnfn core.print_warn 'Something'
28+
assert_success
29+
assert_output 'Warn: warnfn: Something'
30+
}
31+
32+
@test "core.print_info works" {
33+
run infofn core.print_info
34+
assert_success
35+
assert_output 'Info: infofn'
36+
37+
run infofn core.print_info 'Something'
38+
assert_success
39+
assert_output 'Info: infofn: Something'
40+
}

0 commit comments

Comments
 (0)