-
Notifications
You must be signed in to change notification settings - Fork 0
/
message
76 lines (66 loc) · 2 KB
/
message
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
if shellm-ndef; then
shellm-define
## @file message.sh
## @brief Provide useful message functions
shellm source core/format.sh
## @fn void color (color, message)
## @brief Set the color to color, and output message if given
## @param $1 Color to use (see baseColor(3) and envColor(3))
## @param [$2] Message to display (reset to default color after display)
## @return Echo: Color code [and message]
color() { [ -z "$2" ] && echo -e -n "$1" || echo -e "$1$2\033[00m"; }
## @fn int final (exit_code)
## @brief Exit if subshell, return if interactive shell
## @param [$1] Exit code
## @return Code: exit_code (default 1)
final() {
case "${0#-}" in
bash|zsh|csh|ksh|sh|dash)
kill -INT $$ ;;
*) exit "${1:-1}" ;;
esac
}
_status_message() {
local msg="$1"
shift
format "$@" >&2
printf " %-7s " "${msg}" >&2
format R
printf ' '; shift
}
## @fn void err (error)
## @brief Output message on stderr (&2)
## @param $1 Message to display
## @return Echo: message on stderr
err() {
[ $# -gt 1 ] && { _status_message "$1" "${c_error:-$c_red}"; shift; }
echo -e "$1" >&2
}
## @fn int die (error, exit_code)
## @brief Output message on stderr (&2), then exit/return
## @param $1 Message to display
## @return Code: exit_code (default 1)
## @return Echo: message on stderr
die() { err "$1"; final "${2:-1}"; }
## @fn void pause (message)
## @brief Output message on stderr (&2), then wait for Enter
## @param $1 Message to display
## @return Echo: message on stderr
pause() { err "$1"; read -p "Press Enter -- "; }
## @fn void line (char)
## @brief Print a line composed of character char
## @param $1 Character to use
## @return Echo: a line on stderr
line() {
local l=${#1} c
c=$(/usr/bin/tput cols)
if [ "$l" -gt 1 ]; then
local n=$((c/l)); local r=$((c-(n*l)))
# shellcheck disable=SC2031,SC2030
err "$(for i in {0..$n}; do echo -n "$1"; done; echo -n "${1:0:$r}")"
else
# shellcheck disable=SC2031,SC2030
err "$(for i in {0..$c}; do echo -n "$1"; done)"
fi
}
fi # __IO_MESSAGE_SH