summaryrefslogtreecommitdiff
path: root/lua/99/utils.lua
blob: 1dcd09e2c7237953e658fc8c05d37831a1e3374e (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
local M = {}

--- @param str string
---@param word_count number
---@return string[]
function M.split_with_count(str, word_count)
  local out = {}
  local words = vim.split(str, "%s+", { trimempty = true })

  local count = math.min(word_count, #words)
  for i = 1, count do
    table.insert(out, words[i])
  end

  return out
end

function M.copy(t)
  assert(type(t) == "table", "passed in non table into table")
  local out = {}
  for k, v in pairs(t) do
    out[k] = v
  end
  for i, v in ipairs(t) do
    out[i] = v
  end
  return out
end

--- @param dir string
--- @return string
function M.random_file(dir)
  return string.format("%s/99-%d", dir, math.floor(math.random() * 10000))
end

--- @param dir string
--- @param name string
--- @return string
function M.named_tmp_file(dir, name)
  return string.format("%s/99-%s", dir, name)
end

--- @param path string
--- @return table | nil
function M.read_file_json_safe(path)
  local ok, fh = pcall(io.open, path, "r")
  if ok and fh then
    local ok2, content = pcall(fh.read, fh, "*a")
    pcall(fh.close, fh)
    if not ok2 then
      return nil
    end
    local ok3, obj = pcall(vim.json.decode, content)
    if ok3 and obj then
      return obj
    end
  end
end

--- @param obj table
---@param path string
function M.write_file_json_safe(obj, path)
  local ok, fh = pcall(io.open, path, "w")
  if not ok or not fh then
    return
  end

  local obj_str
  ok, obj_str = pcall(vim.json.encode, obj)
  if not ok then
    return
  end

  pcall(fh.write, fh, obj_str)
end

return M