trun

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

handler_temp_output_file.lua (1898B)


      1 -- Trun advanced handler _caches_ output betwen status changes to tmp_file.
      2 --
      3 -- So for exmple, on error you fill quickfix list inside vim.
      4 --
      5 -- viz->tools/trun_to_neovim_quickfix.lua and helper_functions.lua
      6 -- for examples to send command to neovim instances.
      7 --
      8 -- interface
      9 -- - handle(userdata, line): return status
     10 -- - (optional) on_start(userdata): before start reading
     11 -- - (optional) on_update(userdata, line, status): when status changed
     12 -- - (optional) on_end(userdata): after reading end
     13 --
     14 local status_map = {['running'] = 0, ['success'] = 1, ['error'] = -1}
     15 
     16 local function get_status(line)
     17   if line:find('success') then
     18     return status_map.success
     19   elseif line:find('running') then
     20     return status_map.running
     21   else
     22     return status_map.error
     23   end
     24 end
     25 
     26 return {
     27 
     28   -- on_start(userdata): before start reading
     29   on_start = function(data)
     30     -- re-create tmp_file and save it to userdata table.
     31     data.tmpfile = '/tmp/' .. data.name .. '.trun'
     32     os.execute('rm ' .. data.tmpfile .. ' 2>/dev/null')
     33   end,
     34 
     35   -- handle(userdata, line): return status
     36   handle = function(line, data)
     37     -- write each line to tmp_file
     38     local tmpfile = io.open(data.tmpfile, 'a')
     39     tmpfile:write(line, '\n')
     40     tmpfile:close()
     41     return {get_status(line), data.tmpfile}
     42     -- if you send table, each item is added on new line to tmpfile
     43   end,
     44 
     45   -- on_update(userdata, line, status): time when status changed
     46   on_update = function(_, status, data)
     47     -- on success, clear tmp_file.
     48     -- Here status is the same table as it was given inside `handle` function.
     49     if status[1] == status_map.success then
     50       io.open(data.tmpfile, 'w'):close()
     51     end
     52     -- viz->examples/handler_simple.lua for other examples wit neovim-remote
     53   end,
     54 
     55   -- on_end(userdata): after reading end
     56   on_end = function(data)
     57     -- remove tmp_file
     58     os.execute('rm ' .. data.tmpfile)
     59   end,
     60 }