-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatteryStatus.sh
More file actions
executable file
·174 lines (148 loc) · 4.02 KB
/
batteryStatus.sh
File metadata and controls
executable file
·174 lines (148 loc) · 4.02 KB
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
#!/bin/bash
# ##########
# # #
# # #
# ##########
# Help functions
function getCol () {
if [[ ! -v 1 ]]; then
echo "Chyba, nebyl dodán první argument"
exit 1
elif [[ ! -v 2 ]]; then
echo "Chyba, nebyl dodán druhý argument"
exit 1
fi
local colPart1=$1
local colPart2=$2
local colorPrefix='\033['
local colorSufix='m'
echo "${colorPrefix}${colPart1};${colPart2}${colorSufix}"
}
function echoCol () {
if [[ ! -v 1 ]]; then
echo "Chyba, nebyl dodán první argument (barva v escape formě)"
exit 1
elif [[ ! -v 2 ]]; then
echo "Chyba, nebyl dodán druhý argument (zpráva)"
exit 1
fi
local newColor=$1
local message=$2
local resetColor='\033[0m'
echo -e "${newColor}${message}${resetColor}"
}
function getEmptyString () {
local length=$1
printf '%*s' "$length"
}
function fillStringWith () {
local char=$1
local length=$2
local toReturn=""
for i in $(seq 1 ${length});
do
toReturn="${toReturn}${char}"
done
echo "${toReturn}"
}
# MAIN
# NEZNÁMÉ
# Colors
greenCol=$(getCol 0 32)
redCol=$(getCol 0 31)
yellowCol=$(getCol 1 33)
darkGrayCol=$(getCol 1 30)
lightGrayCol=$(getCol 0 37)
whiteCol=$(getCol 1 37)
# Battery constants
batteryCharging='Charging'
# Battery vars
batteryCapacity=$(cat /sys/class/power_supply/BAT0/capacity)
batteryStatus=$(cat /sys/class/power_supply/BAT0/status)
# Ascii art specifika
# ASCII barvy
artColCriticalPower=$redCol
artColNormalPower=$greenCol
artColChargingPower=$yellowCol
artColEmptyPower=$whiteCol
artColShell=$lightGrayCol
artColDelim=$darkGrayCol
# ASCII Fill
artFillShell='#'
artFillPower='X'
artFillEmptyPower='O'
artFillDelim='X'
# Upravitelné rozměry
artEmptySpace='6'
artBatShellWidth='4'
artBatProgressParts='7'
artBatProgressPartDelimWidth='1'
# Automaticky počítané rozměry
artBatOutShellHeight=$(( $artBatShellWidth * 2 ))
artBatProgressPartWidth=$(( $artBatShellWidth * 2 ))
# Barvy zástupců
artFillColShell="$(echoCol ${artColShell} "${artFillShell}")"
artFillColEmptyPower="$(echoCol ${artColEmptyPower} "${artFillEmptyPower}")"
artFillColDelim="$(echoCol ${artColDelim} "${artFillDelim}")"
# VYKRESLOVÁNÍ
echo -e "\n"
for i in $(seq 1 ${artBatShellWidth});
do
artRow="$(getEmptyString $artEmptySpace)$(getEmptyString $artBatShellWidth)"
for i in $(seq 1 ${artBatProgressParts});
do
if [[ ! i -eq artBatProgressParts ]];then
widthOfPart=$(( artBatProgressPartWidth + artBatProgressPartDelimWidth ))
else
widthOfPart=${artBatProgressPartWidth}
fi
artRow="${artRow}$(fillStringWith "${artFillColShell}" ${widthOfPart})"
done
echo -e "${artRow}"
done
for i in $(seq 1 ${artBatOutShellHeight});
do
artRow="$(getEmptyString $artEmptySpace)"
artRow="${artRow}$(fillStringWith "${artFillColShell}" ${artBatShellWidth})"
for i in $(seq 1 ${artBatProgressParts});
do
useCol=""
fillStr=""
batCap=$(( batteryCapacity/ $(( 100 / artBatProgressParts )) ))
if [[ batCap -lt 1 ]];then
useCol="${redCol}"
elif [[ $batteryStatus == $batteryCharging ]];then
useCol="${yellowCol}"
else
useCol="${greenCol}"
fi
if [[ batCap -ge $(( i-1 )) ]];then
fillStr="$(echoCol ${useCol} "${artFillPower}")"
elif [[ batCap -lt 1 ]] && [[ i -eq 1 ]];then
fillStr="$(echoCol ${useCol} "${artFillPower}")"
else
fillStr="${artFillColEmptyPower}"
fi
artRow="${artRow}$(fillStringWith ${fillStr} ${artBatProgressPartWidth})"
if [[ ! i -eq artBatProgressParts ]];then
artRow="${artRow}$(fillStringWith "${artFillColDelim}" $artBatProgressPartDelimWidth)"
fi
done
artRow="${artRow}$(fillStringWith "${artFillColShell}" ${artBatShellWidth})"
echo -e "${artRow}"
done
for i in $(seq 1 ${artBatShellWidth});
do
artRow="$(getEmptyString $artEmptySpace)$(getEmptyString $artBatShellWidth)"
for i in $(seq 1 ${artBatProgressParts});
do
if [[ ! i -eq artBatProgressParts ]];then
widthOfPart=$(( artBatProgressPartWidth + artBatProgressPartDelimWidth ))
else
widthOfPart=${artBatProgressPartWidth}
fi
artRow="${artRow}$(fillStringWith "${artFillColShell}" ${widthOfPart})"
done
echo -e "${artRow}"
done
echo -e "\n"