fetch

[sh] MSYS2-compatible system info prompt
git clone https://git.torresjrjr.com/fetch.git
Log | Files | Refs | README | LICENSE

fetch (2428B)


      1 #!/bin/sh
      2 # A fetch for MSYS2 on Windows.
      3 # @torresjrjr <b@torresjrjr.com>
      4 
      5 
      6 ## Main
      7 
      8 main() {
      9 	config
     10 	formatters
     11 	graphic
     12 	reposition
     13 	status
     14 	echo
     15 }
     16 
     17 
     18 ## Graphic
     19 
     20 graphic() {
     21 	env printf "$c6"
     22 	env printf '%s\n' \
     23 		'      ~       ' \
     24 		'     .-.      ' \
     25 		'    /`v`\     ' \
     26 		'   (/   \)    ' \
     27 		'====="="===<  ' \
     28 		'     |_|      '
     29 	env printf "$c0"
     30 }
     31 
     32 
     33 ## Status
     34 
     35 status() {
     36 	padding; _title  "${c5}%s@%s ${c3}%s"
     37 	padding; _os     "${c6}os      ${c0}%s"
     38 	padding; _uptime "${c6}uptime  ${c0}%s"
     39 	padding; _shell  "${c6}shell   ${c0}%s"
     40 	padding; _sshd   "${c6}sshd    ${c0}%s"
     41 	padding; _tmux   "${c6}tmux    ${c0}%s"
     42 #	padding; _palette
     43 }
     44 
     45 
     46 ## Statuslines
     47 
     48 _title() {
     49 	env printf "$c0$1\n"  "$(whoami)" "$(uname -n)" "$(date -R)"
     50 }
     51 _os() {
     52 	[ -r /etc/os-release ] && . /etc/os-release || PRETTY_NAME=$(uname -o)
     53 	env printf "$c0$1\n"  "$PRETTY_NAME $(uname -r)"
     54 }
     55 _uptime() {
     56 	[ -r /proc/uptime ] && {
     57 		s=$(cat /proc/uptime) s=${s%%.*}
     58 		days=$(($s / 86400)) hours=$(($s / 3600 % 24)) mins=$(($s / 60 % 60))
     59 		since=$(date -d @$(($(date +%s) - $s)) +'%b %d. %H:%M')
     60 		env printf "$c0$1\n"  "${days}d ${hours}h ${mins}m -- ${since}"
     61 	} || {
     62 		env printf "$c0$1\n"  "$(uptime --pretty 2>&- || uptime) -- $(uptime -s)"
     63 	}
     64 }
     65 _shell() {
     66 	env printf "$c0$1\n"  "$(readlink /proc/$PPID/exe) '$SHLVL"
     67 }
     68 _sshd() {
     69 	pids=$(ps ax | awk '/sshd[^@]*@pts/ || /sshd$/ {printf $NF "   "}')
     70 	env printf "$c0$1\n"  "${pids:---}"
     71 }
     72 _tmux() {
     73 	ps ax | grep -v 'grep' | grep -q 'tmux' && tmux_sessions=$(
     74 		tmux ls -F "#{session_created} #{?session_attached,$ul,}#S:#{session_windows}$c0" \
     75 		| sort | awk '{printf $2 "   "}' | xargs -0 printf
     76 	)
     77 	env printf "$c0$1\n"  "${tmux_sessions:---}"
     78 }
     79 _palette() {
     80 	env printf "$rv$c1  $c2  $c3  $c4  $c5  $c6  $c0\n"
     81 }
     82 
     83 
     84 ## Config
     85 
     86 config() {
     87 	config="${XDG_CONFIG_HOME:-$HOME/.config}/fetch/config.sh"
     88 	[ -f "$config" ] && . "$config"
     89 
     90 	graphic_y=$(graphic | wc -l)  # height
     91 	graphic_x=$(graphic | wc -L)  # width
     92 }
     93 
     94 
     95 ## Formatters
     96 
     97 formatters() {
     98 	c0='\x1b[0m'    # normal
     99 
    100 	c1='\x1b[1;31m' # bold red
    101 	c2='\x1b[1;32m' # bold green
    102 	c3='\x1b[1;33m' # bold yellow
    103 	c4='\x1b[1;34m' # bold blue
    104 	c5='\x1b[1;35m' # bold magenta
    105 	c6='\x1b[1;36m' # bold cyan
    106 	c7='\x1b[1;37m' # bold white
    107 
    108 	ul='\x1b[4m'    # underline
    109 	rv='\x1b[7m'    # reverse video
    110 }
    111 
    112 
    113 ## Cursor util
    114 
    115 reposition() { env printf '\e[%sA\r'  "$graphic_y" ;}
    116 padding()    { env printf '\e[%sC'    "$graphic_x" ;}
    117 
    118 
    119 ## Run
    120 
    121 main