repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

feat(cmdline): enhance format detection and fix flickering

559df034849614d3c5e3d7861375ff544a851170

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

- Add per-format border/icon highlights (CmdlineBorder*, CmdlineIcon*)
- Use extmark for icon rendering with proper padding
- Fix cursor flickering by removing vim.defer_fn in cursor.lua
- Add help variations (:h, :he, :hel, :help, :help topic)
- Add shell detection for :!cmd syntax
- Add lua detection for :lua alone (without space)
- Simplify format.lua with hoisted patterns
- Lower zindex to 50 to avoid interfering with native cmdline
- Extend tests for new format patterns

Changed files (8)

neovim/lua/bugabinga/cmdline/PLAN.md

Modified
4 4 # Cmdline Feature Plan
6 6 ## Easy Wins
8 8 ### 1. Per-format border/icon colors
10 -- [ ] Add highlight groups: `CmdlineBorderSearch`, `CmdlineBorderLua`,
10 +- [x] Add highlight groups: `CmdlineBorderSearch`, `CmdlineBorderLua`,
11 11 `CmdlineBorderInput`
12 -- [ ] In `render.lua`, set `winhighlight` based on `fmt.type`
13 -- [ ] Apply icon highlight via extmark instead of plain text
12 +- [x] In `render.lua`, set `winhighlight` based on `fmt.type`
13 +- [x] Apply icon highlight via extmark instead of plain text
14 14 - Files: `render.lua`, `nugu/init.lua`
16 16 ### 2. Separate search direction icons
25 25 ### 4. Virtual-text icon rendering
27 -- [ ] Replace `icon .. ' ' .. text` with extmark-based icon
28 -- [ ] Simplify cursor position math (no more icon_bytes offset)
29 -- [ ] Remove trailing space hack if no longer needed
27 +- [x] Replace `icon .. ' ' .. text` with extmark-based icon
28 +- [x] Simplify cursor position math (no more icon_bytes offset)
29 +- [x] Remove trailing space hack if no longer needed
30 30 - Files: `render.lua`
32 32 ## Medium Effort

neovim/lua/bugabinga/cmdline/cursor.lua

Modified
3 3 return {
4 4 fix = function(win, byte_pos)
5 5 if not vim.api.nvim_win_is_valid(win) then
6 6 return
8 - vim.defer_fn(function()
9 - if not vim.api.nvim_win_is_valid(win) then
10 - return
11 - end
12 - vim.api.nvim_win_set_cursor(win, { 1, byte_pos })
13 - vim.api.nvim__redraw({ cursor = true, win = win, flush = true })
14 - end, 0)
8 + vim.api.nvim_win_set_cursor(win, { 1, byte_pos })
9 + vim.api.nvim__redraw({ cursor = true, win = win, flush = true })
15 10 end,

neovim/lua/bugabinga/cmdline/format.lua

Modified
1 1 local icon = require 'std.icon'
3 +local HELP_PATTERNS = { '^h$', '^he$', '^hel$', '^help$', '^h%s', '^help%s' }
3 5 return {
4 6 detect = function(firstc, content, prompt)
5 7 if prompt and prompt ~= '' then
9 11 return { type = 'search_down', icon = icon.search }
11 13 if firstc == '?' then
12 - return { type = 'search_up', icon = icon.search }
14 + return { type = 'search_up', icon = icon.search_up }
14 - if firstc == ':' then
15 - if content and (content:match '^lua%s+' or content:match '^=%s*' or content:match '^:%s*lua%s*=%s*') then
16 + if firstc == '!' or (firstc == ':' and content and content:match '^!') then
17 + return { type = 'shell', icon = icon.shell }
18 + end
19 + if firstc == ':' and content then
20 + for _, pat in ipairs(HELP_PATTERNS) do
21 + if content:match(pat) then
22 + return { type = 'help', icon = icon.help }
23 + end
24 + end
25 + if content:match '^lua%s*' or content:match '^=' then
16 26 return { type = 'lua', icon = icon.vim }
18 28 return { type = 'cmdline', icon = icon.command }
20 30 return { type = 'cmdline', icon = icon.command }
21 31 end,
22 -}

neovim/lua/bugabinga/cmdline/render.lua

Modified
7 7 local state = {
8 8 buffer = -1,
9 9 window = -1,
10 - icon_width = 0,
10 + current_fmt = nil,
13 +local BORDER_HIGHLIGHT_MAP = {
14 + search_down = 'CmdlineBorderSearch',
15 + search_up = 'CmdlineBorderSearch',
16 + shell = 'CmdlineBorderShell',
17 + help = 'CmdlineBorderHelp',
18 + lua = 'CmdlineBorderLua',
19 + input = 'CmdlineBorderInput',
20 + cmdline = 'CmdlineBorderCmdline',
23 +local ICON_HIGHLIGHT_MAP = {
24 + search_down = 'CmdlineIconSearch',
25 + search_up = 'CmdlineIconSearch',
26 + shell = 'CmdlineIconShell',
27 + help = 'CmdlineIconHelp',
28 + lua = 'CmdlineIconLua',
29 + input = 'CmdlineIconInput',
30 + cmdline = 'CmdlineIconCmdline',
33 +local function get_winhighlight(type)
34 + local border_hl = BORDER_HIGHLIGHT_MAP[type] or 'CmdlineBorderCmdline'
35 + return string.format('Normal:NormalFloat,FloatBorder:%s,FloatBorderCorner:%s', border_hl, border_hl)
36 +end
13 38 local function get_buf()
14 39 if vim.api.nvim_buf_is_valid(state.buffer) then
15 40 return state.buffer
20 45 return state.buffer
23 -local function get_win(buf, width)
48 +local function get_win(buf, width, fmt)
24 49 if vim.api.nvim_win_is_valid(state.window) then
25 50 return state.window
34 59 style = 'minimal',
35 60 border = vim.g.border_style,
36 61 focusable = false,
37 - zindex = 200,
62 + zindex = 50,
39 64 vim.wo[state.window].cursorline = false
40 65 vim.wo[state.window].winbar = ''
41 66 vim.wo[state.window].foldenable = false
42 - vim.wo[state.window].winhighlight = 'Normal:NormalFloat,FloatBorder:FloatBorder'
67 + vim.wo[state.window].winhighlight = get_winhighlight(fmt.type)
43 68 return state.window
48 73 function M.show(cmdline_state)
49 74 local text = cmdline_state:text()
50 75 local fmt = format.detect(cmdline_state.firstc, text, cmdline_state.prompt)
51 - local icon = fmt.icon
52 - local icon_bytes = #icon
53 - state.icon_width = icon_bytes
54 - local display_text = icon .. ' ' .. text .. ' '
76 + state.current_fmt = fmt
77 + local display_text = ' ' .. text .. ' '
55 78 local width = vim.o.columns
56 79 local buf = get_buf()
57 - local win = get_win(buf, width)
80 + local win = get_win(buf, width, fmt)
58 81 local is_new = not position.get_cmdline_anchor()
59 82 if is_new then
60 83 local screen_pos = position.at_cursor(width)
78 101 vim.api.nvim_buf_set_lines(buf, 0, -1, false, { display_text })
79 102 vim.api.nvim_buf_clear_namespace(buf, ns, 0, -1)
103 + local icon_hl = ICON_HIGHLIGHT_MAP[fmt.type] or 'CmdlineIconCmdline'
104 + vim.api.nvim_buf_set_extmark(buf, ns, 0, 0, {
105 + virt_text = { { fmt.icon, icon_hl } },
106 + virt_text_pos = 'overlay',
107 + })
80 108 local cursor_byte = cmdline_state:cursor_byte()
81 - local cursor_col = icon_bytes + 1 + cursor_byte
109 + local cursor_col = 2 + cursor_byte
82 110 local anchor = position.get_cmdline_anchor()
83 111 if anchor then
84 112 anchor.cursor = cursor_col
92 120 if vim.api.nvim_win_is_valid(state.window) then
93 121 vim.api.nvim_win_hide(state.window)
95 - state.icon_width = 0
123 + state.window = -1
124 + state.current_fmt = nil
96 125 position.set_cmdline_anchor(nil)
97 126 vim.g.ui_cmdline_pos = nil
101 130 if not vim.api.nvim_win_is_valid(state.window) then
102 131 return
104 - local icon_bytes = state.icon_width
105 133 local cursor_byte = cmdline_state:cursor_byte()
106 - vim.api.nvim_buf_clear_namespace(state.buffer, ns, 0, -1)
107 - local cursor_col = icon_bytes + 1 + cursor_byte
134 + local cursor_col = 2 + cursor_byte
108 135 local anchor = position.get_cmdline_anchor()
109 136 if anchor then
110 137 anchor.cursor = cursor_col
113 140 cursor.fix(state.window, cursor_col)
116 -return M
143 +return M

neovim/lua/bugabinga/nugu/init.lua

Modified
156 156 _.PmenuKind { fg = ui_minor }
157 157 _.PmenuKindSel { fg = ui_important_local }
158 158 _.BugabingaCmdlineCursor { fg = ui_important_local, bg = ui_unfocus, bold = true }
159 +_.CmdlineBorderSearch { fg = content_accent, bg = _.NormalFloat.bg }
160 +_.CmdlineBorderShell { fg = warning, bg = _.NormalFloat.bg }
161 +_.CmdlineBorderHelp { fg = info, bg = _.NormalFloat.bg }
162 +_.CmdlineBorderLua { fg = content_focus, bg = _.NormalFloat.bg }
163 +_.CmdlineBorderInput { fg = ui_important_local, bg = _.NormalFloat.bg }
164 +_.CmdlineBorderCmdline { fg = ui_minor, bg = _.NormalFloat.bg }
165 +_.CmdlineIconSearch { fg = content_accent }
166 +_.CmdlineIconShell { fg = warning }
167 +_.CmdlineIconHelp { fg = info }
168 +_.CmdlineIconLua { fg = content_focus }
169 +_.CmdlineIconInput { fg = ui_important_local }
170 +_.CmdlineIconCmdline { fg = ui_minor }
159 171 _.Question { fg = ui_important_local, bold = true }
160 172 _.QuickFixLine = _.PmenuSel
161 173 _.SpecialKey { fg = error, bg = content_unfocus, bold = true }

neovim/lua/std/icon.lua

Modified
121 121 format = '󰁦',
122 122 hammer = '󱁢',
123 123 search = '󰍉',
124 + search_up = '󰤁',
125 + shell = '',
126 + help = '󰋝',
127 + calculator = '󰃬',
124 128 star = '󰧑',

neovim/tests/bugabinga/cmdline/format_spec.lua

Modified
16 16 it('detects ? as search_up', function()
17 17 local result = format.detect('?', 'hello', '')
18 18 assert.equals('search_up', result.type)
19 - assert.equals('󰍉', result.icon)
19 + assert.equals('󰤁', result.icon)
20 + end)
22 + it('detects ! as shell', function()
23 + local result = format.detect('!', 'ls -la', '')
24 + assert.equals('shell', result.type)
25 + assert.equals('', result.icon)
26 + end)
28 + it('detects :help as help', function()
29 + local result = format.detect(':', 'help mytopic', '')
30 + assert.equals('help', result.type)
31 + assert.equals('󰋝', result.icon)
32 + end)
34 + it('detects :h as help', function()
35 + local result = format.detect(':', 'h', '')
36 + assert.equals('help', result.type)
37 + assert.equals('󰋝', result.icon)
38 + end)
40 + it('detects :he as help', function()
41 + local result = format.detect(':', 'he', '')
42 + assert.equals('help', result.type)
43 + assert.equals('󰋝', result.icon)
44 + end)
46 + it('detects :hel as help', function()
47 + local result = format.detect(':', 'hel', '')
48 + assert.equals('help', result.type)
49 + assert.equals('󰋝', result.icon)
50 + end)
52 + it('detects :help alone as help', function()
53 + local result = format.detect(':', 'help', '')
54 + assert.equals('help', result.type)
55 + assert.equals('󰋝', result.icon)
56 + end)
58 + it('detects :h with topic as help', function()
59 + local result = format.detect(':', 'h usr_01', '')
60 + assert.equals('help', result.type)
61 + assert.equals('󰋝', result.icon)
62 + end)
64 + it('detects :!cmd as shell', function()
65 + local result = format.detect(':', '!ls', '')
66 + assert.equals('shell', result.type)
67 + assert.equals('', result.icon)
20 68 end)
22 70 it('detects :lua as lua', function()
25 73 assert.equals('', result.icon)
26 74 end)
76 + it('detects :lua alone as lua', function()
77 + local result = format.detect(':', 'lua', '')
78 + assert.equals('lua', result.type)
79 + assert.equals('', result.icon)
80 + end)
28 82 it('detects := as lua', function()
29 83 local result = format.detect(':', '= { foo = 1 }', '')
30 84 assert.equals('lua', result.type)

neovim/tests/bugabinga/cmdline/render_spec.lua

Modified
32 32 render.hide()
35 - it('sets buffer text with icon prefix', function()
35 + it('sets buffer text without icon prefix (icon via extmark)', function()
36 36 local state = require 'bugabinga.cmdline.state'
37 37 local render = require 'bugabinga.cmdline.render'
38 38 local cmdline_state = state.new({
44 44 local buf = anchor.buf
45 45 local lines = vim.api.nvim_buf_get_lines(buf, 0, 1, false)
46 46 assert.is_true(#lines > 0)
47 - assert.is_true(lines[1]:find('󰘳') ~= nil)
47 + assert.is_true(lines[1]:find('󰘳') == nil)
48 + assert.is_true(lines[1]:find(':test') ~= nil)
49 + local ns = vim.api.nvim_create_namespace 'bugabinga.cmdline'
50 + local extmarks = vim.api.nvim_buf_get_extmarks(buf, ns, 0, -1, { details = true })
51 + assert.is_true(#extmarks > 0)
48 52 render.hide()
51 - it('has trailing space for cursor positioning', function()
55 + it('has leading and trailing space for icon and cursor positioning', function()
52 56 local state = require 'bugabinga.cmdline.state'
53 57 local render = require 'bugabinga.cmdline.render'
54 58 local cmdline_state = state.new({
59 63 local anchor = pos.get_cmdline_anchor()
60 64 local buf = anchor.buf
61 65 local lines = vim.api.nvim_buf_get_lines(buf, 0, 1, false)
62 - assert.equals('󰘳 :vim ', lines[1])
63 - local icon = require('std.icon')
64 - local icon_bytes = #icon.command
66 + assert.equals(' :vim ', lines[1])
65 67 local cursor_byte = cmdline_state:cursor_byte()
66 - local expected_col = icon_bytes + 1 + cursor_byte
67 - assert.equals(icon_bytes + 1 + 4, expected_col)
68 - assert.equals(10, #lines[1])
68 + local expected_col = 1 + cursor_byte
69 + assert.equals(1 + 4, expected_col)
70 + assert.equals(7, #lines[1])
69 71 render.hide()