pfetch/pfetch

117 lines
2.9 KiB
Plaintext
Raw Normal View History

2019-09-24 09:33:23 +02:00
#!/bin/sh
# shellcheck source=/dev/null
#
# pfetch - Simple POSIX sh fetch script.
die() {
printf '\033[31;1merror\033[m: %s.\n' "$@" >&2
exit 1
}
log() {
printf '\033[3%s;1m%s\033[m%s%s\n' "$1" "$2" "$3" "${4:-unknown}"
}
get_os() {
case $kernel_name in
Linux|GNU*) os=linux ;;
*)
die "Unknown OS detected '$kernel_name'" \
"Open an issue on GitHub to add support for your OS"
;;
esac
}
get_distro() {
case $os in
linux)
. /etc/os-release && distro=$PRETTY_NAME
;;
esac
log 1 os " " "$distro"
}
2019-09-24 09:46:56 +02:00
get_kernel() {
log 1 kernel " " "$kernel_version"
}
get_uptime() {
2019-09-24 09:53:49 +02:00
# Uptime works by retrieving the data in total seconds and then
# converting that data into days, hours and minutes using simple
# math.
2019-09-24 09:46:56 +02:00
case $os in
linux)
IFS=. read -r s _ < /proc/uptime
;;
esac
2019-09-24 09:53:49 +02:00
# Convert the uptime from seconds into days, hours and minutes.
2019-09-24 09:46:56 +02:00
d=$((s / 60 / 60 / 24))
h=$((s / 60 / 60 % 24))
m=$((s / 60 % 60))
2019-09-24 09:53:49 +02:00
# Only append days, hours and minutes if they're non-zero.
2019-09-24 09:46:56 +02:00
[ "$d" = 0 ] || uptime="${uptime}${d}d "
[ "$h" = 0 ] || uptime="${uptime}${h}h "
[ "$m" = 0 ] || uptime="${uptime}${m}m "
log 1 uptime " " "${uptime:-0m}"
}
2019-09-24 10:03:33 +02:00
get_memory() {
case $os in
# Used memory is calculated using the following "formula" (Linux):
# MemUsed = MemTotal + Shmem - MemFree - Buffers - Cached - SReclaimable
# Source: https://github.com/KittyKatt/screenFetch/issues/386
linux)
# Parse the '/proc/meminfo' file splitting on ':' and 'k'.
# The format of the file is 'key: 000kB' and an additional
# split is used on 'k' to filter out 'kB'.
while IFS=:k read -r key val _; do
case $key in
MemTotal)
mem_used=$((mem_used + val))
mem_total=$val
;;
Shmem)
mem_used=$((mem_used + val))
;;
MemFree|Buffers|Cached|SReclaimable)
mem_used=$((mem_used - val))
;;
esac
done < /proc/meminfo
mem_used=$((mem_used / 1024))
mem_total=$((mem_total / 1024))
;;
esac
log 1 memory " " "${mem_used}MiB / ${mem_total}MiB"
}
2019-09-24 09:33:23 +02:00
main() {
# Hide 'stderr' unless the first argument is '-v'. This saves
# polluting the script with '2>/dev/null'.
[ "$1" = -v ] || exec 2>/dev/null
# Store the output of 'uname' to avoid calling it multiple times
# throughout the script. 'read <<EOF' is the simplest way of reading
# a command into a list of variables.
2019-09-24 09:48:11 +02:00
read -r kernel_name kernel_version kernel_machine <<EOF
$(uname -srm)
EOF
2019-09-24 09:33:23 +02:00
get_os
get_distro
2019-09-24 09:46:56 +02:00
get_kernel
get_uptime
2019-09-24 10:03:33 +02:00
get_memory
2019-09-24 09:33:23 +02:00
}
main "$@"