0.0.7 • Published 2 years ago

cli-list-select v0.0.7

Weekly downloads
6
License
MIT
Repository
github
Last release
2 years ago

CI

cli-list-select

Simple command line interactive list

Install

npm i cli-list-select

Description

Package exports single function

const list = require('cli-list-select');

Function is async and has 2 arguments.

Function arguments

NameTypeDescription
itemsany[]items to be displayed
options?Optionoptions

Returns Result

Options

FieldTypeDefaultDescription
printItem(item: any, index: number, isFocused: bool, isChecked: bool) => stringStringfunction that provides string representation for an item
indexnumber0initial focus position
singleCheckboolfalsetells if only one item can be checked
checksnumber[] | number[] | NaNinitially checked items
handlersHandlers{}custom key handlers

Handlers

It is a map.

FieldTypeDescription
keystringkey name
value(arg: HandlerArg) => voidkey handler

Default handlers are

KeyHandler
upmove focus to previous item
downmove focus to next item
spacetoggle check of the focused item
returnclose the list

HandlerArg

FieldTypeDescription
indexnumbercurrent focus
setIndex(index: number) => voidfunction that sets current focus
toggleCheck(index: number) => voidfunction that toggles check state of an item
end(note: any) => voidfunction that closes the list

Result

FieldTypeDescription
indexnumberfocus
checksnumber[] | numberchecked items
noteanynote returned in the end function

Call

Just print a list

await list(['A', 'B', 'C']);
-[ ] A
 [ ] B
 [ ] C

Set initial focus

await list(['A', 'B', 'C'], { index: 1 });
 [ ] A
-[ ] B
 [ ] C

Check an item

 [*] A
-[*] B
 [ ] C

Single check mode

await list(['A', 'B', 'C'], { singleCheck: true });
 [ ] A
-[*] B
 [ ] C

Initial checks

await list(['A', 'B', 'C'], { checks: [0, 2] });
-[*] A
 [ ] B
 [*] C

Custom print

await list([{ data: 'A' }, { data: 'B' }, { data: 'C' }], {
    printItem: item => `<${item.data}>`
});
-[ ] <A>
 [ ] <B>
 [ ] <C>

Custom handlers

await list(['A', 'B', 'C'], {
    handlers: {
        'q': ({ end }) => end('Q'),
    },
});

Examples