Simple command line interactive list
npm i cli-list-select
Package exports single function
const list = require('cli-list-select');
Function is async and has 2 arguments.
Function arguments
Name | Type | Description |
---|---|---|
items | any[] | items to be displayed |
options? | Option | options |
Returns Result
Field | Type | Default | Description |
---|---|---|---|
printItem | (item: any, index: number, isFocused: bool, isChecked: bool) => string | String | function that provides string representation for an item |
index | number | 0 | initial focus position |
singleCheck | bool | false | tells if only one item can be checked |
checks | number[] | number | [] | NaN | initially checked items |
handlers | Handlers | {} | custom key handlers |
It is a map.
Field | Type | Description |
---|---|---|
key | string | key name |
value | (arg: HandlerArg) => void | key handler |
Default handlers are
Key | Handler |
---|---|
up | move focus to previous item |
down | move focus to next item |
space | toggle check of the focused item |
return | close the list |
Field | Type | Description |
---|---|---|
index | number | current focus |
setIndex | (index: number) => void | function that sets current focus |
toggleCheck | (index: number) => void | function that toggles check state of an item |
end | (note: any) => void | function that closes the list |
Field | Type | Description |
---|---|---|
index | number | focus |
checks | number[] | number | checked items |
note | any | note returned in the end function |
await list(['A', 'B', 'C']);
-[ ] A
[ ] B
[ ] C
await list(['A', 'B', 'C'], { index: 1 });
[ ] A
-[ ] B
[ ] C
[*] A
-[*] B
[ ] C
await list(['A', 'B', 'C'], { singleCheck: true });
[ ] A
-[*] B
[ ] C
await list(['A', 'B', 'C'], { checks: [0, 2] });
-[*] A
[ ] B
[*] C
await list([{ data: 'A' }, { data: 'B' }, { data: 'C' }], {
printItem: item => `<${item.data}>`
});
-[ ] <A>
[ ] <B>
[ ] <C>
await list(['A', 'B', 'C'], {
handlers: {
'q': ({ end }) => end('Q'),
},
});