Skip to content

Commit

Permalink
fix: Replace colors with chalk.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwalton committed Jan 9, 2022
1 parent f71618e commit 76a6080
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 86 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cli-table3
cli-table3
===============================================================================

[![npm version](https://img.shields.io/npm/v/cli-table3.svg)](https://www.npmjs.com/package/cli-table3)
Expand Down Expand Up @@ -30,7 +30,7 @@ unmaintained. `cli-table3` includes all the additional features from

- Customizable characters that constitute the table.
- Color/background styling in the header through
[colors.js](https://github.com/marak/colors.js)
[chalk](https://github.com/chalk/chalk)
- Column width customization
- Text truncation based on predefined widths
- Text alignment (left, right, center)
Expand Down
2 changes: 1 addition & 1 deletion advanced-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@
```javascript
var table = new Table({colWidths:[5],style:{head:[],border:[]}});

table.push([colors.red('hello')]);
table.push([chalk.red('hello')]);

```

6 changes: 3 additions & 3 deletions basic-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@
```


##### Use ansi colors (i.e. colors.js) to style text within the cells at will, even across multiple lines
##### Use ansi colors (i.e. chalk) to style text within the cells at will, even across multiple lines
![table image](./examples/screenshots/multi-line-colors.png)
```javascript
var table = new Table({style:{border:[],header:[]}});

table.push([
colors.red('Hello\nhow\nare\nyou?'),
colors.blue('I\nam\nfine\nthanks!')
chalk.red('Hello\nhow\nare\nyou?'),
chalk.blue('I\nam\nfine\nthanks!')
]);

```
Expand Down
24 changes: 12 additions & 12 deletions examples/basic-usage-examples.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Table = require('../src/table');
const colors = require('colors/safe');
const chalk = require('chalk');

module.exports = function (runTest) {
function it(name, fn) {
Expand All @@ -18,11 +18,11 @@ module.exports = function (runTest) {
}

let expected = [
colors.gray('┌───') + colors.gray('┬───┐'),
colors.gray('│') + colors.red(' a ') + colors.gray('│') + colors.red(' b ') + colors.gray('│'),
colors.gray('├───') + colors.gray('┼───┤'),
colors.gray('│') + ' c ' + colors.gray('│') + ' d ' + colors.gray('│'),
colors.gray('└───') + colors.gray('┴───┘'),
chalk.gray('┌───') + chalk.gray('┬───┐'),
chalk.gray('│') + chalk.red(' a ') + chalk.gray('│') + chalk.red(' b ') + chalk.gray('│'),
chalk.gray('├───') + chalk.gray('┼───┤'),
chalk.gray('│') + ' c ' + chalk.gray('│') + ' d ' + chalk.gray('│'),
chalk.gray('└───') + chalk.gray('┴───┘'),
];

return [makeTable, expected, 'basic-usage-with-colors'];
Expand Down Expand Up @@ -152,21 +152,21 @@ module.exports = function (runTest) {
return [makeTable, expected];
});

it('Use ansi colors (i.e. colors.js) to style text within the cells at will, even across multiple lines', function () {
it('Use ansi colors (i.e. chalk) to style text within the cells at will, even across multiple lines', function () {
function makeTable() {
let table = new Table({ style: { border: [], header: [] } });

table.push([colors.red('Hello\nhow\nare\nyou?'), colors.blue('I\nam\nfine\nthanks!')]);
table.push([chalk.red('Hello\nhow\nare\nyou?'), chalk.blue('I\nam\nfine\nthanks!')]);

return table;
}

let expected = [
'┌───────┬─────────┐',
'│ ' + colors.red('Hello') + ' │ ' + colors.blue('I') + ' │',
'│ ' + colors.red('how') + ' │ ' + colors.blue('am') + ' │',
'│ ' + colors.red('are') + ' │ ' + colors.blue('fine') + ' │',
'│ ' + colors.red('you?') + ' │ ' + colors.blue('thanks!') + ' │',
'│ ' + chalk.red('Hello') + ' │ ' + chalk.blue('I') + ' │',
'│ ' + chalk.red('how') + ' │ ' + chalk.blue('am') + ' │',
'│ ' + chalk.red('are') + ' │ ' + chalk.blue('fine') + ' │',
'│ ' + chalk.red('you?') + ' │ ' + chalk.blue('thanks!') + ' │',
'└───────┴─────────┘',
];

Expand Down
6 changes: 3 additions & 3 deletions examples/col-and-row-span-examples.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Table = require('../src/table');
const colors = require('colors/safe');
const chalk = require('chalk');

module.exports = function (runTest) {
function it(name, fn) {
Expand Down Expand Up @@ -290,12 +290,12 @@ module.exports = function (runTest) {
style: { head: [], border: [] },
});

table.push([colors.red('hello')]);
table.push([chalk.red('hello')]);

return table;
}

let expected = ['┌─────┐', '│ ' + colors.red('he') + '… │', '└─────┘'];
let expected = ['┌─────┐', '│ ' + chalk.red('he') + '… │', '└─────┘'];

return [makeTable, expected, 'truncation-with-colors'];
});
Expand Down
4 changes: 2 additions & 2 deletions lib/print-example.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/* eslint-env jest */
/* eslint-disable no-console */

const colors = require('colors/safe');
const chalk = require('chalk');
const fs = require('fs');

function logExample(fn) {
runPrintingExample(
fn,
function logName(name) {
console.log(colors.gray('========= ') + name + colors.gray(' ================'));
console.log(chalk.gray('========= ') + name + chalk.gray(' ================'));
},
console.log, //logTable
console.log, //logCode
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"devDependencies": {
"ansi-256-colors": "^1.1.0",
"chalk": "^4.1.2",
"cli-table": "^0.3.1",
"eslint-config-prettier": "^6.0.0",
"eslint-plugin-prettier": "^3.0.0",
Expand All @@ -26,7 +27,7 @@
"prettier": "2.3.2"
},
"optionalDependencies": {
"colors": "^1.1.2"
"chalk": "^4.1.2"
},
"scripts": {
"changelog": "lerna-changelog",
Expand Down
6 changes: 3 additions & 3 deletions src/cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ class Cell {
wrapWithStyleColors(styleProperty, content) {
if (this[styleProperty] && this[styleProperty].length) {
try {
let colors = require('colors/safe');
let chalk = require('chalk');
for (let i = this[styleProperty].length - 1; i >= 0; i--) {
colors = colors[this[styleProperty][i]];
chalk = chalk[this[styleProperty][i]];
}
return colors(content);
return chalk(content);
} catch (e) {
return content;
}
Expand Down
24 changes: 13 additions & 11 deletions test/cell-test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
describe('Cell', function () {
const colors = require('colors');
const chalk = require('chalk');
const Cell = require('../src/cell');
const { ColSpanCell, RowSpanCell } = Cell;
const { mergeOptions } = require('../src/utils');

chalk.level = 3;

function defaultOptions() {
//overwrite coloring of head and border by default for easier testing.
return mergeOptions({ style: { head: [], border: [] } });
Expand Down Expand Up @@ -603,7 +605,7 @@ describe('Cell', function () {

it('will draw in the color specified by border style', function () {
cell.border = ['gray'];
expect(cell.draw('top')).toEqual(colors.gray('┌───────'));
expect(cell.draw('top')).toEqual(chalk.gray('┌───────'));
});
});

Expand All @@ -626,7 +628,7 @@ describe('Cell', function () {

it('will draw in the color specified by border style', function () {
cell.border = ['gray'];
expect(cell.draw('bottom')).toEqual(colors.gray('└───────'));
expect(cell.draw('bottom')).toEqual(chalk.gray('└───────'));
});
});

Expand All @@ -639,8 +641,8 @@ describe('Cell', function () {
it('draws an empty line', function () {
cell.border = ['gray'];
cell.head = ['red'];
expect(cell.drawEmpty()).toEqual(colors.gray('L') + colors.red(' '));
expect(cell.drawEmpty(true)).toEqual(colors.gray('L') + colors.red(' ') + colors.gray('R'));
expect(cell.drawEmpty()).toEqual(chalk.gray('L') + chalk.red(' '));
expect(cell.drawEmpty(true)).toEqual(chalk.gray('L') + chalk.red(' ') + chalk.gray('R'));
});
});

Expand Down Expand Up @@ -682,17 +684,17 @@ describe('Cell', function () {
it('left and right will be drawn in color of border style', function () {
cell.border = ['gray'];
cell.x = 0;
expect(cell.draw(0)).toEqual(colors.gray('L') + ' hello ');
expect(cell.draw(0)).toEqual(chalk.gray('L') + ' hello ');
cell.drawRight = true;
expect(cell.draw(0)).toEqual(colors.gray('L') + ' hello ' + colors.gray('R'));
expect(cell.draw(0)).toEqual(chalk.gray('L') + ' hello ' + chalk.gray('R'));
});

it('text will be drawn in color of head style if y == 0', function () {
cell.head = ['red'];
cell.x = cell.y = 0;
expect(cell.draw(0)).toEqual('L' + colors.red(' hello '));
expect(cell.draw(0)).toEqual('L' + chalk.red(' hello '));
cell.drawRight = true;
expect(cell.draw(0)).toEqual('L' + colors.red(' hello ') + 'R');
expect(cell.draw(0)).toEqual('L' + chalk.red(' hello ') + 'R');
});

it('text will NOT be drawn in color of head style if y == 1', function () {
Expand All @@ -707,9 +709,9 @@ describe('Cell', function () {
cell.border = ['gray'];
cell.head = ['red'];
cell.x = cell.y = 0;
expect(cell.draw(0)).toEqual(colors.gray('L') + colors.red(' hello '));
expect(cell.draw(0)).toEqual(chalk.gray('L') + chalk.red(' hello '));
cell.drawRight = true;
expect(cell.draw(0)).toEqual(colors.gray('L') + colors.red(' hello ') + colors.gray('R'));
expect(cell.draw(0)).toEqual(chalk.gray('L') + chalk.red(' hello ') + chalk.gray('R'));
});
});

Expand Down
14 changes: 8 additions & 6 deletions test/table-test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
describe('@api Table ', function () {
const Table = require('..');
const colors = require('colors/safe');
const chalk = require('chalk');

chalk.level = 3;

it('wordWrap with colored text', function () {
let table = new Table({ style: { border: [], head: [] }, wordWrap: true, colWidths: [7, 9] });

table.push([colors.red('Hello how are you?'), colors.blue('I am fine thanks!')]);
table.push([chalk.red('Hello how are you?'), chalk.blue('I am fine thanks!')]);

let expected = [
'┌───────┬─────────┐',
'│ ' + colors.red('Hello') + ' │ ' + colors.blue('I am') + ' │',
'│ ' + colors.red('how') + ' │ ' + colors.blue('fine') + ' │',
'│ ' + colors.red('are') + ' │ ' + colors.blue('thanks!') + ' │',
'│ ' + colors.red('you?') + ' │ │',
'│ ' + chalk.red('Hello') + ' │ ' + chalk.blue('I am') + ' │',
'│ ' + chalk.red('how') + ' │ ' + chalk.blue('fine') + ' │',
'│ ' + chalk.red('are') + ' │ ' + chalk.blue('thanks!') + ' │',
'│ ' + chalk.red('you?') + ' │ │',
'└───────┴─────────┘',
];

Expand Down
Loading

0 comments on commit 76a6080

Please sign in to comment.