Actualizacion maquina principal
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / inquirer / lib / utils / paginator.js
1 'use strict';
2
3 var _ = {
4   sum: require('lodash/sum'),
5   flatten: require('lodash/flatten'),
6 };
7 var chalk = require('chalk');
8
9 /**
10  * The paginator keeps track of a pointer index in a list and returns
11  * a subset of the choices if the list is too long.
12  */
13
14 class Paginator {
15   constructor(screen) {
16     this.pointer = 0;
17     this.lastIndex = 0;
18     this.screen = screen;
19   }
20
21   paginate(output, active, pageSize) {
22     pageSize = pageSize || 7;
23     var middleOfList = Math.floor(pageSize / 2);
24     var lines = output.split('\n');
25
26     if (this.screen) {
27       lines = this.screen.breakLines(lines);
28       active = _.sum(lines.map((lineParts) => lineParts.length).splice(0, active));
29       lines = _.flatten(lines);
30     }
31
32     // Make sure there's enough lines to paginate
33     if (lines.length <= pageSize) {
34       return output;
35     }
36
37     // Move the pointer only when the user go down and limit it to the middle of the list
38     if (
39       this.pointer < middleOfList &&
40       this.lastIndex < active &&
41       active - this.lastIndex < pageSize
42     ) {
43       this.pointer = Math.min(middleOfList, this.pointer + active - this.lastIndex);
44     }
45
46     this.lastIndex = active;
47
48     // Duplicate the lines so it give an infinite list look
49     var infinite = _.flatten([lines, lines, lines]);
50     var topIndex = Math.max(0, active + lines.length - this.pointer);
51
52     var section = infinite.splice(topIndex, pageSize).join('\n');
53     return section + '\n' + chalk.dim('(Move up and down to reveal more choices)');
54   }
55 }
56
57 module.exports = Paginator;