Skip to content

Commit

Permalink
Fix width calc w/ certain new ANSI escapes
Browse files Browse the repository at this point in the history
Fixes width calculations that were off when table contained terminal
hyperlink ANSI escape codes, which are some newer kind of ANSI escape
codes, supported by some terminals, such as iTerm.

This was accomplished by replacing regexes with use of the
[strip-ansi](https://www.npmjs.com/package/strip-ansi) module, which
presumably has more up-to-date regexes that know how to deal with these
newer ANSI escape codes.
  • Loading branch information
msabramo committed Sep 25, 2021
1 parent 89392b2 commit 631c19d
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 26 deletions.
27 changes: 23 additions & 4 deletions examples/revs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

var Table = require('../lib');
var hyperlinker = require('hyperlinker');

/**
* Example.
Expand All @@ -22,12 +23,16 @@ table.push(

console.log(table.toString());


/* compact */
var table = new Table({
head: ['Rel', 'Change', 'By', 'When']
, colWidths: [6, 21, 25, 17]
, style : {compact : true, 'padding-left' : 1}
head: ['Rel', 'Change', 'By', 'Link', 'When']
, style: {
'padding-left': 1
, 'padding-right': 1
, head: []
, border: []
}
, colWidths: [6, 21, 25, 17, 17]
});

table.push(
Expand All @@ -39,6 +44,20 @@ table.push(

console.log(table.toString());

/* with hyperlinks */
var table = new Table({
head: ['Rel', 'Change', 'By', 'Link', 'When']
, colWidths: [6, 21, 25, 17, 17]
, style : {compact : true, 'padding-left' : 1}
});

table.push(
['v0.1', 'testing something cool', '[email protected]', hyperlinker('link', 'https://adobe.com'), '7 minutes ago']
, ['v0.1', 'testing something cool', '[email protected]', hyperlinker('link', 'https://adobe.com'), '8 minutes ago']
);

console.log(table.toString());

/* headless */
var headless_table = new Table();
headless_table.push(['v0.1', 'Testing something cool', '[email protected]', '7 minutes ago']);
Expand Down
4 changes: 2 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var stripAnsi = require('strip-ansi');

/**
* Repeats a string.
Expand Down Expand Up @@ -80,8 +81,7 @@ exports.options = options;
// see: http://en.wikipedia.org/wiki/ANSI_escape_code
//
exports.strlen = function(str){
var code = /\u001b\[(?:\d*;){0,5}\d*m/g;
var stripped = ("" + str).replace(code,'');
var stripped = stripAnsi(str);
var split = stripped.split("\n");
return split.reduce(function (memo, s) { return (s.length > memo) ? s.length : memo }, 0);
}
52 changes: 33 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
"table"
],
"dependencies": {
"colors": "1.0.3"
"colors": "1.0.3",
"strip-ansi": "^6.0.1"
},
"devDependencies": {
"eslint": "^7.23.0",
"eslint-plugin-json": "^2.1.2",
"eslint-plugin-no-async-foreach": "^0.1.1",
"expresso": "~0.9",
"hyperlinker": "^1.0.0",
"should": "~0.6"
},
"main": "lib",
Expand Down
32 changes: 32 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

require('should');
var hyperlinker = require('hyperlinker');

var Table = require('../');

Expand Down Expand Up @@ -43,6 +44,37 @@ module.exports = {
table.toString().should.eql(expected.join("\n"));
},

'test table with ANSI hyperlink escape codes': function (){
var table = new Table({
head: ['Rel', 'Change', 'By', 'Link', 'When']
, colWidths: [6, 21, 25, 17, 17]
, style : {compact : true, 'padding-left' : 1, head: [], border: []}
});

table.push(
['v0.1', 'Testing something cool', '[email protected]', hyperlinker('link', 'https://adobe.com'), '7 minutes ago']
, ['v0.1', 'Testing something cool', '[email protected]', hyperlinker('link', 'https://adobe.com'), '8 minutes ago']
);

var expected = [
'┌──────┬─────────────────────┬─────────────────────────┬─────────────────┬─────────────────┐'
, '│ Rel │ Change │ By │ Link │ When │'
, '├──────┼─────────────────────┼─────────────────────────┼─────────────────┼─────────────────┤'
, '│ v0.1 │ Testing something … │ [email protected] │ \x1B]8;;https://adobe.com\x07link\x1B]8;;\x07 │ 7 minutes ago │'
, '│ v0.1 │ Testing something … │ [email protected] │ \x1B]8;;https://adobe.com\x07link\x1B]8;;\x07 │ 8 minutes ago │'
, '└──────┴─────────────────────┴─────────────────────────┴─────────────────┴─────────────────┘'
// '┌──────┬─────────────────────┬─────────────────────────┬─────────────────┐'
// , '│ Rel │ Change │ By │ When │'
// , '├──────┼─────────────────────┼─────────────────────────┼─────────────────┤'
// , '│ v0.1 │ Testing something … │ [email protected] │ 7 minutes ago │'
// , '├──────┼─────────────────────┼─────────────────────────┼─────────────────┤'
// , '│ v0.1 │ Testing something … │ [email protected] │ 8 minutes ago │'
// , '└──────┴─────────────────────┴─────────────────────────┴─────────────────┘'
];

table.toString().should.eql(expected.join("\n"));
},

'test width property': function (){
var table = new Table({
head: ['Cool'],
Expand Down

0 comments on commit 631c19d

Please sign in to comment.