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