Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: port breakLines to wrap-ansi #1106

Merged
merged 4 commits into from Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -44,6 +44,5 @@
"jest": {
"coverageDirectory": "./coverage/",
"collectCoverage": true
},
"dependencies": {}
SBoudrias marked this conversation as resolved.
Show resolved Hide resolved
}
}
15 changes: 5 additions & 10 deletions packages/core/lib/utils.js
@@ -1,19 +1,14 @@
const wrapAnsi = require('wrap-ansi');

/**
* Force line returns at specific width. This function is ANSI code friendly and it'll
* ignore invisible codes during width calculation.
* @param {string} content
* @param {number} width
* @return {string}
*/
exports.breakLines = (content, width) => {
const regex = new RegExp('(?:(?:\\033[[0-9;]*m)*.?){1,' + width + '}', 'g');
return content
exports.breakLines = (content, width) =>
content
.split('\n')
.flatMap((line) => {
const chunk = line.match(regex);
// Remove the last match as it's always empty
chunk.pop();
return chunk || '';
})
.map((line) => wrapAnsi(line, width, { trim: false, hard: true }))
.join('\n');
};
3 changes: 2 additions & 1 deletion packages/core/package.json
Expand Up @@ -21,7 +21,8 @@
"mute-stream": "^0.0.8",
"run-async": "^2.3.0",
"string-width": "^4.1.0",
"strip-ansi": "^6.0.0"
"strip-ansi": "^6.0.0",
"wrap-ansi": "^7.0.0"
},
"publishConfig": {
"access": "public"
Expand Down
36 changes: 36 additions & 0 deletions packages/inquirer/examples/terminal-link.js
@@ -0,0 +1,36 @@
/**
* A terminal-link example. We expect no odd line breaks.
* Note: you will need a compatible terminal to see the rendered links. https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda
* For screenshots of the expected behavior, see https://github.com/SBoudrias/Inquirer.js/pull/1106
*/

'use strict';
const inquirer = require('..');
const terminalLink = require('terminal-link');

inquirer
.prompt([
{
type: 'list',
name: 'size',
message: 'What size do you need?',
choices: [
'Jumbo',
'Large',
'Standard',
'Medium',
'Small',
'Micro which is truly and surely the ' +
terminalLink(
'very very very very very very smallest',
'https://www.google.com/search?q=very+very+very+very+very+very+very+very+very+very+long'
),
],
filter(val) {
return val.toLowerCase();
},
},
])
.then((answers) => {
console.log(JSON.stringify(answers, null, ' '));
});
12 changes: 5 additions & 7 deletions packages/inquirer/lib/utils/screen-manager.js
@@ -1,6 +1,7 @@
'use strict';
const util = require('./readline');
const cliWidth = require('cli-width');
const wrapAnsi = require('wrap-ansi');
const stripAnsi = require('strip-ansi');
const stringWidth = require('string-width');
const ora = require('ora');
Expand Down Expand Up @@ -156,13 +157,10 @@ class ScreenManager {
breakLines(lines, width = this.normalizedCliWidth()) {
// Break lines who're longer than the cli width so we can normalize the natural line
// returns behavior across terminals.
const regex = new RegExp(`(?:(?:\\033[[0-9;]*m)*.?){1,${width}}`, 'g');
return lines.map((line) => {
const chunk = line.match(regex);
// Last match is always empty
chunk.pop();
return chunk || '';
});
// re: trim: false; by default, `wrap-ansi` trims whitespace, which
// is not what we want.
// re: hard: true; by default', `wrap-ansi` does soft wrapping
return lines.map((line) => wrapAnsi(line, width, { trim: false, hard: true }));
}

/**
Expand Down
6 changes: 4 additions & 2 deletions packages/inquirer/package.json
Expand Up @@ -26,7 +26,8 @@
"mocha": "^9.2.2",
"mockery": "^2.1.0",
"nyc": "^15.0.0",
"sinon": "^13.0.1"
"sinon": "^13.0.1",
"terminal-link": "^2.1.1"
},
"scripts": {
"test": "nyc mocha test/**/* -r ./test/before",
Expand All @@ -50,6 +51,7 @@
"rxjs": "^7.5.5",
"string-width": "^4.1.0",
"strip-ansi": "^6.0.0",
"through": "^2.3.6"
"through": "^2.3.6",
"wrap-ansi": "^7.0.0"
}
}