From 3fa16b090133c31d926a7bf4ff9a5e1f1d0239b4 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Fri, 14 Jul 2023 15:47:33 -0500 Subject: [PATCH 1/2] fix: only enter data into neovim terminal when console is shown Closes #587 --- lua/neogit/process.lua | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lua/neogit/process.lua b/lua/neogit/process.lua index c8fcf983c..0107d94a0 100644 --- a/lua/neogit/process.lua +++ b/lua/neogit/process.lua @@ -101,12 +101,10 @@ local function create_preview_buffer() }, } - local chan = vim.api.nvim_open_term(buffer.handle, {}) - preview_buffer = { - chan = chan, buffer = buffer, current_span = nil, + content = "", } end @@ -140,15 +138,15 @@ end function Process.show_console() create_preview_buffer() + vim.api.nvim_chan_send(vim.api.nvim_open_term(preview_buffer.buffer.handle, {}), preview_buffer.content) local win = preview_buffer.buffer:show() scroll_to_end(win) + vim.cmd.stopinsert() -- Ensures we're not in insert mode -- vim.api.nvim_win_call(win, function() -- vim.cmd.normal("G") -- end) end -local nvim_chan_send = vim.api.nvim_chan_send - ---@param process Process ---@param data string local function append_log(process, data) @@ -158,11 +156,12 @@ local function append_log(process, data) end if preview_buffer.current_span ~= process.job then - nvim_chan_send(preview_buffer.chan, string.format("> %s\r\n", table.concat(process.cmd, " "))) + preview_buffer.content = preview_buffer.content + .. string.format("> %s\r\n", table.concat(process.cmd, " ")) preview_buffer.current_span = process.job end - nvim_chan_send(preview_buffer.chan, data .. "\r\n") + preview_buffer.content = preview_buffer.content .. data .. "\r\n" end vim.schedule(function() From c4b01408ec4f1cef80c2b6410a1a184c995836a5 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Fri, 14 Jul 2023 16:10:41 -0500 Subject: [PATCH 2/2] fix: properly scroll the end of viewable text in console --- lua/neogit/process.lua | 35 +++++------------------------------ 1 file changed, 5 insertions(+), 30 deletions(-) diff --git a/lua/neogit/process.lua b/lua/neogit/process.lua index 0107d94a0..5954c2013 100644 --- a/lua/neogit/process.lua +++ b/lua/neogit/process.lua @@ -108,40 +108,15 @@ local function create_preview_buffer() } end ---from https://github.com/stevearc/overseer.nvim/blob/82ed207195b58a73b9f7d013d6eb3c7d78674ac9/lua/overseer/util.lua#L119 ----@param win number -local function scroll_to_end(win) - local bufnr = vim.api.nvim_win_get_buf(win) - local lnum = vim.api.nvim_buf_line_count(bufnr) - local last_line = vim.api.nvim_buf_get_lines(bufnr, -2, -1, true)[1] - -- Hack: terminal buffers add a bunch of empty lines at the end. We need to ignore them so that - -- we don't end up scrolling off the end of the useful output. - -- This has the unfortunate effect that we may not end up tailing the output as more arrives - if vim.bo[bufnr].buftype == "terminal" then - local half_height = math.floor(vim.api.nvim_win_get_height(win) / 2) - for i = lnum, 1, -1 do - local prev_line = vim.api.nvim_buf_get_lines(bufnr, i - 1, i, true)[1] - if prev_line ~= "" then - -- Only scroll back if we detect a lot of padding lines, and the total real output is - -- small. Otherwise the padding may be legit - if lnum - i >= half_height and i < half_height then - lnum = i - last_line = prev_line - end - break - end - end - end - vim.api.nvim_win_set_cursor(win, { lnum, vim.api.nvim_strwidth(last_line) }) -end - function Process.show_console() create_preview_buffer() vim.api.nvim_chan_send(vim.api.nvim_open_term(preview_buffer.buffer.handle, {}), preview_buffer.content) - local win = preview_buffer.buffer:show() - scroll_to_end(win) - vim.cmd.stopinsert() -- Ensures we're not in insert mode + preview_buffer.buffer:show() + -- Scroll to the end of viewable text + vim.cmd.startinsert() + vim.defer_fn(vim.cmd.stopinsert, 50) + -- vim.api.nvim_win_call(win, function() -- vim.cmd.normal("G") -- end)