]> git.r.bdr.sh - rbdr/nota.nvim/blame - lua/util.lua
Add minimum workable functionality
[rbdr/nota.nvim] / lua / util.lua
CommitLineData
503d09fc
RBR
1-------------------------------------------------------------------------------
2-- Utilities shared by all modules. Categorize better if > 5 public functions
3-------------------------------------------------------------------------------
4local Util = {}
5-------------------------------------------------------------------------------
6-- Internal Functions
7-------------------------------------------------------------------------------
8local fs = vim.loop
9
10local function create_directory(directory)
11 local stat = fs.fs_stat(directory)
12 if stat then
13 if stat.type == 'directory' then
14 return
15 else
16 error('Expected directory but found file at: ' .. directory)
17 end
18 else
19 local parent = directory:match('(.+)/[^/]*$')
20 if parent then
21 create_directory(parent)
22 end
23 local success, error = fs.fs_mkdir(directory, 493)
24 if not success then
25 error('Could not directory at: ' .. directory .. '. ' .. error)
26 end
27 end
28end
29-------------------------------------------------------------------------------
30-- Public Interface
31-------------------------------------------------------------------------------
32function Util.ensure_directory_exists(path)
33 local full_path = vim.fn.expand(path)
34 create_directory(path)
35end
36
37function Util.join(...)
38 local separator = '/'
39 local paths = {...}
40 return table.concat(paths, separator):gsub(separator..'+', separator)
41end
42
43function Util.directory_name(file_path)
44 local pattern = '(.+)/[^/]+$'
45 return file_path:match(pattern)
46end
47
48return Util