Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 64 additions & 1 deletion lua/gitlab/actions/discussions/winbar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,68 @@ local add_drafts_and_resolvable = function(
return base_title
end

-- Returns true if the input string (after removing highlighting and alignment strings) is wider
-- than the discussion split.
---@return boolean
local too_long = function(str)
local d = require("gitlab.actions.discussions")
return #str:gsub("%%#[^#]+#", ""):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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The solution is not particularly elegant, but unfortunately, Lua tables do not keep order of insertion, so it's not possible to do simply something like this if we want the winbar to adapt in a predictable way:

local shortenings = {
  ["Inline "] = "".
  [" Mode"] = "",
}
for pattern, replacement in pairs(shortenings) do
  if too_long(str) then
    str:gsub(pattern, replacement)
  end
end

str = str:gsub("Inline ", "")
end
if too_long(str) then
str = str:gsub("by thread", "thread")
str = str:gsub("by reply", "reply")
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("↓ thread", "↓")
str = str:gsub("↑ reply", "↑")
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 @@ -176,7 +238,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 %s %s",
discussion_title,
separator,
Expand All @@ -188,6 +250,7 @@ M.make_winbar = function(t)
separator,
help
)
return adapt_to_winwidth(result)
end

---Returns a string for the winbar indicating the sort method
Expand Down
Loading