Skip to content

Instantly share code, notes, and snippets.

@memeplex
Last active May 23, 2016 13:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save memeplex/8115385 to your computer and use it in GitHub Desktop.
Save memeplex/8115385 to your computer and use it in GitHub Desktop.
h2status is a trivial (about 50 LOC) bash wrapper to i3status that nevertheless allows to conveniently: (i) write custom modules in bash to add full-fledged json entries to the status bar, (ii) write mouse event handlers in bash with access to the full set of event parameters as bash variables, (iii) arbitrarily transform the final output to cha…
#!/bin/bash
function status { :; }
function on_event { :; }
function transform { cat; }
function entry {
echo -ne '{"name":"'$1'","full_text":"'$2'"'${3:+,$3}'},'
}
function cached_status {
local file=$tmp$1 interval=$2 command=$3
read_status $1
[[ -f $file ]] && (( $(date +%s) - $(stat -c %Y $file) < $interval )) && return
touch $file
{ write_status $1 "$(eval $command)"; } >/dev/null &
}
function read_status {
[[ -f $tmp$1 ]] && flock -s ${tmp}lock -c "cat $tmp$1"
}
function write_status {
flock ${tmp}lock -c "echo '$2' > $tmp$1"
update
}
function update {
killall -USR1 i3status
}
function event_loop {
while read line
do
eval $(echo "$line" | sed -re 's/^,|\{|\}|"//g' -e 'y/,:/;=/')
on_event > /dev/null
done
}
function status_loop {
read; read; echo -e '{"version":1,"click_events":true}\n['
while read line
do
echo "$line" | sed -r 's/^(,?\[)\{/\1'"$(status)"'{/' | transform
done
}
tmp=/tmp/h2status_
[[ $1 == "update" && ! $2 ]] && { update; exit 0; }
[[ $1 == "write" && $2 && $3 && ! $4 ]] && { write_status $2 "$3"; exit 0; }
[[ $* ]] && { echo "Usage: h2status [update|write <module> <value>]"; exit 1; }
. ~/.h2statusrc
trap 'rm -f $tmp*; exit' EXIT INT TERM HUP
rm -f $tmp*
i3status | status_loop &
event_loop
#!/bin/bash
# Use this as a template for your own configuration, to be placed in ~/.h2statusrc.
# All three functions are optional as a default NOP implementation is provided.
# Remember to configure output_format = "i3bar" in ~/.i3status.conf.
function status {
entry sync_random "SR: $RANDOM" '"color":"$00FF00"'
entry async_random "ar: $(cached_status async_random 30 'echo $RANDOM')"
}
function on_event {
case $name in
async_random) [[ $button == 1 ]] && write_status async_random $RANDOM
esac
}
function transform {
sed -r 's/ar:/AR:/'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment