Skip to content

Instantly share code, notes, and snippets.

@johnae
Last active December 3, 2015 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnae/fc8e04acef49999fc5c9 to your computer and use it in GitHub Desktop.
Save johnae/fc8e04acef49999fc5c9 to your computer and use it in GitHub Desktop.
Tmux notifier

This is the tmux notifier I use for spook (a guard replacement - eg. watch for changes and run something). Clone this gist to:

~/.spook/notifiers/tmux

like so:

cd ~
git clone https://gist.github.com/fc8e04acef49999fc5c9.git ~/.spook/notifiers/tmux

and you're good to go.

uv = require "uv"
tmux_set_status = (status) ->
os.execute "tmux set status-left '#{status}' > /dev/null"
tmux_default_status = '#[fg=colour16,bg=colour254,bold]'
tmux_fail_status = (info) ->
tmux_default_status .. '#[fg=white,bg=red] FAIL: ' .. project_name! .. " (#{info.elapsed_time} s) " .. '#[fg=red,bg=colour234,nobold]'
tmux_pass_status = (info) ->
tmux_default_status .. '#[fg=white,bg=green] PASS: ' .. project_name! .. " (#{info.elapsed_time} s) " .. '#[fg=green,bg=colour234,nobold]'
tmux_test_status = (info) ->
tmux_default_status .. '#[fg=white,bg=cyan] TEST: ' .. project_name! .. ' #[fg=cyan,bg=colour234,nobold]'
runs = ->
os.getenv('TMUX')
timer = nil
stop_timer = ->
if timer
timer\stop!
timer\close!
timer = nil
start_reset_timer = ->
stop_timer!
uv.update_time!
timer = uv.new_timer!
timer\start 7000, 0, ->
tmux_set_status tmux_default_status
stop_timer!
start = (info) ->
tmux_set_status tmux_test_status(info)
start_reset_timer!
-- we can use uv:s signal handling to ensure something runs
-- if you press ctrl-c for example, here we ensure tmux status
-- line is reset to it's original state before exiting spook
sigint = uv.new_signal!
uv.signal_start sigint, "sigint", (signal) ->
print "got #{signal}, shutting down"
stop_timer!
tmux_set_status tmux_default_status
os.exit 1
finish = (success, info) ->
if success
tmux_set_status tmux_pass_status(info)
else
tmux_set_status tmux_fail_status(info)
start_reset_timer!
:start, :finish, :runs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment