Skip to content

Commit 7df365a

Browse files
committed
feat: archive current-file creates directories
1 parent d91c958 commit 7df365a

File tree

2 files changed

+125
-25
lines changed

2 files changed

+125
-25
lines changed

README.md

+37-12
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,42 @@ Rocks.nvim 🗿
3131

3232
## Config
3333

34+
Two configuration steps are required. First, add an archive workspace:
35+
```lua
36+
require("neorg").setup({
37+
load = {
38+
["core.dirman"] = {
39+
config = {
40+
workspaces = {
41+
archive = "path/to/your/archive",
42+
},
43+
},
44+
},
45+
["external.interim-ls"] = {
46+
-- find interim-ls options here:
47+
-- https://github.com/benlubas/neorg-interim-ls?tab=readme-ov-file#install
48+
},
49+
["external.archive"] = {
50+
-- default config
51+
config = {
52+
-- (Optional) Archive workspace name, defaults to "archive"
53+
workspace = "archive",
54+
-- (Optional) Enable/disable confirming archive operations
55+
confirm = true
56+
}
57+
}
58+
},
59+
})
3460
```
35-
["external.interim-ls"] = {
36-
-- find interim-ls options here:
37-
-- https://github.com/benlubas/neorg-interim-ls?tab=readme-ov-file#install
38-
}
39-
["external.archive"] = {
40-
-- default config
41-
config = {
42-
-- (Optional) Archive workspace name, defaults to "archive"
43-
workspace = "archive",
44-
}
45-
},
46-
```
4761

62+
## Usage
63+
64+
The archive module adds the following commands:
65+
66+
### `:Neorg archive current-file`
67+
Moves the currently opened file to the archive: `archive-workspace/workspace-name/path-to-file`
68+
69+
### `:Neorg archive current-directory`
70+
Moves the current file's directory to the archive: `archive-workspace/workspace-name/path-to-directory`
71+
### `:Neorg archive restore`
72+
Moves an archived file back to it's workspace from `archive-workspace/workspace/file,norg` to `workspace/file.norg`
+88-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
local neorg = require("neorg.core")
2-
local modules, lib, log = neorg.modules, neorg.lib, neorg.log
2+
local modules, log = neorg.modules, neorg.log
33

44
local dirman ---@type core.dirman
5+
local neorgcmd ---@type core.neorgcmd
56
local refactor ---@type external.refactor
67

78
local module = modules.create("external.archive")
89

910
module.config.public = {
1011
-- (Optional) Archive workspace name, defaults to "archive"
1112
workspace = "archive",
13+
-- (Optional) Enable/disable confirming archive operations
14+
confirm = true
1215
}
1316

1417
module.events.subscribed = {
1518
["core.neorgcmd"] = {
16-
["external.archive.current-file"] = true,
17-
["external.archive.current-directory"] = true,
18-
["external.archive.restore"] = true,
19+
["archive.current-file"] = true,
20+
["archive.current-directory"] = true,
21+
["archive.restore"] = true,
1922
},
2023
}
2124

@@ -25,43 +28,115 @@ module.setup = function()
2528
success = true,
2629
requires = {
2730
"core.dirman",
28-
"external.interim-ls"
31+
"core.neorgcmd",
32+
"external.refactor"
2933
},
3034
}
3135
end
3236

3337
module.load = function()
3438
dirman = module.required["core.dirman"]
3539
refactor = module.required["external.refactor"]
40+
neorgcmd = module.required["core.neorgcmd"]
41+
42+
local archive_workspace = dirman.get_workspace(module.config.public.workspace)
43+
if (not archive_workspace) then
44+
log.fatal("[neorg-archive] Archive workspace not found! Please add one to your Neorg config")
45+
return
46+
end
47+
48+
neorgcmd.add_commands_from_table({
49+
archive = {
50+
min_args = 1,
51+
max_args = 1,
52+
args = 1,
53+
condition = "norg",
54+
subcommands = {
55+
["current-file"] = {
56+
args = 0,
57+
name = "archive.current-file",
58+
},
59+
["current-directory"] = {
60+
args = 0,
61+
name = "archive.current-directory",
62+
},
63+
["restore"] = {
64+
args = 0,
65+
name = "archive.restore",
66+
},
67+
68+
69+
},
70+
},
71+
})
3672
end
3773

3874
module.on_event = function(event)
3975
if event.split_type[1] == "core.neorgcmd" then
4076
if event.split_type[2] == "archive.current-file" then
4177
module.public.archive_current_file()
4278
elseif event.split_type[2] == "archive.current-directory" then
43-
module.public.archive_current_file()
79+
module.public.archive_current_directory()
4480
elseif event.split_type[2] == "archive.restore" then
45-
module.public.archive_current_file()
81+
module.public.restore()
4682
end
4783
end
4884
end
4985

5086
module.public.archive_current_file = function()
51-
local workspace = dirman.get_current_workspace()
87+
if (not module.private.confirm_archive_operation("archive current file")) then
88+
return
89+
end
90+
91+
local workspace = dirman.get_workspace_match()
92+
if (workspace == module.config.public.workspace) then
93+
log.error("Cannot archive files within the archive workspace!")
94+
return
95+
end
5296

53-
local _, archive_path = dirman.get_workspace(module.config.public.workspace)
97+
local archive_path = tostring(dirman.get_workspace(module.config.public.workspace))
98+
local current_path = vim.api.nvim_buf_get_name(0)
99+
local current_workspace_path = tostring(dirman.get_workspace(workspace))
54100

55-
-- get current workspace
56-
-- get workspace path
57-
-- get archive workspace path
58-
-- use refactor module to move file archive workspace path / workspace-name / subpath of prior workspace
101+
local new_path = current_path:gsub("^" .. current_workspace_path, archive_path .. "/" .. workspace)
102+
103+
local success = refactor.rename_file(current_path, new_path)
104+
if (not success) then
105+
log.error("Failed to archive " .. current_path)
106+
else
107+
log.info("Archived file under " .. new_path)
108+
end
59109
end
60110

61111
module.public.archive_current_directory = function()
112+
if (not module.private.confirm_archive_operation("archive current directory")) then
113+
return
114+
end
115+
116+
local workspace = dirman.get_current_workspace()
117+
if (workspace == module.config.public.workspace) then
118+
log.error("Cannot archive files within the archive workspace!")
119+
return
120+
end
121+
-- TODO
62122
end
63123

64124
module.public.archive_restore = function()
125+
if (not module.private.confirm_archive_operation("restore current file")) then
126+
return
127+
end
128+
-- TODO
129+
end
130+
131+
132+
--- Confirm archive operations based on module configuration
133+
---@param operation string #The name of the operation to confirm, used in user prompt
134+
---@return boolean #Confirmation status, if false abort operation
135+
module.private.confirm_archive_operation = function(operation)
136+
if (module.config.public.confirm) then
137+
return vim.fn.confirm("Are you sure you want to " .. operation .. "? y/n", "&Yes\n&No", 2) == 1
138+
end
139+
return true
65140
end
66141

67142
return module

0 commit comments

Comments
 (0)