-
Notifications
You must be signed in to change notification settings - Fork 0
/
padd_size_checker.sh
executable file
·156 lines (133 loc) · 4.35 KB
/
padd_size_checker.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
#!/usr/bin/env sh
SizeChecker(){
console_height=$(stty size | awk '{ print $1 }')
console_width=$(stty size | awk '{ print $2 }')
# Mega
if [ "$console_width" -ge "80" ] && [ "$console_height" -ge "26" ]; then
padd_size="mega"
width=80
height=26
# Below Mega. Gives you Regular.
elif [ "$console_width" -ge "60" ] && [ "$console_height" -ge "22" ]; then
padd_size="regular"
width=60
height=22
# Below Regular. Gives you Slim.
elif [ "$console_width" -ge "60" ] && [ "$console_height" -ge "21" ]; then
padd_size="slim"
width=60
height=21
# Below Slim. Gives you Tiny.
elif [ "$console_width" -ge "53" ] && [ "$console_height" -ge "20" ]; then
padd_size="tiny"
width=53
height=20
# Below Tiny. Gives you Mini.
elif [ "$console_width" -ge "40" ] && [ "$console_height" -ge "18" ]; then
padd_size="mini"
width=40
height=18
# Below Mini. Gives you Micro.
elif [ "$console_width" -ge "30" ] && [ "$console_height" -ge "16" ]; then
padd_size="micro"
width=30
height=16
# Below Micro, Gives you Nano.
elif [ "$console_width" -ge "24" ] && [ "$console_height" -ge "12" ]; then
padd_size="nano"
width=24
height=12
# Below Nano. Gives you Pico.
elif [ "$console_width" -ge "20" ] && [ "$console_height" -ge "10" ]; then
padd_size="pico"
width=20
height=10
# Below Pico. Gives you nothing...
else
# Nothing is this small, sorry
padd_size="ant"
width=0
height=0
fi
}
GenerateOutput() {
# Clear the screen and move cursor to (0,0).
# This mimics the 'clear' command.
# https://vt100.net/docs/vt510-rm/ED.html
# https://vt100.net/docs/vt510-rm/CUP.html
# E3 extension `\e[3J` to clear the scrollback buffer (see 'man clear')
printf '\e[H\e[2J\e[3J'
# draw the PADD field
row=1
column=1
while [ "$row" -le ${height} ]
do
while [ "$column" -le ${width} ]
do
if [ "$row" = 1 ] || [ "$row" = "${height}" ] || [ "$column" = 1 ] || [ "$column" = "${width}" ]; then
printf "*"
else
printf " "
fi
column=$((column+1))
done
# don't add a new line below the PADD field
if [ "$row" -lt "${height}" ]; then
echo
fi
column=1
row=$((row+1))
done
# draw info within the PADD field
tput cup 2 2; printf "Console width: %s" "${console_width}"
tput cup 3 2; printf "Console height: %s" "${console_height}"
tput cup 4 2; printf "PADD width: %s" "${width}"
tput cup 5 2; printf "PADD height: %s" "${height}"
tput cup 6 2; printf "PADD size: %s" "${padd_size}"
# move the cursor to the lower rigth corner
tput cup $((height-1)) $((width-2))
}
CleanExit(){
# save the return code of the script
err=$?
#clear the line
printf '\e[0K\n'
# Show the cursor
# https://vt100.net/docs/vt510-rm/DECTCEM.html
printf '\e[?25h'
# if background sleep is running, kill it
# http://mywiki.wooledge.org/SignalTrap#When_is_the_signal_handled.3F
kill "${sleepPID}" > /dev/null 2>&1
exit $err # exit the script with saved $?
}
TerminalResize(){
# if a terminal resize is trapped, kill the sleep function within the
# loop to trigger SizeChecker
kill "${sleepPID}" > /dev/null 2>&1
}
######### MAIN #########
# Hide the cursor.
# https://vt100.net/docs/vt510-rm/DECTCEM.html
printf '\e[?25l'
# Trap on exit
trap 'CleanExit' INT TERM EXIT
# Trap the window resize signal (handle window resize events)
trap 'TerminalResize' WINCH
while :; do
SizeChecker
GenerateOutput
# Sleep infinity
# sending sleep in the background and wait for it
# this way the TerminalResize trap can kill the sleep
# and force a instant re-draw of the dashboard
# https://stackoverflow.com/questions/32041674/linux-how-to-kill-sleep
#
# saving the PID of the background sleep process to kill it on exit and resize
sleep infinity &
sleepPID=$!
wait $!
# when the sleep is killed by the trap, a new round starts
done
# Hint: to resize another terminal (e.g. connected via SSH)
# printf '\e[8;16;30t' > /dev/pts/0
# The device file descriptor can be obtained by `tty``