-
Notifications
You must be signed in to change notification settings - Fork 4
/
androfetch
executable file
·362 lines (299 loc) · 8.84 KB
/
androfetch
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/data/data/com.termux/files/usr/bin/bash
# Author : laraib07 (https://github.com/laraib07)
readonly CACHE_DIR="$HOME/.cache/androfetch"
readonly CACHE_FILE="$CACHE_DIR/cache"
declare -A cache
# Colors
G='\033[1;32m' #green
W='\033[1;37m' #bold white
off='\033[0m' #simple white
## FUNCTIONS
#Info printing
function fetch() {
# Hide the cursor and disable line wrap.
printf '\e[?25l\e[?7l\n'
# loop up to length of ascii_logo array
# print logo and strings according to index
for i in "${!ascii_logo[@]}";do
printf "${G}${ascii_logo[$i]}${off} ${info[$i]} \n" #print ascii_logo and info strings
done
printf "${off}" #turn off colors
# unhide the cursor and unable line wrap.
printf "\e[?25h\e[?7h\n"
}
function show_help() {
printf "A pretty system information tool written in bash for termux on android.\n"
printf "Usage: androfetch [-hcit] [-n [name]] [-l [filepath]]
-h\t print this usage.
-c\t build new cache.
-i\t toggle icon visibility.
-t\t toggle description title visibility.
-n\t set user name.
-l\t set logo.\n"
}
function error() {
printf "$(basename "$0"): $*\n" >&2
exit 1
}
# Create an array of all the strings to be fetched
# add icons and description title if toggled
function load_icons_and_strings() {
# Meslo Nerd font preferred for powerline icons
# if you don't use above font you can toggle
# icons to disable it with ’androfetch -i’
# by default its enabled.
# 2 white-spaces are intentional to make whole output pretty with reasonable spacing
if [[ "${cache[Icon_Toggle]}" == "1" ]]; then
OS="${W}\uf6a6 ${off}"
HOST="${W}\uf6c3 ${off}"
KER="${W}\uf83c ${off}"
UPT="${W}\uf49b ${off}"
PKGS="${W}\uf8d6 ${off}"
SH="${W}\ue7a2 ${off}"
TERMINAL="${W}\uf489 ${off}"
CHIP="${W}\uf2db ${off}"
CPU="${W}\ue266 ${off}"
fi
# Load description titles
if [[ "${cache[Desc_Title_Toggle]}" == "1" ]]; then
OS+="${G}OS${off}: "
HOST+="${G}Host${off}: "
KER+="${G}Kernel${off}: "
UPT+="${G}Uptime${off}: "
PKGS+="${G}Packages${off}: "
SH+="${G}Shell${off}: "
TERMINAL+="${G}Terminal${off}: "
CHIP+="${G}Chipset${off}: "
CPU+="${G}CPU${off}: "
fi
info=( \
"${G}${cache[user]}${W}@${G}localhost" \
"${W}-----------------" \
"${OS}${cache[OS]}" \
"${HOST}${cache[Model]}" \
"${KER}${cache[Kernel]}" \
"${UPT}${uptime}" \
"${PKGS}${pkg}" \
"${SH}${cache[Shell]}" \
"${TERMINAL}termux" \
"${CHIP}${cache[Chipset]}" \
"${CPU}${cache[CPU]}" \
)
}
# Set logo to default logo
function load_default_logo() {
# default ascii_logo
ascii_logo=( \
" ╲ ▁▂▂▂▁ ╱ " \
" ▄███████▄ " \
" ▄██ ███ ██▄ " \
" ▄███████████▄ " \
" ▄█ ▄▄▄▄▄▄▄▄▄▄▄▄▄ █▄ " \
" ██ █████████████ ██ " \
" ██ █████████████ ██ " \
" ██ █████████████ ██ " \
" █████████████ " \
" ███████████ " \
" ██ ██ " \
" ██ ██ " \
)
}
# If custom logo array has less indexes than info array
# Make it equal to number of indexes of info array
# Else info will not be indented properly
function match_arr_length() {
# this will add lines containing whitespace to custom_logo
logo_width=${#ascii_logo[0]} #width of logo (columns)
while [ ${#ascii_logo[@]} -lt ${#info[@]} ]; do
ascii_logo+=("$(printf "%${logo_width}s")") # number of whitespaces is equal to logo width
done
}
# Set custom logo
# use default logo if reset is passed in argument
# it will create an array from file with every line as
# seperate element
#
# Logo file format
# check sample_logo file
#
#
# ╲ ▁▂▂▂▁ ╱
# ▄███████▄
# ▄██ ███ ██▄
# ▄███████████▄
# ▄█ ▄▄▄▄▄▄▄▄▄▄▄▄▄ █▄
# ██ █████████████ ██
# ██ █████████████ ██
# ██ █████████████ ██
# █████████████
# ███████████
# ██ ██
# ██ ██
# without quoting or commenting
# add whitespaces to make every line equal in width
# do not use broad logo which will cover significant space.
# although you can use as many lines as you like like
# command: androfetch -l <filepath>
function set_logo() {
if [[ -z "$1" ]]; then
load_default_logo
else
if [ -f "$1" ];then
unset ascii_logo #unset current logo
while IFS= read -r line; do
ascii_logo+=("$line")
done < "$1"
match_arr_length
else
error "file not found"
fi
fi
create_cache
}
# toggle values of different components.
function toggle_value() {
if [[ "${cache[$1]}" == "0" ]];then
cache[$1]=1
else
cache[$1]=0
fi
create_cache
}
# Set custom user name
# using reset as username will set it to default
function set_user() {
if [[ -z "$1" ]]; then
cache[user]=$(logname)
else
cache[user]=$1
fi
create_cache
}
# Get shell name used by user
# and get version info if its zsh, bash or fish.
function get_shell() {
case ${shell:=${SHELL##*/}} in
bash)
ver=$("$SHELL" -c "printf %s \"\$BASH_VERSION\"")
shell+=" "${ver/-*}
shell=${shell/\(*\)}
;;
zsh)
shell=$("$SHELL" --version)
shell=${shell% *} # removing extra stuff
;;
fish)
ver=$("$SHELL" --version)
shell+=" ${ver##* }"
;;
esac
printf "${shell}"
}
#Get kernel info
function get_kernel() {
kernel=$(uname -r)
printf "${kernel%%-*}"
}
#Get CPU info
function get_cpu() {
cpuid=$(grep "Hardware" /proc/cpuinfo)
cpuid=${cpuid##?*: }
printf "${cpuid%,*}"
}
# Load frequently updated info like packages and uptime
function load_non_cache_variables() {
#Packages
pkg="$(grep -c "Package:" $PREFIX/var/lib/dpkg/status) (dpkg)"
#Uptime
uptime=$(uptime -p)
uptime=${uptime:3} # removing 'up '
uptime=${uptime//s/} # removing every 's'
uptime=${uptime/week/w}
uptime=${uptime/day/d}
uptime=${uptime/hour/h}
uptime=${uptime/minute/m}
}
#Load infrequent updated info like android
# version cpu chipset and icon_toggle.
function load_cache_variables() {
cache=( \
[user]="${cache[user]:=$(logname)}" \
[OS]="Android $(getprop ro.build.version.release)" \
[Model]="$(getprop ro.product.manufacturer) $(getprop ro.product.model)" \
[Chipset]="$(getprop ro.board.platform)" \
[Kernel]="$(get_kernel)" \
[Shell]="$(get_shell)" \
[CPU]="$(get_cpu)" \
[Icon_Toggle]="${cache[Icon_Toggle]:=1}" \
[Desc_Title_Toggle]="${cache[Desc_Title_Toggle]:=1}" \
)
}
# Create cache
function create_cache() {
# For info
printf "cache=($(for i in "${!cache[@]}";do printf "[$i]='${cache[$i]}' ";done))\n" > "$CACHE_FILE"
# For icon
printf "ascii_logo=($(for i in "${ascii_logo[@]}";do printf "\'$i\' ";done))\n" >> "$CACHE_FILE"
}
function getopts_get_optional_argument() {
eval next_token=\${$OPTIND}
if [[ -n $next_token && $next_token != -* ]]; then
OPTIND=$((OPTIND + 1))
OPTARG=$next_token
else
OPTARG=""
fi
}
# Driver code
function main() {
# checking cache dir
if ! [ -d "$CACHE_DIR" ];then
mkdir -p "$CACHE_DIR"
fi
# load cache variables first if cache don't exists
# else source cache
# It's important to do this before anything else
# So custom username and icon_toggle's value don't get deleted
if [ ! -f "$CACHE_FILE" ];then
# This block is executed only on very first run
# It will set all default options
load_cache_variables
load_default_logo # use default logo
else
source $CACHE_FILE
fi
while getopts ":hcinlt" arg ; do
case "${arg}" in
h ) show_help; exit 0 ;;
c ) rm $CACHE_FILE; load_cache_variables ;;
i ) toggle_value "Icon_Toggle" ;;
t ) toggle_value "Desc_Title_Toggle" ;;
n )
getopts_get_optional_argument $@
set_user "$OPTARG"
;;
l )
use_custom_logo=1
getopts_get_optional_argument $@
logo_file="$OPTARG"
;;
: ) error "option needs an argument.";;
\?) error "unknown option." ;;
esac
done
# Create cache if not found
if [ ! -f "$CACHE_FILE" ];then
create_cache
fi
load_non_cache_variables # load non-cached variables
load_icons_and_strings # create array of strings to fetch
# Always set logo after everything else, just before fetching
# to align info with logo
if [[ -n $use_custom_logo ]];then
set_logo $logo_file
fi
## Start fetching
fetch
exit 0
}
main "$@"