util.lua 3.32 KB
Newer Older
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
1 2
--[[
Utility module for Alpine Wall
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
3
Copyright (C) 2012-2019 Kaarle Ritvanen
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
4
See LICENSE file for license details
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
5 6 7
]]--


8
local M = {}
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
9

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
10 11 12
local lpc = require('lpc')


13
function M.split(s, sep)
14 15 16
   if s == '' then return {} end
   local res = {}
   while true do
17
      local si, ei = s:find(sep, 1, true)
18 19 20 21
      if not si then
	 table.insert(res, s)
	 return res
      end
22 23
      table.insert(res, s:sub(1, si - 1))
      s = s:sub(ei + 1, -1)
24 25 26
   end
end

27
function M.list(var)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
28
   if not var then return {} end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
29 30 31
   if type(var) ~= 'table' then return {var} end
   if not next(var) then return {} end
   return var[1] and var or {var}
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
32 33
end

34
function M.listpairs(var) return ipairs(M.list(var)) end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
35

36
function M.filter(var, func)
37 38 39 40 41
   local res = {}
   for i, v in ipairs(var) do if func(v) then table.insert(res, v) end end
   return res
end

42
function M.map(var, func)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
43 44 45 46 47
   local res = {}
   for k, v in pairs(var) do res[k] = func(v) end
   return res
end

48
function M.maplist(var, func)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
49
   if not var then return var end
50
   return M.map(M.list(var), func)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
51 52
end

53 54
function M.contains(tbl, value)
   for k, v in M.listpairs(tbl) do if v == value then return true end end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
55 56
   return false
end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
57

58
function M.keys(tbl)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
59 60 61 62 63
   local res = {}
   for k, v in pairs(tbl) do table.insert(res, k) end
   return res
end

64
function M.values(tbl)
65 66 67 68 69
   local res = {}
   for k, v in pairs(tbl) do table.insert(res, v) end
   return res   
end

70 71
function M.sortedkeys(tbl)
   local res = M.keys(tbl)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
72 73 74 75
   table.sort(res)
   return ipairs(res)
end

76 77
function M.extend(tbl1, tbl2)
   for i, var in M.listpairs(tbl2) do table.insert(tbl1, var) end
78
   return tbl1
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
79
end
80

81
function M.update(tbl1, tbl2)
82 83 84 85
   if tbl2 then for k, v in pairs(tbl2) do tbl1[k] = v end end
   return tbl1
end

86 87 88 89 90
function M.setdefault(t, k, v)
   if t[k] == nil then t[k] = v end
   return t[k]
end

91 92 93
function M.copy(var)
   return type(var) == 'table' and M.update({}, var) or var
end
94

95
function M.compare(a, b)
96 97 98 99 100 101
   local t = type(a)
   if t ~= type(b) then return false end
   if t ~= 'table' then return a == b end

   local keys = {}
   for k, v in pairs(a) do
102
      if not M.compare(v, b[k]) then return false end
103 104 105
      table.insert(keys, k)
   end
   for k, v in pairs(b) do
106
      if not M.contains(keys, k) then return false end
107 108 109 110
   end
   return true
end

111 112 113 114 115 116 117 118 119 120 121 122 123
function M.join(a, sep, b)
   local comps = {}
   local function add(s)
      if not s then return end
      s = tostring(s)
      if s > '' then table.insert(comps, s) end
   end
   add(a)
   add(b)
   if comps[1] then return table.concat(comps, sep) end
end


Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
124 125 126
function M.quote(s) return '"'..s:gsub('(["\\])', '\\%1')..'"' end


127
function M.printtabulars(tables)
128
   local colwidth = {}
129 130 131
   for i, tbl in ipairs(tables) do
      for j, row in ipairs(tbl) do
	 for k, col in ipairs(row) do
132
	    colwidth[k] = math.max(colwidth[k] or 0, col:len())
133
	 end
134 135
      end
   end
136 137 138 139 140 141
   for i, tbl in ipairs(tables) do
      for j, row in ipairs(tbl) do
	 for k = 1,#row do
	    if k > 1 then io.write('  ') end
	    io.write(row[k])
	    if k < #row then
142
	       for l = 1,colwidth[k] - row[k]:len() do io.write(' ') end
143 144 145
	    end
	 end
	 io.write('\n')
146 147 148 149
      end
      io.write('\n')
   end
end
150

151 152
function M.printtabular(tbl) M.printtabulars({tbl}) end

153 154 155

function M.printmsg(msg) io.stderr:write(msg..'\n') end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
156 157 158 159 160 161 162 163

function M.run(...)
   local pid, stdin, stdout = lpc.run(...)
   stdin:close()
   stdout:close()
   return pid
end

164
return M