Socket
Socket
Sign inDemoInstall

wrap-ansi

Package Overview
Dependencies
Maintainers
3
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

wrap-ansi - npm Package Compare versions

Comparing version 3.0.1 to 4.0.0

116

index.js
'use strict';
const stringWidth = require('string-width');
const stripAnsi = require('strip-ansi');
const ansiStyles = require('ansi-styles');

@@ -12,30 +13,2 @@ const ESCAPES = new Set([

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.values().next().value}[${code}m`;

@@ -45,8 +18,8 @@

// the extra characters added by ansi escape codes
const wordLengths = str => str.split(' ').map(s => stringWidth(s));
const wordLengths = string => string.split(' ').map(character => stringWidth(character));
// Wrap a long word across multiple rows
// Ansi escape codes do not count towards length
const wrapWord = (rows, word, cols) => {
const arr = Array.from(word);
const wrapWord = (rows, word, columns) => {
const characters = [...word];

@@ -56,17 +29,15 @@ let insideEscape = false;

for (const item of arr.entries()) {
const i = item[0];
const char = item[1];
const charLength = stringWidth(char);
for (const [index, character] of characters.entries()) {
const characterLength = stringWidth(character);
if (visible + charLength <= cols) {
rows[rows.length - 1] += char;
if (visible + characterLength <= columns) {
rows[rows.length - 1] += character;
} else {
rows.push(char);
rows.push(character);
visible = 0;
}
if (ESCAPES.has(char)) {
if (ESCAPES.has(character)) {
insideEscape = true;
} else if (insideEscape && char === 'm') {
} else if (insideEscape && character === 'm') {
insideEscape = false;

@@ -80,5 +51,5 @@ continue;

visible += charLength;
visible += characterLength;
if (visible === cols && i < arr.length - 1) {
if (visible === columns && index < characters.length - 1) {
rows.push('');

@@ -100,10 +71,8 @@ visible = 0;

// 'hard' will never allow a string to take up more
// than cols characters
// than columns characters
//
// 'soft' allows long words to expand past the column length
const exec = (str, cols, opts) => {
const options = opts || {};
if (str.trim() === '') {
return options.trim === false ? str : str.trim();
const exec = (string, columns, options = {}) => {
if (string.trim() === '') {
return options.trim === false ? string : string.trim();
}

@@ -115,10 +84,6 @@

const lengths = wordLengths(str);
const words = str.split(' ');
const lengths = wordLengths(string);
const rows = [''];
for (const item of Array.from(words).entries()) {
const i = item[0];
const word = item[1];
for (const [index, word] of string.split(' ').entries()) {
rows[rows.length - 1] = options.trim === false ? rows[rows.length - 1] : rows[rows.length - 1].trim();

@@ -128,3 +93,3 @@ let rowLength = stringWidth(rows[rows.length - 1]);

if (rowLength || word === '') {
if (rowLength === cols && options.wordWrap === false) {
if (rowLength === columns && options.wordWrap === false) {
// If we start with a new word but the current row length equals the length of the columns, add a new row

@@ -140,14 +105,14 @@ rows.push('');

// In 'hard' wrap mode, the length of a line is
// never allowed to extend past 'cols'
if (lengths[i] > cols && options.hard) {
// never allowed to extend past 'columns'
if (lengths[index] > columns && options.hard) {
if (rowLength) {
rows.push('');
}
wrapWord(rows, word, cols);
wrapWord(rows, word, columns);
continue;
}
if (rowLength + lengths[i] > cols && rowLength > 0) {
if (options.wordWrap === false && rowLength < cols) {
wrapWord(rows, word, cols);
if (rowLength + lengths[index] > columns && rowLength > 0) {
if (options.wordWrap === false && rowLength < columns) {
wrapWord(rows, word, columns);
continue;

@@ -159,4 +124,4 @@ }

if (rowLength + lengths[i] > cols && options.wordWrap === false) {
wrapWord(rows, word, cols);
if (rowLength + lengths[index] > columns && options.wordWrap === false) {
wrapWord(rows, word, columns);
continue;

@@ -168,21 +133,18 @@ }

pre = rows.map(r => options.trim === false ? r : r.trim()).join('\n');
pre = rows.map(row => options.trim === false ? row : row.trim()).join('\n');
for (const item of Array.from(pre).entries()) {
const i = item[0];
const char = item[1];
for (const [index, character] of [...pre].entries()) {
ret += character;
ret += char;
if (ESCAPES.has(char)) {
const code = parseFloat(/\d[^m]*/.exec(pre.slice(i, i + 4)));
if (ESCAPES.has(character)) {
const code = parseFloat(/\d[^m]*/.exec(pre.slice(index, index + 4)));
escapeCode = code === END_CODE ? null : code;
}
const code = ESCAPE_CODES.get(Number(escapeCode));
const code = ansiStyles.codes.get(Number(escapeCode));
if (escapeCode && code) {
if (pre[i + 1] === '\n') {
if (pre[index + 1] === '\n') {
ret += wrapAnsi(code);
} else if (char === '\n') {
} else if (character === '\n') {
ret += wrapAnsi(escapeCode);

@@ -197,8 +159,8 @@ }

// For each newline, invoke the method separately
module.exports = (str, cols, opts) => {
return String(str)
module.exports = (string, columns, options) => {
return String(string)
.normalize()
.split('\n')
.map(line => exec(line, cols, opts))
.map(line => exec(line, columns, options))
.join('\n');
};
{
"name": "wrap-ansi",
"version": "3.0.1",
"description": "Wordwrap a string with ANSI escape codes",
"license": "MIT",
"repository": "chalk/wrap-ansi",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"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"
}
"name": "wrap-ansi",
"version": "4.0.0",
"description": "Wordwrap a string with ANSI escape codes",
"license": "MIT",
"repository": "chalk/wrap-ansi",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=6"
},
"scripts": {
"test": "xo && nyc ava"
},
"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": {
"ansi-styles": "^3.2.0",
"string-width": "^2.1.1",
"strip-ansi": "^4.0.0"
},
"devDependencies": {
"ava": "^0.25.0",
"chalk": "^2.0.1",
"coveralls": "^3.0.0",
"has-ansi": "^3.0.0",
"nyc": "^13.0.1",
"strip-ansi": "^4.0.0",
"xo": "^0.22.0"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc