| Current Path : /home/emeraadmin/www/4d695/ |
| Current File : /home/emeraadmin/www/4d695/cli-columns.tar |
package.json 0000644 00000002557 15170143015 0007040 0 ustar 00 {
"_id": "cli-columns@4.0.0",
"_inBundle": true,
"_location": "/npm/cli-columns",
"_phantomChildren": {},
"_requiredBy": [
"/npm"
],
"author": {
"name": "Shannon Moeller",
"email": "me@shannonmoeller",
"url": "http://shannonmoeller.com"
},
"bugs": {
"url": "https://github.com/shannonmoeller/cli-columns/issues"
},
"dependencies": {
"string-width": "^4.2.3",
"strip-ansi": "^6.0.1"
},
"description": "Columnated lists for the CLI.",
"devDependencies": {
"chalk": "^4.1.2"
},
"engines": {
"node": ">= 10"
},
"eslintConfig": {
"extends": "eslint:recommended",
"env": {
"node": true
},
"parserOptions": {
"ecmaVersion": 8
}
},
"files": [
"*.js"
],
"homepage": "https://github.com/shannonmoeller/cli-columns#readme",
"keywords": [
"ansi",
"cli",
"column",
"columnate",
"columns",
"grid",
"list",
"log",
"ls",
"row",
"rows",
"unicode",
"unix"
],
"license": "MIT",
"main": "index.js",
"name": "cli-columns",
"prettier": {
"singleQuote": true
},
"repository": {
"type": "git",
"url": "git+https://github.com/shannonmoeller/cli-columns.git"
},
"scripts": {
"lint": "npx eslint --fix '*.js' && npx prettier --write '*.js'",
"test": "node test.js && node color.js"
},
"version": "4.0.0"
}
test.js 0000644 00000004240 15170143015 0006056 0 ustar 00 'use strict';
const assert = require('assert');
const chalk = require('chalk');
const stripAnsi = require('strip-ansi');
const columns = require('./index.js');
const tests = [];
function test(msg, fn) {
tests.push([msg, fn]);
}
process.nextTick(async function run() {
for (const [msg, fn] of tests) {
try {
await fn(assert);
console.log(`pass - ${msg}`);
} catch (error) {
console.error(`fail - ${msg}`, error);
process.exit(1);
}
}
});
// prettier-ignore
test('should print one column list', t => {
const cols = columns(['foo', ['bar', 'baz'], ['bar', 'qux']], {
width: 1
});
const expected =
'bar\n' +
'bar\n' +
'baz\n' +
'foo\n' +
'qux';
t.equal(cols, expected);
});
// prettier-ignore
test('should print three column list', t => {
const cols = columns(['foo', ['bar', 'baz'], ['bat', 'qux']], {
width: 16
});
const expected =
'bar baz qux \n' +
'bat foo ';
t.equal(cols, expected);
});
// prettier-ignore
test('should print complex list', t => {
const cols = columns(
[
'foo', 'bar', 'baz',
chalk.cyan('嶜憃撊') + ' 噾噿嚁',
'blue' + chalk.bgBlue('berry'),
chalk.red('apple'), 'pomegranate',
'durian', chalk.green('star fruit'),
'apricot', 'banana pineapple'
],
{
width: 80
}
);
const expected =
'apple bar durian star fruit \n' +
'apricot baz foo 嶜憃撊 噾噿嚁 \n' +
'banana pineapple blueberry pomegranate ';
t.equal(stripAnsi(cols), expected);
});
// prettier-ignore
test('should optionally not sort', t => {
const cols = columns(
[
'foo', 'bar', 'baz',
chalk.cyan('嶜憃撊') + ' 噾噿嚁',
'blue' + chalk.bgBlue('berry'),
chalk.red('apple'), 'pomegranate',
'durian', chalk.green('star fruit'),
'apricot', 'banana pineapple'
],
{
sort: false,
width: 80
}
);
const expected =
'foo 嶜憃撊 噾噿嚁 pomegranate apricot \n' +
'bar blueberry durian banana pineapple \n' +
'baz apple star fruit ';
t.equal(stripAnsi(cols), expected);
});
color.js 0000644 00000000623 15170143015 0006216 0 ustar 00 const chalk = require('chalk');
const columns = require('.');
// prettier-ignore
const values = [
'blue' + chalk.bgBlue('berry'),
'笔菠萝' + chalk.yellow('苹果笔'),
chalk.red('apple'), 'pomegranate',
'durian', chalk.green('star fruit'),
'パイナップル', 'apricot', 'banana',
'pineapple', chalk.bgRed.yellow('orange')
];
console.log('');
console.log(columns(values));
console.log('');
index.js 0000644 00000003133 15170143015 0006206 0 ustar 00 'use strict';
const stringWidth = require('string-width');
const stripAnsi = require('strip-ansi');
const concat = Array.prototype.concat;
const defaults = {
character: ' ',
newline: '\n',
padding: 2,
sort: true,
width: 0,
};
function byPlainText(a, b) {
const plainA = stripAnsi(a);
const plainB = stripAnsi(b);
if (plainA === plainB) {
return 0;
}
if (plainA > plainB) {
return 1;
}
return -1;
}
function makeArray() {
return [];
}
function makeList(count) {
return Array.apply(null, Array(count));
}
function padCell(fullWidth, character, value) {
const valueWidth = stringWidth(value);
const filler = makeList(fullWidth - valueWidth + 1);
return value + filler.join(character);
}
function toRows(rows, cell, i) {
rows[i % rows.length].push(cell);
return rows;
}
function toString(arr) {
return arr.join('');
}
function columns(values, options) {
values = concat.apply([], values);
options = Object.assign({}, defaults, options);
let cells = values.filter(Boolean).map(String);
if (options.sort !== false) {
cells = cells.sort(byPlainText);
}
const termWidth = options.width || process.stdout.columns;
const cellWidth =
Math.max.apply(null, cells.map(stringWidth)) + options.padding;
const columnCount = Math.floor(termWidth / cellWidth) || 1;
const rowCount = Math.ceil(cells.length / columnCount) || 1;
if (columnCount === 1) {
return cells.join(options.newline);
}
return cells
.map(padCell.bind(null, cellWidth, options.character))
.reduce(toRows, makeList(rowCount).map(makeArray))
.map(toString)
.join(options.newline);
}
module.exports = columns;
license 0000644 00000002142 15170143015 0006105 0 ustar 00 The MIT License (MIT)
Copyright (c) Shannon Moeller <me@shannonmoeller.com> (shannonmoeller.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.