-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo-helper.sh
143 lines (124 loc) · 2.75 KB
/
demo-helper.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
#!/usr/bin/env bash
# 📌 Original file at:
# https://github.com/rytswd/cli-demo-helper
###---- ⬇️ HOW TO USE ⬇️ ---------------------------------------------------###
#
# Simply source this file to import functions, and use them like:
#
# comment "This is a comment! About to start command..."
# execute "ls -la"
#
# You can configure speed, prompt, etc.
# For more information, please check out:
# https://github.com/rytswd/cli-demo-helper
#
###---- ⬇️ SETUP ⬇️ --------------------------------------------------------###
# Colour setup
__reset=$(tput sgr0)
__red=$(
tput bold
tput setaf 1
)
__green=$(
tput bold
tput setaf 2
)
__blue=$(
tput bold
tput setaf 6
)
__yellow=$(
tput bold
tput setaf 3
)
__white=$(
tput bold
tput setaf 7
)
readonly reset=$__reset
readonly red=$__red
readonly green=$__green
readonly blue=$__blue
readonly yellow=$__yellow
readonly white=$__white
# Speed
type_speed=${demo_helper_type_speed:="1200"} # In characters per min notation
interval="100" # In ms
# Prompt
prompt=${demo_helper_prompt:="$yellow$ $reset"}
###---- ⬇️ EXTRA CONFIGURATION ⬇️ ------------------------------------------###
# TBD
###---- ⬇️ Internal Prep ⬇️ ------------------------------------------------###
sleep_duration=$(echo "scale=2 ; 60 / $type_speed" | bc)
# type_out imitates typing out the provided string
function type_out() {
s=$*
while [ ${#s} -gt 0 ]; do
printf '%.1s' "$s"
s=${s#?}
sleep "$sleep_duration"
done
}
# write_prompt writes PS1 like prompt
function write_prompt() {
echo -n "$prompt"
}
###---- ⬇️ MAIN FUNCTIONS ⬇️ -----------------------------------------------###
function clear_terminal() {
clear
write_prompt
}
# comment writes hash and then any content as comment
function comment() {
echo -n "$blue# "
type_out "$*"
echo " $reset"
write_prompt
read -rs
}
# comment_r writes comment in red
function comment_r() {
echo -n "$red# "
type_out "$*"
echo " $reset"
write_prompt
read -rs
}
# comment_g writes comment in green
function comment_g() {
echo -n "$green# "
type_out "$*"
echo " $reset"
write_prompt
read -rs
}
# comment_b writes comment in blue
function comment_b() {
echo -n "$blue# "
type_out "$*"
echo " $reset"
write_prompt
read -rs
}
# comment_w writes comment in white
function comment_w() {
echo -n "$white# "
type_out "$*"
echo " $reset"
write_prompt
read -rs
}
function execute() {
type_out "$*"
read -rs
echo
eval "$*"
r=$?
echo
write_prompt
read -rs
return $r
}
# Ensure the process starts with some prompt
write_prompt
trap "echo" EXIT