]> git.r.bdr.sh - rbdr/nota.nvim/blob - lua/util.lua
Add rescheduling tasks
[rbdr/nota.nvim] / lua / util.lua
1 -------------------------------------------------------------------------------
2 -- Utilities shared by all modules. Categorize better if > 5 public functions
3 -------------------------------------------------------------------------------
4 local Util = {}
5 -------------------------------------------------------------------------------
6 -- Internal Functions
7 -------------------------------------------------------------------------------
8 local fs = vim.loop
9
10 local 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
28 end
29 -------------------------------------------------------------------------------
30 -- Public Interface
31 -------------------------------------------------------------------------------
32 -- File Utils
33
34 function Util.ensure_directory_exists(path)
35 local full_path = vim.fn.expand(path)
36 create_directory(path)
37 end
38
39 -- Path Utils
40
41 function Util.join(...)
42 local separator = '/'
43 local paths = {...}
44 return table.concat(paths, separator):gsub(separator..'+', separator)
45 end
46
47 function Util.directory_name(file_path)
48 local pattern = '(.+)/[^/]+$'
49 return file_path:match(pattern)
50 end
51
52 -- Date Utils
53
54 function Util.is_valid_date(date_string)
55 local pattern = '(%d+)-(%d+)-(%d+)'
56 local year, month, day = date_string:match(pattern)
57 if year and month and day then
58 return true
59 end
60 return false
61 end
62
63 function Util.is_before_today(date_string)
64 local pattern = '(%d+)-(%d+)-(%d+)'
65 local year, month, day = date_string:match(pattern)
66 if year and month and day then
67 local today = os.date('*t')
68 local today_date = os.time({year = today.year, month = today.month, day = today.day})
69 local date = os.time({year = tonumber(year), month = tonumber(month), day = tonumber(day)})
70 return date < today_date
71 else
72 return false
73 end
74 end
75
76 return Util