[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository has been archived by the owner on Nov 3, 2022. It is now read-only.

35 limit on keyInSelect #60

Closed
tdelov opened this issue Aug 23, 2017 · 9 comments
Closed

35 limit on keyInSelect #60

tdelov opened this issue Aug 23, 2017 · 9 comments

Comments

@tdelov
Copy link
tdelov commented Aug 23, 2017

Just wondering why the keyInSelect array limit is hard coded to 35?
I had lists larger than 35 items.

if (!Array.isArray(items) || !items.length || items.length > 35) {

@tdelov
Copy link
Author
tdelov commented Aug 23, 2017

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!

@anseki
Copy link
Owner
anseki commented Aug 23, 2017

See: https://github.com/anseki/readline-sync#itemscount

And, you should consider small terminal. That is, you should use e.g. 10 items array.

@anseki anseki reopened this Aug 23, 2017
@tdelov
Copy link
Author
tdelov commented Aug 23, 2017

I think this is more about my understanding rather than being a problem.
I originally though the pagination was part of the module.
I came up with something like this

#!/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  )

@anseki
Copy link
Owner
anseki commented Aug 24, 2017

This line:

var slection = "";

should be:

var selection = "";

@anseki
Copy link
Owner
anseki commented Aug 24, 2017

This is a common example of the pagination (paging).
NOTE: I didn't test this code, you should test it if you use this code for your app.

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]}`);

@tdelov tdelov closed this as completed Aug 26, 2017
@sebastienfi
Copy link

@anseki Thanks for your code!

@anseki
Copy link
Owner
anseki commented Jun 6, 2019

😄

@aadhar54
Copy link

@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 .

@anseki
Copy link
Owner
anseki commented Mar 24, 2021

Hi @aadhar54, thank you for the comment.
Could you make new issue for another topic? And explain more about what is the code you can't understand.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants