Skip to content

Commit

Permalink
Fix issues with listbox plugin and add new function for clever input …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
skywind3000 committed Feb 24, 2024
1 parent 333d541 commit c34d67f
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 77 deletions.
8 changes: 6 additions & 2 deletions autoload/quickui/listbox.vim
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,10 @@ function! s:vim_create_listbox(textlist, opts)
let hwnd.hotkey = items.keymap
let hwnd.opts = deepcopy(a:opts)
let hwnd.context = has_key(a:opts, 'context')? a:opts.context : {}
let minsize = strdisplaywidth(get(a:opts, 'title', ''))
let minsize = max([items.displaywidth, minsize])
let border = get(a:opts, 'border', g:quickui#style#border)
let w = has_key(a:opts, 'w')? a:opts.w : items.displaywidth
let w = has_key(a:opts, 'w')? a:opts.w : minsize
let h = has_key(a:opts, 'h')? a:opts.h : items.nrows
if h + 6 > &lines
let h = &lines - 6
Expand Down Expand Up @@ -467,7 +469,9 @@ function! s:nvim_create_listbox(textlist, opts)
let hwnd.opts = deepcopy(a:opts)
let hwnd.context = has_key(a:opts, 'context')? a:opts.context : {}
let border = get(a:opts, 'border', g:quickui#style#border)
let w = has_key(a:opts, 'w')? a:opts.w : items.displaywidth
let minsize = strdisplaywidth(get(a:opts, 'title', ''))
let minsize = max([items.displaywidth, minsize])
let w = has_key(a:opts, 'w')? a:opts.w : minsize + 2
let h = has_key(a:opts, 'h')? a:opts.h : items.nrows
if h + 6 > &lines
let h = &lines - 6
Expand Down
16 changes: 14 additions & 2 deletions autoload/quickui/tools.vim
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ endfunc

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

function! quickui#tools#clever_listbox(name, content, opts)
let opts = deepcopy(a:opts)
let opts.index = get(s:previous_cursor, a:name, -1)
let opts.index = get(s:previous_cursor, a:name, 0)
let opts.keep_name = a:name
let opts.callback = function('s:remember_cursor_listbox')
call quickui#listbox#open(a:content, opts)
endfunc

function! quickui#tools#clever_inputlist(name, content, opts)
let opts = deepcopy(a:opts)
let opts.index = get(s:previous_cursor, a:name, 0)
let opts.keep_name = a:name
" let opts.callback = function('s:remember_cursor_listbox')
let hr = quickui#listbox#inputlist(a:content, opts)
if hr >= 0
let s:previous_cursor[a:name] = hr
endif
return hr
endfunc


"----------------------------------------------------------------------
" terminal
Expand Down
4 changes: 2 additions & 2 deletions test/test_confirm.vim
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
let g:quickui_confirm_border = 'double'
" let g:quickui_confirm_border = 1

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

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

if choice == 0
echo "make up your mind!"
Expand Down
142 changes: 71 additions & 71 deletions test/test_listbox.vim
Original file line number Diff line number Diff line change
@@ -1,71 +1,71 @@
"----------------------------------------------------------------------
" testing suit
"----------------------------------------------------------------------
if 1
let lines = [
\ "[1]\tOpen &File\t(F3)",
\ "[2]\tChange &Directory\t(F2)",
\ "[3]\tHelp",
\ "",
\ "[4]\tE&xit",
\ "[4]\t哈哈哈E&xit",
\ ]
for ix in range(1000)
let lines += ['line: ' . ix]
endfor
function! MyCallback(code)
let hwnd = g:quickui#listbox#current
let context = hwnd.context
echo "exit: ". a:code . ' context: '. context . ' in: ' . hwnd.tag
endfunc
let opts = {'title':'Select', 'border':1, 'index':400, 'close':'button'}
let opts.context = 'asdfasdf'
let opts.callback = 'MyCallback'
let opts.border = 0
" let opts.title = ''
" let opts.close = 'none'
let opts.bordercolor = 'WildMenu'
let opts.keymap = {'=':'TAG:2', '-':'TAG:3'}
if 0
let inst = quickui#listbox#open(lines, opts)
call popup_show(inst.winid)
else
let code = quickui#listbox#inputlist(lines, opts)
echo "code: " . code
endif
endif

if 0
let content = [
\ [ 'echo 1', 'echo 100' ],
\ [ 'echo 2', 'echo 200' ],
\ [ 'echo 3', 'echo 300' ],
\ [ 'echo 4' ],
\ [],
\ [ 'echo 5', 'echo 500' ],
\]
let opts = {'title': 'select'}
call quickui#listbox#any(content, opts)
endif


if 0
let content = [
\ [ 'echo 1', 'echo 100' ],
\ [ 'echo 2', 'echo 200' ],
\ [ 'echo 3', 'echo 300' ],
\ [ 'echo 4' ],
\ [ 'echo 5', 'echo 500' ],
\]
let opts = {'title': 'select'}
call quickui#listbox#open(content, opts)
endif

if 0
let linelist = [
\ "line 1",
\ "line 2",
\ "line 3" ]
echo quickui#listbox#inputlist(linelist, {'title':'select'})
endif

"----------------------------------------------------------------------
" testing suit
"----------------------------------------------------------------------
if 1
let lines = [
\ "[1]\tOpen &File\t(F3)",
\ "[2]\tChange &Directory\t(F2)",
\ "[3]\tHelp",
\ "",
\ "[4]\tE&xit",
\ "[4]\t哈哈哈E&xit",
\ ]
for ix in range(1000)
let lines += ['line: ' . ix]
endfor
function! MyCallback(code)
let hwnd = g:quickui#listbox#current
let context = hwnd.context
echo "exit: ". a:code . ' context: '. context . ' in: ' . hwnd.tag
endfunc
let opts = {'title':'Select', 'border':1, 'index':400, 'close':'button'}
let opts.context = 'asdfasdf'
let opts.callback = 'MyCallback'
let opts.border = 0
" let opts.title = ''
" let opts.close = 'none'
let opts.bordercolor = 'WildMenu'
let opts.keymap = {'=':'TAG:2', '-':'TAG:3'}
if 0
let inst = quickui#listbox#open(lines, opts)
call popup_show(inst.winid)
else
let code = quickui#listbox#inputlist(lines, opts)
echo "code: " . code
endif
endif

if 0
let content = [
\ [ 'echo 1', 'echo 100' ],
\ [ 'echo 2', 'echo 200' ],
\ [ 'echo 3', 'echo 300' ],
\ [ 'echo 4' ],
\ [],
\ [ 'echo 5', 'echo 500' ],
\]
let opts = {'title': 'select'}
call quickui#listbox#any(content, opts)
endif


if 0
let content = [
\ [ 'echo 1', 'echo 100' ],
\ [ 'echo 2', 'echo 200' ],
\ [ 'echo 3', 'echo 300' ],
\ [ 'echo 4' ],
\ [ 'echo 5', 'echo 500' ],
\]
let opts = {'title': 'select'}
call quickui#listbox#open(content, opts)
endif

if 0
let linelist = [
\ "line 1",
\ "line 2",
\ "line 3" ]
echo quickui#listbox#inputlist(linelist, {'title':'select'})
endif

0 comments on commit c34d67f

Please sign in to comment.