bin

scripts or programs i made
git clone git://git.wael.cc/bin
Log | Files | Refs

fetch (4141B)


      1 #!/bin/sh
      2 # shellcheck source=/dev/null
      3 #
      4 # The MIT License (MIT)
      5 #
      6 # Copyright (c) 2022 wael <wael@wael.cc>
      7 #
      8 # Permission is hereby granted, free of charge, to any person obtaining a copy
      9 # of this software and associated documentation files (the "Software"), to deal
     10 # in the Software without restriction, including without limitation the rights
     11 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     12 # copies of the Software, and to permit persons to whom the Software is
     13 # furnished to do so, subject to the following conditions:
     14 #
     15 # The above copyright notice and this permission notice shall be included in all
     16 # copies or substantial portions of the Software.
     17 #
     18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     19 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     20 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     21 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     22 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     23 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     24 # SOFTWARE.
     25 
     26 print() {
     27 	printf "\033[1;34m%6s\033[m %s\n" "${1}" "$2"
     28 }
     29 
     30 print_uptime() {
     31 	IFS=. read -r i _ < /proc/uptime
     32 
     33 	d="$((i / 60 / 60 / 24))"
     34 	h="$((i / 60 / 60 % 24))"
     35 	m="$((i / 60 % 60))"
     36 
     37 	[ $m -gt 0 ] && up="${m}m $((i % 60))s"
     38 	[ $h -gt 0 ] && up="${h}h $up"
     39 	[ $d -gt 0 ] && up="${d}d $up"
     40 
     41 	print up "$up"
     42 }
     43 
     44 print_x11_wm() {
     45 	! [ "$DISPLAY" ] || ! command -v xprop 1>/dev/null || {
     46 		# query for the root window for _NET_SUPPORTING_WM_CHECK
     47 		#   '_NET_SUPPORTING_WM_CHECK(WINDOW): window id # 0x40000f'
     48 		id="$(xprop -root _NET_SUPPORTING_WM_CHECK)"
     49 		# query for window id 0x40000f for _NET_WM_NAME
     50 		#   '_NET_WM_NAME(UTF8_STRING) = "dwm"'
     51 		wm="$(xprop -id "${id##*\# }" _NET_WM_NAME)"
     52 		wm="${wm##*= \"}" # _NET_WM_NAME(UTF8_STRING) = "dwm" -> dwm"
     53 		wm="${wm%%\"}"    # dwm" -> dwm
     54 
     55 		print wm "$wm"
     56 	}
     57 }
     58 
     59 print_cpu() {
     60 	# read (loop over) each line of the /proc/cpuinfo,
     61 	# and check if that line starts with 'model name',
     62 	# and if it does, remove all that is up to ':'
     63 	#   model name: cpu -> cpu
     64 	while read -r line; do case $line in 
     65 		model\ name*) cpu=${line##*: }; break ;; 
     66 	esac; done < /proc/cpuinfo
     67 	
     68 	# remove useless information.
     69 	cpu=${cpu% CPU*}
     70 	cpu=${cpu#*Core(TM) *}
     71 
     72 	print cpu "$cpu"
     73 }
     74 
     75 print_mem() {
     76 	# loop over /proc/meminfo with delimiter : to
     77 	# seperate strings
     78 	#   $n              $v       $_
     79 	#   MemTotal:       16156544 kB
     80 	# loop till $n is MemTotal or MemAvailable, 
     81 	# take $v of that $n, and divide it by 1024.
     82 	# 16156544 -> 15777
     83 	# source from pfetch from https://github.com/KittyKatt/screenFetch/issues/386
     84 	while IFS=': ' read -r n v _; do case $n in 
     85 		MemTotal) totalmem=$((v / 1024)) ;; 
     86 		MemAvailable) availmem=$((v / 1024)) ;; 
     87 	esac; done < /proc/meminfo
     88 
     89 	print mem "$((totalmem - availmem))M / ${totalmem}M"
     90 }
     91 
     92 print_gpu() {
     93 	# similar to the cpu loop, except read from a pciutils program:
     94 	#  X:XX.X VGA: XX Corporation XXX [GeForce XXX XXXX Rev. A] (rev aX)
     95 	while read -r line; do case $line in 
     96 			*VGA*)
     97 				gpu=${line##*: }    # XX Corporation XXX [GeForce XXX XXXX Rev. A] (rev aX)
     98 				gpu=${gpu%]*}       # XX Corporation XXX [GeForce XXX XXXX Rev. A
     99 				gpu=${gpu#*[}       # GeForce XXX XXXX Rev. A
    100 				gpu=${gpu% (*}      # (backup) '(rev aX)' removed
    101 
    102 				# some lspci implemetations such as busybox does not work.
    103 				[ -n "$gpu" ] || break
    104 
    105 				print gpu "$gpu"
    106 				unset gpu
    107 			;; 
    108 		esac; done <<-EOF
    109 		$(lspci)
    110 	EOF
    111 }
    112 
    113 # read -r hostname < /proc/sys/kernel/hostname
    114 read -r osrelease < /proc/sys/kernel/osrelease
    115 read -r product_name < /sys/devices/virtual/dmi/id/product_name
    116 read -r product_version < /sys/devices/virtual/dmi/id/product_version
    117 . /etc/os-release
    118 
    119 # shellcheck disable=SC2041
    120 # shellcheck disable=SC2086
    121 for pd in '/var/db/kiss/installed/*'; do
    122 	set -- $pd
    123 	[ $# -gt 1 ] && pkgs=$# && break
    124 done
    125 
    126 print kern "$osrelease"
    127 print dist "$NAME"
    128 print_x11_wm
    129 print sh "$SHELL"
    130 print pkgs "$pkgs"
    131 print host "$product_name $product_version"
    132 print_uptime
    133 print_cpu
    134 print_mem
    135 #print_gpu