Skip to content

Commit f9289b6

Browse files
cameronrsudo-tee
authored andcommitted
fix(topbar): fix spacing
There was an off by one for the mode string. I tried cleaning the code up to make it easier to think about and, hopefully, extend, if needed.
1 parent 1d799d2 commit f9289b6

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

lua/opencode/ui/topbar.lua

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
local util = require('opencode.util')
2+
13
local M = {}
24

35
local state = require('opencode.state')
@@ -42,39 +44,26 @@ local function get_mode_highlight()
4244
end
4345

4446
local function create_winbar_text(description, model_info, mode_info, show_guard_indicator, win_width)
45-
-- Calculate how many visible characters we have
46-
-- Format: " [GUARD ]description padding model_info MODE "
47-
-- Where [GUARD ] is optional
47+
local left_content = ''
48+
local right_content = ''
4849

49-
local guard_info = ''
50-
local guard_visible_width = 0
5150
if show_guard_indicator then
52-
guard_info = prompt_guard_indicator.get_formatted()
53-
guard_visible_width = 2 -- icon + space
51+
left_content = left_content .. prompt_guard_indicator.get_formatted() .. ' '
5452
end
5553

56-
-- Calculate used width: leading space + guard + trailing space + model + mode
57-
local mode_info_str = get_mode_highlight() .. mode_info .. '%*'
58-
local mode_visible_width = #mode_info
59-
local model_visible_width = #model_info
60-
61-
-- Reserve space: 1 (padding) + guard_visible_width (with padding) + model + 1 (space before mode) + mode + 1 (padding)
62-
local reserved_width = 1 + guard_visible_width + model_visible_width + 1 + mode_visible_width + 1
54+
right_content = model_info .. ' ' .. get_mode_highlight() .. mode_info .. '%*'
6355

64-
-- Available width for description and padding
65-
local available_for_desc = win_width - reserved_width
56+
local desc_width = win_width - util.strdisplaywidth(left_content) - util.strdisplaywidth(right_content)
6657

67-
-- Truncate description if needed
68-
if #description > available_for_desc then
69-
local space_for_desc = available_for_desc - 4 -- -4 for "... "
70-
description = description:sub(1, space_for_desc) .. '...'
58+
local desc_formatted
59+
if #description >= desc_width then
60+
local ellipsis = '... '
61+
desc_formatted = description:sub(1, desc_width - #ellipsis) .. ellipsis
62+
else
63+
desc_formatted = description .. string.rep(' ', math.floor(desc_width - #description))
7164
end
7265

73-
-- Calculate padding to right-align model and mode
74-
local padding_width = available_for_desc - #description
75-
local padding = string.rep(' ', math.max(0, padding_width))
76-
77-
return string.format(' %s %s%s%s %s ', guard_info, description, padding, model_info, mode_info_str)
66+
return left_content .. desc_formatted .. right_content
7867
end
7968

8069
local function update_winbar_highlights(win_id)
@@ -122,8 +111,13 @@ function M.render()
122111
vim.wo[win].winbar = ' '
123112

124113
local show_guard_indicator = prompt_guard_indicator.is_denied()
125-
vim.wo[win].winbar =
126-
create_winbar_text(get_session_desc(), format_model_info(), format_mode_info(), show_guard_indicator, vim.api.nvim_win_get_width(win))
114+
vim.wo[win].winbar = create_winbar_text(
115+
get_session_desc(),
116+
format_model_info(),
117+
format_mode_info(),
118+
show_guard_indicator,
119+
vim.api.nvim_win_get_width(win)
120+
)
127121

128122
update_winbar_highlights(win)
129123
end)

lua/opencode/util.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,4 +415,9 @@ function M.get_markdown_filetype(filename)
415415
return vim.fn.fnamemodify(filename, ':e')
416416
end
417417

418+
function M.strdisplaywidth(str)
419+
local str = str:gsub('%%#.-#', ''):gsub('%%[%*]', '')
420+
return vim.fn.strdisplaywidth(str)
421+
end
422+
418423
return M

0 commit comments

Comments
 (0)