-
Notifications
You must be signed in to change notification settings - Fork 8
/
ramfetch4christmas
145 lines (130 loc) · 7.33 KB
/
ramfetch4christmas
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
#!/usr/bin/env bash
# MIT License
#
# Copyright (c) 2022 gentoo-btw
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# colors for tty sessions
if [[ "$TERM" = "linux" ]]; then
BLK='\e[0;30m'
RED='\e[0;31m'
GRN='\e[1;32m'
YEL='\e[1;33m'
BLU='\e[0;34m'
PURP='\e[0;35m'
CYN='\e[0;36m'
WHT='\e[0;37m'
else
# colors for gui
BLK='\e[0;30m'
RED='\e[0;31m'
GRN='\e[2;32m'
YEL='\e[0;33m'
BLU='\e[0;34m'
PURP='\e[0;35m'
CYN='\e[0;36m'
WHT='\e[0;37m'
fi
# blue arrow
BLUE_ARROW="$(printf ${BLU}"->"${WHT})"
# sets version
RAMFETCH_VER="v1.0.1"
# help and version
[[ $@ =~ --help|-h ]] && {
cat <<EOF
$BLUE_ARROW ramfetch version $RAMFETCH_VER
Usage:
--help (or -h for short) -- shows this help
--version (or -v for short) -- shows version
EOF
exit 0
}
[[ $@ =~ --version|-v ]] && {
cat <<EOF
$BLUE_ARROW ramfetch version $RAMFETCH_VER. made by gentoo-btw
https://github.com/gentoo-btw/ramfetch
EOF
exit 0
}
# Sed input. Basically use regex to get the number + kB part and replace it itself to ensure the capture group is printed
# 's/[^0-9]*\([0-9]* kB\)/\1/'
# 's' - subsitution
# '/' - regex start
# '[^0-9]*' - match all except 0-9 zero or more times
# '\' - escape parenthesis
# '(' - capture group, everything inside will be saved as result of it
# '[0-9]*' match 0-9 zero or more times exluding unit (' kB') [unit is always kB]
# '\' - escape parenthesis
# ')' - capture group close
# ' kB' -
# '/' subsitution end, replace start
# '\1' - everything from capture group
# '/' - end replace
# Fetches memory information from /proc/meminfo
# Desired feild captured with case, sed extracts value
# numfmt converts kB to more readable format
while read -r REPLY; do
case "${REPLY}" in
(MemTotal:*) memtotal=$(echo "$REPLY" | sed 's/[^0-9]*\([0-9]*\) kB/\1/' | numfmt --to=iec-i --from-unit=1024 --suffix=B --format="%9.2f" --invalid='ignore') ;;
(MemFree:*) memfree=$(echo "$REPLY" | sed 's/[^0-9]*\([0-9]*\) kB/\1/' | numfmt --to=iec-i --from-unit=1024 --suffix=B --format="%9.2f" --invalid='ignore') ;;
(MemAvailable:*) memavailable=$(echo "$REPLY" | sed 's/[^0-9]*\([0-9]*\) kB/\1/' | numfmt --to=iec-i --from-unit=1024 --suffix=B --format="%9.2f" --invalid='ignore') ;;
(HugePages_Free:*) hugepg_free=$(echo "$REPLY" | sed 's/[^0-9]*\([0-9]*\)/\1/' | numfmt --to=iec-i --from-unit=1024 --suffix=B --format="%9.2f" --invalid='ignore') ;;
(HugePages_Total:*) hugepg_total=$(echo "$REPLY" | sed 's/[^0-9]*\([0-9]*\)/\1/' | numfmt --to=iec-i --from-unit=1024 --suffix=B --format="%9.2f" --invalid='ignore') ;;
(Buffers:*) buffers=$(echo "$REPLY" | sed 's/[^0-9]*\([0-9]*\) kB/\1/' | numfmt --to=iec-i --from-unit=1024 --suffix=B --format="%9.2f" --invalid='ignore') ;;
(Cached:*) cached=$(echo "$REPLY" | sed 's/[^0-9]*\([0-9]*\) kB/\1/' | numfmt --to=iec-i --from-unit=1024 --suffix=B --format="%9.2f" --invalid='ignore') ;;
(SwapTotal:*) swaptotal=$(echo "$REPLY" | sed 's/[^0-9]*\([0-9]*\) kB/\1/' | numfmt --to=iec-i --from-unit=1024 --suffix=B --format="%9.2f" --invalid='ignore') ;;
(SwapFree:*) swapfree=$(echo "$REPLY" | sed 's/[^0-9]*\([0-9]*\) kB/\1/' | numfmt --to=iec-i --from-unit=1024 --suffix=B --format="%9.2f" --invalid='ignore') ;;
(Zswap:*) zswap=$(echo "$REPLY" | sed 's/[^0-9]*\([0-9]*\) kB/\1/' | numfmt --to=iec-i --from-unit=1024 --suffix=B --format="%9.2f" --invalid='ignore') ;;
(Zswapped:*) zswapped=$(echo "$REPLY" | sed 's/[^0-9]*\([0-9]*\) kB/\1/' | numfmt --to=iec-i --from-unit=1024 --suffix=B --format="%9.2f" --invalid='ignore') ;;
(SwapCached:*) swapcached=$(echo "$REPLY" | sed 's/[^0-9]*\([0-9]*\) kB/\1/' | numfmt --to=iec-i --from-unit=1024 --suffix=B --format="%9.2f" --invalid='ignore') ;;
(Dirty:*) dirty=$(echo "$REPLY" | sed 's/[^0-9]*\([0-9]*\) kB/\1/' | numfmt --to=iec-i --from-unit=1024 --suffix=B --format="%9.2f" --invalid='ignore') ;;
(KernelStack:*) kernel_stack=$(echo "$REPLY" | sed 's/[^0-9]*\([0-9]*\) kB/\1/' | numfmt --to=iec-i --from-unit=1024 --suffix=B --format="%9.2f" --invalid='ignore') ;;
(Percpu:*) per_cpu=$(echo "$REPLY" | sed 's/[^0-9]*\([0-9]*\) kB/\1/' | numfmt --to=iec-i --from-unit=1024 --suffix=B --format="%9.2f" --invalid='ignore') ;;
esac
done < /proc/meminfo
# colorblocks/hostname, and the green arrow
GRN_ARROW="$(printf ${GRN}"->"${WHT})"
HOSTNAME="$(hostname)"
host_user_len=$(echo ${#HOSTNAME} + ${#USER} + 1 | bc)
host_user="$(printf "%b%s%b%c%b%s%b" "${RED}" "$USER" "${WHT}" "@" "${GRN}" "$HOSTNAME" "${WHT}")"
colorblocks="$(printf "%b%s%b%s%b%s%b%s%b%s%b%s%b%s%b%s%b" "${BLK}" "███" "${RED}" "███" "${GRN}" "███" "${BLU}" "███" "${YEL}" "███" "${PURP}" "███" "${CYN}" "███" "${WHT}" "███" "${WHT}")"
# for ascii logo
yellowpart="$(printf "%b" "${YEL}")"
greenpart="$(printf "%b" "${GRN}")"
end="$(printf "%b" "${WHT}")"
# output
cat <<EOF
$greenpart *$end $host_user
$greenpart ***$end $(printf '%0.s-' $(seq 1 $host_user_len))
$greenpart **o**$end RAM Total $GRN_ARROW $(printf "\t") ${memtotal:-null}
$greenpart *******$end RAM Free $GRN_ARROW $(printf "\t") ${memfree:-null}
$greenpart *****o***$end RAM Available $GRN_ARROW $(printf "\t") ${memavailable:-null}
$greenpart *************$end Buffers $GRN_ARROW $(printf "\t") ${buffers:-null}
$greenpart ***************$end Cached $GRN_ARROW $(printf "\t") ${cached:-null}
$greenpart ***o*************$end Swap Total $GRN_ARROW $(printf "\t") ${swaptotal:-null}
$greenpart ************o******$end Swap Free $GRN_ARROW $(printf "\t") ${swapfree:-null}
$greenpart*****************o***$end Zswap $GRN_ARROW $(printf "\t\t") ${zswap:-null}
$yellowpart MmM$end Zswapped $GRN_ARROW $(printf "\t") ${zswapped:-null}
Swap Cached $GRN_ARROW $(printf "\t") ${swapcached:-null}
Dirty $GRN_ARROW $(printf "\t\t") ${dirty:-null}
Kernel Stack $GRN_ARROW $(printf "\t") ${kernel_stack:-null}
Per CPU $GRN_ARROW $(printf "\t") ${per_cpu:-null}
HugePages Free $GRN_ARROW $(printf "") ${hugepg_free:-null}
HugePages Total $GRN_ARROW $(printf "") ${hugepg_total:-null}
$colorblocks
EOF