Skip to main content
added 179 characters in body
Source Link
Matt
  • 21.6k
  • 1
  • 13
  • 25

Perhaps, you want inputlist(). It does not provide the item numbers, but it's quite easy to add them manually:

" MyChoice({list} [, {prompt}])
" Lets user choose an item from {list}. Returns the selected item index or -1.
" If {prompt} is omitted the default prompt is used.
function! MyChoice(list, ...)
    let l:prompt = a:0 ? a:1 : "Input number: "
    let l:idx = inputlist(insert(map(copy(a:list), 'string'(1 + v:key) . ". " . v:val'), l:prompt))
    if l:idx >= 1 && l:idx <= len(a:list)
        return l:idx - 1
    endif
    return -1
endfunction 

" example usage
let choice = MyChoice(['foo', 'bar', 'baz'])

Perhaps, you want inputlist(). It does not provide the item numbers, but it's quite easy to add them manually:

function! MyChoice(list, ...)
    let l:prompt = a:0 ? a:1 : "Input number: "
    let l:idx = inputlist(insert(map(copy(a:list), 'string(1 + v:key) . ". " . v:val'), l:prompt))
    if l:idx >= 1 && l:idx <= len(a:list)
        return l:idx - 1
    endif
    return -1
endfunction

let choice = MyChoice(['foo', 'bar', 'baz'])

Perhaps, you want inputlist(). It does not provide the item numbers, but it's quite easy to add them manually:

" MyChoice({list} [, {prompt}])
" Lets user choose an item from {list}. Returns the selected item index or -1.
" If {prompt} is omitted the default prompt is used.
function! MyChoice(list, ...)
    let l:prompt = a:0 ? a:1 : "Input number:"
    let l:idx = inputlist(insert(map(copy(a:list), '(1 + v:key) . ". " . v:val'), l:prompt))
    if l:idx >= 1 && l:idx <= len(a:list)
        return l:idx - 1
    endif
    return -1
endfunction 

" example usage
let choice = MyChoice(['foo', 'bar', 'baz'])
Source Link
Matt
  • 21.6k
  • 1
  • 13
  • 25

Perhaps, you want inputlist(). It does not provide the item numbers, but it's quite easy to add them manually:

function! MyChoice(list, ...)
    let l:prompt = a:0 ? a:1 : "Input number: "
    let l:idx = inputlist(insert(map(copy(a:list), 'string(1 + v:key) . ". " . v:val'), l:prompt))
    if l:idx >= 1 && l:idx <= len(a:list)
        return l:idx - 1
    endif
    return -1
endfunction

let choice = MyChoice(['foo', 'bar', 'baz'])