Skip to content

Commit

Permalink
Merge c28e99f into 7bcd854
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholaschiasson committed Sep 20, 2019
2 parents 7bcd854 + c28e99f commit 210f189
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
23 changes: 19 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
const ansiStyles = require('ansi-styles');
const stringWidth = require('string-width');
const stripAnsi = require('strip-ansi');
const ansiStyles = require('ansi-styles');

const ESCAPES = new Set([
'\u001B',
Expand All @@ -10,6 +10,9 @@ const ESCAPES = new Set([

const END_CODE = 39;

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

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

// Calculate the length of words split on ' ', ignoring
Expand All @@ -22,6 +25,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 +40,23 @@ const wrapWord = (rows, word, columns) => {

if (ESCAPES.has(character)) {
isInsideEscape = true;
} else if (isInsideEscape && character === 'm') {
isInsideEscape = false;
continue;
/* istanbul ignore if: cannot enter on terminals not supporting hyperlinks */
if (word.slice(index).startsWith(`${character}${ANSI_ESCAPE_LINK}`)) {
isInsideLinkEscape = true;
}
}

if (isInsideEscape) {
/* istanbul ignore if: cannot enter on terminals not supporting hyperlinks */
if (isInsideLinkEscape) {
if (character === ANSI_ESCAPE_BELL) {
isInsideEscape = false;
isInsideLinkEscape = false;
}
} else if (character === 'm') {
isInsideEscape = false;
}

continue;
}

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"engines": {
"node": ">=8"
Expand Down Expand Up @@ -48,7 +48,8 @@
"dependencies": {
"ansi-styles": "^4.0.0",
"string-width": "^4.1.0",
"strip-ansi": "^5.0.0"
"strip-ansi": "^5.0.0",
"terminal-link": "^2.0.0"
},
"devDependencies": {
"ava": "^2.1.0",
Expand Down
19 changes: 19 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,21 @@ 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 = terminalLink.isSupported ?
'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' :
'hi http://js.io\nhttps://testlogi\nn:testpassword@a\nreallylongurltha\ntneedstogetwrapp\ned.com/with_some\n/path?andparams=\nbogus&more=evenm\norebogus#evensom\nehashparams=notd\noinganything';
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 = terminalLink.isSupported ?
`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` :
`hi http://js.io\n${chalk.bgGreen('https://testlogi\nn:testpassword@a\nreallylongurltha\ntneedstogetwrapp\ned.com/with_some\n/path?andparams=\nbogus&more=evenm\norebogus#evensom\nehashparams=notd\noinganything')}`;
t.is(wrapAnsi(input, 16, {hard: true}), expected);
});

0 comments on commit 210f189

Please sign in to comment.