-
-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
maxVisible
option for select prompts (#225)
* Add `maxVisible` option for select prompts * Add reusable `entriesToDisplay` utility method * Use `entriesToDisplay` in both select and multiselect
- Loading branch information
1 parent
fb4010a
commit d7d2c37
Showing
5 changed files
with
100 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Determine what entries should be displayed on the screen, based on the | ||
* currently selected index and the maximum visible. Used in list-based | ||
* prompts like `select` and `multiselect`. | ||
* | ||
* @param {number} cursor the currently selected entry | ||
* @param {number} total the total entries available to display | ||
* @param {number} [maxVisible] the number of entries that can be displayed | ||
*/ | ||
module.exports = (cursor, total, maxVisible) => { | ||
maxVisible = maxVisible || total; | ||
|
||
let startIndex = Math.min(total- maxVisible, cursor - Math.floor(maxVisible / 2)); | ||
if (startIndex < 0) startIndex = 0; | ||
|
||
let endIndex = Math.min(startIndex + maxVisible, total); | ||
|
||
return { startIndex, endIndex }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
const test = require('tape'); | ||
const { entriesToDisplay } = require('../lib/util'); | ||
|
||
test('entriesToDisplay', t => { | ||
t.plan(11); | ||
t.deepEqual(entriesToDisplay(0, 8, 5), { startIndex: 0, endIndex: 5 }, 'top of list'); | ||
t.deepEqual(entriesToDisplay(1, 8, 5), { startIndex: 0, endIndex: 5 }, '+1 from top'); | ||
t.deepEqual(entriesToDisplay(2, 8, 5), { startIndex: 0, endIndex: 5 }, '+2 from top'); | ||
t.deepEqual(entriesToDisplay(3, 8, 5), { startIndex: 1, endIndex: 6 }, '+3 from top'); | ||
t.deepEqual(entriesToDisplay(4, 8, 5), { startIndex: 2, endIndex: 7 }, '-3 from bottom'); | ||
t.deepEqual(entriesToDisplay(5, 8, 5), { startIndex: 3, endIndex: 8 }, '-2 from bottom'); | ||
t.deepEqual(entriesToDisplay(6, 8, 5), { startIndex: 3, endIndex: 8 }, '-1 from bottom'); | ||
t.deepEqual(entriesToDisplay(7, 8, 5), { startIndex: 3, endIndex: 8 }, 'bottom of list'); | ||
|
||
t.deepEqual(entriesToDisplay(0, 10, 11), { startIndex: 0, endIndex: 10 }, 'top of list when maxVisible greater than total'); | ||
t.deepEqual(entriesToDisplay(9, 10, 11), { startIndex: 0, endIndex: 10 }, 'bottom of list maxVisible greater than total'); | ||
|
||
t.deepEqual(entriesToDisplay(0, 10), { startIndex: 0, endIndex:10 }, 'maxVisible is optional'); | ||
}); |