Advertisement
fish830911

hjcbar.sh

Jul 6th, 2020
867
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.24 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # Run this script with:
  4. # killall -9 lemonbar hjcbar.sh; hjcbar.sh | lemonbar -a 50 -p -f "ibm plex mono:size=14" -f "DejaVu Sans Mono:size=14" -f "Noto Sans CJK TC:size=14" -f "IPAGothic:size=14"  -f  "Font Awesome:size=14" -B "#282828" -F "#ebdbb2" -n "hjcbar" | sh &
  5.  
  6. delim='┋'
  7.  
  8. calc () { awk "BEGIN { print int($*) }"; }
  9.  
  10. toggle () { [ $toggle -eq 1 ] && toggle= || toggle=1; }
  11.  
  12. time () { time="%{A1:$TERMINAL -e calcurse &:}$(date '+%b-%d %H:%M:%S')%{A1}"; }
  13.  
  14. volume () { volume="%{A1:pulsemixer --toggle-mute; pkill -1 hjcbar.sh:}%{A3:$TERMINAL -e pulsemixer &:}%{A5:pulsemixer --change-volume +1; pkill -1 hjcbar.sh:}%{A4:pulsemixer --change-volume -1; pkill -1 hjcbar.sh:}$(amixer get Master | grep -o "[0-9]*%\|\[on\]\|\[off\]" | sed "s/\[on\]//;s/\[off\]//")%{A4}%{A5}%{A3}%{A1}"; }
  15.  
  16. network () { network="%{A1:$TERMINAL -e nmtui &:}$(printf '%s ' "$(grep "^\s*w" /proc/net/wireless | awk '{ print "", int($3 * 100 / 70) "%" }')" "$(sed "s/down/❌/;s/up//;s/unknown//" /sys/class/net/e*/operstate)")%{A1}"; }
  17.  
  18. system () {
  19.    total=$(($(cat -u /proc/stat | head -n 1 | cut -b 6- | tr ' ' '+')))
  20.    idle=$(($(cat -u /proc/stat | head -n 1 | cut -b 6- | cut -d ' ' -f 4,6 | tr ' ' '+')))
  21.    [ -z $pretotal ] && pretotal=$((total-1))
  22.    [ -z $preidle ] && preidle=$((idle-1))
  23.    cpu=" $(calc "(($total-$pretotal)-($idle-$preidle))/($total-$pretotal)*100")%"
  24.    pretotal=$total
  25.    preidle=$idle
  26.    memory=" $(free -h | sed -n '2{p;q}' | cut -d ' ' -f 19)"
  27.    system="%{A1:$TERMINAL -e htop &:}$memory $cpu%{A1}"
  28. }
  29.  
  30. weather () {
  31.        [ "$(stat -c %y "$HOME/.local/share/weatherreport" 2>/dev/null | cut -d' ' -f1)" = "$(date '+%Y-%m-%d')" ]
  32.     case $? in
  33.         0) weather="%{A1:$TERMINAL -e less $HOME/.local/share/weatherreport &:}$(sed '16q;d' "$HOME/.local/share/weatherreport" | grep -wo "[0-9]*%" | sort -n | sed -e '$!d' | sed -e "s/^/ /g" | tr -d '\n' &&
  34.         sed '13q;d' "$HOME/.local/share/weatherreport" | grep -o "m\\(-\\)*[0-9]\\+" | sort -n -t 'm' -k 2n | sed -e 1b -e '$!d' | tr '\n|m' ' ' | awk '{print " ",$1 "°","",$2 "°"}')%{A}" ;;
  35.         *) ping -q -c 1 1.1.1.1 >/dev/null &&
  36.         curl -s "wttr.in/$location" > "$HOME/.local/share/weatherreport" &&
  37.         weather="%{A1:$TERMINAL -e less $HOME/.local/share/weatherreport &:}$(sed '16q;d' "$HOME/.local/share/weatherreport" | grep -wo "[0-9]*%" | sort -n | sed -e '$!d' | sed -e "s/^/ /g" | tr -d '\n' &&
  38.         sed '13q;d' "$HOME/.local/share/weatherreport" | grep -o "m\\(-\\)*[0-9]\\+" | sort -n -t 'm' -k 2n | sed -e 1b -e '$!d' | tr '\n|m' ' ' | awk '{print " ",$1 "°","",$2 "°"}')%{A}" ;;
  39.         esac
  40. }
  41.  
  42. battery () {
  43.    for x in /sys/class/power_supply/BAT?; do
  44.     num="${x##*BAT}"
  45.     AC="$(cat $x/status)"
  46.     CAP="$(cat $x/capacity)"
  47.     case $AC in
  48.         "Charging") battery="$(printf '%s' "$battery" " $num: $CAP%")" ;;
  49.         *)
  50.         case "$CAP" in
  51.             100|9[0-9]) battery="$(printf '%s' "$battery" " $num: $CAP%")" ;;
  52.             8[0-9]|7[0-9]) battery="$(printf '%s' "$battery" " $num: $CAP%")" ;;
  53.             6[0-9]|5[0-9]) battery="$(printf '%s' "$battery" " $num: $CAP%")" ;;
  54.             4[0-9]|3[0-9]) battery="$(printf '%s' "$battery" " $num: $CAP%")" ;;
  55.             *) battery="$(printf '%s' "$battery" " $num: $CAP%")" ;;
  56.         esac
  57.     esac
  58.    done
  59. }
  60.  
  61. groups() {
  62.    cur=$(xprop -root _NET_CURRENT_DESKTOP)
  63.    cur=${cur##* }
  64.    tot=$(xprop -root _NET_NUMBER_OF_DESKTOPS)
  65.    tot=${tot##* }
  66.    wincount="$(xdotool search --all --onlyvisible --desktop $cur "" 2>/dev/null | wc -l)"
  67.    monitorcount=$(xrandr --listactivemonitors | tail -n +2 | wc -l)
  68.    idle='-'
  69.    active='†'
  70.    occupy='o'
  71.    case "$monitorcount" in
  72.        '1')
  73.         line=$(printf '%*s ' "$((tot-1))" | tr ' ' "$idle")
  74.         for w in $(wmctrl -l | cut -d ' ' -f3 | uniq); do
  75.         line=$(printf '%s' "$line" | sed "s/^\(.\{$w\}\)./\1$occupy/")
  76.         done
  77.         ws=$(echo $line | sed "s/^\(.\{$cur\}\)./\1$active/")
  78.         ;;
  79.     '2') sep=$(calc "$tot/$monitorcount")
  80.         if [ $cur -lt $sep ]; then
  81.         line=$(printf '%*s ' "$tot" | tr ' ' "$idle")
  82.         for w in $(wmctrl -l | cut -d ' ' -f3 | uniq); do
  83.             w=$((w+1))
  84.             line=$(printf '%s' "$line" | sed "s/^\(.\{$w\}\)./\1$occupy/")
  85.         done
  86.         ws=$(echo $line | sed "s/^\(.\{$cur\}\)./\1$active/; s/\(.\{$sep\}\)./\1 /")
  87.         else
  88.         cur=$(calc "$cur+1")
  89.         line=$(printf '%*s ' "$tot" | tr ' ' "$idle")
  90.         for w in $(wmctrl -l | cut -d ' ' -f3 | uniq); do
  91.             w=$((w+1))
  92.             line=$(printf '%s' "$line" | sed "s/^\(.\{$w\}\)./\1$occupy/")
  93.         done
  94.         ws=$(echo $line | sed "s/^\(.\{$cur\}\)./\1$active/; s/\(.\{$sep\}\)./\1 /")
  95.         fi
  96.         ;;
  97.     esac
  98.  
  99.     # Version w/o occupy:
  100.     # case "$monitorcount" in
  101.     # '1') ws=$(printf '%*s ' "$((tot-1))" | tr ' ' "$idle" | sed "s/^\(.\{$cur\}\)./\1$active/") ;;
  102.     # '2') sep=$(calc "$tot/$monitorcount")
  103.         # if [ $cur -lt $sep ]; then
  104.         # ws=$(printf '%*s ' "$tot" | tr ' ' "$idle" | sed "s/^\(.\{$cur\}\)./\1$active/; s/\(.\{$sep\}\)./\1 /")
  105.         # else
  106.         # cur=$(calc "$cur+1")
  107.         # ws=$(printf '%*s ' "$tot" | tr ' ' "$idle" | sed "s/^\(.\{$cur\}\)./\1$active/; s/\(.\{$sep\}\)./\1 /")
  108.         # fi
  109.         # ;;
  110.     # esac
  111. }
  112.  
  113. music () {
  114.     playicon=""
  115.     pauseicon=""
  116.     format=$(mpc -f "[%title%]|[%file%]" 2>/dev/null)
  117.     name=$(printf '%s' "$format" | head -n 1)
  118.     name="${name##*/}" status="$(printf '%s' "$format" | grep -o "\[playing\]\|\[paused\]" | tr '\n' ' ' | sed "s/\[playing\]/$playicon/; s/\[paused\]/$pauseicon/")"
  119.  
  120.     music="%{A1:mpc toggle > /dev/null; pkill -15 hjcbar.sh:}%{A3:$TERMINAL -e ncmpcpp &:}$([ ${#name} -ge 40 ] && printf '%s' "$(printf '%s' "$name" | cut -b 1-40)... $status" || printf '%s' "$name $status")%{A3}%{A1}"
  121. }
  122.  
  123. musictime () { musictime=$(mpc status 2>/dev/null | grep -o "[0-9]*:[0-9]*/[0-9]*:[0-9]*"); }
  124.  
  125. # This loop will fill a buffer with our infos, and output it to stdout.
  126. update () {
  127.    left="${ws} ${wincount} $delim ${system}"
  128.  
  129.    right="${musictime} ${music} ${volume} $delim ${weather} $delim ${network} $delim ${battery} $delim ${time}"
  130.    monitorlist=$(xrandr --listactivemonitors | tail -n +2 | cut -d ' ' -f 6)
  131.    tmp=0
  132.    for m in $monitorlist; do
  133.     BAR="$BAR%{S${tmp}} %{l}%{A1:arandr &:}$m%{A1} $delim ${left}%{r}${right}"
  134.     tmp=$((tmp+1))
  135.    done
  136.   echo "$BAR" | tr '\n' ' '
  137.    unset tmp BAR
  138.  
  139.    # Hide bar in full screen
  140.    wid=$(xdo id -a "hjcbar")
  141.    for w in $(echo $wid); do
  142.     xdo above -t "$(xdo id -N Bspwm -n root | sort | head -n 1)" "$w"
  143.    done
  144. }
  145.  
  146. refresh () {
  147.    case "$sig" in
  148.     1) volume ;;
  149.     14) unset ws; groups ;;
  150.     15) music ;;
  151.     '') unset ws network battery time system; time; system; battery; network; musictime; groups ;;
  152.    esac
  153.    unset sig
  154. }
  155.  
  156. recursive () {
  157.    # [ $status = $playicon ] && sleep 1 && kill "$PID"
  158.    [ $status = $playicon ] && sleep 1 && pkill -6 hjcbar.sh
  159. }
  160.  
  161. trap 'sig=1;' 1
  162. trap 'sig=6;' 6
  163. trap 'sig=14;' 14
  164. trap 'sig=15;' 15
  165.  
  166. time; volume; system; weather; battery; network; groups; music; musictime
  167. while :; do
  168.    # rec
  169.    refresh; update &
  170.    sleep 1 &
  171.    PID=$!
  172.    echo $PID > /tmp/PID
  173.    # recursive &
  174.    wait $PID
  175. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement