Difference between revisions of "Openbsd-statusbar-snippets"

From Personal Knowledgebase
(Created page with "= Dzen2 Snippets = <pre> #!/bin/sh sleep 2 # Get screen width SCRN_WIDTH=`xrandr --prop | grep "^Screen 0" | cut -d, -f2 | awk '{print $2}'` #HALF_SCRN=`dc -e "$SCRN_WIDTH...")
 
 
Line 103: Line 103:
 
echo "$PICCPU $CPU_USAGE "@" $CPU_SPEED $PICTEMP $CPU_TEMP $DOT $PICMEM $USEDMEM $DOT $PICNET $TOTTRAF $DOT $POWER $DOT $CLOCK"
 
echo "$PICCPU $CPU_USAGE "@" $CPU_SPEED $PICTEMP $CPU_TEMP $DOT $PICMEM $USEDMEM $DOT $PICNET $TOTTRAF $DOT $POWER $DOT $CLOCK"
 
done | $DZEN
 
done | $DZEN
 +
</pre>
 +
 +
= Lemonbar snippets =
 +
 +
<pre>
 +
#!/bin/sh -e
 +
 +
# lemonbar takes a formatted string as input
 +
# problem: i want to update different parts in different intervals
 +
# solution: setup fifo and put in information about what should be
 +
# updated. Create main loop that reads fifo, parses information, updates
 +
# what needs to be updated and fills in old information from previous
 +
# runs. feed the result to lemonbar.
 +
 +
FONT=""9x15
 +
 +
# create fifo
 +
rm -f /tmp/lemonbar.fifo
 +
mkfifo /tmp/lemonbar.fifo
 +
 +
# kill jobs when master script is killed
 +
set +o nohup
 +
 +
# remove fifo when script is killed
 +
trap cleanup 1 2 3 13 15
 +
cleanup() {
 +
    printf "Cleaning up...\n"
 +
    rm -f /tmp/lemonbar.fifo;
 +
}
 +
 +
# update scripts
 +
update_desktop() { printf '%i\n' "$1"; }
 +
update_date() { date +"%Y-%m-%d %H:%M"; }
 +
update_temp() { sysctl -n hw.sensors.cpu0.temp0 | cut -d"." -f1; }
 +
update_load() { sysctl -n vm.loadavg | cut -d" " -f1; }
 +
update_bat() { apm -l; }
 +
update_vol() { doas mixerctl -n outputs.master; }
 +
update_wname() {
 +
    WNAME=$(wname "$1" || printf "")
 +
    if ! [ -z $WNAME ]; then
 +
        printf '%s' "$WNAME"
 +
    fi
 +
}
 +
 +
# update whenever the desktop tag is changed
 +
xprop -spy -root -notype _NET_CURRENT_DESKTOP | \
 +
while read desktop;
 +
do
 +
    printf "%s\n" "$desktop" | awk '{ print $3 }';
 +
done > /tmp/lemonbar.fifo &
 +
 +
# update whenever the window focus is changed
 +
xprop -spy -root -notype _NET_ACTIVE_WINDOW | \
 +
while read window;
 +
do
 +
    printf "%s\n" "$window" | cut -d"#" -f2;
 +
done > /tmp/lemonbar.fifo &
 +
 +
# update in 20 second intervals
 +
while true;
 +
do
 +
    printf '%s\n' "DATE"
 +
    printf '%s\n' "TEMP"
 +
    printf '%s\n' "LOAD"
 +
    printf '%s\n' "BAT"
 +
    sleep 20
 +
done > /tmp/lemonbar.fifo &
 +
 +
# update once (will then be updated from outside)
 +
print "VOL" >> /tmp/lemonbar.fifo &
 +
 +
# read fifo and create output for lemonbar
 +
while read line;
 +
do
 +
    case $line in
 +
        "DATE") _date=$(update_date); ;;
 +
        "TEMP") _temp=$(update_temp); ;;
 +
        "LOAD") _load=$(update_load); ;;
 +
        "BAT") _bat=$(update_bat); ;;
 +
        "VOL") _vol=$(update_vol); ;;
 +
        # "0x"*) _wname=$(update_wname "$line"); ;;
 +
        *) _desktop=$(update_desktop "$line"); ;;
 +
    esac
 +
    printf "[%i] %s %%{r} VOL: %s | BAT: %i%% | LOAD: %s | CPU %i° | %s\n" "$_desktop" "$_wname" "$_vol" "$_bat" "$_load" "$_temp" "$_date"
 +
done < /tmp/lemonbar.fifo | lemonbar -d -f "xft:NotoMono:size=11" -B "#29261d" -F "#e8e9ca"
 +
cleanup
 +
 
</pre>
 
</pre>

Latest revision as of 23:34, 26 July 2020

Dzen2 Snippets

#!/bin/sh

sleep 2

# Get screen width
SCRN_WIDTH=`xrandr --prop | grep "^Screen 0" | cut -d, -f2 | awk '{print $2}'`
#HALF_SCRN=`dc -e "$SCRN_WIDTH 2 / p"`
DZEN_WIDTH=740

# Settings
BG='#222222'  # background
FG='#cccccc'  # foreground
FN='fixed' # font
XX=`dc -e "$SCRN_WIDTH $DZEN_WIDTH - p"`
YY=0
WDTH=$DZEN_WIDTH
 
DZEN="dzen2 -ta r -fg $FG -bg $BG -fn $FN -h 14 -x $XX -y $YY -w $WDTH"

PICCPU="^i($HOME/.dzen/bitmaps/1/cpu.xbm)" 
PICTEMP="^i($HOME/.dzen/bitmaps/1/temp.xbm)" 
PICMEM="^i($HOME/.dzen/bitmaps/1/mem.xbm)" 
PICAC="^i($HOME/.dzen/bitmaps/1/power-ac.xbm)"
PICBAT="^i($HOME/.dzen/bitmaps/1/power-bat2.xbm)"
PICUP="^fg(orange3)^i($HOME/.dzen/bitmaps/1/up.xbm)^fg()"
PICDOWN="^fg(#80AA83)^i($HOME/.dzen/bitmaps/1/down.xbm)^fg()"
PICNETWIRED="^i($HOME/.dzen/bitmaps/1/net-wired2.xbm)"
PICNETWIFI="^i($HOME/.dzen/bitmaps/1/net-wifi.xbm)"
PICNET="^i($HOME/.dzen/bitmaps/1/net-wired.xbm)"
#DOT="^p(5)^c(5)^p(5)"
DOT="^c(5)^p(5)"
 
while true; do
sleep .5

eval `vmstat -c 2 | awk 'NR > 3 {print "FREEMEM="$5*1024 "\nCPU_U="$17 "\nCPU_S="$18 "\nCPU_I="$19}'`

# CPU
CPU_USAGE=`echo "100-$CPU_I" | bc`"%"
CPU_SPD=`sysctl -n hw.cpuspeed`
CPU_SPEED=`echo "$CPU_SPD/1000" | bc`"GHz"
CPU_TEMP=`sysctl -n hw.sensors.cpu0.temp0 | cut -d. -f1`"C"

# Memory
TOTALMEM=`sysctl -n hw.physmem` 
USEDMEM=`echo "($TOTALMEM-$FREEMEM)*100/$TOTALMEM" | bc`"%"

# Battery and AC 
eval `sysctl -n \
  hw.sensors.acpibat0.watthour0 \
  hw.sensors.acpibat0.watthour1 \
  hw.sensors.acpibat0.watthour3 \
  hw.sensors.acpibat0.raw0 \
  hw.sensors.acpiac0.indicator0 \
  | awk '{print "SYSCTL" NR "=" $1}' `

if [ -z $SYSCTL5 ]; then
  AC_BOOL=${SYSCTL1}
  BAT="No"
else
        BAT="Yes"
  BAT_FULL=${SYSCTL1}
        BAT_WARN=${SYSCTL2}
  BAT_CURRENT=${SYSCTL3}
  BAT_STATE=${SYSCTL4} 
  BAT_PERC=`echo "100 * ${BAT_CURRENT} / ${BAT_FULL}" | bc`
  AC_BOOL=${SYSCTL5}
fi

if  [[ ${AC_BOOL} = "On" ]]; then
        if [[ ${BAT} = "No" ]]; then
                POWER="$PICAC"
        else
                if [[ ${BAT_STATE} = 0 ]]; then
                  POWER="^fg(#80AA83)$PICBAT ${BAT_PERC}% ^fg()"
                elif [[ ${BAT_STATE} = 2 ]]; then
                  POWER="^fg(#8083AA)$PICBAT ${BAT_PERC}% ^fg()"
                fi
        fi
else
        if [[ ${BAT_CURRENT} -lt ${BAT_WARN} ]]; then
          POWER="^fg(red)$PICBAT ${BAT_PERC}%^fg()"
        else
          POWER="^fg(orange3)$PICBAT ${BAT_PERC}%^fg()"
        fi
fi

# Network
#eval `ifstat -i em0,urtwn0 0.1 1 | awk '/[^a-z][0-9\.]+/ {print "EM0DOWN=" $1 "; EM0UP=" $2 "; WPI0DOWN=" $3 "; WPI0UP=" $4}'`
eval `ifstat -T 0.1 1 | awk '/[^a-z][0-9\.]+/ {print "TDOWN=" $(NF-1) "; TUP=" $NF}'`
 
EM0TRAF="${EM0DOWN}kB/s${PICDOWN} ${EM0UP}kB/s${PICUP}"
WPI0TRAF="${WPI0DOWN}kB/s${PICDOWN} ${WPI0UP}kB/s${PICUP}"
TOTTRAF="${TDOWN}kB/s${PICDOWN} ${TUP}kB/s${PICUP}"
 
# Clock
CLOCK=`date +"%d.%m.%Y %k:%M"`
 
#echo "$PICCPU $CPU_USAGE "@" $CPU_SPEED $PICTEMP $CPU_TEMP $DOT $PICMEM $USEDMEM $DOT $PICNETWIRED $EM0TRAF $PICNETWIFI $WPI0TRAF $DOT $POWER $DOT $CLOCK"
echo "$PICCPU $CPU_USAGE "@" $CPU_SPEED $PICTEMP $CPU_TEMP $DOT $PICMEM $USEDMEM $DOT $PICNET $TOTTRAF $DOT $POWER $DOT $CLOCK"
done | $DZEN

Lemonbar snippets

#!/bin/sh -e
 
 # lemonbar takes a formatted string as input
 # problem: i want to update different parts in different intervals
 # solution: setup fifo and put in information about what should be
 # updated. Create main loop that reads fifo, parses information, updates
 # what needs to be updated and fills in old information from previous
 # runs. feed the result to lemonbar.
 
 FONT=""9x15
 
 # create fifo
 rm -f /tmp/lemonbar.fifo
 mkfifo /tmp/lemonbar.fifo
 
 # kill jobs when master script is killed
 set +o nohup
 
 # remove fifo when script is killed
 trap cleanup 1 2 3 13 15
 cleanup() {
     printf "Cleaning up...\n"
     rm -f /tmp/lemonbar.fifo;
 }
 
 # update scripts
 update_desktop() { printf '%i\n' "$1"; }
 update_date() { date +"%Y-%m-%d %H:%M"; }
 update_temp() { sysctl -n hw.sensors.cpu0.temp0 | cut -d"." -f1; }
 update_load() { sysctl -n vm.loadavg | cut -d" " -f1; }
 update_bat() { apm -l; }
 update_vol() { doas mixerctl -n outputs.master; }
 update_wname() {
     WNAME=$(wname "$1" || printf "")
     if ! [ -z $WNAME ]; then
         printf '%s' "$WNAME"
     fi
 }
 
 # update whenever the desktop tag is changed
 xprop -spy -root -notype _NET_CURRENT_DESKTOP | \
 while read desktop;
 do
     printf "%s\n" "$desktop" | awk '{ print $3 }';
 done > /tmp/lemonbar.fifo &
 
 # update whenever the window focus is changed
 xprop -spy -root -notype _NET_ACTIVE_WINDOW | \
 while read window;
 do
     printf "%s\n" "$window" | cut -d"#" -f2;
 done > /tmp/lemonbar.fifo &
 
 # update in 20 second intervals
 while true;
 do
     printf '%s\n' "DATE"
     printf '%s\n' "TEMP"
     printf '%s\n' "LOAD"
     printf '%s\n' "BAT"
     sleep 20
 done > /tmp/lemonbar.fifo &
 
 # update once (will then be updated from outside)
 print "VOL" >> /tmp/lemonbar.fifo &
 
 # read fifo and create output for lemonbar
 while read line;
 do
     case $line in
         "DATE") _date=$(update_date); ;; 
         "TEMP") _temp=$(update_temp); ;; 
         "LOAD") _load=$(update_load); ;; 
         "BAT") _bat=$(update_bat); ;; 
         "VOL") _vol=$(update_vol); ;;
         # "0x"*) _wname=$(update_wname "$line"); ;; 
         *) _desktop=$(update_desktop "$line"); ;; 
     esac
     printf "[%i] %s %%{r} VOL: %s | BAT: %i%% | LOAD: %s | CPU %i° | %s\n" "$_desktop" "$_wname" "$_vol" "$_bat" "$_load" "$_temp" "$_date"
 done < /tmp/lemonbar.fifo | lemonbar -d -f "xft:NotoMono:size=11" -B "#29261d" -F "#e8e9ca"
 cleanup