terminal-paginator
Advanced tools
Comparing version 1.1.0 to 2.0.0
49
index.js
'use strict'; | ||
var debug = require('debug')('terminal-paginator'); | ||
var extend = require('extend-shallow'); | ||
var log = require('log-utils'); | ||
@@ -21,5 +22,16 @@ | ||
Paginator.prototype.paginate = function(output, selected, limit) { | ||
limit = limit || this.options.limit || 7; | ||
/** | ||
* Paginate the given `output` string and update the cursor position. | ||
* | ||
* @param {String} `output` | ||
* @param {Number} `pos` | ||
* @param {Object} `options` | ||
* @return {String} | ||
* @api public | ||
*/ | ||
Paginator.prototype.paginate = function(output, pos, options) { | ||
var opts = extend({limit: 7}, this.options, options); | ||
var lines = output.split('\n'); | ||
var limit = opts.limit; | ||
@@ -37,26 +49,25 @@ // Return if we don't have enough visible lines to paginate | ||
// position the middle of the visible list | ||
if (this.position < middle && this.lastIndex < selected && selected - this.lastIndex < limit) { | ||
this.position = Math.min(middle, this.position + selected - this.lastIndex); | ||
if (this.position < middle && this.lastIndex < pos && pos - this.lastIndex < limit) { | ||
this.position = Math.min(middle, this.position + pos - this.lastIndex); | ||
} | ||
// store reference to the index of the currently selected item | ||
this.lastIndex = selected; | ||
// store reference to the index of the currently pos item | ||
this.lastIndex = pos; | ||
if (opts.filterList === true) { | ||
lines = lines.filter(Boolean); | ||
} | ||
// Duplicate lines to create the illusion of an infinite list | ||
var infinite = lines.concat(lines).concat(lines).filter(Boolean); | ||
var topIndex = Math.max(0, selected + lines.length - this.position); | ||
var infinite = lines.concat(lines).concat(lines); | ||
var topIndex = Math.max(0, pos + lines.length - this.position); | ||
if (this.options.radio === true && this.firstRender) { | ||
this.firstRender = false; | ||
topIndex = 0; | ||
if (!infinite[0].trim()) { | ||
topIndex++; | ||
} | ||
// Create the visible list based on the limit and current cursor position | ||
var visible = infinite.splice(topIndex, limit); | ||
visible = visible.join('\n'); | ||
if (this.footer) { | ||
visible += '\n'; | ||
visible += log.dim(this.footer); | ||
} | ||
// Create the visible list based on the limit and current cursor position | ||
var visible = infinite.splice(topIndex, limit).join('\n'); | ||
visible += '\n'; | ||
visible += log.dim(this.footer); | ||
// ensure that output has a leading newline, so that the first | ||
@@ -63,0 +74,0 @@ // list item begins on the next line after the prompt question |
{ | ||
"name": "terminal-paginator", | ||
"description": "Paginator used for rendering lists or choices for prompts in the terminal.", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"homepage": "https://github.com/enquirer/terminal-paginator", | ||
@@ -24,2 +24,3 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"debug": "^2.6.6", | ||
"extend-shallow": "^2.0.1", | ||
"log-utils": "^0.2.1" | ||
@@ -26,0 +27,0 @@ }, |
@@ -70,2 +70,2 @@ # terminal-paginator [![NPM version](https://img.shields.io/npm/v/terminal-paginator.svg?style=flat)](https://www.npmjs.com/package/terminal-paginator) [![NPM monthly downloads](https://img.shields.io/npm/dm/terminal-paginator.svg?style=flat)](https://npmjs.org/package/terminal-paginator) [![NPM total downloads](https://img.shields.io/npm/dt/terminal-paginator.svg?style=flat)](https://npmjs.org/package/terminal-paginator) | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on May 07, 2017._ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on May 24, 2017._ |
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
8199
68
3
+ Addedextend-shallow@^2.0.1
+ Addedextend-shallow@2.0.1(transitive)
+ Addedis-extendable@0.1.1(transitive)