diff --git a/lua/gitlab/actions/discussions/winbar.lua b/lua/gitlab/actions/discussions/winbar.lua index 5c831846..3208edc1 100644 --- a/lua/gitlab/actions/discussions/winbar.lua +++ b/lua/gitlab/actions/discussions/winbar.lua @@ -143,6 +143,60 @@ local add_drafts_and_resolvable = function( return base_title end +-- Returns true if the input string (after removing highlighting strings) is wider than the +-- discussion split. +---@return boolean +local too_long = function(str) + local d = require("gitlab.actions.discussions") + return #str:gsub("%%#[^#]+#", "") > vim.fn.winwidth(d.split.winid) +end + +-- Returns the input winbar string shortened to fit into the windo width. +---@return string +local adapt_to_winwidth = function(str) + if too_long(str) then + str = str:gsub("Inline ", "") + end + if too_long(str) then + str = str:gsub(" Mode", "") + end + if too_long(str) then + str = str:gsub("Help", "H") + end + if too_long(str) then + str = str:gsub("just now", "now") + str = str:gsub("(%d+) minutes?", "%1m") + str = str:gsub("(%d+) hours?", "%1h") + str = str:gsub("(%d+) days?", "%1d") + end + if too_long(str) then + str = str:gsub("Updated", "U") + end + if too_long(str) then + str = str:gsub("Draft", "D") + str = str:gsub("Live", "L") + end + if too_long(str) then + str = str:gsub("(%d+) comments?", "%1c") + end + if too_long(str) then + str = str:gsub("(%d+/%d+) threads?", "%1t") + end + if too_long(str) then + str = str:gsub("(%d+) drafts?", "%1d") + end + if too_long(str) then + str = str:gsub("(%d+%a) ago", "%1") + end + if too_long(str) then + str = str:gsub("Comments", "C") + end + if too_long(str) then + str = str:gsub("Notes", "N") + end + return str +end + ---@param t WinbarTable M.make_winbar = function(t) local discussion_title = add_drafts_and_resolvable( @@ -175,7 +229,7 @@ M.make_winbar = function(t) local separator = "%#Comment#|" local end_section = "%=" local help = "%#Comment#Help: " .. (t.help_keymap and t.help_keymap:gsub(" ", "") .. " " or "unmapped") - return string.format( + local result = string.format( " %s %s %s %s %s %s %s", discussion_title, separator, @@ -185,6 +239,7 @@ M.make_winbar = function(t) separator, help ) + return adapt_to_winwidth(result) end ---Returns a string for the winbar indicating the mode type, live or draft