[Pkg-javascript-commits] [node-wrap-ansi] 01/04: Import Upstream version 2.0.0

Paolo Greppi paolog-guest at moszumanska.debian.org
Sat Nov 26 00:20:28 UTC 2016


This is an automated email from the git hooks/post-receive script.

paolog-guest pushed a commit to branch master
in repository node-wrap-ansi.

commit ce9dfea7e15640a4e0f08328394a72eccd17dd60
Author: Paolo Greppi <paolo.greppi at libpf.com>
Date:   Wed Nov 23 13:18:30 2016 +0000

    Import Upstream version 2.0.0
---
 .editorconfig  |  15 ++++++
 .gitattributes |   1 +
 .gitignore     |   2 +
 .travis.yml    |   6 +++
 index.js       | 162 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 license        |  21 ++++++++
 package.json   |  67 ++++++++++++++++++++++++
 readme.md      |  63 ++++++++++++++++++++++
 screenshot.png | Bin 0 -> 11331 bytes
 test.js        |  96 ++++++++++++++++++++++++++++++++++
 10 files changed, 433 insertions(+)

diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..8f9d77e
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,15 @@
+root = true
+
+[*]
+indent_style = tab
+end_of_line = lf
+charset = utf-8
+trim_trailing_whitespace = true
+insert_final_newline = true
+
+[{package.json,*.yml}]
+indent_style = space
+indent_size = 2
+
+[*.md]
+trim_trailing_whitespace = false
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..176a458
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1 @@
+* text=auto
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..c9106a7
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+node_modules
+.nyc_output
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..9d1ba36
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,6 @@
+language: node_js
+node_js:
+  - 'stable'
+  - '0.12'
+  - '0.10'
+after_success: npm run coverage
diff --git a/index.js b/index.js
new file mode 100755
index 0000000..f00fa92
--- /dev/null
+++ b/index.js
@@ -0,0 +1,162 @@
+'use strict';
+var stringWidth = require('string-width');
+
+var 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.
+function wrapWord(rows, word, cols) {
+	var insideEscape = false;
+	var visible = rows[rows.length - 1].length;
+
+	for (var i = 0; i < word.length; i++) {
+		var x = word[i];
+
+		rows[rows.length - 1] += x;
+
+		if (ESCAPES.indexOf(x) !== -1) {
+			insideEscape = true;
+		} else if (insideEscape && x === 'm') {
+			insideEscape = false;
+			continue;
+		}
+
+		if (insideEscape) {
+			continue;
+		}
+
+		visible++;
+
+		if (visible >= cols && i < word.length - 1) {
+			rows.push('');
+			visible = 0;
+		}
+	}
+
+	// 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.
+//
+// 'hard' will never allow a string to take up more
+// than cols characters.
+//
+// 'soft' allows long words to expand past the column length.
+function exec(str, cols, opts) {
+	var options = opts || {};
+
+	var pre = '';
+	var ret = '';
+	var escapeCode;
+
+	var lengths = wordLengths(str);
+	var words = str.split(' ');
+	var rows = [''];
+
+	for (var i = 0, word; (word = words[i]) !== undefined; i++) {
+		var 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'.
+		if (lengths[i] > cols && options.hard) {
+			if (rowLength) {
+				rows.push('');
+			}
+			wrapWord(rows, word, cols);
+			continue;
+		}
+
+		if (rowLength + lengths[i] > cols && rowLength > 0) {
+			rows.push('');
+		}
+
+		rows[rows.length - 1] += word;
+	}
+
+	pre = rows.map(function (r) {
+		return r.trim();
+	}).join('\n');
+
+	for (var j = 0; j < pre.length; j++) {
+		var y = pre[j];
+
+		ret += y;
+
+		if (ESCAPES.indexOf(y) !== -1) {
+			var code = parseFloat(/[0-9][^m]*/.exec(pre.slice(j, j + 4)));
+			escapeCode = code === END_CODE ? null : code;
+		}
+
+		if (escapeCode && ESCAPE_CODES[escapeCode]) {
+			if (pre[j + 1] === '\n') {
+				ret += wrapAnsi(ESCAPE_CODES[escapeCode]);
+			} else if (y === '\n') {
+				ret += wrapAnsi(escapeCode);
+			}
+		}
+	}
+
+	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');
+};
diff --git a/license b/license
new file mode 100644
index 0000000..654d0bf
--- /dev/null
+++ b/license
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) Sindre Sorhus <sindresorhus at gmail.com> (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:
+
+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.
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..960ea2d
--- /dev/null
+++ b/package.json
@@ -0,0 +1,67 @@
+{
+  "name": "wrap-ansi",
+  "version": "2.0.0",
+  "description": "Wordwrap a string with ANSI escape codes",
+  "license": "MIT",
+  "repository": "chalk/wrap-ansi",
+  "author": {
+    "name": "Sindre Sorhus",
+    "email": "sindresorhus at gmail.com",
+    "url": "sindresorhus.com"
+  },
+  "maintainers": [
+    "Sindre Sorhus <sindresorhus at gmail.com> (sindresorhus.com)",
+    "Joshua Appelman <jappelman at xebia.com> (jbnicolai.com)",
+    "JD Ballard <i.am.qix at gmail.com> (github.com/qix-)",
+    "Benjamin Coe <ben at npmjs.com> (github.com/bcoe)"
+  ],
+  "engines": {
+    "node": ">=0.10.0"
+  },
+  "scripts": {
+    "test": "xo && nyc node test.js",
+    "coverage": "nyc --reporter=text-lcov node test.js | 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"
+  },
+  "devDependencies": {
+    "ava": "0.0.4",
+    "chalk": "^1.1.0",
+    "coveralls": "^2.11.4",
+    "has-ansi": "^2.0.0",
+    "nyc": "^3.2.2",
+    "strip-ansi": "^3.0.0",
+    "xo": "*"
+  }
+}
diff --git a/readme.md b/readme.md
new file mode 100644
index 0000000..f2e0a6a
--- /dev/null
+++ b/readme.md
@@ -0,0 +1,63 @@
+# wrap-ansi [![Build Status](https://travis-ci.org/chalk/wrap-ansi.svg?branch=master)](https://travis-ci.org/chalk/wrap-ansi) [![Coverage Status](https://coveralls.io/repos/chalk/wrap-ansi/badge.svg?branch=master&service=github)](https://coveralls.io/github/chalk/wrap-ansi?branch=master)
+
+> Wordwrap a string with [ANSI escape codes](http://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles)
+
+
+## Install
+
+```
+$ npm install --save wrap-ansi
+```
+
+
+## Usage
+
+```js
+const chalk = require('chalk');
+const wrapAnsi = require('wrap-ansi');
+
+const input = 'The quick brown ' + chalk.red('fox jumped over ') +
+	'the lazy ' + chalk.green('dog and then ran away with the unicorn.');
+
+console.log(wrapAnsi(input, 20));
+```
+
+<img width="331" src="screenshot.png">
+
+
+## API
+
+### wrapAnsi(input, columns, [options])
+
+Wrap words to the specified column width.
+
+#### input
+
+Type: `string`
+
+String with ANSI escape codes. Like one styled by [`chalk`](https://github.com/chalk/chalk).
+
+#### columns
+
+Type: `number`
+
+Number of columns to wrap the text to.
+
+#### options.hard
+
+Type: `boolean`  
+Default: `false`
+
+By default the wrap is soft, meaning long words may extend past the column width. Setting this to `true` will make it hard wrap at the column width.
+
+
+## Related
+
+- [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes
+- [chalk](https://github.com/chalk/chalk) - Terminal string styling done right
+- [jsesc](https://github.com/mathiasbynens/jsesc) - Generate ASCII-only output from Unicode strings. Useful for creating test fixtures.
+
+
+## License
+
+MIT © [Sindre Sorhus](http://sindresorhus.com)
diff --git a/screenshot.png b/screenshot.png
new file mode 100644
index 0000000..1f7af46
Binary files /dev/null and b/screenshot.png differ
diff --git a/test.js b/test.js
new file mode 100755
index 0000000..f976a75
--- /dev/null
+++ b/test.js
@@ -0,0 +1,96 @@
+'use strict';
+var test = require('ava');
+var chalk = require('chalk');
+var hasAnsi = require('has-ansi');
+var stripAnsi = require('strip-ansi');
+var fn = require('./');
+
+// when "hard" is false
+
+var fixture = 'The quick brown ' + chalk.red('fox jumped over ') + 'the lazy ' + chalk.green('dog and then ran away with the unicorn.');
+var fixture2 = '12345678\n901234567890';
+
+test('wraps string at 20 characters', function (t) {
+	var res20 = fn(fixture, 20);
+
+	t.assert(res20 === 'The quick brown \u001b[31mfox\u001b[39m\n\u001b[31mjumped over \u001b[39mthe lazy\n\u001b[32mdog and then ran\u001b[39m\n\u001b[32maway with the\u001b[39m\n\u001b[32municorn.\u001b[39m');
+	t.assert(stripAnsi(res20).split('\n').every(function (x) {
+		return x.length <= 20;
+	}));
+
+	t.end();
+});
+
+test('wraps string at 30 characters', function (t) {
+	var res30 = fn(fixture, 30);
+
+	t.assert(res30 === 'The quick brown \u001b[31mfox jumped\u001b[39m\n\u001b[31mover \u001b[39mthe lazy \u001b[32mdog and then ran\u001b[39m\n\u001b[32maway with the unicorn.\u001b[39m');
+	t.assert(stripAnsi(res30).split('\n').every(function (x) {
+		return x.length <= 30;
+	}));
+
+	t.end();
+});
+
+test('does not break strings longer than "cols" characters', function (t) {
+	var res5 = fn(fixture, 5, {hard: false});
+
+	t.assert(res5 === 'The\nquick\nbrown\n\u001b[31mfox\u001b[39m\n\u001b[31mjumped\u001b[39m\n\u001b[31mover\u001b[39m\n\u001b[31m\u001b[39mthe\nlazy\n\u001b[32mdog\u001b[39m\n\u001b[32mand\u001b[39m\n\u001b[32mthen\u001b[39m\n\u001b[32mran\u001b[39m\n\u001b[32maway\u001b[39m\n\u001b[32mwith\u001b[39m\n\u001b[32mthe\u001b[39m\n\u001b[32municorn.\u001b[39m');
+	t.assert(
+		stripAnsi(res5).split('\n').filter(function (x) {
+			return x.length > 5;
+		}).length > 0
+	);
+
+	t.end();
+});
+
+test('handles colored string that wraps on to multiple lines', function (t) {
+	var res = fn(chalk.green('hello world') + ' hey!', 5, {hard: false});
+	var lines = res.split('\n');
+	t.assert(hasAnsi(lines[0]));
+	t.assert(hasAnsi(lines[1]));
+	t.assert(hasAnsi(lines[2]) === false);
+	t.end();
+});
+
+test('does not prepend newline if first string is greater than "cols"', function (t) {
+	var res = fn(chalk.green('hello') + '-world', 5, {hard: false});
+	t.assert(res.split('\n').length === 1);
+	t.end();
+});
+
+// when "hard" is true
+
+test('breaks strings longer than "cols" characters', function (t) {
+	var res5 = fn(fixture, 5, {hard: true});
+
+	t.assert(res5 === 'The\nquick\nbrown\n\u001b[31mfox\u001b[39m\n\u001b[31mjumpe\u001b[39m\n\u001b[31md\u001b[39m\n\u001b[31mover\u001b[39m\n\u001b[31m\u001b[39mthe\nlazy\n\u001b[32mdog\u001b[39m\n\u001b[32mand\u001b[39m\n\u001b[32mthen\u001b[39m\n\u001b[32mran\u001b[39m\n\u001b[32maway\u001b[39m\n\u001b[32mwith\u001b[39m\n\u001b[32mthe\u001b[39m\n\u001b[32munico\u001b[39m\n\u001b[32mrn.\u001b[39m');
+	t.assert(stripAnsi(res5).split('\n').every(function (x) {
+		return x.length <= 5;
+	}));
+
+	t.end();
+});
+
+test('removes last row if it contained only ansi escape codes', function (t) {
+	var res = fn(chalk.green('helloworld'), 2, {hard: true});
+
+	t.assert(stripAnsi(res).split('\n').every(function (x) {
+		return x.length === 2;
+	}));
+
+	t.end();
+});
+
+test('does not prepend newline if first word is split', function (t) {
+	var res = fn(chalk.green('hello') + 'world', 5, {hard: true});
+	t.assert(res.split('\n').length === 2);
+	t.end();
+});
+
+test('takes into account line returns inside input', function (t) {
+	var res20 = fn(fixture2, 10, {hard: true});
+	t.assert(res20 === '12345678\n9012345678\n90');
+	t.end();
+});

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-wrap-ansi.git



More information about the Pkg-javascript-commits mailing list