Skip to content

Commit c593147

Browse files
committed
feat: New printing functions
1 parent 5c4f98b commit c593147

File tree

2 files changed

+88
-7
lines changed

2 files changed

+88
-7
lines changed

docs/api.md

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@ Core functions for any Bash program
1515
* [core.err_exists()](#coreerr_exists)
1616
* [core.panic()](#corepanic)
1717
* [core.print_stacktrace()](#coreprint_stacktrace)
18+
* [core.print_fatal_fn()](#coreprint_fatal_fn)
1819
* [core.print_error_fn()](#coreprint_error_fn)
1920
* [core.print_warn_fn()](#coreprint_warn_fn)
2021
* [core.print_info_fn()](#coreprint_info_fn)
22+
* [core.print_debug_fn()](#coreprint_debug_fn)
2123
* [core.print_die()](#coreprint_die)
24+
* [core.print_fatal()](#coreprint_fatal)
2225
* [core.print_error()](#coreprint_error)
2326
* [core.print_warn()](#coreprint_warn)
2427
* [core.print_info()](#coreprint_info)
28+
* [core.print_debug()](#coreprint_debug)
2529
* [core.should_output_color()](#coreshould_output_color)
2630
* [core.get_package_info()](#coreget_package_info)
2731
* [core.init()](#coreinit)
@@ -155,25 +159,46 @@ core.trap_add 'err_handler' ERR
155159

156160
_Function has no arguments._
157161

162+
### core.print_fatal_fn()
163+
164+
Print a fatal error message including the function name of the callee
165+
to standard error
166+
167+
#### Arguments
168+
169+
* **$1** (string): message
170+
158171
### core.print_error_fn()
159172

160-
Print an error message to standard error
173+
Print an error message including the function name of the callee
174+
to standard error
161175

162176
#### Arguments
163177

164178
* **$1** (string): message
165179

166180
### core.print_warn_fn()
167181

168-
Print a warning message to standard error
182+
Print a warning message including the function name of the callee
183+
to standard error
169184

170185
#### Arguments
171186

172187
* **$1** (string): message
173188

174189
### core.print_info_fn()
175190

176-
Print an informative message to standard output
191+
Print an informative message including the function name of the callee
192+
to standard output
193+
194+
#### Arguments
195+
196+
* **$1** (string): message
197+
198+
### core.print_debug_fn()
199+
200+
Print a debug message including the function name of the callee
201+
to standard output
177202

178203
#### Arguments
179204

@@ -187,6 +212,14 @@ Print a error message to standard error and die
187212

188213
* **$1** (string): message
189214

215+
### core.print_fatal()
216+
217+
Print a fatal error message to standard error
218+
219+
#### Arguments
220+
221+
* **$1** (string): message
222+
190223
### core.print_error()
191224

192225
Print an error message to standard error
@@ -211,6 +244,14 @@ Print an informative message to standard output
211244

212245
* **$1** (string): message
213246

247+
### core.print_debug()
248+
249+
Print a debug message to standard output if the environment variable "DEBUG" is present
250+
251+
#### Arguments
252+
253+
* **$1** (string): message
254+
214255
### core.should_output_color()
215256

216257
(DEPRECATED). Determine if color should be printed. Note that this doesn't

pkg/src/public/bash-core.sh

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,37 +279,69 @@ core.print_stacktrace() {
279279
fi
280280
} >&2
281281

282-
# @description Print an error message to standard error
282+
# @description Print a fatal error message including the function name of the callee
283+
# to standard error
284+
# @arg $1 string message
285+
core.print_fatal_fn() {
286+
local msg="$1"
287+
288+
core.print_fatal "${FUNCNAME[1]}()${msg:+": "}$msg"
289+
}
290+
291+
# @description Print an error message including the function name of the callee
292+
# to standard error
283293
# @arg $1 string message
284294
core.print_error_fn() {
285295
local msg="$1"
286296

287297
core.print_error "${FUNCNAME[1]}()${msg:+": "}$msg"
288298
}
289299

290-
# @description Print a warning message to standard error
300+
# @description Print a warning message including the function name of the callee
301+
# to standard error
291302
# @arg $1 string message
292303
core.print_warn_fn() {
293304
local msg="$1"
294305

295306
core.print_warn "${FUNCNAME[1]}()${msg:+": "}$msg"
296307
}
297308

298-
# @description Print an informative message to standard output
309+
# @description Print an informative message including the function name of the callee
310+
# to standard output
299311
# @arg $1 string message
300312
core.print_info_fn() {
301313
local msg="$1"
302314

303315
core.print_info "${FUNCNAME[1]}()${msg:+": "}$msg"
304316
}
317+
# @description Print a debug message including the function name of the callee
318+
# to standard output
319+
# @arg $1 string message
320+
core.print_debug_fn() {
321+
local msg="$1"
322+
323+
core.print_debug "${FUNCNAME[1]}()${msg:+": "}$msg"
324+
}
305325

306326
# @description Print a error message to standard error and die
307327
# @arg $1 string message
308328
core.print_die() {
309-
core.print_error "$1"
329+
core.print_fatal "$1"
310330
exit 1
311331
}
312332

333+
# @description Print a fatal error message to standard error
334+
# @arg $1 string message
335+
core.print_fatal() {
336+
local msg="$1"
337+
338+
if core.private.should_print_color 2; then
339+
printf "\033[1;35m%s:\033[0m %s\n" 'Fatal' "$msg" >&2
340+
else
341+
printf "%s: %s\n" 'Fatal' "$msg" >&2
342+
fi
343+
}
344+
313345
# @description Print an error message to standard error
314346
# @arg $1 string message
315347
core.print_error() {
@@ -346,6 +378,14 @@ core.print_info() {
346378
fi
347379
}
348380

381+
# @description Print a debug message to standard output if the environment variable "DEBUG" is present
382+
# @arg $1 string message
383+
core.print_debug() {
384+
if [[ -v DEBUG ]]; then
385+
printf "%s: %s\n" 'Debug' "$msg"
386+
fi
387+
}
388+
349389
# @description (DEPRECATED). Determine if color should be printed. Note that this doesn't
350390
# use tput because simple environment variable checking heuristics suffice. Deprecated because this code
351391
# has been moved to bash-std

0 commit comments

Comments
 (0)