Fixing some of requested changes for pull request

This commit is contained in:
Felipe 2018-02-16 01:26:53 -02:00
parent 4c26f9128d
commit eb2565ce3d
6 changed files with 63 additions and 23 deletions

View file

@ -21,4 +21,28 @@ suite('chalk', () => {
bench('nested styles', () => {
chalk.red('the fox jumps', chalk.underline.bgBlue('over the lazy dog') + '!');
});
const wrappedChalk = new chalk.constructor({
wrapper: {
before: '@',
after: '#'
}
});
bench('wrapped single style', () => {
wrappedChalk.red('the fox jumps');
});
bench('wrapped several styles', () => {
wrappedChalk.blue.bgRed.bold('the fox jumps over the lazy dog');
});
const cachedWrapper = wrappedChalk.blue.bgRed.bold;
bench('cached wrapped styles', () => {
cachedWrapper('the fox jumps over the lazy dog');
});
bench('nested wrapped styles', () => {
wrappedChalk.red('the fox jumps', wrappedChalk.underline.bgBlue('over the lazy dog') + '!');
});
});

View file

@ -24,7 +24,7 @@ function applyOptions(obj, options) {
obj.enabled = 'enabled' in options ? options.enabled : obj.level > 0;
// By default, wrappers are empty strings
const defaultWrapper = {pre: '', post: ''};
const defaultWrapper = {before: '', after: ''};
obj.wrapper = typeof options.wrapper === 'object' ? Object.assign({}, defaultWrapper, options.wrapper) : defaultWrapper;
}
@ -196,8 +196,8 @@ function applyStyle() {
// Replace any instances already present with a re-opening code
// otherwise only the part of the string until said closing code
// will be colored, and the rest will simply be 'plain'.
const open = this.wrapper.pre + code.open + this.wrapper.post;
const close = this.wrapper.pre + code.close + this.wrapper.post;
const open = this.wrapper.before + code.open + this.wrapper.after;
const close = this.wrapper.before + code.close + this.wrapper.after;
str = open + str.replace(code.closeRe, code.open) + close;

View file

@ -3,7 +3,7 @@
"version": "2.3.1",
"description": "Terminal string styling done right",
"license": "MIT",
"repository": "chalk/chalk",
"repository": "https://github.com/felipenmoura/chalk",
"engines": {
"node": ">=4"
},

View file

@ -165,19 +165,36 @@ The wrapper marks the unprintable characters from style tags.
A wrapper can be added to the styles, so you can escape characters or add marks to then.
By default, these wrappers are empty strings `""`.
The wrappers object has two properties, `pre` and `post`.
The wrappers object has two properties, `before` and `after`.
For example:
```js
const ctx = new chalk.constructor({wrapper: {
pre: '>',
post: '<',
}});
const ctx = new chalk.constructor({
wrapper: {
before: '>',
after: '<'
}
});
ctx.red('foo') // outputs "><foo><"
ctx.red('foo') // outputs ">\u001B[31m<foo>\u001B[39m<"
```
This can be specially useful when escaping characters, using it into a _PS1_ string or debugging and outputing it into different terminals/TTYs.
This can be specially useful when escaping characters, using it into a _PS1_ string or debugging and outputing it into different terminals/TTYs.
That's because _PS1_ uses the number of _printable_ characters to know the length of the string and to position the cursor. If you don't mark the colour codes as "unprintable" by using `\[` and `\]`, those characteres will be used to determine the length of the string, mispositioning the cursor.
```js
const ctx = new chalk.constructor({
wrapper: {
before: '\\[',
after: '\\]'
}
});
ctx.red('foo') // outputs "\\[\u001B[31m\\]foo\\[\u001B[39m\\]" (marking the colour codes as unpritable)
```
That would output "foo" in red, as that is the printable result.
### chalk.supportsColor

View file

@ -5,12 +5,14 @@ import test from 'ava';
// Spoof supports-color
require('./_supports-color')(__dirname);
const m = require('..');
const Chalk = require('..');
m.wrapper = {
pre: '@',
post: '#'
};
const m = new Chalk.constructor({
wrapper: {
before: '@',
after: '#'
}
});
test('add wrapper to underline', t => {
t.is(m.underline('foo'), '@\u001B[4m#foo@\u001B[24m#');

11
types/index.d.ts vendored
View file

@ -11,12 +11,10 @@ export const enum Level {
export interface ChalkOptions {
enabled?: boolean;
level?: Level;
wrapper?: Wrapper;
}
export interface Wrapper {
pre: String,
post: String
wrapper?: {
before: String,
after: String
}
}
export interface ChalkConstructor {
@ -37,7 +35,6 @@ export interface Chalk {
constructor: ChalkConstructor;
enabled: boolean;
level: Level;
wrapper: Wrapper;
rgb(r: number, g: number, b: number): this;
hsl(h: number, s: number, l: number): this;
hsv(h: number, s: number, v: number): this;