Skip to content

Commit ded00f0

Browse files
committed
feat: Add -P flag to print with newline
Before, `-p` would automatically print with newline. But this is problematic for functions like `term.cursor_down`, since only the escape without the newline would be emitted. This fixes that. This also is a breaking change because it no longer allows passing multiple arguments. That is, passing `-p -d` is no longer possible. It must be passed like `-pd` now. This simplifies the processing of arguments
1 parent f6e9a23 commit ded00f0

File tree

2 files changed

+22
-44
lines changed

2 files changed

+22
-44
lines changed

basalt.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ errexit = 'on'
2222
pipefail = 'on'
2323

2424
[run.shoptOptions]
25+
extglob = 'on'
2526
nullglob = 'on'
2627
shift_verbose = 'on'

pkg/src/util/util.sh

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ term.private_util_validate_p() {
77
if (($# - 1 > args_excluding_flags)); then
88
core.panic 'Incorrect argument count'
99
elif (($# - 1 == args_excluding_flags)); then
10-
if [[ "$1" == -p ]]; then
11-
flag_print='yes'
10+
if [[ $1 == -?(@(p|P)) ]]; then
11+
case $1 in
12+
*p*) flag_print='yes' ;;
13+
*P*) flag_print='yes-big' ;;
14+
esac
1215
REPLY_SHIFT=1
1316
else
1417
core.panic 'Invalid flag'
@@ -23,49 +26,19 @@ term.private_util_validate_pd() {
2326
local args_excluding_flags="$1"
2427
if ! shift; then core.panic 'Failed to shift'; fi
2528

26-
if (($# - 2 == args_excluding_flags)); then
27-
case $1 in
28-
-p) flag_print='yes' ;;
29-
-d) end=$'\e[0m' ;;
30-
-pd|-dp) flag_print='yes'; end=$'\e[0m' ;;
31-
*) core.panic 'Invalid flag' ;;
32-
esac
33-
case $2 in
34-
-p)
35-
if [ "$flag_print" = 'yes' ]; then
36-
core.panic "Flag '-p' was already specified"
37-
fi
38-
flag_print='yes'
39-
;;
40-
-d)
41-
if [ -n "$end" ]; then
42-
core.panic "Flag '-d' was already specified"
43-
fi
29+
if (($# - 1 == args_excluding_flags)); then
30+
if [[ $1 == -?(d|@(p|P)|d@(p|P)|@(p|P)d) ]]; then
31+
case $1 in
32+
*p*) flag_print='yes' ;;
33+
*P*) flag_print='yes-newline' ;;
34+
esac
35+
if [[ $1 == *d* ]]; then
4436
end=$'\e[0m'
45-
;;
46-
-pd|-dp)
47-
if [ "$flag_print" = 'yes' ]; then
48-
core.panic "Flag '-p' was already specified"
49-
fi
50-
if [ -n "$end" ]; then
51-
core.panic "Flag '-d' was already specified"
52-
fi
53-
flag_print='yes'
54-
end=$'\e[0m'
55-
;;
56-
*)
57-
core.panic 'Invalid flag'
58-
;;
59-
esac
60-
REPLY_SHIFT=2
61-
elif (($# - 1 == args_excluding_flags)); then
62-
case $1 in
63-
-p) flag_print='yes' ;;
64-
-d) end=$'\e[0m' ;;
65-
-pd|-dp) flag_print='yes'; end=$'\e[0m' ;;
66-
*) core.panic 'Invalid flag' ;;
67-
esac
68-
REPLY_SHIFT=1
37+
fi
38+
REPLY_SHIFT=1
39+
else
40+
core.panic 'Invalid flag'
41+
fi
6942
elif (($# > args_excluding_flags)); then
7043
core.panic 'Incorrect argument count'
7144
else
@@ -79,6 +52,8 @@ term.private_util_set_reply() {
7952

8053
REPLY="$value"
8154
if [ "$flag_print" = 'yes' ]; then
55+
printf '%s' "$REPLY"
56+
elif [ "$flag_print" = 'yes-newline' ]; then
8257
printf '%s\n' "$REPLY"
8358
fi
8459
}
@@ -93,6 +68,8 @@ term.private_util_set_reply2() {
9368
# shellcheck disable=SC2059
9469
printf -v REPLY "$@"
9570
if [ "$flag_print" = 'yes' ]; then
71+
printf '%s' "$REPLY"
72+
elif [ "$flag_print" = 'yes-newline' ]; then
9673
printf '%s\n' "$REPLY"
9774
fi
9875
}

0 commit comments

Comments
 (0)