Skip to content

Commit

Permalink
Use noice to show output nicely from commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Shatur committed Feb 9, 2023
1 parent e566edd commit a84feae
Showing 1 changed file with 4 additions and 50 deletions.
54 changes: 4 additions & 50 deletions plugin/fugitive.lua
Original file line number Diff line number Diff line change
@@ -1,53 +1,7 @@
local Job = require('plenary.job')

vim.g.fugitive_legacy_commands = false

local function run_command(command, args)
local title = 'Running ' .. command .. ' ' .. table.concat(args, ' ')
local output = ''
local length = 0
local notification
local current_win
local height

local on_data = function(_, data)
if #output ~= 0 then
output = output .. '\n'
end
output = output .. data

notification = vim.notify(output, vim.log.levels.INFO, {
title = title,
replace = notification,
on_open = function(win)
current_win = win
height = vim.api.nvim_win_get_height(current_win)
end,
})

if height then
vim.api.nvim_win_set_height(current_win, height + length)
end
length = length + 1
end

local job = Job:new({
command = command,
args = args,
on_stdout = vim.schedule_wrap(on_data),
on_stderr = vim.schedule_wrap(on_data),
on_exit = function(_, code, signal)
if code ~= 0 or signal ~= 0 then
vim.notify(output .. '\nExited with code: ' .. (signal == 0 and code or 128 + signal), vim.log.levels.ERROR, { title = title, replace = notification })
end
end,
})

job:start()
end

vim.keymap.set({ '', 't', 'i' }, '<A-g>', '<Cmd>vert Git<CR>))', { noremap = true, desc = 'Open Fugitive' })
vim.keymap.set('', '<Leader>gs', function() run_command('git', { 'status' }) end, { noremap = true, desc = 'Show git status' })
vim.keymap.set('', '<Leader>gP', function() run_command('git', { 'pull' }) end, { noremap = true, desc = 'Git pull' })
vim.keymap.set('', '<Leader>gp', function() run_command('git', { 'push' }) end, { noremap = true, desc = 'Git push' })
vim.keymap.set('', '<Leader>gfp', function() run_command('git', { 'push', '--force' }) end, { noremap = true, desc = 'Git push (force)' })
vim.keymap.set('', '<Leader>gs', function() vim.cmd.Git('status') end, { noremap = true, desc = 'Show git status' })
vim.keymap.set('', '<Leader>gP', function() vim.cmd.Git('pull') end, { noremap = true, desc = 'Git pull' })
vim.keymap.set('', '<Leader>gp', function() vim.cmd.Git('push') end, { noremap = true, desc = 'Git push' })
vim.keymap.set('', '<Leader>gfp', function() vim.cmd.Git('push', '--force') end, { noremap = true, desc = 'Git push (force)' })

0 comments on commit a84feae

Please sign in to comment.