Fixing some of requested changes for pull request
This commit is contained in:
parent
4c26f9128d
commit
eb2565ce3d
6 changed files with 63 additions and 23 deletions
24
benchmark.js
24
benchmark.js
|
|
@ -21,4 +21,28 @@ suite('chalk', () => {
|
||||||
bench('nested styles', () => {
|
bench('nested styles', () => {
|
||||||
chalk.red('the fox jumps', chalk.underline.bgBlue('over the lazy dog') + '!');
|
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') + '!');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
6
index.js
6
index.js
|
|
@ -24,7 +24,7 @@ function applyOptions(obj, options) {
|
||||||
obj.enabled = 'enabled' in options ? options.enabled : obj.level > 0;
|
obj.enabled = 'enabled' in options ? options.enabled : obj.level > 0;
|
||||||
|
|
||||||
// By default, wrappers are empty strings
|
// 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;
|
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
|
// Replace any instances already present with a re-opening code
|
||||||
// otherwise only the part of the string until said closing code
|
// otherwise only the part of the string until said closing code
|
||||||
// will be colored, and the rest will simply be 'plain'.
|
// will be colored, and the rest will simply be 'plain'.
|
||||||
const open = this.wrapper.pre + code.open + this.wrapper.post;
|
const open = this.wrapper.before + code.open + this.wrapper.after;
|
||||||
const close = this.wrapper.pre + code.close + this.wrapper.post;
|
const close = this.wrapper.before + code.close + this.wrapper.after;
|
||||||
|
|
||||||
str = open + str.replace(code.closeRe, code.open) + close;
|
str = open + str.replace(code.closeRe, code.open) + close;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
"version": "2.3.1",
|
"version": "2.3.1",
|
||||||
"description": "Terminal string styling done right",
|
"description": "Terminal string styling done right",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": "chalk/chalk",
|
"repository": "https://github.com/felipenmoura/chalk",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=4"
|
"node": ">=4"
|
||||||
},
|
},
|
||||||
|
|
|
||||||
29
readme.md
29
readme.md
|
|
@ -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.
|
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 `""`.
|
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:
|
For example:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const ctx = new chalk.constructor({wrapper: {
|
const ctx = new chalk.constructor({
|
||||||
pre: '>',
|
wrapper: {
|
||||||
post: '<',
|
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
|
### chalk.supportsColor
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,12 +5,14 @@ import test from 'ava';
|
||||||
// Spoof supports-color
|
// Spoof supports-color
|
||||||
require('./_supports-color')(__dirname);
|
require('./_supports-color')(__dirname);
|
||||||
|
|
||||||
const m = require('..');
|
const Chalk = require('..');
|
||||||
|
|
||||||
m.wrapper = {
|
const m = new Chalk.constructor({
|
||||||
pre: '@',
|
wrapper: {
|
||||||
post: '#'
|
before: '@',
|
||||||
};
|
after: '#'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
test('add wrapper to underline', t => {
|
test('add wrapper to underline', t => {
|
||||||
t.is(m.underline('foo'), '@\u001B[4m#foo@\u001B[24m#');
|
t.is(m.underline('foo'), '@\u001B[4m#foo@\u001B[24m#');
|
||||||
|
|
|
||||||
11
types/index.d.ts
vendored
11
types/index.d.ts
vendored
|
|
@ -11,12 +11,10 @@ export const enum Level {
|
||||||
export interface ChalkOptions {
|
export interface ChalkOptions {
|
||||||
enabled?: boolean;
|
enabled?: boolean;
|
||||||
level?: Level;
|
level?: Level;
|
||||||
wrapper?: Wrapper;
|
wrapper?: {
|
||||||
}
|
before: String,
|
||||||
|
after: String
|
||||||
export interface Wrapper {
|
}
|
||||||
pre: String,
|
|
||||||
post: String
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ChalkConstructor {
|
export interface ChalkConstructor {
|
||||||
|
|
@ -37,7 +35,6 @@ export interface Chalk {
|
||||||
constructor: ChalkConstructor;
|
constructor: ChalkConstructor;
|
||||||
enabled: boolean;
|
enabled: boolean;
|
||||||
level: Level;
|
level: Level;
|
||||||
wrapper: Wrapper;
|
|
||||||
rgb(r: number, g: number, b: number): this;
|
rgb(r: number, g: number, b: number): this;
|
||||||
hsl(h: number, s: number, l: number): this;
|
hsl(h: number, s: number, l: number): this;
|
||||||
hsv(h: number, s: number, v: number): this;
|
hsv(h: number, s: number, v: number): this;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue