Skip to content

Commit e54dc8e

Browse files
committed
feat: make gitpad more extensible by passing filename
1 parent c7f8386 commit e54dc8e

File tree

2 files changed

+65
-31
lines changed

2 files changed

+65
-31
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ A minimal neovim plugin for taking down notes for git projects and per branch
99
of floating windows.
1010
- Supports creating and toggling a separate `{branch}-branchpad.md` file for each branch,
1111
if desired.
12+
- Extensible note list (daily notes, per-file notes, etc.)
1213

1314
## ⚡️ Requirements
1415
- Neovim >= 0.7.2
@@ -44,6 +45,29 @@ using [lazy.nvim](https://github.com/folke/lazy.nvim):
4445
end,
4546
desc = 'gitpad branch',
4647
},
48+
-- Daily notes
49+
{
50+
'<leader>pd',
51+
function()
52+
local date_filename = 'daily-' .. os.date('%Y-%m-%d.md')
53+
require('gitpad').toggle_gitpad { filename = date_filename }
54+
end,
55+
desc = 'gitpad daily notes',
56+
},
57+
-- Per file notes
58+
{
59+
'<leader>pf',
60+
function()
61+
local bufname = vim.fn.bufname()
62+
if bufname == '' then
63+
vim.notify('empty bufname')
64+
return
65+
end
66+
bufname = bufname .. '.md'
67+
require('gitpad').toggle_gitpad { filename = bufname }
68+
end,
69+
desc = 'gitpad per file notes',
70+
},
4771
},
4872
end
4973
}

lua/gitpad/init.lua

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
local M = {}
2+
local H = {}
23
local uv = vim.uv or vim.loop
34
local gitpad_win_id = nil
45

56
M.config = {
67
border = 'single',
7-
dir = vim.fn.stdpath('data') .. '/gitpad',
8+
dir = vim.fs.normalize(vim.fn.stdpath('data') .. '/gitpad'),
89
style = '',
910
default_text = nil,
1011
on_attach = nil,
1112
}
1213

13-
local function is_git_dir()
14+
function H.is_git_dir()
1415
if vim.fn.system('git rev-parse --is-inside-work-tree 2>/dev/null') ~= '' then
1516
return true
1617
end
@@ -24,17 +25,34 @@ local function is_git_dir()
2425
return false
2526
end
2627

27-
function M.init_gitpad_file(params)
28-
local is_branch = params.is_branch or false
28+
function H.get_branch_filename()
29+
-- local branch_name = vim.fn.systemlist('basename `git rev-parse --abbrev-ref HEAD`')[1]
30+
local branch_name = vim.fn.systemlist('git branch --show-current')[1]
31+
return H.clean_filename(branch_name)
32+
end
33+
34+
function H.clean_filename(filename)
35+
if filename == nil then
36+
return nil
37+
end
38+
39+
-- remove any spaces in the branch name and replace with a hyphen
40+
-- replace any forward slashes with a colon so that the file is not created in a subdirectory
41+
filename = filename:gsub('%s+', '-'):gsub('/', ':')
42+
return filename
43+
end
44+
45+
function M.init_gitpad_file(opts)
46+
local clean_name = H.clean_filename(opts.filename) or nil
2947

3048
-- get the git repository name of the current directory
31-
if not is_git_dir() then
49+
if not H.is_git_dir() then
3250
return
3351
end
3452

3553
-- create the repository directory if it doesn't exist
3654
local repository_name = vim.fn.systemlist('basename `git rev-parse --show-toplevel`')[1]
37-
local notes_dir = vim.fs.normalize(M.config.dir) .. '/' .. repository_name
55+
local notes_dir = vim.fs.normalize(M.config.dir .. '/' .. repository_name)
3856

3957
-- create the notes directory if it doesn't exist
4058
if not uv.fs_stat(notes_dir) then
@@ -44,18 +62,15 @@ function M.init_gitpad_file(params)
4462
local gitpad_file_path
4563
local gitpad_default_text
4664

47-
if is_branch then
48-
-- local branch_name = vim.fn.systemlist('basename `git rev-parse --abbrev-ref HEAD`')[1]
49-
local branch_name = vim.fn.systemlist('git branch --show-current')[1]
50-
51-
-- remove any spaces in the branch name and replace with a hyphen
52-
local filename = branch_name:gsub('%s+', '-')
65+
if clean_name ~= nil then
66+
gitpad_default_text = '# ' .. clean_name .. ' \n\nThis is your new gitpad file.\n'
5367

54-
-- replace any forward slashes with a colon so that the file is not created in a subdirectory
55-
filename = branch_name:gsub('/', ':')
68+
if clean_name == H.get_branch_filename() then
69+
clean_name = clean_name .. '-branchpad.md'
70+
gitpad_default_text = '# ' .. clean_name .. ' Branchpad\n\nThis is your gitpad branch file.\n'
71+
end
5672

57-
gitpad_file_path = notes_dir .. '/' .. vim.fn.fnameescape(filename) .. '-branchpad.md'
58-
gitpad_default_text = '# ' .. branch_name .. ' Branchpad\n\nThis is your Gitpad Branch file.\n'
73+
gitpad_file_path = notes_dir .. '/' .. vim.fn.fnameescape(clean_name)
5974
else
6075
gitpad_file_path = notes_dir .. '/gitpad.md'
6176
gitpad_default_text = '# Gitpad\n\nThis is your Gitpad file.\n'
@@ -84,7 +99,7 @@ function M.init_gitpad_file(params)
8499
return gitpad_file_path
85100
end
86101

87-
function M.close_window(params)
102+
function M.close_window(opts)
88103
local wininfo = vim.fn.getwininfo(gitpad_win_id)
89104

90105
-- We might have closed the window not via this method so we need to
@@ -99,7 +114,7 @@ function M.close_window(params)
99114

100115
-- Just ensure that we are closing the correct window
101116
-- This is just to prevent closing a gitpad project window or gitpad branch window
102-
if bufname == params.path then
117+
if bufname == opts.path then
103118
vim.api.nvim_win_close(gitpad_win_id, true)
104119
gitpad_win_id = nil
105120
return true
@@ -108,13 +123,9 @@ function M.close_window(params)
108123
return false
109124
end
110125

111-
function M.open_window(params)
112-
local path = params.path
113-
local is_branch = params.is_branch or false
126+
function M.open_window(opts)
127+
local path = opts.path
114128
local title = ' gitpad '
115-
if is_branch then
116-
title = ' gitpad:branch '
117-
end
118129

119130
local ui = vim.api.nvim_list_uis()[1]
120131
local width = math.floor((ui.width * 0.5) + 0.5)
@@ -171,21 +182,20 @@ function M.open_window(params)
171182
end
172183
end
173184

174-
function M.toggle_window(params)
175-
if not M.close_window(params) then
176-
M.open_window(params)
185+
function M.toggle_window(opts)
186+
if not M.close_window(opts) then
187+
M.open_window(opts)
177188
end
178189
end
179190

180191
function M.toggle_gitpad(opts)
181192
opts = opts or {}
182-
opts.is_branch = opts.is_branch or false
183-
local path = M.init_gitpad_file { is_branch = opts.is_branch }
184-
M.toggle_window { is_branch = opts.is_branch, path = path }
193+
local path = M.init_gitpad_file(opts)
194+
M.toggle_window { path = path }
185195
end
186196

187197
function M.toggle_gitpad_branch()
188-
M.toggle_gitpad { is_branch = true }
198+
M.toggle_gitpad { filename = H.get_branch_filename() }
189199
end
190200

191201
M.setup = function(opts)

0 commit comments

Comments
 (0)