pfetch/pfetch
2019-09-24 10:53:49 +03:00

81 lines
1.8 KiB
Bash
Executable file

#!/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"
}
get_kernel() {
log 1 kernel " " "$kernel_version"
}
get_uptime() {
# Uptime works by retrieving the data in total seconds and then
# converting that data into days, hours and minutes using simple
# math.
case $os in
linux)
IFS=. read -r s _ < /proc/uptime
;;
esac
# Convert the uptime from seconds into days, hours and minutes.
d=$((s / 60 / 60 / 24))
h=$((s / 60 / 60 % 24))
m=$((s / 60 % 60))
# Only append days, hours and minutes if they're non-zero.
[ "$d" = 0 ] || uptime="${uptime}${d}d "
[ "$h" = 0 ] || uptime="${uptime}${h}h "
[ "$m" = 0 ] || uptime="${uptime}${m}m "
log 1 uptime " " "${uptime:-0m}"
}
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.
read -r kernel_name kernel_version kernel_machine <<EOF
$(uname -srm)
EOF
get_os
get_distro
get_kernel
get_uptime
}
main "$@"