commit 2b79ace8640bd0b224239beca781dc646f2dccce
parent 8bb5f8cb9cf63f29a072d2e7acb8f2fa85f4fe55
Author: tms <nemi@skaut.cz>
Date: Thu, 10 Jun 2021 12:40:18 +0200
Rename & status can get exact name of trun
Diffstat:
M | Makefile | | | 10 | +++++----- |
D | track_run.lua | | | 79 | ------------------------------------------------------------------------------- |
D | track_run_status.lua | | | 35 | ----------------------------------- |
A | trun.lua | | | 79 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | trun_status.lua | | | 52 | ++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 files changed, 136 insertions(+), 119 deletions(-)
diff --git a/Makefile b/Makefile
@@ -5,11 +5,11 @@ all:
@echo "Nothing to do, try \"make install\" instead."
install:
- @install -v -d "$(BINDIR)/" && install -m 0755 -v "./track_run.lua" "$(BINDIR)/track_run"
- @install -v -d "$(BINDIR)/" && install -m 0755 -v "./track_run_status.lua" "$(BINDIR)/track_run_status"
+ @install -v -d "$(BINDIR)/" && install -m 0755 -v "./trun.lua" "$(BINDIR)/trun"
+ @install -v -d "$(BINDIR)/" && install -m 0755 -v "./trun_status.lua" "$(BINDIR)/trun_status"
-uninstall: track_run.lua track_run_status.lua
- @rm -vrf "$(BINDIR)/track_run"
- @rm -vrf "$(BINDIR)/track_run_status"
+uninstall: trun.lua trun_status.lua
+ @rm -vrf "$(BINDIR)/trun"
+ @rm -vrf "$(BINDIR)/trun_status"
.PHONY: all install uninstall
diff --git a/track_run.lua b/track_run.lua
@@ -1,79 +0,0 @@
-#!/usr/bin/env lua
-
--- <run cmd> | run_track <handler> <name>
-local handlerName = arg[1]
-if not handlerName then
- error('No handler provided!')
-end
-local name = arg[2] or 'trun'
-
-local handlerPath = os.getenv('CONFIG') .. '/trun/?.lua'
-package.path = package.path .. ';' .. handlerPath
-local handler = require(handlerName)
-
--- status dir
-local status_dir = os.getenv('HOME') .. '/.cache/trun'
-if not io.open(status_dir) then
- os.execute('mkdir ' .. status_dir)
-end
-
--- status file
-local status_file = status_dir .. '/' .. name .. '.' .. handlerName
-local file = io.open(status_file, 'w')
-if not file then
- os.execute('touch ' .. status_file)
- file = io.open(status_file, 'w')
-end
-
------------------------------------------------------------------------------------------------------------------------
--- handling signals
------------------------------------------------------------------------------------------------------------------------
-local signal = require 'posix.signal'
-
-local function cleanup()
- os.execute('rm ' .. status_file)
- if handler.onEnd then
- handler.onEnd()
- end
-end
-
-local function onExit(signum)
- cleanup()
- os.exit(signum)
-end
-
-signal.signal(signal.SIGINT, onExit)
-signal.signal(signal.SIGTERM, onExit)
-signal.signal(signal.SIGKILL, onExit)
-signal.signal(signal.SIGHUP, onExit)
-
------------------------------------------------------------------------------------------------------------------------
-
-local status
-
-local function updateFile(status)
- file = io.open(status_file, 'w')
- file:write(status)
- file:close()
- if handler.onUpdate then
- handler.onUpdate()
- end
-end
-
-if handler.onStart then
- handler.onStart()
-end
-
-local lastStatus
-for line in io.lines() do
- print(line)
-
- lastStatus = status
- status = handler.handle(line)
- if status and status ~= lastStatus then
- updateFile(status)
- end
-end
-
--- cleanup
-cleanup()
diff --git a/track_run_status.lua b/track_run_status.lua
@@ -1,35 +0,0 @@
-#!/usr/bin/env lua
-
-local status_dir = os.getenv('HOME') .. '/.cache/trun'
-if not io.open(status_dir) then
- return ''
-end
-
-local ds = {[0] = 'ffa500', [1] = '00ff00', [-1] = 'ff0000'}
-
-local files = {}
-local list = io.popen('ls ' .. status_dir)
-for f in list:lines() do
- table.insert(files, f)
-end
-
-local result = {}
-for _, f in pairs(files) do
- local fname = f:match('(.*)%.dart$')
- if fname then
- local status_file = status_dir .. '/' .. f
- local file = io.open(status_file, 'r')
-
- if not file then
- table.insert(result, '')
- else
- local status = file:read()
- table.insert(result, '%{F#' .. ds[tonumber(status)] .. '} ' .. fname:upper() .. ' %{F-}')
- file:close()
- end
- end
-end
-
-if #result > 0 then
- print("[" .. table.concat(result, ',') .. "]")
-end
diff --git a/trun.lua b/trun.lua
@@ -0,0 +1,79 @@
+#!/usr/bin/env lua
+
+-- <run cmd> | run_track <handler> <name>
+local handlerName = arg[1]
+if not handlerName then
+ error('No handler provided!')
+end
+local name = arg[2] or 'trun'
+
+local handlerPath = os.getenv('CONFIG') .. '/trun/?.lua'
+package.path = package.path .. ';' .. handlerPath
+local handler = require(handlerName)
+
+-- status dir
+local status_dir = os.getenv('HOME') .. '/.cache/trun'
+if not io.open(status_dir) then
+ os.execute('mkdir ' .. status_dir)
+end
+
+-- status file
+local status_file = status_dir .. '/' .. name .. '.' .. handlerName
+local file = io.open(status_file, 'w')
+if not file then
+ os.execute('touch ' .. status_file)
+ file = io.open(status_file, 'w')
+end
+
+-----------------------------------------------------------------------------------------------------------------------
+-- handling signals
+-----------------------------------------------------------------------------------------------------------------------
+local signal = require 'posix.signal'
+
+local function cleanup()
+ os.execute('rm ' .. status_file)
+ if handler.onEnd then
+ handler.onEnd()
+ end
+end
+
+local function onExit(signum)
+ cleanup()
+ os.exit(signum)
+end
+
+signal.signal(signal.SIGINT, onExit)
+signal.signal(signal.SIGTERM, onExit)
+signal.signal(signal.SIGKILL, onExit)
+signal.signal(signal.SIGHUP, onExit)
+
+-----------------------------------------------------------------------------------------------------------------------
+
+local status
+
+local function updateFile(output)
+ file = io.open(status_file, 'w')
+ file:write(table.concat(output, ' '))
+ file:close()
+ if handler.onUpdate then
+ handler.onUpdate()
+ end
+end
+
+if handler.onStart then
+ handler.onStart()
+end
+
+local lastStatus
+for line in io.lines() do
+ io.stdout:write(line)
+
+ lastStatus = status
+ status = handler.handle(line)
+ if status and status ~= lastStatus then
+ updateFile(status)
+ end
+end
+
+-- cleanup
+cleanup()
diff --git a/trun_status.lua b/trun_status.lua
@@ -0,0 +1,52 @@
+#!/usr/bin/env lua
+
+local status_dir = os.getenv('HOME') .. '/.cache/trun'
+if not io.open(status_dir) then
+ return ''
+end
+
+local name = arg[1]
+
+local ds = {[0] = 'ffa500', [1] = '00ff00', [-1] = 'ff0000'}
+
+local files = {}
+local list = io.popen('ls ' .. status_dir)
+for f in list:lines() do
+ table.insert(files, f)
+end
+
+local result = {}
+for _, f in pairs(files) do
+ local fname, _ = f:match('(.*)%.(.*)')
+ if fname then
+ if name then
+ if fname == name then
+ local status_file = status_dir .. '/' .. f
+ local file = io.open(status_file, 'r')
+
+ if not file then
+ table.insert(result, '')
+ else
+ local status = file:read()
+ table.insert(result, '%{F#' .. ds[tonumber(status)] .. '} ' .. fname:upper() .. ' %{F-}')
+ file:close()
+ end
+ end
+ else
+ local status_file = status_dir .. '/' .. f
+ local file = io.open(status_file, 'r')
+
+ if not file then
+ table.insert(result, '')
+ else
+ local status = file:read()
+ table.insert(result, '%{F#' .. ds[tonumber(status)] .. '} ' .. fname:upper() .. ' %{F-}')
+ file:close()
+ end
+ end
+ end
+end
+
+if #result > 0 then
+ print('[' .. table.concat(result, ',') .. ']')
+end