feat(terminal): add configurable cursor render delay for terminal buffers

Add cursor position restoration with configurable timing to handle terminal
     buffer rendering asynchronously. This fixes cursor column being reset to 0
     when refreshing jj log commands.

     Changes:
     - Add `terminal.cursor_render_delay` configuration option (default: 10ms)
     - Add buffer.get_cursor() and buffer.set_cursor() helpers in core/buffer
       * buffer.set_cursor() uses defer_fn with configurable delay for terminal buffers
       * Automatic position validation with clamping to buffer bounds
       * Line number clamped to [1, line_count]
       * Column clamped to [0, line_length] based on actual line content
       * Validation happens inside deferred callback to check against final rendered content
     - Extract clamp_cursor_position() helper to eliminate code duplication
     - Refactor terminal module to use new buffer cursor helpers
     - Store and restore cursor position when cycling through log commands
     - Update README with terminal configuration section and example usage

     The delay is necessary because nvim_open_term() + nvim_chan_send() have
     asynchronous rendering in the terminal emulator layer. Setting the cursor
     before rendering completes results in the column being reset to 0. Users
     experiencing issues can increase the delay value if needed.
This commit is contained in:
NicolasGB
2025-11-25 09:10:12 +01:00
committed by Nicolas GB
parent 0cd1f554a6
commit 7329e89a85
6 changed files with 146 additions and 12 deletions
+8 -3
View File
@@ -15,7 +15,7 @@ local terminal = require("jj.ui.terminal")
--- @field raw_flags? string
---@type jj.cmd.log_opts
local default_log_opts = { summary = false, reversed = false, no_graph = false, limit = 20, raw_flats = nil }
local default_log_opts = { summary = false, reversed = false, no_graph = false, limit = 20, raw_flags = nil }
--- Jujutsu log
--- @param opts? jj.cmd.log_opts Optional command options
@@ -24,12 +24,17 @@ function M.log(opts)
return
end
-- If a log was already being displayed before this command we will want to maintain the cursor position
if terminal.state.buf_cmd == "log" then
terminal.store_cursor_position()
end
local jj_cmd = "jj log"
local merged_opts = vim.tbl_extend("force", default_log_opts, opts or {})
-- If a raw has been given simply execute it as is
if merged_opts.raw then
return terminal.run(string.format("%s %s", jj_cmd, merged_opts.raw), M.log_keymaps())
if merged_opts.raw_flags then
return terminal.run(string.format("%s %s", jj_cmd, merged_opts.raw_flags), M.log_keymaps())
end
for key, value in pairs(merged_opts) do