terminal-paginator
Advanced tools
Comparing version 0.1.0 to 0.2.0
19
index.js
'use strict'; | ||
var flatten = require('arr-flatten'); | ||
var debug = require('debug')('terminal-paginator'); | ||
var log = require('log-utils'); | ||
@@ -13,2 +13,3 @@ | ||
function Paginator(options) { | ||
debug('initializing from <%s>', __filename); | ||
this.options = options || {}; | ||
@@ -20,7 +21,11 @@ this.lastIndex = 0; | ||
Paginator.prototype.paginate = function(output, selected, pageSize) { | ||
var m = output.match(/^\n+/); | ||
var prefix = m ? m[0] : ''; | ||
output = output.replace(/^\n+/, ''); | ||
pageSize = pageSize || this.options.pageSize || 7; | ||
var lines = output.split('\n'); | ||
// Make sure there's enough lines to paginate | ||
if (lines.length <= pageSize + 2) { | ||
// return if there are not enough lines to paginate | ||
if (pageSize >= lines.length - 2) { | ||
return output; | ||
@@ -30,3 +35,3 @@ } | ||
// Move the pointer when a down keypress is entered, and limit it to 3 | ||
if (this.pointer < 3 && this.lastIndex < selected && selected - this.lastIndex < 9) { | ||
if (this.pointer < 3 && this.lastIndex < selected && (selected - this.lastIndex) < 9) { | ||
this.pointer = Math.min(3, this.pointer + selected - this.lastIndex); | ||
@@ -38,7 +43,9 @@ } | ||
// Duplicate the lines so it give an infinite list look | ||
var infinite = flatten([lines, lines, lines]); | ||
var infinite = lines.concat(lines).concat(lines); | ||
var topIndex = Math.max(0, selected + lines.length - this.pointer); | ||
var section = infinite.splice(topIndex, pageSize).join('\n'); | ||
return section + '\n' + log.dim('(Move up and down to reveal more choices)'); | ||
section += '\n'; | ||
section += log.dim('(Move up and down to reveal more choices)'); | ||
return prefix + section; | ||
}; | ||
@@ -45,0 +52,0 @@ |
{ | ||
"name": "terminal-paginator", | ||
"description": "Paginator used for rendering lists or choices for prompts in the terminal.", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/enquirer/terminal-paginator", | ||
@@ -52,3 +52,3 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"dependencies": { | ||
"arr-flatten": "^1.0.1", | ||
"debug": "^2.2.0", | ||
"log-utils": "^0.2.1" | ||
@@ -84,2 +84,2 @@ }, | ||
} | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6862
41
+ Addeddebug@^2.2.0
+ Addeddebug@2.6.9(transitive)
+ Addedms@2.0.0(transitive)
- Removedarr-flatten@^1.0.1
- Removedarr-flatten@1.1.0(transitive)