local M = {} local defaults = { out_waiting = "...", out_no_jj_repo = "~no_jj_repo~", out_no_desc = "~no_desc~", max_desc_length = 47, } M.config = vim.deepcopy(defaults) M.update_jj_status = function(bufnr) vim.api.nvim_buf_set_var(bufnr, "jj_desc", M.config.out_waiting) local current_file = vim.api.nvim_buf_get_name(bufnr) if current_file == "" or current_file == nil then vim.api.nvim_buf_del_var(bufnr, "jj_desc") return end if not vim.fn.filereadable(current_file) then vim.api.nvim_buf_del_var(bufnr, "jj_desc") return end local jj_root = vim.fs.find( ".jj", { upward = true, stop = vim.loop.os_homedir(), path = current_file, type = "directory", } )[1] if not jj_root then vim.api.nvim_buf_set_var(bufnr, "jj_desc", M.config.out_no_jj_repo) return end local repo_root = vim.fs.dirname(jj_root) local _cmd = string.format("jj log -R %s -r @ -T description --no-graph", repo_root) local cmd = { "jj", "log", "-R", repo_root, "-r", "@", "-T", "description", "--no-graph", } vim.system(cmd, { text = true, cwd = vim.fs.dirname(repo_root) }, function(obj) if obj.code ~= 0 then vim.api.nvim_buf_del_var(bufnr, "jj_desc") return end local output = vim.trim(obj.stdout or "") output = output:gsub("\n", ""):gsub("^%s*", ""):gsub("%s*$", "") if output == "" then output = M.config.out_no_desc end if #output > (M.config.max_desc_length + 3) then output = string.sub(output, 1, M.config.max_desc_length) .. "..." end vim.schedule(function() if vim.api.nvim_buf_is_valid(bufnr) then vim.api.nvim_buf_set_var(bufnr, "jj_desc", output) if bufnr == vim.api.nvim_get_current_buf() then vim.cmd.redrawstatus() end end end) end) end M.setup = function(opts) M.config = vim.tbl_deep_extend("force", M.config, opts or {}) local group = vim.api.nvim_create_augroup("JJLogPlugin", { clear = true }) vim.api.nvim_create_autocmd({ "BufEnter", "BufWinEnter", "BufWritePost", "FocusGained" }, { group = group, pattern = "*", callback = function(args) M.update_jj_status(args.buf) end, }) end return M