trun

Script for parsing any output. Yes, it is all it does.
git clone git://gtms.dev/trun
Log | Files | Refs | README | LICENSE

trun-fmt.lua (1995B)


      1 #!/usr/bin/env lua
      2 
      3 -- Usage: trun_format.lua [<name>]
      4 --
      5 -- Return formatted string for all truns to use inside status bar of your WM.
      6 -- It depends on status codes to be:
      7 -- - `0`  - running - orange
      8 -- - `1`  - success - green
      9 -- - `-1` - error   - red
     10 --
     11 -- You can change colors. viz->#Config
     12 --
     13 -- example output: [%{F#ff0000}NAME%{F-} %{F#00ff00}OTHERNAME%{F-}]
     14 --
     15 -- To get only single trun, add `name` as argument.
     16 --
     17 -- Config
     18 local status_dir_def = os.getenv('XDG_CACHE_HOME') .. '/trun'
     19 local status_dir = os.getenv('TRUN_STATUS_DIR') or status_dir_def
     20 -- map status to foreground colors.
     21 -- local status_map = {[0] = 'ffa500', [1] = '00ff00', [-1] = 'ff0000'}
     22 local icon_map = {[0] = '', [1] = '', [-1] = ''}
     23 ---
     24 
     25 -- silent fail if dir not exists
     26 if not io.open(status_dir) then return '' end
     27 
     28 local trun_name = arg[1]
     29 
     30 -- get all status files
     31 local status_files = {}
     32 local list = io.popen('ls ' .. status_dir)
     33 for f in list:lines() do table.insert(status_files, f) end
     34 
     35 -- format status_file to string
     36 local format = function(file, name)
     37   local output
     38   if not file then
     39     table.insert(output, '')
     40   else
     41     local status = file:read('*n') -- 'n' means read a number
     42     -- local color = status_map[tonumber(status)]
     43     local icon = icon_map[tonumber(status)]
     44     -- Edit this to your liking
     45     output = string.format('%s %s', icon, name:upper())
     46     file:close()
     47   end
     48   return output
     49 end
     50 
     51 local result = {}
     52 -- For every file fill out results
     53 for _, status_file_name in ipairs(status_files) do
     54   local name, _ = status_file_name:match('(.*)%.(.*)')
     55   if name then
     56     local status_file_path = status_dir .. '/' .. status_file_name
     57     local status_file = io.open(status_file_path, 'r')
     58     if trun_name then
     59       if name == trun_name then table.insert(result, format(status_file, name)) end
     60     else
     61       table.insert(result, format(status_file, name))
     62     end
     63   end
     64 end
     65 
     66 -- print out results
     67 if #result > 0 then print('[' .. table.concat(result, ',') .. ']') end