Skip to content

Commit

Permalink
Feat: Adapt winbar string to window width
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubbortlik committed Nov 26, 2024
1 parent d8d289d commit 7dd7890
Showing 1 changed file with 56 additions and 1 deletion.
57 changes: 56 additions & 1 deletion lua/gitlab/actions/discussions/winbar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(" ", "<space>") .. " " or "unmapped")
return string.format(
local result = string.format(
" %s %s %s %s %s %s %s",
discussion_title,
separator,
Expand All @@ -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
Expand Down

0 comments on commit 7dd7890

Please sign in to comment.