aboutsummaryrefslogtreecommitdiff
path: root/lib/util.lua
blob: 3ceb4cf23134dc4ed2a7446dfbc540199f55eba2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
--
-- melisma - extensible and scriptable audio engine
-- Copyright (C) 2026  Ludvig Gunne Lindström
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <https://www.gnu.org/licenses/>.
--

local M = {}

function M.deepstring(value,visited,depth,is_key)
  if not is_key and type(value) == 'string' then return '\'' .. value .. '\'' end
  if type(value) ~= 'table' then return tostring(value) end
  if next(value) == nil then return '{}' end

  visited = visited or {}
  visited[value] = true

  depth = depth or 0
  local indent = string.rep('  ',depth)

  local str = {}
  local function append(...) table.insert(str,string.format(...)) end

  append('{ ')
  for k,v in pairs(value) do
    if visited[k] then k='{...}' else k=M.deepstring(k,visited,depth+1,true) end
    if visited[v] then v='{...}' else v=M.deepstring(v,visited,depth+1) end
    append(indent .. '  ' .. '%s = %s, ',k,v)
  end

  append(indent .. '}')
  return table.concat(str,'\n')
end

function M.deepcopy(x)
  if type(x) ~= 'table' then return x end
  local copy = {}
  for k,v in pairs(x) do copy[k] = M.deepcopy(v) end
  return copy
end

function M.size(x)
  local s = 0
  for _,_ in pairs(x) do
    s = s + 1
  end
  return s
end

function M.image(a,b,c,d,x) return (x - a)/(b - a)*(d - c) + c end

function M.select(list,prompt)
  for i,entry in ipairs(list) do
    print(i,entry)
  end

  while true do
    io.write(prompt .. '> ')
    io.flush()

    local input = io.read('*l')
    local i = tonumber(input)

    if i == nil then
      print()
      error()
    end

    if i >= 1 and i <= #list then
      return list[i]
    end
  end
end

function M.splitcmd(str)
  local words = {}
  local word = ''
  local state = 'skip'

  for i = 1,#str do
    local c = string.sub(str,i,i)

    if state == 'skip' then
      if c == '"' then
        state = 'string'
      elseif c ~= ' ' then
        state = 'take'
        word = word .. c
      end
    elseif state == 'take' then
      if c == ' ' then
        state = 'skip'
        table.insert(words,word)
        word = ''
      elseif c == '"' then
        state = 'string'
      else
        word = word .. c
      end
    elseif state == 'string' then
      if c == '"' then
        state = 'take'
      else
        word = word .. c
      end
    end
  end

  if state ~= 'skip' then
    table.insert(words,word)
  end

  return words
end

function M.openmodule(module)
  if module.lazy_require_getmodule ~= nil then
    module = module.lazy_require_getmodule()
  end

  for k,v in pairs(module) do
    if string.sub(k,1,2) ~= '__' then
      _G[k] = v
    end
  end
end

return M