From 9da62d982eb4d7799f83041b8e75818f03ad5d2e Mon Sep 17 00:00:00 2001 From: "Harrison (Harry) Cramer" <32515581+harrisoncramer@users.noreply.github.com> Date: Wed, 13 Dec 2023 18:51:18 -0500 Subject: [PATCH] Bugfix: Pipeline Job Names w/ Whitespace (#140) Fix for the pipeline job output where the job name contained whitespace characters. Users will now see the output in a new tab. Support has been added for Windows OS. --- lua/gitlab/actions/pipeline.lua | 48 +++++++++++++++++++++------------ lua/gitlab/utils/init.lua | 8 ++++++ 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/lua/gitlab/actions/pipeline.lua b/lua/gitlab/actions/pipeline.lua index 7cb086d..55282a8 100644 --- a/lua/gitlab/actions/pipeline.lua +++ b/lua/gitlab/actions/pipeline.lua @@ -61,16 +61,28 @@ M.open = function() table.insert(lines, "") table.insert(lines, "Jobs:") + + local longest_title = u.get_longest_string(u.map(pipeline_jobs, function(v) + return v.name + end)) + + local function row_offset(name) + local offset = longest_title - string.len(name) + local res = string.rep(" ", offset + 5) + return res + end + for _, pipeline_job in ipairs(pipeline_jobs) do - table.insert( - lines, - string.format( - "%s (%s) %s", - state.settings.pipeline[pipeline_job.status], - pipeline_job.status, - pipeline_job.name - ) + local offset = row_offset(pipeline_job.name) + local row = string.format( + "%s%s %s (%s)", + pipeline_job.name, + offset, + state.settings.pipeline[pipeline_job.status] or "*", + pipeline_job.status or "" ) + + table.insert(lines, row) end vim.schedule(function() @@ -107,15 +119,17 @@ M.see_logs = function() local bufnr = vim.api.nvim_get_current_buf() local linnr = vim.api.nvim_win_get_cursor(0)[1] local text = u.get_line_content(bufnr, linnr) - local last_word = u.get_last_word(text) - if last_word == nil then + + local job_name = string.match(text, "(.-)%s%s%s%s%s") + + if job_name == nil then u.notify("Cannot find job name", vim.log.levels.ERROR) return end local j = nil for _, pipeline_job in ipairs(M.pipeline_jobs) do - if pipeline_job.name == last_word then + if pipeline_job.name == job_name then j = pipeline_job end end @@ -144,18 +158,18 @@ M.see_logs = function() end M.pipeline_popup:unmount() - vim.cmd.enew() + vim.cmd.tabnew() bufnr = vim.api.nvim_get_current_buf() vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) - -- TODO: Fix for Windows - local job_file_path = string.format("/tmp/gitlab.nvim.job-%d", j.id) - vim.cmd("w! " .. job_file_path) - vim.cmd.bd() + local temp_file = os.tmpname() + local job_file_path = string.format(temp_file, j.id) - vim.cmd.enew() + vim.cmd("w! " .. job_file_path) vim.cmd("term cat " .. job_file_path) + + vim.api.nvim_buf_set_name(0, job_name) end) end diff --git a/lua/gitlab/utils/init.lua b/lua/gitlab/utils/init.lua index 28073a5..6e8de7a 100644 --- a/lua/gitlab/utils/init.lua +++ b/lua/gitlab/utils/init.lua @@ -226,6 +226,14 @@ M.get_longest_string = function(list) return longest end +M.map = function(tbl, f) + local t = {} + for k, v in pairs(tbl) do + t[k] = f(v) + end + return t +end + M.notify = function(msg, lvl) vim.notify("gitlab.nvim: " .. msg, lvl) end