Basic OpenBSD support

Working: user@host, distro name, shell, cpu, mem, pkgs
This commit is contained in:
GaugeK committed 2020-08-21 11:27:03 +10:00
1 parent 4d6732246e
commit 16a223b6c9
1 file changed
+103 -80
+103 -80
View File
@@ -1,7 +1,7 @@
#!/bin/sh
#
# fet.sh
# a fetch in pure POSIX shell
# fet.sh
# a fetch in pure POSIX shell
#
# supress errors
@@ -14,98 +14,123 @@ _() { # [ a = b ] with globbing
}
## Terminal
while [ ! "$term" ]; do
# 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##*/}") ;; # 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
awesome|xmonad|qtile|i3*|*box*|*wm*) wm="$c"; break;;
esac
done
fi
[ "$XDG_CURRENT_DESKTOP" ] && wm="$XDG_CURRENT_DESKTOP"
[ "$DESKTOP_SESSION" ] && wm="$DESKTOP_SESSION"
## Distro
o=/etc/os-release
[ -f $o ] && . $o # 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
_ "$line" "MemTotal*" && set -- $line && break
done < /proc/meminfo
mem="$(( $2 / 1000 ))MB"
if [ -e /proc/$$/comm ]; then
while [ ! "$term" ]; do
# 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"
## Processor
# get name of binary
read -r name < "/proc/$ppid/comm"
case $name in
*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
[ "$DISPLAY" ] && [ ! "$wm" ] &&
# 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
awesome|xmonad|qtile|i3*|*box*|*wm*) wm="$c"; break;;
esac
done
## 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
_ "$line" "MemTotal*" && set -- $line && break
done < /proc/meminfo
mem="$(( $2 / 1000 ))MB"
## Processor
while read -r line; do
case $line in
vendor_id*) vendor="${line##*: } ";;
model\ name*) cpu="${line##*: }"; break;;
esac
done < /proc/cpuinfo
## Uptime
# the simple math is shamefully stolen from aosync
IFS=. read -r uptime _ < /proc/uptime
d=$((uptime / 60 / 60 / 24))
up=$(printf %02d:%02d $((uptime / 60 / 60 % 24)) $((uptime / 60 % 60)))
[ "$d" -gt 0 ] && up="${d}d $up"
## Kernel
read -r _ _ version _ < /proc/version
kernel="${version%%-*}"
_ "$version" "*Microsoft*" && ID="fake $ID"
## Motherboard // laptop
read -r model < /sys/devices/virtual/dmi/id/product_name
## Packages
# 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
pkgs=${###0}
read -r host < /proc/sys/kernel/hostname
elif [ -f /var/run/dmesg.boot ]; then
## OpenBSD cpu/mem/name
while read -r line; do
case $line in
# set $mem to the nicely formatted brackets
"real mem"*)
mem=${line##*\(}; mem=${mem%\)*}
;;
# set $cpu to everything before a comma and after the field name
cpu0:*)
set -- $line; vendor="${2%\(*} "
cpu=${line##cpu0: }; cpu=${cpu%%,*}; break
;;
# First 2 words in the file are OpenBSD <version>
*)
[ "$ID" ] || { set -- $line; ID="$1 $2"; }
esac
done < /var/run/dmesg.boot
[ -d /var/db/pkg ] && set -- /var/db/pkg/* && pkgs=$#
read -r host < /etc/myname
host=${host%.*}
fi
## GTK
while read -r line; do
case $line in
vendor_id*) vendor="${line##*: } ";;
model\ name*) cpu="${line##*: }"; break;;
esac
done < /proc/cpuinfo
_ "$line" "gtk-theme*" && gtk="${line##*=}" && break
done < "${XDG_CONFIG_HOME:=$HOME/.config}/gtk-3.0/settings.ini"
# Shorten $cpu and $vendor
# this is so messy due to so many inconsistencies in the model names
vendor="${vendor##*Authentic}"
vendor="${vendor##*Genuine}"
# this is so messy due to so many inconsistencies in the model names
cpu="${cpu##*) }"
cpu="${cpu%% @*}"
cpu="${cpu%% CPU}"
cpu="${cpu##CPU }"
cpu="${cpu##*AMD }"
cpu="${cpu%% with*}"
cpu="${cpu% *-Core*}"
## Uptime
# the simple math is shamefully stolen from aosync
IFS=. read -r uptime _ < /proc/uptime
d=$((uptime / 60 / 60 / 24))
up=$(printf %02d:%02d $((uptime / 60 / 60 % 24)) $((uptime / 60 % 60)))
[ "$d" -gt 0 ] && up="${d}d $up"
## Kernel
read -r _ _ version _ < /proc/version
kernel="${version%%-*}"
_ "$version" "*Microsoft*" && ID="fake $ID"
## GTK
while read -r line; do
_ "$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
# 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() {
printf ' '
@@ -120,8 +145,6 @@ print() {
"${accent:-4}" "$1" "${separator:- ~ }" "$2"
}
read -r host < /proc/sys/kernel/hostname
# default value
: "${info:=n user os sh wm up gtk cpu mem host kern pkgs term col n}"