neosfetch/nsfetch
2023-06-27 13:40:48 +02:00

131 lines
4.1 KiB
Bash
Executable file

#!/bin/sh
# neosfetch
# Minimal user-centric fetch script
#
# Copyright (C) 2023 speedie
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
NSFETCH_CONFIG="${NSFETCH_CONFIG:-${XDG_CONFIG_HOME:-${HOME}/.config}/nsfetch/nsfetchrc}"
NSFETCH_V="${NSFETCH_V:-0.1}"
d_user() { printf "%s" "$(whoami)"; }
d_hostname() { [ -f "${ETC_DIR}/hostname" ] && cat /etc/hostname; }
d_tram() { printf "%d" "$(($(grep MemTotal /proc/meminfo | awk '{ print $2 }') / ${MEMDIV:-1024}))"; }
d_fram() { printf "%d" "$(($(grep MemFree /proc/meminfo | awk '{ print $2 }') / ${MEMDIV:-1024}))"; }
d_uram() { printf "%d" "$(($(d_tram) - $(d_fram)))"; }
d_kernel() { uname -s; }
d_kernelv() { uname -r; }
d_cpu_model() { lscpu | sed -nr '/Model name/ s/.*:\s*(.*) @ .*/\1/p'; }
d_cores() { printf "%d" "$(($(grep -c "cores" /proc/cpuinfo) / 2))"; }
d_threads() { grep -c "cores" /proc/cpuinfo; }
rc() {
mkdir -p "$(dirname "${NSFETCH_CONFIG}")"
[ ! -f "${NSFETCH_CONFIG}" ] && cat << EOF > "${NSFETCH_CONFIG}"
# neosfetch default configuration file
# See https://git.speedie.site/speedie/neosfetch for more information.
ETC_DIR="\${ETC_DIR:-/etc}" # Path to your /etc directory
MEMDIV="1024" # Number to divide RAM count in KB with
ns_pf() {
p "\$(d_user)@\$(d_hostname)"
p "RAM: \$(d_tram) MiB"
p "CPU: \$(d_cpu_model)"
p "KERNEL: \$(d_kernel)"
a
}
d_pkgc() {
case "\$dist" in
"arch") pkgc="\$(pacman -Q | grep -c "")" ;;
"debian") pkgc="\$(dpkg --get-selections | grep -c "")" ;;
"void") pkgc="\$(xbps-query -s | grep -c "")" ;;
gentoo|funtoo) pkgc="\$(ls -d /var/db/pkg/*/* | grep -c "")" ;;
*) pkgc="0" ;;
esac
printf "%d" "\${pkgc:-0}"
}
d_dist() {
case "\$dist" in
"arch") printf "Arch Linux\n" ;;
"debian") printf "Debian GNU/Linux\n" ;;
"void") printf "Void Linux\n" ;;
"gentoo") printf "Gentoo Linux\n" ;;
"funtoo") printf "Funtoo Linux\n" ;;
*) printf "Unknown\n" ;;
esac
}
rs() {
[ ! -f "\${ETC_DIR}/os-release" ] && printf "%s: %s/os-release not found. Is %s set up properly?\n" "\$(basename "\$0")" "\${ETC_DIR}" "\${ETC_DIR}" >> /dev/stderr && return 1
case "\$(cat "\${ETC_DIR}/os-release")" in
*Arch*|*arch*) dist="arch" ;;
*Artix*|*artix*) dist="arch" ;;
*Debian*|*debian*) dist="debian" ;;
*Gentoo*|*gentoo*) dist="gentoo" ;;
*Funtoo*|*funtoo*) dist="funtoo" ;;
*Void*|*void*) dist="void" ;;
*) dist="unknown" ;;
esac
return 0
}
p() {
i="\${i:-1}"
printf "%s %s\n" "\$(printf "%s" "\${art:-NULL}" | head -n \$i | tail -1)" "\${1:-NULL}" >> /tmp/nsf_buf
i="\$((i+1))"
}
a() {
cat "/tmp/nsf_buf"; rm -f "/tmp/nsf_buf"
}
EOF
[ ! -f "${NSFETCH_CONFIG}" ] && printf "%s: failed to create config file '%s'\n" "$(basename "$0")" "${NSFETCH_CONFIG}" >> /dev/stderr && exit 1
. "${NSFETCH_CONFIG}"
}
ra() {
for v in "$@"; do
case "$v" in
"") continue ;;
-h|--help) printf "usage: %s [-h] [-v]\n" "$(basename "$0")" ;;
-v|--version) printf "%s %s\n" "$(basename $0)" "${NSFETCH_V:-0}" && exit 0 ;;
*) printf "%s: Invalid argument: '%s'\n" "$(basename "$0")" "$v" >> /dev/stderr && return 1
esac
done
[ -z "$v" ] && return 0
}
pf() {
[ -z "$(command -v ns_pf)" ] && printf "%s: ns_pf func not found in '%s'\n" "$(basename "$0")" "${NSFETCH_CONFIG}" >> /dev/stderr && return 1
ns_pf
}
r() {
ra "$@" || exit $?
rc || exit $?
rs || exit $?
pf || exit $?
return 0
}
r "$@"; exit $?