commit ba72b740d57390327387f353924c97a127ec3255
parent d361ad6edbe369bc3422544d71b8f5c43ee051e4
Author: Tomas Nemec <nemi@skaut.cz>
Date: Tue, 8 Feb 2022 11:55:54 +0100
feat: possibility to modify status test
Handy to not trigger `on_update` on everyt change if status is table.
Diffstat:
2 files changed, 13 insertions(+), 16 deletions(-)
diff --git a/tools/trun-fmt.lua b/tools/trun-fmt.lua
@@ -18,22 +18,19 @@
local status_dir_def = os.getenv('XDG_CACHE_HOME') .. '/trun'
local status_dir = os.getenv('TRUN_STATUS_DIR') or status_dir_def
-- map status to foreground colors.
-local status_map = {[0] = 'ffa500', [1] = '00ff00', [-1] = 'ff0000'}
+-- local status_map = {[0] = 'ffa500', [1] = '00ff00', [-1] = 'ff0000'}
+local icon_map = {[0] = '', [1] = '', [-1] = ''}
---
-- silent fail if dir not exists
-if not io.open(status_dir) then
- return ''
-end
+if not io.open(status_dir) then return '' end
local trun_name = arg[1]
-- get all status files
local status_files = {}
local list = io.popen('ls ' .. status_dir)
-for f in list:lines() do
- table.insert(status_files, f)
-end
+for f in list:lines() do table.insert(status_files, f) end
-- format status_file to string
local format = function(file, name)
@@ -42,9 +39,10 @@ local format = function(file, name)
table.insert(output, '')
else
local status = file:read('*n') -- 'n' means read a number
- local color = status_map[tonumber(status)]
+ -- local color = status_map[tonumber(status)]
+ local icon = icon_map[tonumber(status)]
-- Edit this to your liking
- output = string.format('%%{F#%s} %s %%{F-}', color, name:upper())
+ output = string.format('%s %s', icon, name:upper())
file:close()
end
return output
@@ -58,9 +56,7 @@ for _, status_file_name in ipairs(status_files) do
local status_file_path = status_dir .. '/' .. status_file_name
local status_file = io.open(status_file_path, 'r')
if trun_name then
- if name == trun_name then
- table.insert(result, format(status_file, name))
- end
+ if name == trun_name then table.insert(result, format(status_file, name)) end
else
table.insert(result, format(status_file, name))
end
@@ -68,6 +64,4 @@ for _, status_file_name in ipairs(status_files) do
end
-- print out results
-if #result > 0 then
- print('[' .. table.concat(result, ',') .. ']')
-end
+if #result > 0 then print('[' .. table.concat(result, ',') .. ']') end
diff --git a/trun.lua b/trun.lua
@@ -27,6 +27,7 @@
-- There are these hook functions you can implement:
-- - on_start(userdata): before first line is passed to handle function
-- - on_update(userdata, line, status): when status change
+-- - on_status_test(userdata, status, lastStatus): custom status test
-- - on_end(userdata): when script end
--
-- ## Userdata
@@ -119,7 +120,9 @@ local lastStatus
for line in io.lines() do
lastStatus = status
status = handler.handle(userdata, line)
- if status and status ~= lastStatus then
+
+ local status_test = handler.on_status_test or function(_, s, lastS) return s ~= lastS end
+ if status and status_test(userdata, status,lastStatus) then
update_file(status)
if handler.on_update then
handler.on_update(userdata, line, status)