commit 68acdc6590a668a601fe47f4052297a8bb51fe4f
parent e04a967ee510a15b79b871371ce760aa86dab61e
Author: tms <nemi@skaut.cz>
Date: Mon, 26 Oct 2020 10:21:57 +0100
handle signals
Diffstat:
3 files changed, 45 insertions(+), 6 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,4 +1,5 @@
BINDIR = /usr/local/bin
+LUAPATH = /usr/lib/lua/5.4
all:
@echo "Nothing to do, try \"make install\" instead."
@@ -6,9 +7,11 @@ all:
install:
@install -v -d "$(BINDIR)/" && install -m 0755 -v "./dart_run_track.lua" "$(BINDIR)/dart_run_track"
@install -v -d "$(BINDIR)/" && install -m 0755 -v "./dart_run_status.lua" "$(BINDIR)/dart_run_status"
+ @install -v -d "$(LUAPATH)/" && install -m 0755 -v "./dart_run_track_hook.lua" "$(LUAPATH)/dart_run_track_hook.lua"
uninstall: dart_run_track.lua dart_run_status.lua
@rm -vrf "$(BINDIR)/dart_run_track"
@rm -vrf "$(BINDIR)/dart_run_status"
+ @rm -vrf "$(LUAPATH)/dart_run_track_hook"
.PHONY: all install uninstall
diff --git a/dart_run_track.lua b/dart_run_track.lua
@@ -1,11 +1,16 @@
#!/usr/bin/env lua
local name = arg[1] or 'dart'
-local hookFile = loadfile(os.getenv('HOME') .. '/.local/src/dart_run_track/dart_run_track_hook.lua')
+
+-- Hook defines 3 methods
+--
+-- onStart: call once before start reading
+-- onUpdate: calls every time when stdin reads a line
+-- onEnd: calls once after stream end
+local hook = require 'dart_run_track_hook'
-- status dir
local status_dir = os.getenv('HOME') .. '/.cache/dart_run_track'
-print(status_dir)
if not io.open(status_dir) then os.execute('mkdir ' .. status_dir) end
-- status file
@@ -20,6 +25,22 @@ io.output(file)
local ds = {['running'] = 0, ['success'] = 1, ['severe'] = -1}
local status
+-----------------------------------------------------------------------------------------------------------------------
+-- handling signals
+-----------------------------------------------------------------------------------------------------------------------
+local signal = require 'posix.signal'
+
+local function cleanup()
+ os.execute('rm ' .. status_file)
+ if hook.onEnd then hook.onEnd() end
+end
+
+signal.signal(signal.SIGINT, function(signum)
+ cleanup()
+ os.exit(signum)
+end)
+-----------------------------------------------------------------------------------------------------------------------
+
-- [INFO] Succeeded after 29.8s with 304 outputs (8 actions)
local function isSucceed(line)
return string.match(line, '%[INFO%] Succeeded')
@@ -51,9 +72,11 @@ local function updateFile(status)
file = io.open(status_file, 'w')
file:write(status)
file:close()
- if hookFile then hookFile() end
+ if hook.onUpdate then hook.onUpdate() end
end
+if hook.onStart then hook.onStart() end
+
local lastStatus
local lineType
for line in io.lines() do
@@ -68,5 +91,4 @@ for line in io.lines() do
end
-- cleanup
-os.execute('rm ' .. status_file)
-
+cleanup()
diff --git a/dart_run_track_hook.lua b/dart_run_track_hook.lua
@@ -1 +1,15 @@
-os.execute('polybar-msg hook dartruntrack 1 &> /dev/null')
+local M = {}
+
+local function updatePolybar()
+ os.execute('polybar-msg hook dartruntrack 1 &> /dev/null')
+end
+
+function M.onUpdate()
+ updatePolybar()
+end
+
+function M.onEnd()
+ updatePolybar()
+end
+
+return M