Skip to content

Commit c34d67f

Browse files
author
skywind3000
committed
Fix issues with listbox plugin and add new function for clever input list
- Adjusted listbox width calculation to consider title length - Added minsize calculation for title in listbox creation - Fixed default index value in clever_context and clever_listbox functions - Added new function quickui#tools#clever_inputlist for handling input lists in a clever way
1 parent 333d541 commit c34d67f

File tree

4 files changed

+93
-77
lines changed

4 files changed

+93
-77
lines changed

autoload/quickui/listbox.vim

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,10 @@ function! s:vim_create_listbox(textlist, opts)
141141
let hwnd.hotkey = items.keymap
142142
let hwnd.opts = deepcopy(a:opts)
143143
let hwnd.context = has_key(a:opts, 'context')? a:opts.context : {}
144+
let minsize = strdisplaywidth(get(a:opts, 'title', ''))
145+
let minsize = max([items.displaywidth, minsize])
144146
let border = get(a:opts, 'border', g:quickui#style#border)
145-
let w = has_key(a:opts, 'w')? a:opts.w : items.displaywidth
147+
let w = has_key(a:opts, 'w')? a:opts.w : minsize
146148
let h = has_key(a:opts, 'h')? a:opts.h : items.nrows
147149
if h + 6 > &lines
148150
let h = &lines - 6
@@ -467,7 +469,9 @@ function! s:nvim_create_listbox(textlist, opts)
467469
let hwnd.opts = deepcopy(a:opts)
468470
let hwnd.context = has_key(a:opts, 'context')? a:opts.context : {}
469471
let border = get(a:opts, 'border', g:quickui#style#border)
470-
let w = has_key(a:opts, 'w')? a:opts.w : items.displaywidth
472+
let minsize = strdisplaywidth(get(a:opts, 'title', ''))
473+
let minsize = max([items.displaywidth, minsize])
474+
let w = has_key(a:opts, 'w')? a:opts.w : minsize + 2
471475
let h = has_key(a:opts, 'h')? a:opts.h : items.nrows
472476
if h + 6 > &lines
473477
let h = &lines - 6

autoload/quickui/tools.vim

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ endfunc
432432

433433
function! quickui#tools#clever_context(name, content, opts)
434434
let opts = deepcopy(a:opts)
435-
let opts.index = get(s:previous_cursor, a:name, -1)
435+
let opts.index = get(s:previous_cursor, a:name, 0)
436436
let opts.keep_name = a:name
437437
let opts.callback = function('s:remember_cursor_context')
438438
let content = quickui#context#reduce_items(a:content)
@@ -441,12 +441,24 @@ endfunc
441441

442442
function! quickui#tools#clever_listbox(name, content, opts)
443443
let opts = deepcopy(a:opts)
444-
let opts.index = get(s:previous_cursor, a:name, -1)
444+
let opts.index = get(s:previous_cursor, a:name, 0)
445445
let opts.keep_name = a:name
446446
let opts.callback = function('s:remember_cursor_listbox')
447447
call quickui#listbox#open(a:content, opts)
448448
endfunc
449449

450+
function! quickui#tools#clever_inputlist(name, content, opts)
451+
let opts = deepcopy(a:opts)
452+
let opts.index = get(s:previous_cursor, a:name, 0)
453+
let opts.keep_name = a:name
454+
" let opts.callback = function('s:remember_cursor_listbox')
455+
let hr = quickui#listbox#inputlist(a:content, opts)
456+
if hr >= 0
457+
let s:previous_cursor[a:name] = hr
458+
endif
459+
return hr
460+
endfunc
461+
450462

451463
"----------------------------------------------------------------------
452464
" terminal

test/test_confirm.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
let g:quickui_confirm_border = 'double'
1+
" let g:quickui_confirm_border = 1
22

33
let question = "What do you want ?"
44
let choices = "&Apples\n&Oranges\n&Bananas"
55

6-
let choice = quickui#confirm#open(question, choices, 1, 'Confirm')
6+
let choice = quickui#confirm#open(question, choices, 2, 'Confirm')
77

88
if choice == 0
99
echo "make up your mind!"

test/test_listbox.vim

Lines changed: 71 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,71 @@
1-
"----------------------------------------------------------------------
2-
" testing suit
3-
"----------------------------------------------------------------------
4-
if 1
5-
let lines = [
6-
\ "[1]\tOpen &File\t(F3)",
7-
\ "[2]\tChange &Directory\t(F2)",
8-
\ "[3]\tHelp",
9-
\ "",
10-
\ "[4]\tE&xit",
11-
\ "[4]\t哈哈哈E&xit",
12-
\ ]
13-
for ix in range(1000)
14-
let lines += ['line: ' . ix]
15-
endfor
16-
function! MyCallback(code)
17-
let hwnd = g:quickui#listbox#current
18-
let context = hwnd.context
19-
echo "exit: ". a:code . ' context: '. context . ' in: ' . hwnd.tag
20-
endfunc
21-
let opts = {'title':'Select', 'border':1, 'index':400, 'close':'button'}
22-
let opts.context = 'asdfasdf'
23-
let opts.callback = 'MyCallback'
24-
let opts.border = 0
25-
" let opts.title = ''
26-
" let opts.close = 'none'
27-
let opts.bordercolor = 'WildMenu'
28-
let opts.keymap = {'=':'TAG:2', '-':'TAG:3'}
29-
if 0
30-
let inst = quickui#listbox#open(lines, opts)
31-
call popup_show(inst.winid)
32-
else
33-
let code = quickui#listbox#inputlist(lines, opts)
34-
echo "code: " . code
35-
endif
36-
endif
37-
38-
if 0
39-
let content = [
40-
\ [ 'echo 1', 'echo 100' ],
41-
\ [ 'echo 2', 'echo 200' ],
42-
\ [ 'echo 3', 'echo 300' ],
43-
\ [ 'echo 4' ],
44-
\ [],
45-
\ [ 'echo 5', 'echo 500' ],
46-
\]
47-
let opts = {'title': 'select'}
48-
call quickui#listbox#any(content, opts)
49-
endif
50-
51-
52-
if 0
53-
let content = [
54-
\ [ 'echo 1', 'echo 100' ],
55-
\ [ 'echo 2', 'echo 200' ],
56-
\ [ 'echo 3', 'echo 300' ],
57-
\ [ 'echo 4' ],
58-
\ [ 'echo 5', 'echo 500' ],
59-
\]
60-
let opts = {'title': 'select'}
61-
call quickui#listbox#open(content, opts)
62-
endif
63-
64-
if 0
65-
let linelist = [
66-
\ "line 1",
67-
\ "line 2",
68-
\ "line 3" ]
69-
echo quickui#listbox#inputlist(linelist, {'title':'select'})
70-
endif
71-
1+
"----------------------------------------------------------------------
2+
" testing suit
3+
"----------------------------------------------------------------------
4+
if 1
5+
let lines = [
6+
\ "[1]\tOpen &File\t(F3)",
7+
\ "[2]\tChange &Directory\t(F2)",
8+
\ "[3]\tHelp",
9+
\ "",
10+
\ "[4]\tE&xit",
11+
\ "[4]\t哈哈哈E&xit",
12+
\ ]
13+
for ix in range(1000)
14+
let lines += ['line: ' . ix]
15+
endfor
16+
function! MyCallback(code)
17+
let hwnd = g:quickui#listbox#current
18+
let context = hwnd.context
19+
echo "exit: ". a:code . ' context: '. context . ' in: ' . hwnd.tag
20+
endfunc
21+
let opts = {'title':'Select', 'border':1, 'index':400, 'close':'button'}
22+
let opts.context = 'asdfasdf'
23+
let opts.callback = 'MyCallback'
24+
let opts.border = 0
25+
" let opts.title = ''
26+
" let opts.close = 'none'
27+
let opts.bordercolor = 'WildMenu'
28+
let opts.keymap = {'=':'TAG:2', '-':'TAG:3'}
29+
if 0
30+
let inst = quickui#listbox#open(lines, opts)
31+
call popup_show(inst.winid)
32+
else
33+
let code = quickui#listbox#inputlist(lines, opts)
34+
echo "code: " . code
35+
endif
36+
endif
37+
38+
if 0
39+
let content = [
40+
\ [ 'echo 1', 'echo 100' ],
41+
\ [ 'echo 2', 'echo 200' ],
42+
\ [ 'echo 3', 'echo 300' ],
43+
\ [ 'echo 4' ],
44+
\ [],
45+
\ [ 'echo 5', 'echo 500' ],
46+
\]
47+
let opts = {'title': 'select'}
48+
call quickui#listbox#any(content, opts)
49+
endif
50+
51+
52+
if 0
53+
let content = [
54+
\ [ 'echo 1', 'echo 100' ],
55+
\ [ 'echo 2', 'echo 200' ],
56+
\ [ 'echo 3', 'echo 300' ],
57+
\ [ 'echo 4' ],
58+
\ [ 'echo 5', 'echo 500' ],
59+
\]
60+
let opts = {'title': 'select'}
61+
call quickui#listbox#open(content, opts)
62+
endif
63+
64+
if 0
65+
let linelist = [
66+
\ "line 1",
67+
\ "line 2",
68+
\ "line 3" ]
69+
echo quickui#listbox#inputlist(linelist, {'title':'select'})
70+
endif
71+

0 commit comments

Comments
 (0)