-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.sh
215 lines (182 loc) · 5.26 KB
/
lib.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# shell: bash zsh
# shellcheck shell=bash disable=2155
if [ "$BASH_VERSION" != "" ]; then
__DH_ROOT="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
else
__DH_ROOT="${funcsourcetrace[1]%/*}"
fi
source "$__DH_ROOT"/constants.sh
source "$__DH_ROOT"/utils.sh
# Clean the input message by removing all Dahlia codes.
#
# Cleans all Dahlia codes and unescapes escaped markers in the input message.
#
# Environment Variables:
# - DAHLIA_MARKER: The marker to be used. Processed with `__dh_setup_marker`.
#
# Arguments:
# $@: The input message to be cleaned. Read using `__dh_get_input`.
#
# Returns:
# The cleaned message.
#
# Usage:
# dahlia_clean "Hello, &RWorld!&r" #-> "Hello, World!"
# echo "Hello, &RWorld!&r" | dahlia_clean #-> "Hello, World!"
#
dahlia_clean() {
local marker=${DAHLIA_MARKER:-'&'}
if [ ${#marker} != 1 ]; then
__dh_error "Invalid marker '$marker'"
return 1
fi
local escaped_marker=$(__dh_escape "$marker")
# HACK: `\x90` is unused according to ASCII spec, so it's highly unlikely to be used as a marker
msg=$(__dh_get_input "$@" | sed -E $'s\x90'"${escaped_marker}${__DH_CODE_REGEX}"$'\x90\x90g')
echo -n "${msg//"${marker}_"/${marker}}"
}
# Cleans the input message by removing all ANSI codes.
#
# Arguments:
# $@: The input message to be cleaned.
#
# Returns:
# The cleaned message with all ANSI codes removed.
#
# Usage:
# dahlia_clean_ansi "Hello, \033[31mWorld!\033[0m" #-> "Hello, World!"
# echo "Hello, \033[31mWorld!\033[0m" | dahlia_clean_ansi #-> "Hello, World!"
#
dahlia_clean_ansi() {
__dh_get_input "$@" | sed -E "s/${__DH_ANSI_REGEX}//g"
}
# Prints short testing string that showcases all possible formatting codes.
#
# Returns:
# The exit status of the `dahlia_print` function. Should be always 0.
#
# Usage:
# dahlia_test #-> "0123456789abcdefhijklmno" with formatting
#
dahlia_test() {
local str=""
for ch in 0 1 2 3 4 5 6 7 8 9 a b c d e f; do
str+="&${ch}${ch}&rc"
done
for ch in h i j k l m n o; do
str+="&${ch}${ch}&r${ch}"
done
DAHLIA_MARKER='&' dahlia_print "$str"
return $?
}
# Prints the input message with Dahlia codes converted to ANSI.
#
# Internally it is the same as `dahlia_convert`, except that it appends trailing newline in same way
# as `echo`. Provided mostly for specification compliance.
#
# Arguments:
# $@: The input to be converted and printed.
#
# Returns:
# Prints the converted message and returns the exit status of the `dahlia_convert` function.
#
# Usage:
# dahlia_print "&2Hello, &5World!" #-> "Hello, World!\n"
#
dahlia_print() {
local msg
msg="$(dahlia_convert "$@")"
local code=$?
[ "$code" != 0 ] && return "$code"
echo "$msg"
}
# Convert the input message into ANSI format.
#
# Handles the conversion of Dahlia codes to ANSI codes.
#
# Environment Variables:
# - NO_COLOR: If set, the function will clean the input and return.
# - DAHLIA_AUTO_RESET: If not set, the function will set it to 1 by default.
# - DAHLIA_DEPTH: If not set, the function will set it to AUTO by default.
# - DAHLIA_MARKER: If not set, the function will set it to & by default.
#
# Arguments:
# $@: The input to be converted.
#
# Returns:
# Prints the converted message to stdout (no trailing newline).
# If no color mode is used, it returns the exit status of the `dahlia_clean` function.
# If the marker is invalid, it prints the error message to stderr and returns 1.
# If the depth or some code is invalid, it prints the error message to stderr and returns 1.
#
# Usage:
# dahlia_convert "&2Hello," "World!" #-> "Hello, World!"
#
dahlia_convert() {
# Default values
if [ "$NO_COLOR" != "" ]; then
dahlia_clean "$@"
return $?
fi
DAHLIA_DEPTH=${DAHLIA_DEPTH:-AUTO}
# Try to infer depth from envars
local parsed_depth
if ! parsed_depth=$(__dh_infer_depth); then
__dh_error "Invalid depth '$DAHLIA_DEPTH'"
return 1
fi
# If depth is parsed as 0, treat it as NO_COLOR
if [ "$parsed_depth" = 0 ]; then
dahlia_clean "$@"
return $?
fi
local marker=${DAHLIA_MARKER:-'&'}
if [ ${#marker} != 1 ]; then
__dh_error "Invalid marker '$marker'"
return 1
fi
local escaped_marker=$(__dh_escape "$marker")
# Load the message
local msg="$(__dh_get_input "$@")"
# Handle AUTO_RESET
local reset="${marker}R"
[[ ${DAHLIA_AUTO_RESET:-1} != 0 && $msg != *"$reset" ]] && msg+="$reset"
local regex="${escaped_marker}${__DH_CODE_REGEX}"
# For each code in the message
local ansi
while read -r code; do
# Try to convert it to ANSI
if ! ansi=$(__dh_get_ansi "${code/"$marker"/}" "$parsed_depth"); then
__dh_error "Invalid code '$code'"
return 1
fi
# Replace all occurences
msg="${msg//"$code"/$ansi}"
done < <(__dh_findall_regex "$msg" "$regex")
# Unescape markers
echo -n "${msg//"${marker}_"/"$marker"}"
}
# Prints a prompt to the user and returns the user's input.
#
# The prompt is converted using the `dahlia_convert` function.
#
# Arguments:
# $1: The prompt to be displayed to the user.
#
# Returns:
# Prints the user's input. Or if the conversion fails, returns the exit status of the
# `dahlia_convert` function.
#
# Usage:
# echo "John" | dahlia_input "&2Enter your name: " #-> Enter your name: John
#
dahlia_input() {
# Try to print the prompt
dahlia_convert "$1"
local code=$?
[ "$code" != 0 ] && return "$code"
# Read from stdin
local msg
read -r msg
echo -n "$msg"
}