@log-rush/log-formatter
Version:
Parse/Format/Style colored logs
207 lines • 8.28 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var _a;
import { Color256, Color8, EffectKey, EFFECTS, BlinkEffect, ColorEffect, ColorModeEffect, ConcealedEffect, CrossedOutEffect, ItalicEffect, NegativeEffect, TextWeightEffect, UnderlineEffect, } from './effects';
import { DefaultSGREffects } from './types';
var applyEffect = function (key, value) {
return function (command) {
var _a;
if (command.startsWith(ChainCommandCharacter) || command.length === 0) {
return {
matches: true,
remainingCommand: command.substring(1),
alteredEffects: (_a = {},
_a[key] = value,
_a),
};
}
return {
matches: false,
};
};
};
var setEffects = function (effects) {
return function (command) {
if (command.startsWith(ChainCommandCharacter) || command.length === 0) {
return {
matches: true,
remainingCommand: command.substring(1),
alteredEffects: __assign({}, effects),
};
}
return {
matches: false,
};
};
};
var setEffectsSetter = function (valueSetter) {
return function (command) {
var result = valueSetter(command);
if (result) {
return {
matches: true,
remainingCommand: result.remaining,
alteredEffects: __assign({}, result.effects),
};
}
return { matches: false };
};
};
var createParseColorResultMapper = function (type) { return function (command) {
var _a;
var result = parseColor(command);
if (!result)
return undefined;
return {
effects: (_a = {},
_a[type] = result.color,
_a["".concat(type, "Mode")] = result.mode,
_a),
remaining: result.remaining,
};
}; };
var ChainCommandCharacter = EFFECTS[EffectKey.ChainCommand];
/**
* @internal
*/
export var parseColor = function (command) {
if (command.startsWith(EFFECTS[EffectKey.ColorModeRGB])) {
var result = parseRGBColor(command.slice(EFFECTS[EffectKey.ColorModeRGB].length));
if (!result)
return undefined;
return __assign(__assign({}, result), { mode: ColorModeEffect[EffectKey.ColorModeRGB] });
}
if (command.startsWith(EFFECTS[EffectKey.ColorMode256])) {
var result = parse256Color(command.slice(EFFECTS[EffectKey.ColorMode256].length));
if (!result)
return undefined;
return __assign(__assign({}, result), { mode: ColorModeEffect[EffectKey.ColorMode256] });
}
if (command.startsWith(EFFECTS[EffectKey.ColorMode8])) {
var result = parse8Color(command.slice(EFFECTS[EffectKey.ColorMode8].length));
if (!result)
return undefined;
return __assign(__assign({}, result), { mode: ColorModeEffect[EffectKey.ColorMode8] });
}
return undefined;
};
/**
* @internal
*/
export var parseRGBColor = function (command) {
var amountsOfChainCharacters = command.split('').reduce(function (sum, char) { return sum + +(char === ChainCommandCharacter); }, 0);
if (amountsOfChainCharacters >= 2) {
var _a = command.split(ChainCommandCharacter), r = _a[0], g = _a[1], b = _a[2], rest = _a.slice(3);
var _b = [r, g, b].map(function (str) { return parseInt(str, 10); }), numR = _b[0], numG = _b[1], numB = _b[2];
if ([numR, numG, numB].some(isNaN))
return undefined;
if ([numR, numG, numB].some(function (num) { return num > 255; }))
return undefined;
return {
color: "#".concat(numR.toString(16).padStart(2, '0')).concat(numG.toString(16).padStart(2, '0')).concat(numB
.toString(16)
.padStart(2, '0')),
remaining: rest.join(ChainCommandCharacter),
};
}
else {
return undefined;
}
};
/**
* @internal
*/
export var parse256Color = function (command) {
if (command.length < 4 && Color256.some(function (color) { return color === command; })) {
return {
color: command,
remaining: '',
};
}
else if (command.includes(ChainCommandCharacter)) {
var index = command.indexOf(ChainCommandCharacter);
var result = parse256Color(command.substring(0, index));
return result
? {
color: result.color,
remaining: command.substring(index + 1),
}
: undefined;
}
};
/**
* @internal
*/
export var parse8Color = function (command) {
if (command.length === 1 && Color8.some(function (color) { return command === EFFECTS[color]; })) {
return {
color: command,
remaining: '',
};
}
else if (command.length > 1 && command.includes(ChainCommandCharacter)) {
var index = command.indexOf(ChainCommandCharacter);
var result = parse8Color(command.substring(0, index));
return result
? {
color: result.color,
remaining: command.substring(index + 1),
}
: undefined;
}
return undefined;
};
/**
* @internal
*/
export var CommandParserMap = (_a = {},
_a[EffectKey.Reset] = function (command) {
if (command.startsWith(ChainCommandCharacter) || command.length === 0) {
return {
matches: true,
alteredEffects: DefaultSGREffects,
remainingCommand: command.substring(1),
};
}
return {
matches: false,
};
},
_a[EffectKey.Bold] = applyEffect('weight', TextWeightEffect[EffectKey.Bold]),
_a[EffectKey.Faint] = applyEffect('weight', TextWeightEffect[EffectKey.Faint]),
_a[EffectKey.Italic] = applyEffect('italic', ItalicEffect[EffectKey.Italic]),
_a[EffectKey.Underline] = applyEffect('underline', UnderlineEffect[EffectKey.Underline]),
_a[EffectKey.BlinkSlow] = applyEffect('blink', BlinkEffect[EffectKey.BlinkSlow]),
_a[EffectKey.BlinkRapid] = applyEffect('blink', BlinkEffect[EffectKey.BlinkRapid]),
_a[EffectKey.NegativeImage] = applyEffect('inverted', NegativeEffect[EffectKey.NegativeImage]),
_a[EffectKey.ConcealedCharacters] = applyEffect('concealed', ConcealedEffect[EffectKey.ConcealedCharacters]),
_a[EffectKey.CrossedOut] = applyEffect('crossedOut', CrossedOutEffect[EffectKey.CrossedOut]),
_a[EffectKey.DoublyUnderlined] = applyEffect('underline', UnderlineEffect[EffectKey.DoublyUnderlined]),
_a[EffectKey.NormalColorAndWeight] = setEffects({
weight: TextWeightEffect.Default,
foreground: ColorEffect.Default,
background: ColorEffect.Default,
foregroundMode: ColorModeEffect.Default,
backgroundMode: ColorModeEffect.Default,
}),
_a[EffectKey.NotItalic] = applyEffect('italic', ItalicEffect[EffectKey.NotItalic]),
_a[EffectKey.NotUnderlined] = applyEffect('underline', UnderlineEffect[EffectKey.NotUnderlined]),
_a[EffectKey.Steady] = applyEffect('blink', BlinkEffect[EffectKey.Steady]),
_a[EffectKey.PositiveImage] = applyEffect('inverted', NegativeEffect[EffectKey.PositiveImage]),
_a[EffectKey.RevealedCharacters] = applyEffect('concealed', ConcealedEffect[EffectKey.RevealedCharacters]),
_a[EffectKey.NotCrossedOut] = applyEffect('crossedOut', CrossedOutEffect[EffectKey.NotCrossedOut]),
_a[EffectKey.Foreground] = setEffectsSetter(createParseColorResultMapper('foreground')),
_a[EffectKey.BrightForeground] = setEffectsSetter(createParseColorResultMapper('foreground')),
_a[EffectKey.Background] = setEffectsSetter(createParseColorResultMapper('background')),
_a[EffectKey.BrightBackground] = setEffectsSetter(createParseColorResultMapper('background')),
_a);
//# sourceMappingURL=commands.js.map