]>
Commit | Line | Data |
---|---|---|
1 | ------------------------------------------------------------------------------- | |
2 | -- Tools to deal with the plan file | |
3 | ------------------------------------------------------------------------------- | |
4 | local Plan = {} | |
5 | ||
6 | local Configuration = require('nota.configuration') | |
7 | local Util = require('nota.util') | |
8 | ------------------------------------------------------------------------------- | |
9 | -- Internal Functions | |
10 | ------------------------------------------------------------------------------- | |
11 | local function open_or_create_from_template(file_path, force_template) | |
12 | ||
13 | local plan_file = io.open(file_path, 'r') | |
14 | if force_template or not plan_file then | |
15 | local template_contents = Configuration.load_template('plan') | |
16 | local date = os.date('%Y-%m-%d') | |
17 | ||
18 | template_contents = template_contents .. '[' .. date .. ']\n' | |
19 | plan_file = io.open(file_path, 'w') | |
20 | plan_file:write(template_contents) | |
21 | plan_file:close() | |
22 | end | |
23 | vim.cmd('edit ' .. file_path) | |
24 | end | |
25 | ||
26 | local function copy(plan_path, archive_path) | |
27 | local plan_file = io.open(plan_path, 'r') | |
28 | local archive_file = io.open(archive_path, 'wb') | |
29 | local plan_content = plan_file:read('*a') | |
30 | archive_file:write(plan_content) -- Write the content to the target file | |
31 | ||
32 | plan_file:close() | |
33 | archive_file:close() | |
34 | end | |
35 | ||
36 | ------------------------------------------------------------------------------- | |
37 | -- Public Interface | |
38 | ------------------------------------------------------------------------------- | |
39 | ||
40 | --- Opens the active plan file | |
41 | function Plan.open(force_template) | |
42 | local plan_path = vim.fn.expand(Configuration.configuration.plan.plan_file) | |
43 | local plan_parent = Util.directory_name(plan_path) | |
44 | Util.ensure_directory_exists(plan_parent) | |
45 | open_or_create_from_template(plan_path, force_template) | |
46 | end | |
47 | ||
48 | --- Capture a new plan file and archive the current one | |
49 | function Plan.capture() | |
50 | local fs = vim.loop | |
51 | ||
52 | local plan_path = vim.fn.expand(Configuration.configuration.plan.plan_file) | |
53 | local plan_parent = Util.directory_name(plan_path) | |
54 | Util.ensure_directory_exists(plan_parent) | |
55 | ||
56 | local archive_path = Configuration.path_for(Configuration.configuration.plan.archive) | |
57 | Util.ensure_directory_exists(archive_path) | |
58 | ||
59 | if fs.fs_stat(plan_path) then | |
60 | local archive_filename = os.date('%Y-%m-%d') .. '.md' | |
61 | copy(plan_path, Util.join(archive_path, archive_filename)) | |
62 | end | |
63 | ||
64 | Plan.open(true) | |
65 | end | |
66 | ||
67 | return Plan |