4 sum: require('lodash/sum'),
5 flatten: require('lodash/flatten'),
7 var chalk = require('chalk');
10 * The paginator returns a subset of the choices if the list is too long.
14 constructor(screen, options = {}) {
15 const { isInfinite = true } = options;
18 this.isInfinite = isInfinite;
21 paginate(output, active, pageSize) {
22 pageSize = pageSize || 7;
23 var lines = output.split('\n');
26 lines = this.screen.breakLines(lines);
27 active = _.sum(lines.map((lineParts) => lineParts.length).splice(0, active));
28 lines = _.flatten(lines);
31 // Make sure there's enough lines to paginate
32 if (lines.length <= pageSize) {
35 const visibleLines = this.isInfinite
36 ? this.getInfiniteLines(lines, active, pageSize)
37 : this.getFiniteLines(lines, active, pageSize);
38 this.lastIndex = active;
40 visibleLines.join('\n') +
42 chalk.dim('(Move up and down to reveal more choices)')
46 getInfiniteLines(lines, active, pageSize) {
47 if (this.pointer === undefined) {
50 var middleOfList = Math.floor(pageSize / 2);
51 // Move the pointer only when the user go down and limit it to the middle of the list
53 this.pointer < middleOfList &&
54 this.lastIndex < active &&
55 active - this.lastIndex < pageSize
57 this.pointer = Math.min(middleOfList, this.pointer + active - this.lastIndex);
60 // Duplicate the lines so it give an infinite list look
61 var infinite = _.flatten([lines, lines, lines]);
62 var topIndex = Math.max(0, active + lines.length - this.pointer);
64 return infinite.splice(topIndex, pageSize);
67 getFiniteLines(lines, active, pageSize) {
68 var topIndex = active - pageSize / 2;
71 } else if (topIndex + pageSize > lines.length) {
72 topIndex = lines.length - pageSize;
74 return lines.splice(topIndex, pageSize);
78 module.exports = Paginator;