mirror of
https://github.com/eepykate/fet.sh.git
synced 2026-10-17 10:49:31 +00:00
Minor code optimizations, add comments
Use a function that acts like `[ a = b ]` using case instead of calling case every time, which results in a lot of repeated lines
This commit is contained in:
1 parent
06b87fba25
commit
8fa8da9b28
1 file changed
+37
-28
@@ -1,36 +1,44 @@
|
||||
#!/bin/sh
|
||||
#
|
||||
# this is my attempt at trying to make a fetch using only posix shell
|
||||
# without any external commands
|
||||
# fet.sh
|
||||
# a fetch in pure POSIX shell
|
||||
#
|
||||
|
||||
# supress errors
|
||||
exec 2>/dev/null
|
||||
set --
|
||||
_() { # [ a = b ] with globbing
|
||||
! case $1 in # default return is 0 so double negative saves a line
|
||||
$2) return 1
|
||||
esac
|
||||
}
|
||||
|
||||
## Terminal
|
||||
while [ ! "$term" ]; do
|
||||
while IFS=': ' read -r key val; do
|
||||
case $key in
|
||||
PPid) ppid=$val; break;;
|
||||
esac
|
||||
# loop over lines in /proc/pid/status until it reaches PPid
|
||||
# then save that to a variable and exit the file
|
||||
while read -r line; do
|
||||
_ "$line" "PPid*" && ppid=${line##*:?} && break
|
||||
done < "/proc/${ppid:-$PPID}/status"
|
||||
|
||||
# get name of binary
|
||||
read -r name < "/proc/$ppid/comm"
|
||||
case $name in
|
||||
*sh) ;;
|
||||
"${0##*/}") ;;
|
||||
*[Ll]ogin*|*init*) term=linux;;
|
||||
*) term="$name";;
|
||||
*sh|"${0##*/}") ;; # skip shells
|
||||
*[Ll]ogin*|*init*) break;; # exit when the top is reached
|
||||
*) term="$name" # anything else can be assumed to be the terminal
|
||||
esac
|
||||
done
|
||||
|
||||
## WM/DE
|
||||
# standard variables, mostly used for DEs
|
||||
if [ "$XDG_CURRENT_DESKTOP" ]; then
|
||||
wm="$XDG_CURRENT_DESKTOP"
|
||||
elif [ "$DESKTOP_SESSION" ]; then
|
||||
wm="$DESKTOP_SESSION"
|
||||
elif [ "$DISPLAY" ]; then
|
||||
# non-standard WMs
|
||||
# loop over all processes and check the binary name
|
||||
for i in /proc/*/comm; do
|
||||
read -r c < "$i"
|
||||
case $c in
|
||||
@@ -40,16 +48,15 @@ elif [ "$DISPLAY" ]; then
|
||||
fi
|
||||
|
||||
## Distro
|
||||
. /etc/os-release # lol EZ
|
||||
. /etc/os-release # a common file that has variables about the distro
|
||||
|
||||
## Memory
|
||||
# loop over lines in /proc/meminfo until it reaches MemTotal,
|
||||
# then convert the amount (second word) from KB to MB
|
||||
while read -r line; do
|
||||
case $line in
|
||||
MemTotal*) mem="${line##*: }"; break;;
|
||||
esac
|
||||
_ "$line" "MemTotal*" && set -- $line && break
|
||||
done < /proc/meminfo
|
||||
mem="${mem##* }"
|
||||
mem="$(( ${mem%% *} / 1000 ))"
|
||||
mem="$(( $2 / 1000 ))MB"
|
||||
|
||||
## Processor
|
||||
while read -r line; do
|
||||
@@ -60,7 +67,7 @@ while read -r line; do
|
||||
done < /proc/cpuinfo
|
||||
vendor="${vendor##*Authentic}"
|
||||
vendor="${vendor##*Genuine}"
|
||||
# This is so messy due to so many inconsistencies in the model names
|
||||
# this is so messy due to so many inconsistencies in the model names
|
||||
cpu="${cpu##*) }"
|
||||
cpu="${cpu%% @*}"
|
||||
cpu="${cpu%% CPU}"
|
||||
@@ -79,25 +86,24 @@ up=$(printf %02d:%02d $((uptime / 60 / 60 % 24)) $((uptime / 60 % 60)))
|
||||
## Kernel
|
||||
read -r _ _ version _ < /proc/version
|
||||
kernel="${version%%-*}"
|
||||
case $version in
|
||||
*Microsoft*) [ "$ID" ] && ID="fake $ID";;
|
||||
esac
|
||||
_ "$version" "*Microsoft*" && ID="fake $ID"
|
||||
|
||||
## GTK
|
||||
while read -r line; do
|
||||
case $line in
|
||||
gtk-theme*) gtk="${line##*=}"; break;;
|
||||
esac
|
||||
_ "$line" "gtk-theme*" && gtk="${line##*=}" && break
|
||||
done < "${XDG_CONFIG_HOME:-$HOME/.config}"/gtk-3.0/settings.ini
|
||||
|
||||
## Motherboard // laptop
|
||||
read -r model < /sys/devices/virtual/dmi/id/product_name
|
||||
|
||||
## Packages
|
||||
[ -d /var/lib/pacman/local ] && set -- /var/lib/pacman/local/*
|
||||
[ -d /var/db/xbps ] && set -- /var/db/xbps/.*
|
||||
[ -d /var/db/pkg ] && set -- /var/db/pkg/*/*
|
||||
# clean environment, then make every file in the dir an argument,
|
||||
# then save the argument count to $pkgs
|
||||
set --
|
||||
[ -d /var/db/kiss/installed ] && set -- /var/db/kiss/installed/*
|
||||
[ -d /var/lib/pacman/local ] && set -- /var/lib/pacman/local/*
|
||||
[ -d /var/db/xbps ] && set -- /var/db/xbps/.*
|
||||
[ -d /var/db/pkg ] && set -- /var/db/pkg/*/* # gentoo
|
||||
[ $# -gt 0 ] && pkgs=$#
|
||||
|
||||
col() {
|
||||
@@ -115,7 +121,10 @@ print() {
|
||||
|
||||
read -r host < /proc/sys/kernel/hostname
|
||||
|
||||
for i in ${info:-n user os sh wm up gtk cpu mem host kern pkgs term col n}; do
|
||||
# default value
|
||||
: "${info:=n user os sh wm up gtk cpu mem host kern pkgs term col n}"
|
||||
|
||||
for i in $info; do
|
||||
case $i in
|
||||
n) echo;;
|
||||
os) print os "$ID";;
|
||||
@@ -124,7 +133,7 @@ for i in ${info:-n user os sh wm up gtk cpu mem host kern pkgs term col n}; do
|
||||
up) print up "$up";;
|
||||
gtk) print gtk "${gtk# }";;
|
||||
cpu) print cpu "$vendor $cpu";;
|
||||
mem) print mem "${mem}MB";;
|
||||
mem) print mem "$mem";;
|
||||
host) print host "$model";;
|
||||
kern) print kern "$kernel";;
|
||||
pkgs) print pkgs "$pkgs";;
|
||||
|
||||
Reference in new issue
Block a user