Skip to content

Commit 2858bf4

Browse files
committed
feat: Add print formatting functions
1 parent 5c4775e commit 2858bf4

File tree

4 files changed

+107
-70
lines changed

4 files changed

+107
-70
lines changed

bake

Lines changed: 15 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# Learn more about it [on GitHub](https://github.com/hyperupcall/bake)
1717

1818
if [ "$0" != "${BASH_SOURCE[0]}" ] && [ "$BAKE_INTERNAL_CAN_SOURCE" != 'yes' ]; then
19-
printf '%s\n' 'Error: This file should not be sourced' >&2
19+
printf '%s\n' "Error: This file should not be sourced" >&2
2020
return 1
2121
fi
2222

@@ -91,12 +91,12 @@ bake.assert_cmd() {
9191
fi
9292
}
9393

94-
# @description Change the behavior of Bake. See [guide.md](./docs/guide.md) for details
94+
# @description Change the behavior of Bake
9595
# @arg $1 string Name of config property to change
9696
# @arg $2 string New value of config property
9797
bake.cfg() {
98-
local cfg="$1"
99-
local value="$2"
98+
local cfg=$1
99+
local value=$2
100100

101101
case $cfg in
102102
stacktrace)
@@ -112,12 +112,6 @@ bake.cfg() {
112112
*) __bake_internal_die2 "Config property '$cfg' accepts only either 'yes' or 'no'" ;;
113113
esac
114114
;;
115-
big-print)
116-
case $value in
117-
yes|no) ;;
118-
*) __bake_internal_die2 "Config property '$cfg' accepts only either 'yes' or 'no'" ;;
119-
esac
120-
;;
121115
*)
122116
__bake_internal_die2 "No config property matched '$cfg'"
123117
;;
@@ -236,27 +230,15 @@ __bake_error() {
236230
# @description Nicely prints all 'Bakefile.sh' tasks to standard output
237231
# @internal
238232
__bake_print_tasks() {
239-
printf '%s\n' 'Tasks:'
240-
local str=
241-
242233
# shellcheck disable=SC1007,SC2034
243234
local regex="^(([[:space:]]*function[[:space:]]*)?task\.(.*?)\(\)).*"
244235
local line=
236+
printf '%s\n' 'Tasks:'
245237
while IFS= read -r line || [ -n "$line" ]; do
246238
if [[ "$line" =~ $regex ]]; then
247-
str+=" -> ${BASH_REMATCH[3]}"$'\n'
239+
printf '%s\n' " -> ${BASH_REMATCH[3]}"
248240
fi
249241
done < "$BAKE_FILE"; unset -v line
250-
251-
if [ -z "$str" ]; then
252-
if __bake_is_color; then
253-
str=$' \033[3mNo tasks\033[0m\n'
254-
else
255-
str=$' No tasks\n'
256-
fi
257-
fi
258-
259-
printf '%s' "$str"
260242
} >&2
261243

262244
# @description Prints text that takes up the whole terminal width
@@ -265,21 +247,13 @@ __bake_print_tasks() {
265247
__bake_print_big() {
266248
local print_text="$1"
267249

268-
if [ "$__bake_cfg_big_print" = 'no' ]; then
269-
return
270-
fi
271-
272250
# shellcheck disable=SC1007
273251
local _stty_height= _stty_width=
274252
read -r _stty_height _stty_width < <(
275-
if stty size &>/dev/null; then
253+
if command -v stty &>/dev/null; then
276254
stty size
277255
else
278-
if [ -n "$COLUMNS" ]; then
279-
printf '%s\n' "20 $COLUMNS"
280-
else
281-
printf '%s\n' '20 80'
282-
fi
256+
printf '%s\n' '20 80'
283257
fi
284258
)
285259

@@ -302,7 +276,6 @@ __bake_parse_args() {
302276
unset REPLY; REPLY=
303277
local -i total_shifts=0
304278

305-
# FIXME: bug for when passing -v to child task argument
306279
local __bake_arg=
307280
for arg; do case $arg in
308281
-f)
@@ -322,10 +295,6 @@ __bake_parse_args() {
322295
__bake_internal_die "Specified file '$BAKE_FILE' is not actually a file"
323296
fi
324297
;;
325-
-v)
326-
local bake_version='1.8.2'
327-
printf '%s\n' "Version: $bake_version"
328-
;;
329298
-h)
330299
local flag_help='yes'
331300
if ! shift; then
@@ -342,7 +311,7 @@ __bake_parse_args() {
342311
BAKE_FILE="$BAKE_ROOT/${BAKE_FILE##*/}"
343312
else
344313
if ! BAKE_ROOT=$(
345-
while [ ! -f './Bakefile.sh' ] && [ "$PWD" != / ]; do
314+
while [ ! -f 'Bakefile.sh' ] && [ "$PWD" != / ]; do
346315
if ! cd ..; then
347316
exit 1
348317
fi
@@ -360,8 +329,8 @@ __bake_parse_args() {
360329
fi
361330

362331
if [ "$flag_help" = 'yes' ]; then
363-
cat <<-"EOF"
364-
Usage: bake [-h|-v] [-f <Bakefile>] [var=value ...] <task> [args ...]
332+
cat <<-EOF
333+
Usage: bake [-h] [-f <Bakefile>] [var=value ...] <task> [args ...]
365334
EOF
366335
__bake_print_tasks
367336
exit
@@ -374,27 +343,22 @@ __bake_parse_args() {
374343
# @internal
375344
__bake_main() {
376345
__bake_cfg_stacktrace='no'
377-
__bake_cfg_big_print='yes'
378346

379-
# Environment boilerplate
380347
set -ETeo pipefail
381348
shopt -s dotglob extglob globasciiranges globstar lastpipe shift_verbose
382-
export LANG='C' LC_CTYPE='C' LC_NUMERIC='C' LC_TIME='C' LC_COLLATE='C' \
383-
LC_MONETARY='C' LC_MESSAGES='C' LC_PAPER='C' LC_NAME='C' LC_ADDRESS='C' \
384-
LC_TELEPHONE='C' LC_MEASUREMENT='C' LC_IDENTIFICATION='C' LC_ALL='C'
349+
export LANG='C' LC_CTYPE='C' LC_NUMERIC='C' LC_TIME='C' LC_COLLATE='C' LC_MONETARY='C' \
350+
LC_MESSAGES='C' LC_PAPER='C' LC_NAME='C' LC_ADDRESS='C' LC_TELEPHONE='C' \
351+
LC_MEASUREMENT='C' LC_IDENTIFICATION='C' LC_ALL='C'
385352
trap '__bake_trap_err' 'ERR'
386-
trap ':' 'INT' # Ensure Ctrl-C ends up printing <- ERROR ==== etc.
387353
bake.cfg pedantic-task-cd 'no'
388354

389-
# Parse arguments
390355
# Set `BAKE_{ROOT,FILE}`
391356
BAKE_ROOT=; BAKE_FILE=
392357
__bake_parse_args "$@"
393358
if ! shift "$REPLY"; then
394359
__bake_internal_die 'Failed to shift'
395360
fi
396361

397-
# Set variables à la Make
398362
# shellcheck disable=SC1007
399363
local __bake_key= __bake_value=
400364
local __bake_arg=
@@ -412,8 +376,8 @@ __bake_main() {
412376
;;
413377
*) break
414378
esac done; unset -v __bake_arg
379+
# Note: Don't unset '__bake_variable' or none of the variables will stay set
415380
unset -v __bake_key __bake_value
416-
unset -vn __bake_variable
417381

418382
local __bake_task="$1"
419383
if [ -z "$__bake_task" ]; then
@@ -433,21 +397,6 @@ __bake_main() {
433397
__bake_task= source "$BAKE_FILE"
434398

435399
if declare -f task."$__bake_task" >/dev/null 2>&1; then
436-
local line=
437-
local shouldTestNextLine='no'
438-
while IFS= read -r line; do
439-
if [ "$shouldTestNextLine" = 'yes' ]; then
440-
if [[ $line == *'bake.cfg'*big-print*no* ]]; then
441-
__bake_cfg_big_print='no'
442-
fi
443-
shouldTestNextLine='no'
444-
fi
445-
446-
if [[ $line == @(task."$__bake_task"|init)*'('*')'*'{' ]]; then
447-
shouldTestNextLine='yes'
448-
fi
449-
done < "$BAKE_FILE"; unset -v line shouldTestNextLine
450-
451400
__bake_print_big "-> RUNNING TASK '$__bake_task'"
452401
if declare -f init >/dev/null 2>&1; then
453402
init "$__bake_task"

docs/reference.md

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,41 @@
11
## Index
22

3+
* [std.fprint_error()](#stdfprint_error)
4+
* [std.fprint_warn()](#stdfprint_warn)
5+
* [std.fprint_info()](#stdfprint_info)
36
* [std.find_parent_file()](#stdfind_parent_file)
47
* [std.find_parent_dir()](#stdfind_parent_dir)
5-
* [std.should_output_color()](#stdshould_output_color)
8+
* [std.should_print_color_stdout()](#stdshould_print_color_stdout)
9+
* [std.should_print_color_stderr()](#stdshould_print_color_stderr)
610
* [std.get_package_info()](#stdget_package_info)
711

12+
### std.fprint_error()
13+
14+
Prints a formatted error message
15+
16+
#### Arguments
17+
18+
* **$1** (name): of package
19+
* **$2** (message):
20+
21+
### std.fprint_warn()
22+
23+
Prints a formated warning message
24+
25+
#### Arguments
26+
27+
* **$1** (name): of package
28+
* **$2** (message):
29+
30+
### std.fprint_info()
31+
32+
Prints a formated log message
33+
34+
#### Arguments
35+
36+
* **$1** (name): of package
37+
* **$2** (message):
38+
839
### std.find_parent_file()
940

1041
Finds a parent file
@@ -17,10 +48,17 @@ Finds a parent file
1748

1849
Finds a parent directory
1950

20-
### std.should_output_color()
51+
### std.should_print_color_stdout()
52+
53+
Determine if color should be printed to standard output
54+
55+
_Function has no arguments._
56+
57+
### std.should_print_color_stderr()
58+
59+
Determine if color should be printed to standard error
2160

22-
Determine if color should be printed. Note that this doesn't
23-
use tput because simple environment variable checking heuristics suffice
61+
_Function has no arguments._
2462

2563
### std.get_package_info()
2664

examples/example.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# shellcheck shell=bash
2+
3+
for f in ./pkg/src/{public,util}/*.sh; do
4+
# shellcheck disable=SC1090
5+
source "$f"
6+
done; unset -v f
7+
8+
std.fprint_info 'hookah' 'other'

pkg/src/public/bash-std.sh

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

3+
# @description Prints a formatted error message
4+
# @arg $1 name of package
5+
# @arg $2 message
6+
std.fprint_error () {
7+
local custom="$1"
8+
local msg="$2"
9+
10+
if std.should_print_color_stderr; then
11+
printf "\033[1;31m%s (%s):\033[0m %s\n" 'Error' "$custom" "$msg" >&2
12+
else
13+
printf "%s (%s): %s\n" 'Error' "$custom" "$msg" >&2
14+
fi
15+
}
16+
17+
# @description Prints a formated warning message
18+
# @arg $1 name of package
19+
# @arg $2 message
20+
std.fprint_warn () {
21+
local custom="$1"
22+
local msg="$2"
23+
24+
if std.should_print_color_stderr; then
25+
printf "\033[1;33m%s (%s):\033[0m %s\n" 'Warn' "$custom" "$msg" >&2
26+
else
27+
printf "%s (%s): %s\n" 'Warn' "$custom" "$msg" >&2
28+
fi
29+
}
30+
31+
# @description Prints a formated log message
32+
# @arg $1 name of package
33+
# @arg $2 message
34+
std.fprint_info () {
35+
local custom="$1"
36+
local msg="$2"
37+
38+
if std.should_print_color_stdout; then
39+
printf "\033[1;32m%s (%s):\033[0m %s\n" 'Info' "$custom" "$msg"
40+
else
41+
printf "%s (%s): %s\n" 'Info' "$custom" "$msg"
42+
fi
43+
}
44+
345
# @description Finds a parent file
446
# @arg $1 File name
547
std.find_parent_file() {

0 commit comments

Comments
 (0)