Skip to content

Commit

Permalink
Merge 89333aa into 7bcd854
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholaschiasson authored Oct 22, 2019
2 parents 7bcd854 + 89333aa commit 0c8fb6d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
18 changes: 15 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const ESCAPES = new Set([
]);

const END_CODE = 39;
const ESCAPE_TERMINATOR = 'm';

const ANSI_ESCAPE_BELL = '\u0007';
const ANSI_ESCAPE_LINK = ']8;;';

const wrapAnsi = code => `${ESCAPES.values().next().value}[${code}m`;

Expand All @@ -22,6 +26,7 @@ const wrapWord = (rows, word, columns) => {
const characters = [...word];

let isInsideEscape = false;
let isInsideLinkEscape = false;
let visible = stringWidth(stripAnsi(rows[rows.length - 1]));

for (const [index, character] of characters.entries()) {
Expand All @@ -36,12 +41,19 @@ const wrapWord = (rows, word, columns) => {

if (ESCAPES.has(character)) {
isInsideEscape = true;
} else if (isInsideEscape && character === 'm') {
isInsideEscape = false;
continue;
isInsideLinkEscape = word.indexOf(`${character}${ANSI_ESCAPE_LINK}`, index) === 0;
}

if (isInsideEscape) {
if (isInsideLinkEscape) {
if (character === ANSI_ESCAPE_BELL) {
isInsideEscape = false;
isInsideLinkEscape = false;
}
} else if (character === ESCAPE_TERMINATOR) {
isInsideEscape = false;
}

continue;
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"node": ">=8"
},
"scripts": {
"test": "xo && nyc ava"
"test": "xo && FORCE_HYPERLINK=true nyc ava"
},
"files": [
"index.js"
Expand Down Expand Up @@ -56,6 +56,7 @@
"coveralls": "^3.0.3",
"has-ansi": "^3.0.0",
"nyc": "^14.1.1",
"terminal-link": "^2.0.0",
"xo": "^0.24.0"
}
}
15 changes: 15 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import test from 'ava';
import chalk from 'chalk';
import hasAnsi from 'has-ansi';
import stripAnsi from 'strip-ansi';
import terminalLink from 'terminal-link';
import wrapAnsi from '.';

chalk.enabled = true;
Expand Down Expand Up @@ -148,3 +149,17 @@ test('#27, does not remove spaces in line with ansi escapes when no trimming', t
t.is(wrapAnsi(chalk.bgGreen(` ${chalk.black('OK')} `), 100, {trim: false}), chalk.bgGreen(` ${chalk.black('OK')} `));
t.is(wrapAnsi(chalk.bgGreen(' hello '), 10, {hard: true, trim: false}), chalk.bgGreen(' hello '));
});

test('#35, wraps hyperlinks, preserving clickability in supporting terminals', t => {
const link = 'https://testlogin:testpassword@areallylongurlthatneedstogetwrapped.com/with_some/path?andparams=bogus&more=evenmorebogus#evensomehashparams=notdoinganything';
const input = `hi http://js.io ${terminalLink(link, link, {fallback: text => text})}`;
const expected = 'hi http://js.io\n\u001B]8;;https://testlogin:testpassword@areallylongurlthatneedstogetwrapped.com/with_some/path?andparams=bogus&more=evenmorebogus#evensomehashparams=notdoinganything\u0007https://testlogi\u001B[28m\n\u001B[8mn:testpassword@a\u001B[28m\n\u001B[8mreallylongurltha\u001B[28m\n\u001B[8mtneedstogetwrapp\u001B[28m\n\u001B[8med.com/with_some\u001B[28m\n\u001B[8m/path?andparams=\u001B[28m\n\u001B[8mbogus&more=evenm\u001B[28m\n\u001B[8morebogus#evensom\u001B[28m\n\u001B[8mehashparams=notd\u001B[28m\n\u001B[8moinganything\u001B]8;;\u0007';
t.is(wrapAnsi(input, 16, {hard: true}), expected);
});

test('#35, wraps coloured hyperlinks, preserving clickability in supporting terminals', t => {
const link = 'https://testlogin:testpassword@areallylongurlthatneedstogetwrapped.com/with_some/path?andparams=bogus&more=evenmorebogus#evensomehashparams=notdoinganything';
const input = `hi http://js.io ${terminalLink(chalk.bgGreen(link), link, {fallback: text => text})}`;
const expected = `hi http://js.io\n\u001B]8;;https://testlogin:testpassword@areallylongurlthatneedstogetwrapped.com/with_some/path?andparams=bogus&more=evenmorebogus#evensomehashparams=notdoinganything\u0007${chalk.bgGreen('https://testlogi\nn:testpassword@a\nreallylongurltha\ntneedstogetwrapp\ned.com/with_some\n/path?andparams=\nbogus&more=evenm\norebogus#evensom\nehashparams=notd\noinganything')}\u001B]8;;\u0007`;
t.is(wrapAnsi(input, 16, {hard: true}), expected);
});

0 comments on commit 0c8fb6d

Please sign in to comment.