getos now gets os type (Linux, Mac OS X, OpenBSD) and the new function getdistro gets (Arch Linux, Ubuntu, etc). This way the case statements can always end in an Unknown instead of falling back to linux.

This commit is contained in:
dylan araps 2016-01-04 09:21:13 +11:00
parent 70af2fc12d
commit cd4848e27c
1 changed files with 64 additions and 42 deletions

106
fetch.sh
View File

@ -30,7 +30,7 @@ export LC_ALL=C
info=( info=(
"gettitle" "gettitle"
"underline" "underline"
"OS: getos" "OS: getdistro"
"Kernel: getkernel" "Kernel: getkernel"
"Uptime: getuptime" "Uptime: getuptime"
"Packages: getpackages" "Packages: getpackages"
@ -177,16 +177,10 @@ xoffset=0
# Gather Info {{{ # Gather Info {{{
# Get Operating System # Get Operating System Type
case "$(uname)" in case "$(uname)" in
"Linux") "Linux")
if type -p crux >/dev/null 2>&1; then os="Linux"
os="CRUX"
else
os="$(awk -F'=' '/^NAME=/ {printf $2; exit}' /etc/*ease)"
os=${os#\"*}
os=${os%*\"}
fi
;; ;;
"Darwin") "Darwin")
@ -198,6 +192,29 @@ case "$(uname)" in
;; ;;
esac esac
# Get Distro
getdistro () {
case "$os" in
"Linux" )
if type -p crux >/dev/null 2>&1; then
distro="CRUX"
else
distro="$(awk -F'=' '/^NAME=/ {printf $2; exit}' /etc/*ease)"
distro=${distro#\"*}
distro=${distro%*\"}
fi
;;
"Mac OS X")
distro="Mac OS X $(sw_vers -productVersion)"
;;
"OpenBSD")
distro="OpenBSD"
;;
esac
}
# Get Title # Get Title
gettitle () { gettitle () {
title="${USER}@$(hostname)" title="${USER}@$(hostname)"
@ -211,6 +228,10 @@ getkernel() {
# Get uptime # Get uptime
getuptime () { getuptime () {
case "$os" in case "$os" in
"Linux")
uptime="$(uptime -p)"
;;
"Mac OS X") "Mac OS X")
# TODO: Fix uptime for OS X # TODO: Fix uptime for OS X
uptime="Unknown" uptime="Unknown"
@ -221,14 +242,12 @@ getuptime () {
uptime=${uptime# } uptime=${uptime# }
uptime="${uptime# * up }" uptime="${uptime# * up }"
;; ;;
*) uptime="$(uptime -p)" ;;
esac esac
} }
# Get package count # Get package count
getpackages () { getpackages () {
case "$os" in case "$distro" in
"Arch Linux"|"Parabola GNU/Linux-libre"|"Manjaro"|"Antergos") "Arch Linux"|"Parabola GNU/Linux-libre"|"Manjaro"|"Antergos")
packages="$(pacman -Q | wc -l)" packages="$(pacman -Q | wc -l)"
;; ;;
@ -298,16 +317,8 @@ getwindowmanager () {
# Get cpu # Get cpu
getcpu () { getcpu () {
case $os in case "$os" in
"Mac OS X") "Linux")
cpu="$(sysctl -n machdep.cpu.brand_string)"
;;
"OpenBSD")
cpu="$(sysctl -n hw.model)"
;;
*)
cpu="$(awk -F ': ' '/model name/ {printf $2; exit}' /proc/cpuinfo)" cpu="$(awk -F ': ' '/model name/ {printf $2; exit}' /proc/cpuinfo)"
# We're using lscpu because /proc/cpuinfo doesn't have min/max speed. # We're using lscpu because /proc/cpuinfo doesn't have min/max speed.
@ -322,6 +333,18 @@ getcpu () {
speed=${speed:0:1}.${speed:1} speed=${speed:0:1}.${speed:1}
cpu="$cpu @ ${speed}GHz" cpu="$cpu @ ${speed}GHz"
;; ;;
"Mac OS X")
cpu="$(sysctl -n machdep.cpu.brand_string)"
;;
"OpenBSD")
cpu="$(sysctl -n hw.model)"
;;
*)
cpu="Unknown"
;;
esac esac
# Remove uneeded patterns from cpu output # Remove uneeded patterns from cpu output
@ -337,7 +360,21 @@ getcpu () {
# Get memory # Get memory
getmemory () { getmemory () {
case $os in case "$os" in
"Linux")
mem="$(awk 'NR < 4 {printf $2 " "}' /proc/meminfo)"
# Split the string above into 3 vars
# This is faster than using an array.
set $mem
memtotal=$1
memfree=$2
memavail=$3
memused="$((memtotal - memavail))"
memory="$(( ${memused%% *} / 1024))MB / $(( ${memtotal%% *} / 1024))MB"
;;
"Mac OS X") "Mac OS X")
memtotal=$(printf "$(sysctl -n hw.memsize)"/1024^2 | bc) memtotal=$(printf "$(sysctl -n hw.memsize)"/1024^2 | bc)
memwired=$(vm_stat | awk '/wired/ { print $4 }') memwired=$(vm_stat | awk '/wired/ { print $4 }')
@ -356,17 +393,7 @@ getmemory () {
;; ;;
*) *)
mem="$(awk 'NR < 4 {printf $2 " "}' /proc/meminfo)" memory="Unknown"
# Split the string above into 3 vars
# This is faster than using an array.
set $mem
memtotal=$1
memfree=$2
memavail=$3
memused="$((memtotal - memavail))"
memory="$(( ${memused%% *} / 1024))MB / $(( ${memtotal%% *} / 1024))MB"
;; ;;
esac esac
} }
@ -697,17 +724,12 @@ printinfo () {
fi fi
;; ;;
*getos*)
continue
;;
*:*|*) *:*|*)
# Update the var # Update the var
output=${function/get/} var=${function/get/}
typeset -n output=$output typeset -n output=$var
# Call the function # Call the function
# [ -z "$output" ] && echo "$function"; time $function; continue
[ -z "$output" ] && $function [ -z "$output" ] && $function
;;& ;;&
@ -736,7 +758,7 @@ printinfo () {
# }}} # }}}
# Print The Info {{{ # Call Functions and Finish Up {{{
# Get image # Get image