Minor tweaks (#437)

Co-authored-by: Qix <Qix-@users.noreply.github.com>
This commit is contained in:
Richie Bendall 2021-04-22 20:03:48 +12:00 committed by GitHub
parent 4cf2e40e07
commit f8a3642a81
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 15 deletions

2
index.d.ts vendored
View file

@ -223,7 +223,7 @@ export interface ChalkInstance extends ChalkFunction {
readonly bold: this; readonly bold: this;
/** /**
Modifier: Emitting only a small amount of light. Modifier: Make text slightly darker. (Inconsistent across terminals; might do nothing)
*/ */
readonly dim: this; readonly dim: this;

View file

@ -54,7 +54,7 @@
"matcha": "^0.7.0", "matcha": "^0.7.0",
"nyc": "^15.1.0", "nyc": "^15.1.0",
"tsd": "^0.14.0", "tsd": "^0.14.0",
"xo": "^0.38.2", "xo": "^0.39.1",
"yoctodelay": "^1.2.0" "yoctodelay": "^1.2.0"
}, },
"xo": { "xo": {

View file

@ -9,6 +9,10 @@ import template from './templates.js';
const {stdout: stdoutColor, stderr: stderrColor} = supportsColor; const {stdout: stdoutColor, stderr: stderrColor} = supportsColor;
const {isArray} = Array; const {isArray} = Array;
const GENERATOR = Symbol('GENERATOR');
const STYLER = Symbol('STYLER');
const IS_EMPTY = Symbol('IS_EMPTY');
// `supportsColor.level` → `ansiStyles.color[name]` mapping // `supportsColor.level` → `ansiStyles.color[name]` mapping
const levelMapping = [ const levelMapping = [
'ansi', 'ansi',
@ -59,7 +63,7 @@ Object.setPrototypeOf(createChalk.prototype, Function.prototype);
for (const [styleName, style] of Object.entries(ansiStyles)) { for (const [styleName, style] of Object.entries(ansiStyles)) {
styles[styleName] = { styles[styleName] = {
get() { get() {
const builder = createBuilder(this, createStyler(style.open, style.close, this._styler), this._isEmpty); const builder = createBuilder(this, createStyler(style.open, style.close, this[STYLER]), this[IS_EMPTY]);
Object.defineProperty(this, styleName, {value: builder}); Object.defineProperty(this, styleName, {value: builder});
return builder; return builder;
} }
@ -68,7 +72,7 @@ for (const [styleName, style] of Object.entries(ansiStyles)) {
styles.visible = { styles.visible = {
get() { get() {
const builder = createBuilder(this, this._styler, true); const builder = createBuilder(this, this[STYLER], true);
Object.defineProperty(this, 'visible', {value: builder}); Object.defineProperty(this, 'visible', {value: builder});
return builder; return builder;
} }
@ -101,8 +105,8 @@ for (const model of usedModels) {
get() { get() {
const {level} = this; const {level} = this;
return function (...arguments_) { return function (...arguments_) {
const styler = createStyler(getModelAnsi(model, levelMapping[level], 'color', ...arguments_), ansiStyles.color.close, this._styler); const styler = createStyler(getModelAnsi(model, levelMapping[level], 'color', ...arguments_), ansiStyles.color.close, this[STYLER]);
return createBuilder(this, styler, this._isEmpty); return createBuilder(this, styler, this[IS_EMPTY]);
}; };
} }
}; };
@ -112,8 +116,8 @@ for (const model of usedModels) {
get() { get() {
const {level} = this; const {level} = this;
return function (...arguments_) { return function (...arguments_) {
const styler = createStyler(getModelAnsi(model, levelMapping[level], 'bgColor', ...arguments_), ansiStyles.bgColor.close, this._styler); const styler = createStyler(getModelAnsi(model, levelMapping[level], 'bgColor', ...arguments_), ansiStyles.bgColor.close, this[STYLER]);
return createBuilder(this, styler, this._isEmpty); return createBuilder(this, styler, this[IS_EMPTY]);
}; };
} }
}; };
@ -124,10 +128,10 @@ const proto = Object.defineProperties(() => {}, {
level: { level: {
enumerable: true, enumerable: true,
get() { get() {
return this._generator.level; return this[GENERATOR].level;
}, },
set(level) { set(level) {
this._generator.level = level; this[GENERATOR].level = level;
} }
} }
}); });
@ -168,19 +172,19 @@ const createBuilder = (self, _styler, _isEmpty) => {
// no way to create a function with a different prototype // no way to create a function with a different prototype
Object.setPrototypeOf(builder, proto); Object.setPrototypeOf(builder, proto);
builder._generator = self; builder[GENERATOR] = self;
builder._styler = _styler; builder[STYLER] = _styler;
builder._isEmpty = _isEmpty; builder[IS_EMPTY] = _isEmpty;
return builder; return builder;
}; };
const applyStyle = (self, string) => { const applyStyle = (self, string) => {
if (self.level <= 0 || !string) { if (self.level <= 0 || !string) {
return self._isEmpty ? '' : string; return self[IS_EMPTY] ? '' : string;
} }
let styler = self._styler; let styler = self[STYLER];
if (styler === undefined) { if (styler === undefined) {
return string; return string;