-
Notifications
You must be signed in to change notification settings - Fork 64
35 limit on keyInSelect #60
Comments
My apologies I just noticed the reply message in the previous issue to the same issue about why you think 35 is a good limit! |
See: https://github.com/anseki/readline-sync#itemscount And, you should consider small terminal. That is, you should use e.g. 10 items array. |
I think this is more about my understanding rather than being a problem. #!/usr/bin/env node
var readlineSync = require('readline-sync');
var items = [];
var ranlength = Math.floor(Math.random() * 65 ) + 35
console.log( "ranlength=" , ranlength )
for (var i = 0; i < ranlength ; i = i + 1) {
items.push("array-item" + i);
}
var index = "null"
var selection = "";
var size = 20;
var pagecounter = 0;
for (var i = 0; i < items.length; i += size) {
var smallarray = items.slice(i, i + size);
index = readlineSync.keyInSelect(smallarray, null, {
cancel: 'Next page'
});
if (index === -1) {
pagecounter = pagecounter + size
} else {
//console.log("BREAK")
pagecounter = pagecounter + index
break;
}
}
selection = items[pagecounter]
console.log("Your selection is:", selection ) |
This line: var slection = ""; should be: var selection = ""; |
This is a common example of the pagination (paging). const readlineSync = require('readline-sync');
function selectItem(items) {
if (!items || !items.length) { return -1; }
const MAX_ITEMS = 8,
MAX_PAGE_INDEX = Math.ceil(items.length / MAX_ITEMS) - 1;
let pageIndex = 0;
while (true) {
const PAGE_ITEMS = [];
let indexPrev = -1, indexNext = -1;
if (pageIndex > 0) {
PAGE_ITEMS.push(`(PREVIOUS ${MAX_ITEMS} items)`);
indexPrev = PAGE_ITEMS.length - 1;
}
Array.prototype.push.apply(PAGE_ITEMS,
items.slice(pageIndex * MAX_ITEMS, (pageIndex + 1) * MAX_ITEMS));
if (pageIndex < MAX_PAGE_INDEX) {
PAGE_ITEMS.push(`(NEXT ${pageIndex < MAX_PAGE_INDEX - 1 ? MAX_ITEMS :
items.length - MAX_ITEMS * (pageIndex + 1)} item(s))`);
indexNext = PAGE_ITEMS.length - 1;
}
console.log('\x1B[2J');
const index = readlineSync.keyInSelect(PAGE_ITEMS);
if (indexPrev !== -1 && index === indexPrev) {
pageIndex--;
} else if (indexNext !== -1 && index === indexNext) {
pageIndex++;
} else {
return index === -1 ? index :
(index + pageIndex * MAX_ITEMS - (indexPrev === -1 ? 0 : 1));
}
}
}
const items = ['ITEM 1', 'ITEM 2', 'ITEM 3', 'ITEM 4', 'ITEM 5', 'ITEM 6', 'ITEM 7',
'ITEM 8', 'ITEM 9', 'ITEM 10', 'ITEM 11', 'ITEM 12', 'ITEM 13', 'ITEM 14', 'ITEM 15',
'ITEM 16', 'ITEM 17', 'ITEM 18', 'ITEM 19', 'ITEM 20'
],
index = selectItem(items);
console.log(index === -1 ? '\nYou canceled.' : `\nYou chose ${items[index]}`); |
@anseki Thanks for your code! |
😄 |
@anseki I am unable to understand the code . How to learn js so that I can write it like you and also become a great like you .Pleas guide me Anseki . |
Hi @aadhar54, thank you for the comment. |
Just wondering why the keyInSelect array limit is hard coded to 35?
I had lists larger than 35 items.
readline-sync/lib/readline-sync.js
Line 1258 in 40211f1
The text was updated successfully, but these errors were encountered: