aboutsummaryrefslogtreecommitdiffstats
path: root/lua/git-yodel.lua
diff options
context:
space:
mode:
authorSquibid <me@zacharyscheiman.com>2023-08-02 16:42:28 -0400
committerSquibid <me@zacharyscheiman.com>2023-08-02 16:42:28 -0400
commit33ab722485c15442a70859a88e1fe91377dc2e43 (patch)
tree3552c3609d85b5a793aa72dd6f22f018c5395036 /lua/git-yodel.lua
parentffc9552a256e23fed5c979f5fb0400f49130a8e5 (diff)
downloadgit-yodel-33ab722485c15442a70859a88e1fe91377dc2e43.tar.gz
git-yodel-33ab722485c15442a70859a88e1fe91377dc2e43.tar.bz2
git-yodel-33ab722485c15442a70859a88e1fe91377dc2e43.zip
inital commit
Diffstat (limited to '')
-rw-r--r--lua/git-yodel.lua117
1 files changed, 107 insertions, 10 deletions
diff --git a/lua/git-yodel.lua b/lua/git-yodel.lua
index 5f9a711..e0e8e80 100644
--- a/lua/git-yodel.lua
+++ b/lua/git-yodel.lua
@@ -1,21 +1,118 @@
local buf = require('modules.buf')
+local win = require('modules.win')
local sim = require('utils.simple')
local M = {}
+local C = {}
-local function commitdiff()
- local bufa = buf.create(true, false, 'Commit Diff')
- local conta = sim.cmdcontent('git diff --cached')
- if conta == nil then return end -- make sure we're setting from nothing
- vim.api.nvim_buf_set_option(bufa, 'ft', 'diff')
- vim.api.nvim_buf_set_lines(bufa, 0, -1, false, conta)
- buf.lock(bufa)
+function M.setup(c)
+ c = c or {}
+
+ C.diffpreview = {
+ border = c.diffpreview.border or 'single',
+ width = c.diffpreview.width or 'auto',
+ height = c.diffpreview.height or 'auto',
+ }
+end
+
+-- window columns offset
+local function winc()
+ local a = 0
+ local num = vim.o.number or vim.o.relativenumber
+
+ -- if numberline or relativenumbers are enabled make sure we account for it
+ if num then a = vim.o.numberwidth end
+ -- same for signcolumn
+ if vim.o.signcolumn then a = a + string.match(vim.o.signcolumn, '[0-9]') end
+ -- if both we will have some spacing room between them
+ if num and vim.o.signcolumn then a = a + 1 end
+ -- if we have either we need to account for padding next to the buffer
+ if num or vim.o.signcolumn then a = a + 1 end
+
+ return sim.longbufl(0) + a
+end
+
+-- set window width
+local function winw(b)
+ if C.diffpreview.width == 'auto' then
+ return sim.lower(
+ sim.longbufl(b),
+ vim.o.columns - winc() - 2
+ )
+ else
+ return C.diffpreview.width
+ end
+end
+
+-- set window height
+local function winh(b, x)
+ x = x or 0
+ if C.diffpreview.height == 'auto' then
+ return sim.lower(
+ -- lines in buffer - cmd height - statusline
+ vim.o.lines - vim.o.cmdheight - x - 4,
+ vim.api.nvim_buf_line_count(b)
+ )
+ else
+ return C.diffpreview.height
+ end
end
vim.api.nvim_create_autocmd('FileType', {
- group = 'bufcheck',
- papttern = { 'gitcommit', 'gitrebase' },
- callback = commitdiff()
+ pattern = { 'gitcommit', 'gitrebase' },
+ callback = function()
+ local bufa = buf.create(true, false, 'Commit Diff')
+ local conta = sim.cmdcontent('git diff --cached')
+ if conta == nil then return end -- make sure we're setting from nothing
+ vim.api.nvim_buf_set_option(bufa, 'ft', 'diff')
+ vim.api.nvim_buf_set_lines(bufa, 0, -1, false, conta)
+ buf.lock(bufa)
+
+ local winopts = {
+ row = 1,
+ col = winc(),
+ width = winw(bufa),
+ height = winh(bufa),
+ border = C.diffpreview.border,
+ relative = 'win',
+ style = 'minimal',
+ }
+
+ local wina = win.create(bufa, false, winopts)
+ -- NOTE: we monitor a few events to make sure the window changes positions
+ -- properly
+ vim.api.nvim_create_autocmd({
+ 'VimResized',
+ 'CursorMovedI',
+ 'CursorMoved',
+ }, {
+ callback = function()
+ if vim.o.columns < 100 then
+ win.hide(wina)
+ wina = win.create(bufa, false, {
+ row = vim.api.nvim_buf_line_count(0) + 1,
+ col = 2,
+ width = vim.o.columns - 4,
+ height = winh(bufa, vim.api.nvim_buf_line_count(0)),
+ border = C.diffpreview.border,
+ relative = 'win',
+ style = 'minimal',
+ })
+ else
+ win.hide(wina)
+ wina = win.create(bufa, false, {
+ row = 1,
+ col = winc(),
+ width = winw(bufa),
+ height = winh(bufa),
+ border = C.diffpreview.border,
+ relative = 'win',
+ style = 'minimal',
+ })
+ end
+ end
+ })
+ end
})
return M