Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: port breakLines to wrap-ansi (#1106)
* fix: port breakLines to wrap-ansi This PR addresses incorrect line wrapping with `terminal-link`. It does so by employing the `wrap-ansi` npm. Fixes #1014 Fixes #304 * - update core utils.js to use wrapAnsi as well - move wrapAnsi dependence out of the top-level * revert yarn.lock * add examples/terminal-link.js
- Loading branch information
Showing
6 changed files
with
53 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,5 @@ | |
"jest": { | ||
"coverageDirectory": "./coverage/", | ||
"collectCoverage": true | ||
}, | ||
"dependencies": {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, ' ')); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters