|
| 1 | +local util = require('opencode.util') |
| 2 | + |
1 | 3 | local M = {} |
2 | 4 |
|
3 | 5 | local state = require('opencode.state') |
@@ -42,39 +44,26 @@ local function get_mode_highlight() |
42 | 44 | end |
43 | 45 |
|
44 | 46 | 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 = '' |
48 | 49 |
|
49 | | - local guard_info = '' |
50 | | - local guard_visible_width = 0 |
51 | 50 | 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() .. ' ' |
54 | 52 | end |
55 | 53 |
|
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 .. '%*' |
63 | 55 |
|
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) |
66 | 57 |
|
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)) |
71 | 64 | end |
72 | 65 |
|
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 |
78 | 67 | end |
79 | 68 |
|
80 | 69 | local function update_winbar_highlights(win_id) |
@@ -122,8 +111,13 @@ function M.render() |
122 | 111 | vim.wo[win].winbar = ' ' |
123 | 112 |
|
124 | 113 | 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 | + ) |
127 | 121 |
|
128 | 122 | update_winbar_highlights(win) |
129 | 123 | end) |
|
0 commit comments