git.lirion.de

Of git, get, and gud

aboutsummaryrefslogtreecommitdiffstats
path: root/fs/usr/lib/lirion/ln-initfunctions
blob: 81d74e6b6c046146db6378de499541b9be497d9a (plain)
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
#!/usr/bin/env bash
# shellcheck disable=SC2119,SC2120

# This serves as an alternative to "init functions". So the objective is to display
# an abstract about what a script is doing in the background and delivering a status.
# E.g.:
#   - lnbegin "Doing awesome stuff"
#   - do-something -q 2>/dev/null
#   - case "$?" in
#       0) lnok ;;
#       255) lnwarn "Something uncritical went wrong" ;;
#       *) lnfail "kaboom!" 101;;
#     esac
# 
# This script uses unicode characters. A "legacy" variant where we fall back
# to having e.g. "[ OK ]" instead of a checkmark is planned. (Earlier versions had that, too.)
#
# For a demo of the looks, use SCRIPTNAME -d.
#
# LICENCE: LGPLv3
# AUTHOR: coding æt lirion dot de

declare -x LNINITMSG

function lnbegin {
	printf "[⏲] %b\\033[s..." "$1"
	export LNINITMSG="$1"
}
function lnok {
	printf "\\r\\033[92m[✔] %b\\033[u\\033[K" "$LNINITMSG"
	[ -n "$1" ]&&printf " \\033[37m(%b)\\033[0m" "$1"
	printf "\\033[92m.\033[0m\\n"
	unset LNINITMSG
}
function lnsucc {
	lnok "$1"
}
function lninfo {
	printf "\\r[ℹ]\\033[u\\033[K"
	[ -n "$1" ]&&printf ": %b" "$1"
	printf ".\\n"
	unset LNINITMSG
}
function lnwarn {
	printf "\\r\\033[33m[\\033[1m⚠\\033[0m\\033[33m] %b\\033[u\\033[K" "$LNINITMSG"
	[ -n "$1" ]&&printf " \\033[37m(%b)\\033[0m" "$1"
	printf "\\033[33m.\\033[0m\\n"
	unset LNINITMSG
	return 255
}
function lnfail {
	printf "\\r\\033[31m[✖] \\033[31m%b\\033[u\\033[K" "$LNINITMSG" # was 91m before
	[ -n "$1" ]&&printf " \\033[37m(%b)\\033[0m" "$1"
	printf "\\033[31m.\\033[0m\\n"
	unset LNINITMSG
	[ -n "$2" ]&&return "$2"||return 1
}
function lnskip {
	printf "\\r\\033[37m[↪] %b\\033[u\\033[K" "$LNINITMSG"
	[ -n "$1" ]&&printf " \\033[37m(%b)\\033[0m" "$1"
	printf "\\033[37m.\033[0m\\n"
	unset LNINITMSG
}
function lnretry {
	printf "\\r\\033[36m[↻] %b\\033[u\\033[K" "$LNINITMSG"
	[ -n "$1" ]&&printf " \\033[37m(%b)\\033[0m" "$1"
	printf "\\033[36m.\\033[0m\\n"
	unset LNINITMSG
}
function lnquit {
	printf "\\r\\033[34m[⏻] %b\\033[u\\033[K" "$LNINITMSG"
	[ -n "$1" ]&&printf " \\033[37m(%b)\\033[0m" "$1"
	printf "\\033[34m.\\033[0m\\n"
	unset LNINITMSG
}
function lnprog {
	# here, we neither have a newline at the end of the function nor do we
	# remove the LNINITMSG variable. It is designated for additional
	# information _during execution_.
	[ -z "$1" ] && return 0
	printf "\\033[u\\033[K \\033[38;5;237m(%b)\\033[0m" "$1"
}
function lnsmil {
	printf "\\r\\033[35m[☺] %b\\033[u\\033[K" "$LNINITMSG"
	[ -n "$1" ]&&printf " \\033[37m(%b)\\033[0m" "$1"
	printf "\\033[35m.\\033[0m\\n"
	unset LNINITMSG
}
function demo {
	lnbegin "Progress message";sleep 0.31337;lnprog "additional progress message (does not terminate)";printf -- " -- \\n"
	lnbegin "OK message";sleep 0.31337;lnok "additional success message"
	lnbegin "Info message";sleep 0.31337;lninfo "information without further steps / return value"
	lnbegin "Warning message";sleep 0.31337;lnwarn "additional warning info"
	lnbegin "Failure message";sleep 0.31337;lnfail "additional failure info"
	lnbegin "Skip message";sleep 0.31337;lnskip "additional skip reason"
	lnbegin "Retry message";sleep 0.31337;lnretry "additional reason for new run"
	lnbegin "Quit message";sleep 0.31337;lnquit "additional quit information"
	lnbegin "Lulz";sleep 0.31337;lnsmil
	unset LNINITMSG
}
if [ "$1" == "-d" ] || [ "$1" == "--demo" ];then
	demo
	exit 0
fi