Skip to content

Commit

Permalink
Require Node.js 4 and meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jul 21, 2017
1 parent 02ad690 commit 3483a30
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 197 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,*.yml}]
[*.yml]
indent_style = space
indent_size = 2
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* text=auto
*.js text eol=lf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
yarn.lock
.nyc_output
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: node_js
node_js:
- '8'
- '6'
- '4'
- '0.12'
- '0.10'
after_success: npm run coveralls
161 changes: 78 additions & 83 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,56 @@
'use strict';
var stringWidth = require('string-width');
var stripAnsi = require('strip-ansi');
const stringWidth = require('string-width');
const stripAnsi = require('strip-ansi');

var ESCAPES = [
'\u001b',
'\u009b'
const ESCAPES = [
'\u001B',
'\u009B'
];

var END_CODE = 39;

var ESCAPE_CODES = {
0: 0,
1: 22,
2: 22,
3: 23,
4: 24,
7: 27,
8: 28,
9: 29,
30: 39,
31: 39,
32: 39,
33: 39,
34: 39,
35: 39,
36: 39,
37: 39,
90: 39,
40: 49,
41: 49,
42: 49,
43: 49,
44: 49,
45: 49,
46: 49,
47: 49
};

function wrapAnsi(code) {
return ESCAPES[0] + '[' + code + 'm';
}

// calculate the length of words split on ' ', ignoring
// the extra characters added by ansi escape codes.
function wordLengths(str) {
return str.split(' ').map(function (s) {
return stringWidth(s);
});
}

// wrap a long word across multiple rows.
// ansi escape codes do not count towards length.
const END_CODE = 39;

const ESCAPE_CODES = new Map([
[0, 0],
[1, 22],
[2, 22],
[3, 23],
[4, 24],
[7, 27],
[8, 28],
[9, 29],
[30, 39],
[31, 39],
[32, 39],
[33, 39],
[34, 39],
[35, 39],
[36, 39],
[37, 39],
[90, 39],
[40, 49],
[41, 49],
[42, 49],
[43, 49],
[44, 49],
[45, 49],
[46, 49],
[47, 49]
]);

const wrapAnsi = code => `${ESCAPES[0]}[${code}m`;

// Calculate the length of words split on ' ', ignoring
// the extra characters added by ansi escape codes
const wordLengths = str => str.split(' ').map(s => stringWidth(s));

// Wrap a long word across multiple rows
// Ansi escape codes do not count towards length
function wrapWord(rows, word, cols) {
var insideEscape = false;
var visible = stripAnsi(rows[rows.length - 1]).length;
let insideEscape = false;
let visible = stripAnsi(rows[rows.length - 1]).length;

for (var i = 0; i < word.length; i++) {
var x = word[i];
for (let i = 0; i < word.length; i++) {
const x = word[i];

rows[rows.length - 1] += x;

Expand All @@ -79,41 +73,41 @@ function wrapWord(rows, word, cols) {
}
}

// it's possible that the last row we copy over is only
// ansi escape characters, handle this edge-case.
// It's possible that the last row we copy over is only
// ansi escape characters, handle this edge-case
if (!visible && rows[rows.length - 1].length > 0 && rows.length > 1) {
rows[rows.length - 2] += rows.pop();
}
}

// the wrap-ansi module can be invoked
// in either 'hard' or 'soft' wrap mode.
// The wrap-ansi module can be invoked
// in either 'hard' or 'soft' wrap mode
//
// 'hard' will never allow a string to take up more
// than cols characters.
// than cols characters
//
// 'soft' allows long words to expand past the column length.
// 'soft' allows long words to expand past the column length
function exec(str, cols, opts) {
var options = opts || {};
const options = opts || {};

var pre = '';
var ret = '';
var escapeCode;
let pre = '';
let ret = '';
let escapeCode;

var lengths = wordLengths(str);
var words = str.split(' ');
var rows = [''];
const lengths = wordLengths(str);
const words = str.split(' ');
const rows = [''];

for (var i = 0, word; (word = words[i]) !== undefined; i++) {
var rowLength = stringWidth(rows[rows.length - 1]);
for (let i = 0, word; (word = words[i]) !== undefined; i++) {
let rowLength = stringWidth(rows[rows.length - 1]);

if (rowLength) {
rows[rows.length - 1] += ' ';
rowLength++;
}

// in 'hard' wrap mode, the length of a line is
// never allowed to extend past 'cols'.
// In 'hard' wrap mode, the length of a line is
// never allowed to extend past 'cols'
if (lengths[i] > cols && options.hard) {
if (rowLength) {
rows.push('');
Expand All @@ -139,23 +133,23 @@ function exec(str, cols, opts) {
rows[rows.length - 1] += word;
}

pre = rows.map(function (r) {
return r.trim();
}).join('\n');
pre = rows.map(x => x.trim()).join('\n');

for (var j = 0; j < pre.length; j++) {
var y = pre[j];
for (let j = 0; j < pre.length; j++) {
const y = pre[j];

ret += y;

if (ESCAPES.indexOf(y) !== -1) {
var code = parseFloat(/[0-9][^m]*/.exec(pre.slice(j, j + 4)));
const code = parseFloat(/\d[^m]*/.exec(pre.slice(j, j + 4)));
escapeCode = code === END_CODE ? null : code;
}

if (escapeCode && ESCAPE_CODES[escapeCode]) {
const code = ESCAPE_CODES.get(parseInt(escapeCode, 10));

if (escapeCode && code) {
if (pre[j + 1] === '\n') {
ret += wrapAnsi(ESCAPE_CODES[escapeCode]);
ret += wrapAnsi(code);
} else if (y === '\n') {
ret += wrapAnsi(escapeCode);
}
Expand All @@ -165,9 +159,10 @@ function exec(str, cols, opts) {
return ret;
}

// for each line break, invoke the method separately.
module.exports = function (str, cols, opts) {
return String(str).split('\n').map(function (substr) {
return exec(substr, cols, opts);
}).join('\n');
// For each newline, invoke the method separately
module.exports = (str, cols, opts) => {
return String(str)
.split('\n')
.map(line => exec(line, cols, opts))
.join('\n');
};
20 changes: 4 additions & 16 deletions license
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
The MIT License (MIT)
MIT License

Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.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:
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 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.
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.
126 changes: 60 additions & 66 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,68 +1,62 @@
{
"name": "wrap-ansi",
"version": "2.1.0",
"description": "Wordwrap a string with ANSI escape codes",
"license": "MIT",
"repository": "chalk/wrap-ansi",
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "sindresorhus.com"
},
"maintainers": [
"Sindre Sorhus <[email protected]> (sindresorhus.com)",
"Joshua Appelman <[email protected]> (jbnicolai.com)",
"JD Ballard <[email protected]> (github.com/qix-)",
"Benjamin Coe <[email protected]> (github.com/bcoe)"
],
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "xo && nyc ava",
"coveralls": "nyc report --reporter=text-lcov | coveralls"
},
"files": [
"index.js"
],
"keywords": [
"wrap",
"break",
"wordwrap",
"wordbreak",
"linewrap",
"ansi",
"styles",
"color",
"colour",
"colors",
"terminal",
"console",
"cli",
"string",
"tty",
"escape",
"formatting",
"rgb",
"256",
"shell",
"xterm",
"log",
"logging",
"command-line",
"text"
],
"dependencies": {
"string-width": "^1.0.1",
"strip-ansi": "^3.0.1"
},
"devDependencies": {
"ava": "^0.17.0",
"chalk": "^1.1.0",
"coveralls": "^2.11.4",
"has-ansi": "^2.0.0",
"nyc": "^6.2.1",
"strip-ansi": "^3.0.0",
"xo": "^0.16.0"
}
"name": "wrap-ansi",
"version": "2.1.0",
"description": "Wordwrap a string with ANSI escape codes",
"license": "MIT",
"repository": "chalk/wrap-ansi",
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && nyc ava",
"coveralls": "nyc report --reporter=text-lcov | coveralls"
},
"files": [
"index.js"
],
"keywords": [
"wrap",
"break",
"wordwrap",
"wordbreak",
"linewrap",
"ansi",
"styles",
"color",
"colour",
"colors",
"terminal",
"console",
"cli",
"string",
"tty",
"escape",
"formatting",
"rgb",
"256",
"shell",
"xterm",
"log",
"logging",
"command-line",
"text"
],
"dependencies": {
"string-width": "^2.1.1",
"strip-ansi": "^4.0.0"
},
"devDependencies": {
"ava": "^0.21.0",
"chalk": "^2.0.1",
"coveralls": "^2.11.4",
"has-ansi": "^3.0.0",
"nyc": "^11.0.3",
"strip-ansi": "^4.0.0",
"xo": "^0.18.2"
}
}
Loading

0 comments on commit 3483a30

Please sign in to comment.