Files
2024-08-30 09:44:33 +02:00

147 lines
8.1 KiB
Bash
Executable File

#!/bin/sh
# 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='\033[0;30m'
RED='\033[0;31m'
GRN='\033[1;32m'
YEL='\033[1;33m'
BLU='\033[0;34m'
PURP='\033[0;35m'
CYN='\033[0;36m'
WHT='\033[0;37m'
else
# colors for gui
BLK='\033[0;30m'
RED='\033[0;31m'
GRN='\033[2;32m'
YEL='\033[0;33m'
BLU='\033[0;34m'
PURP='\033[0;35m'
CYN='\033[0;36m'
WHT='\033[0;37m'
fi
# blue arrow
BLUE_ARROW="$(printf "%b->%b" "${BLU}" "${WHT}")"
# sets version
RAMFETCH_VER="v1.0.2"
# help and version
if [ "$*" = "--help" ] || [ "$*" = "-h" ]; then
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
fi
if [ "$*" = "--version" ] || [ "$*" = "-v" ]; then
cat <<EOF
$BLUE_ARROW ramfetch version $RAMFETCH_VER. made by gentoo-btw
https://github.com/gentoo-btw/ramfetch
EOF
exit 0
fi
# 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:*) MEM_TOTAL=$(printf "%s" "$REPLY" | sed 's/[^0-9]*\([0-9]*\) kB/\1/' | numfmt --to=iec-i --from-unit=1024 --suffix=B --format="%9.2f" --invalid='ignore') ;;
(MemFree:*) MEM_FREE=$(printf "%s" "$REPLY" | sed 's/[^0-9]*\([0-9]*\) kB/\1/' | numfmt --to=iec-i --from-unit=1024 --suffix=B --format="%9.2f" --invalid='ignore') ;;
(MemAvailable:*) MEM_AVAILABLE=$(printf "%s" "$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:*) HUGEPAGES_FREE=$(printf "%s" "$REPLY" | sed 's/[^0-9]*\([0-9]*\)/\1/' | numfmt --to=iec-i --from-unit=1024 --suffix=B --format="%9.2f" --invalid='ignore') ;;
(HugePages_Total:*) HUGEPAGES_TOTAL=$(printf "%s" "$REPLY" | sed 's/[^0-9]*\([0-9]*\)/\1/' | numfmt --to=iec-i --from-unit=1024 --suffix=B --format="%9.2f" --invalid='ignore') ;;
(Buffers:*) BUFFERS=$(printf "%s" "$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=$(printf "%s" "$REPLY" | sed 's/[^0-9]*\([0-9]*\) kB/\1/' | numfmt --to=iec-i --from-unit=1024 --suffix=B --format="%9.2f" --invalid='ignore') ;;
(SwapTotal:*) SWAP_TOTAL=$(printf "%s" "$REPLY" | sed 's/[^0-9]*\([0-9]*\) kB/\1/' | numfmt --to=iec-i --from-unit=1024 --suffix=B --format="%9.2f" --invalid='ignore') ;;
(SwapFree:*) SWAP_FREE=$(printf "%s" "$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=$(printf "%s" "$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=$(printf "%s" "$REPLY" | sed 's/[^0-9]*\([0-9]*\) kB/\1/' | numfmt --to=iec-i --from-unit=1024 --suffix=B --format="%9.2f" --invalid='ignore') ;;
(SwapCached:*) SWAP_CACHED=$(printf "%s" "$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=$(printf "%s" "$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=$(printf "%s" "$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=$(printf "%s" "$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 "%b->%b" "${GRN}" "${WHT}")"
HOSTNAME="$(hostname)"
HOST_AND_USER_LEN=$(echo ${#HOSTNAME} + ${#USER} + 1 | bc)
HOST_AND_USER="$(printf "%b%s%b%c%b%s%b" "${GRN}" "$USER" "${WHT}" "@" "${YEL}" "$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
YELLOW_PART="$(printf "%b" "${YEL}")"
GREEN_PART="$(printf "%b" "${GRN}")"
END="$(printf "%b" "${WHT}")"
# output
cat <<EOF
$GREEN_PART ████████████████████████████ $HOST_AND_USER
$GREEN_PART ████████████████████████████████$END $(printf '%0.s-' $(seq 1 "$HOST_AND_USER_LEN"))
$GREEN_PART ████ ████ ███ ███$END RAM Total $GRN_ARROW $(printf "\t") ${MEM_TOTAL:-null}
$GREEN_PART ████ ████ ███ ███$END RAM Free $GRN_ARROW $(printf "\t") ${MEM_FREE:-null}
$GREEN_PART ████ ████ ███ ███$END RAM Available $GRN_ARROW $(printf "\t") ${MEM_AVAILABLE:-null}
$GREEN_PART ████ ████ ███ ███$END Buffers $GRN_ARROW $(printf "\t\t") ${BUFFERS:-null}
$GREEN_PART ████████████████████████████████$END Cached $GRN_ARROW $(printf "\t\t") ${CACHED:-null}
$GREEN_PART ███ ████ █████ █████ ███$END Swap Total $GRN_ARROW $(printf "\t") ${SWAP_TOTAL:-null}
$YELLOW_PART ███ ████ █████ █████ ███$END Swap Free $GRN_ARROW $(printf "\t") ${SWAP_FREE:-null}
Zswap $GRN_ARROW $(printf "\t\t") ${ZSWAP:-null}
Zswapped $GRN_ARROW $(printf "\t") ${ZSWAPPED:-null}
Swap Cached $GRN_ARROW $(printf "\t") ${SWAP_CACHED:-null}
Dirty $GRN_ARROW $(printf "\t\t") ${DIRTY:-null}
Kernel Stack $GRN_ARROW $(printf "\t") ${KERNEL_STACK:-null}
Per CPU $GRN_ARROW $(printf "\t\t") ${PER_CPU:-null}
HugePages Free $GRN_ARROW $(printf "\t") ${HUGEPAGES_FREE:-null}
HugePages Total $GRN_ARROW $(printf "\t") ${HUGEPAGES_TOTAL:-null}
$COLORBLOCKS
EOF