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 width calc w/ certain new ANSI escapes #149

Merged
merged 1 commit into from
Nov 17, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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', 'rauchg@gmail.com', hyperlinker('link', 'https://adobe.com'), '7 minutes ago']
, ['v0.1', 'testing something cool', 'rauchg@gmail.com', 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', 'rauchg@gmail.com', '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', 'rauchg@gmail.com', hyperlinker('link', 'https://adobe.com'), '7 minutes ago']
, ['v0.1', 'Testing something cool', 'rauchg@gmail.com', hyperlinker('link', 'https://adobe.com'), '8 minutes ago']
);

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

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

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