dotfiles
bugabingas dorkfiles
owned by bugabinga
feat(cmdline): prompt-as-title for input() with virt_lines fallback
c160e9534a405b520ebc84829d6aca0acd94d5bc
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
Browse filesCompare with HEAD
Changed files (6)
16 16 @
if (
"puk.plugins.updated.nuon" | path exists ) { mv puk.plugins.updated.nuon puk.plugins.nuon }
20 20 rm --interactive --recursive --force (nvim
"--headless" "--clean" +
'lua print(vim.fn.stdpath[[data]])' +q | complete | get stderr)
21 21 rm --interactive --recursive --force (nvim
"--headless" "--clean" +
'lua print(vim.fn.stdpath[[cache]])' +q | complete | get stderr)
22 22 rm --interactive --recursive --force (nvim
"--headless" "--clean" +
'lua print(vim.fn.stdpath[[state]])' +q | complete | get stderr)
26 - nvim --headless -c
"packadd plenary" -c
"PlenaryBustedDirectory tests/" +q
26 + timeit { nvim --headless --noplugin -u tests/minimal_init.lua -c
"PlenaryBustedDirectory {{ path }} {minimal_init = 'tests/minimal_init.lua'}" +q }
30 - nvim --headless -c
"packadd plenary" -c
'PlenaryBustedFile {{ file }}' +q
30 + nvim -c
"set rtp^=." -c
"luafile tests/manual/input.lua" neovim/lua/bugabinga/cmdline/format.lua
Modified60 60 local hl
= BORDER_HIGHLIGHT_MAP[
type]
or BORDER_HIGHLIGHT_MAP.cmdline
61 61 return string.
format(
'Normal:%s,FloatBorder:%s,FloatBorderCorner:%s', base, hl.border, hl.corner)
64 + prompt_lines = function(prompt, max_width)
65 + if not prompt
or prompt
== '' then 68 + vim.validate({
prompt = { prompt,
'string' },
max_width = { max_width,
'number' } })
70 + for line
in (prompt
.. '\n'):gmatch(
'([^\n]*)\n')
do 71 + table.
insert(lines, line)
73 + if #lines
> 0 and lines[
#lines]
== '' then 77 + for _, line
in ipairs(lines)
do 78 + if vim.fn.strdisplaywidth(line)
<= max_width
then 79 + table.
insert(result, line)
81 + local remaining
= line
82 + while remaining
~= '' and vim.fn.strdisplaywidth(remaining)
> max_width
do 84 + for i
= 1,
#remaining
do 85 + if vim.fn.strdisplaywidth(remaining:sub(
1, i))
> max_width
then 91 + cutoff
= math.
min(max_width,
#remaining)
93 + local chunk
= remaining:sub(
1, cutoff)
94 + local last_space
= chunk:match
'()%s[^%s]*$' 95 + if last_space
and last_space
> 1 then 96 + local ws
= chunk:match
'%s+[^%s]*$' 97 + cutoff
= #chunk
- #ws
98 + chunk
= chunk:sub(
1, cutoff):gsub(
'%s+$',
'')
100 + table.
insert(result, chunk)
101 + remaining
= remaining:sub(cutoff
+ 1):gsub(
'^%s+',
'')
103 + if remaining
~= '' then 104 + table.
insert(result, remaining)
111 + PROMPT_MAX_RATIO
= 0.85,
neovim/lua/bugabinga/cmdline/render.lua
Modified 6 6 local ns
= vim.api.nvim_create_namespace
'bugabinga.cmdline' 7 7 local hl_ns
= vim.api.nvim_create_namespace
'bugabinga.cmdline.hl' 8 +local prompt_ns
= vim.api.nvim_create_namespace
'bugabinga.cmdline.prompt' 99 100 vim.api.nvim_buf_set_lines(buf,
0,
-1,
false, { display_text })
100 101 apply_icon(buf, fmt)
101 102 highlight.apply(buf, hl_ns, text, fmt.lang,
0,
2)
104 + if fmt.type
== 'input' and prompt
and prompt
~= '' then 105 + vim.api.nvim_buf_clear_namespace(buf, prompt_ns,
0,
-1)
106 + local max_prompt_width
= math.
floor(vim.o.columns
* format.PROMPT_MAX_RATIO)
107 + local prompt_is_single_line
= not prompt:match
'\n' 108 + local prompt_fits
= vim.fn.strdisplaywidth(prompt)
<= max_prompt_width
109 + if prompt_is_single_line
and prompt_fits
then 110 + local title_text
= fmt.icon
.. ' ' .. prompt
111 + vim.api.nvim_win_set_config(win, {
112 + border = vim.g.border_style,
113 + title = { { title_text,
'CmdlineBorderInput' } },
114 + title_pos =
'left',
117 + local prompt_chunks
= format.prompt_lines(prompt, max_prompt_width)
118 + local virt_line_tuples
= {}
119 + for _, chunk
in ipairs(prompt_chunks)
do 120 + table.
insert(virt_line_tuples, { { chunk,
'CmdlineBorderInput' } })
122 + vim.api.nvim_buf_set_extmark(buf, prompt_ns,
0,
0, {
123 + virt_lines = virt_line_tuples,
124 + virt_lines_above =
true,
126 + vim.api.nvim_win_set_config(win, {
127 + border = vim.g.border_style,
128 + title = { { fmt.icon,
'CmdlineBorderInput' } },
129 + title_pos =
'left',
130 + height =
#prompt_chunks
+ 1,
102 135 local cursor_byte
= cmdline_state:cursor_byte()
103 136 local cursor_col
= 2 + cursor_byte
104 137 local anchor
= position.get_cmdline_anchor()
113 146 function M.
hide()
114 147 if vim.api.nvim_win_is_valid(state.window)
then 148 + vim.api.nvim_buf_clear_namespace(state.buffer, prompt_ns,
0,
-1)
115 149 vim.api.nvim_win_hide(state.window)
117 151 state.window
= -1 neovim/lua/bugabinga/cmdline/state.lua
Modified 1 +local table = require 'std.table' 3 5 function M.
new(content, pos, firstc, prompt, indent, level)
neovim/tests/bugabinga/cmdline/format_spec.lua
Modified115 115 local result
= format.detect(
':',
nil,
'')
116 116 assert.equals(
'cmdline', result.type)
120 +describe(
'format.prompt_lines',
function()
121 + it(
'returns empty table for nil prompt',
function()
122 + local result
= format.prompt_lines(
nil,
80)
123 + assert.equals(
0,
#result)
126 + it(
'returns empty table for empty prompt',
function()
127 + local result
= format.prompt_lines(
'',
80)
128 + assert.equals(
0,
#result)
131 + it(
'returns single entry for short prompt',
function()
132 + local result
= format.prompt_lines(
'Enter name: ',
80)
133 + assert.equals(
1,
#result)
134 + assert.equals(
'Enter name: ', result[
1])
137 + it(
'returns single entry when prompt fits exactly at max_width',
function()
138 + local prompt
= 'abc' 140 + local result
= format.prompt_lines(prompt, max_w)
141 + assert.equals(
1,
#result)
142 + assert.equals(
'abc', result[
1])
145 + it(
'splits on newline',
function()
146 + local result
= format.prompt_lines(
'line 1\nline 2',
80)
147 + assert.equals(
2,
#result)
148 + assert.equals(
'line 1', result[
1])
149 + assert.equals(
'line 2', result[
2])
152 + it(
'double newline produces blank line',
function()
153 + local result
= format.prompt_lines(
'a\n\nb',
80)
154 + assert.equals(
3,
#result)
155 + assert.equals(
'a', result[
1])
156 + assert.equals(
'', result[
2])
157 + assert.equals(
'b', result[
3])
160 + it(
'trailing newline is stripped',
function()
161 + local result
= format.prompt_lines(
'hello\n',
80)
162 + assert.equals(
1,
#result)
163 + assert.equals(
'hello', result[
1])
166 + it(
'wraps long line at word boundary',
function()
167 + local long
= 'the quick brown fox jumps over the lazy dog' 169 + local result
= format.prompt_lines(long, max_w)
170 + for _, line
in ipairs(result)
do 171 + assert.is_true(vim.fn.strdisplaywidth(line)
<= max_w,
'line exceeds max_width: ' .. line)
173 + local concatenated
= table.
concat(result,
' ')
174 + assert.equals(long, concatenated)
177 + it(
'hard-breaks single word exceeding max_width',
function()
178 + local long
= 'supercalifragilisticexpialidocious' 180 + local result
= format.prompt_lines(long, max_w)
181 + assert.is_true(
#result
> 1,
'single long word should be broken into multiple pieces')
182 + for _, line
in ipairs(result)
do 183 + assert.is_true(vim.fn.strdisplaywidth(line)
<= max_w,
'line exceeds max_width: ' .. line)
187 + it(
'no wrapping when prompt shorter than max_width',
function()
188 + local result
= format.prompt_lines(
'short',
80)
189 + assert.equals(
1,
#result)
190 + assert.equals(
'short', result[
1])
194 +describe(
'format.PROMPT_MAX_RATIO',
function()
195 + it(
'is 0.85',
function()
196 + assert.equals(
0.85, format.PROMPT_MAX_RATIO)
neovim/tests/bugabinga/cmdline/render_spec.lua
Modified226 226 assert.is_true(corner1
~= corner2,
'corner should change: ' .. corner1
.. ' vs ' .. corner2)
231 +describe(
'render.show with input prompt',
function()
232 + before_each(
function()
233 + package.
loaded[
'bugabinga.cmdline.state']
= nil 234 + package.
loaded[
'bugabinga.cmdline.format']
= nil 235 + package.
loaded[
'bugabinga.cmdline.position']
= nil 236 + package.
loaded[
'bugabinga.cmdline.cursor']
= nil 237 + package.
loaded[
'bugabinga.cmdline.highlight']
= nil 238 + package.
loaded[
'bugabinga.cmdline.render']
= nil 241 + after_each(
function()
242 + local ok, render
= pcall(
require,
'bugabinga.cmdline.render')
246 + local ok2, pos
= pcall(
require,
'bugabinga.cmdline.position')
248 + pos.set_cmdline_anchor(
nil)
252 + it(
'input with short prompt does not put prompt in buffer text',
function()
253 + local state
= require 'bugabinga.cmdline.state' 254 + local render
= require 'bugabinga.cmdline.render' 255 + local cmdline_state
= state.new({
256 + { {},
'my input' },
257 + },
8,
':',
'Name: ',
0,
1)
258 + render.show(cmdline_state)
259 + local pos
= require(
'bugabinga.cmdline.position')
260 + local anchor
= pos.get_cmdline_anchor()
261 + local buf
= anchor.buf
262 + local lines
= vim.api.nvim_buf_get_lines(buf,
0,
1,
false)
263 + assert.is_true(lines[
1]:find(
'Name:')
== nil,
'prompt should not appear in buffer: ' .. lines[
1])
264 + assert.is_true(lines[
1]:find(
'my input')
~= nil,
'user input should appear in buffer')
268 + it(
'input with empty prompt uses normal rendering',
function()
269 + local state
= require 'bugabinga.cmdline.state' 270 + local render
= require 'bugabinga.cmdline.render' 271 + local cmdline_state
= state.new({
273 + },
6,
':',
'',
0,
1)
274 + render.show(cmdline_state)
275 + local pos
= require(
'bugabinga.cmdline.position')
276 + local anchor
= pos.get_cmdline_anchor()
277 + local buf
= anchor.buf
278 + local lines
= vim.api.nvim_buf_get_lines(buf,
0,
1,
false)
279 + assert.is_true(lines[
1]:find(
':hello')
~= nil)
283 + it(
'input format uses input icon (prompt_prefix)',
function()
284 + local state
= require 'bugabinga.cmdline.state' 285 + local render
= require 'bugabinga.cmdline.render' 286 + local cmdline_state
= state.new({
288 + },
0,
':',
'Prompt: ',
0,
1)
289 + render.show(cmdline_state)
290 + local fmt_type
= render.get_format_type()
291 + assert.equals(
'input', fmt_type)
295 + it(
'cursor column is offset by 2 for input',
function()
296 + local state
= require 'bugabinga.cmdline.state' 297 + local render
= require 'bugabinga.cmdline.render' 298 + local cmdline_state
= state.new({
300 + },
2,
':',
'Prompt: ',
0,
1)
301 + render.show(cmdline_state)
302 + local pos
= require(
'bugabinga.cmdline.position')
303 + local anchor
= pos.get_cmdline_anchor()
304 + local cursor
= vim.api.nvim_win_get_cursor(anchor.win)
305 + assert.equals(
2 + 2, cursor[
2],
'cursor should be 2 + cursor_byte')
310 +describe(
'render.show with input prompt - multi-line (virt_lines)',
function()
311 + before_each(
function()
312 + package.
loaded[
'bugabinga.cmdline.state']
= nil 313 + package.
loaded[
'bugabinga.cmdline.format']
= nil 314 + package.
loaded[
'bugabinga.cmdline.position']
= nil 315 + package.
loaded[
'bugabinga.cmdline.cursor']
= nil 316 + package.
loaded[
'bugabinga.cmdline.highlight']
= nil 317 + package.
loaded[
'bugabinga.cmdline.render']
= nil 320 + after_each(
function()
321 + local ok, render
= pcall(
require,
'bugabinga.cmdline.render')
325 + local ok2, pos
= pcall(
require,
'bugabinga.cmdline.position')
327 + pos.set_cmdline_anchor(
nil)
331 + it(
'multi-line prompt sets virt_lines in buffer',
function()
332 + local state
= require 'bugabinga.cmdline.state' 333 + local render
= require 'bugabinga.cmdline.render' 334 + local cmdline_state
= state.new({
336 + },
5,
':',
'Line 1\nLine 2\nLine 3',
0,
1)
337 + render.show(cmdline_state)
338 + local pos
= require(
'bugabinga.cmdline.position')
339 + local anchor
= pos.get_cmdline_anchor()
340 + local buf
= anchor.buf
341 + local prompt_ns
= vim.api.nvim_create_namespace(
'bugabinga.cmdline.prompt')
342 + local extmarks
= vim.api.nvim_buf_get_extmarks(buf, prompt_ns,
0,
-1, {
details =
true })
343 + assert.is_true(
#extmarks
> 0,
'should have virt_lines extmark for multi-line prompt')
344 + local mark
= extmarks[
1]
345 + local opts
= mark[
4]
346 + assert.is_not_nil(opts.virt_lines,
'extmark should have virt_lines')
347 + assert.is_true(opts.virt_lines_above,
'virt_lines should be above')
351 + it(
'multi-line prompt window height is prompt_chunks + 1',
function()
352 + local state
= require 'bugabinga.cmdline.state' 353 + local render
= require 'bugabinga.cmdline.render' 354 + local cmdline_state
= state.new({
356 + },
1,
':',
'Line 1\nLine 2',
0,
1)
357 + render.show(cmdline_state)
358 + local pos
= require(
'bugabinga.cmdline.position')
359 + local anchor
= pos.get_cmdline_anchor()
360 + local height
= vim.api.nvim_win_get_config(anchor.win).height
361 + assert.equals(
2 + 1, height,
'height should be 2 (prompt lines) + 1 (input line)')
365 + it(
'multi-line prompt splits on newlines',
function()
366 + local state
= require 'bugabinga.cmdline.state' 367 + local render
= require 'bugabinga.cmdline.render' 368 + local cmdline_state
= state.new({
370 + },
1,
':',
'first line\nsecond line',
0,
1)
371 + render.show(cmdline_state)
372 + local pos
= require(
'bugabinga.cmdline.position')
373 + local anchor
= pos.get_cmdline_anchor()
374 + local buf
= anchor.buf
375 + local prompt_ns
= vim.api.nvim_create_namespace(
'bugabinga.cmdline.prompt')
376 + local extmarks
= vim.api.nvim_buf_get_extmarks(buf, prompt_ns,
0,
-1, {
details =
true })
377 + local opts
= extmarks[
1][
4]
378 + assert.equals(
2,
#opts.virt_lines,
'should have 2 virt_lines for 2 prompt lines')
382 + it(
'double newline creates blank virt_line',
function()
383 + local state
= require 'bugabinga.cmdline.state' 384 + local render
= require 'bugabinga.cmdline.render' 385 + local cmdline_state
= state.new({
387 + },
1,
':',
'a\n\nb',
0,
1)
388 + render.show(cmdline_state)
389 + local pos
= require(
'bugabinga.cmdline.position')
390 + local anchor
= pos.get_cmdline_anchor()
391 + local buf
= anchor.buf
392 + local prompt_ns
= vim.api.nvim_create_namespace(
'bugabinga.cmdline.prompt')
393 + local extmarks
= vim.api.nvim_buf_get_extmarks(buf, prompt_ns,
0,
-1, {
details =
true })
394 + local opts
= extmarks[
1][
4]
395 + assert.equals(
3,
#opts.virt_lines,
'should have 3 virt_lines: a, blank, b')
400 +describe(
'render.hide clears prompt state',
function()
401 + before_each(
function()
402 + package.
loaded[
'bugabinga.cmdline.state']
= nil 403 + package.
loaded[
'bugabinga.cmdline.format']
= nil 404 + package.
loaded[
'bugabinga.cmdline.position']
= nil 405 + package.
loaded[
'bugabinga.cmdline.cursor']
= nil 406 + package.
loaded[
'bugabinga.cmdline.highlight']
= nil 407 + package.
loaded[
'bugabinga.cmdline.render']
= nil 410 + after_each(
function()
411 + local ok, render
= pcall(
require,
'bugabinga.cmdline.render')
415 + local ok2, pos
= pcall(
require,
'bugabinga.cmdline.position')
417 + pos.set_cmdline_anchor(
nil)
421 + it(
'hide after input clears prompt namespace',
function()
422 + local state
= require 'bugabinga.cmdline.state' 423 + local render
= require 'bugabinga.cmdline.render' 424 + local cmdline_state
= state.new({
426 + },
1,
':',
'Line 1\nLine 2',
0,
1)
427 + render.show(cmdline_state)
428 + local pos
= require(
'bugabinga.cmdline.position')
429 + local anchor
= pos.get_cmdline_anchor()
430 + local buf
= anchor.buf
432 + local prompt_ns
= vim.api.nvim_create_namespace(
'bugabinga.cmdline.prompt')
433 + local extmarks
= vim.api.nvim_buf_get_extmarks(buf, prompt_ns,
0,
-1, {})
434 + assert.equals(
0,
#extmarks,
'prompt namespace should be empty after hide')