From c34d67fbacd1dfb2b226aa2509a5f4d07783fae0 Mon Sep 17 00:00:00 2001 From: skywind3000 Date: Sat, 24 Feb 2024 22:21:11 +0800 Subject: [PATCH] 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 --- autoload/quickui/listbox.vim | 8 +- autoload/quickui/tools.vim | 16 +++- test/test_confirm.vim | 4 +- test/test_listbox.vim | 142 +++++++++++++++++------------------ 4 files changed, 93 insertions(+), 77 deletions(-) diff --git a/autoload/quickui/listbox.vim b/autoload/quickui/listbox.vim index 42b57e7..cfed4bc 100644 --- a/autoload/quickui/listbox.vim +++ b/autoload/quickui/listbox.vim @@ -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 @@ -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 diff --git a/autoload/quickui/tools.vim b/autoload/quickui/tools.vim index c87d13f..67738c6 100644 --- a/autoload/quickui/tools.vim +++ b/autoload/quickui/tools.vim @@ -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) @@ -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 diff --git a/test/test_confirm.vim b/test/test_confirm.vim index 8850c3b..fb90ad0 100644 --- a/test/test_confirm.vim +++ b/test/test_confirm.vim @@ -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!" diff --git a/test/test_listbox.vim b/test/test_listbox.vim index ab9e15d..08006b6 100644 --- a/test/test_listbox.vim +++ b/test/test_listbox.vim @@ -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 +