@log-rush/log-formatter
Version:
Parse/Format/Style colored logs
214 lines • 9.32 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;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommandParserMap = exports.parse8Color = exports.parse256Color = exports.parseRGBColor = exports.parseColor = void 0;
var effects_1 = require("./effects");
var types_1 = require("./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 = (0, exports.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_1.EFFECTS[effects_1.EffectKey.ChainCommand];
/**
* @internal
*/
var parseColor = function (command) {
if (command.startsWith(effects_1.EFFECTS[effects_1.EffectKey.ColorModeRGB])) {
var result = (0, exports.parseRGBColor)(command.slice(effects_1.EFFECTS[effects_1.EffectKey.ColorModeRGB].length));
if (!result)
return undefined;
return __assign(__assign({}, result), { mode: effects_1.ColorModeEffect[effects_1.EffectKey.ColorModeRGB] });
}
if (command.startsWith(effects_1.EFFECTS[effects_1.EffectKey.ColorMode256])) {
var result = (0, exports.parse256Color)(command.slice(effects_1.EFFECTS[effects_1.EffectKey.ColorMode256].length));
if (!result)
return undefined;
return __assign(__assign({}, result), { mode: effects_1.ColorModeEffect[effects_1.EffectKey.ColorMode256] });
}
if (command.startsWith(effects_1.EFFECTS[effects_1.EffectKey.ColorMode8])) {
var result = (0, exports.parse8Color)(command.slice(effects_1.EFFECTS[effects_1.EffectKey.ColorMode8].length));
if (!result)
return undefined;
return __assign(__assign({}, result), { mode: effects_1.ColorModeEffect[effects_1.EffectKey.ColorMode8] });
}
return undefined;
};
exports.parseColor = parseColor;
/**
* @internal
*/
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;
}
};
exports.parseRGBColor = parseRGBColor;
/**
* @internal
*/
var parse256Color = function (command) {
if (command.length < 4 && effects_1.Color256.some(function (color) { return color === command; })) {
return {
color: command,
remaining: '',
};
}
else if (command.includes(ChainCommandCharacter)) {
var index = command.indexOf(ChainCommandCharacter);
var result = (0, exports.parse256Color)(command.substring(0, index));
return result
? {
color: result.color,
remaining: command.substring(index + 1),
}
: undefined;
}
};
exports.parse256Color = parse256Color;
/**
* @internal
*/
var parse8Color = function (command) {
if (command.length === 1 && effects_1.Color8.some(function (color) { return command === effects_1.EFFECTS[color]; })) {
return {
color: command,
remaining: '',
};
}
else if (command.length > 1 && command.includes(ChainCommandCharacter)) {
var index = command.indexOf(ChainCommandCharacter);
var result = (0, exports.parse8Color)(command.substring(0, index));
return result
? {
color: result.color,
remaining: command.substring(index + 1),
}
: undefined;
}
return undefined;
};
exports.parse8Color = parse8Color;
/**
* @internal
*/
exports.CommandParserMap = (_a = {},
_a[effects_1.EffectKey.Reset] = function (command) {
if (command.startsWith(ChainCommandCharacter) || command.length === 0) {
return {
matches: true,
alteredEffects: types_1.DefaultSGREffects,
remainingCommand: command.substring(1),
};
}
return {
matches: false,
};
},
_a[effects_1.EffectKey.Bold] = applyEffect('weight', effects_1.TextWeightEffect[effects_1.EffectKey.Bold]),
_a[effects_1.EffectKey.Faint] = applyEffect('weight', effects_1.TextWeightEffect[effects_1.EffectKey.Faint]),
_a[effects_1.EffectKey.Italic] = applyEffect('italic', effects_1.ItalicEffect[effects_1.EffectKey.Italic]),
_a[effects_1.EffectKey.Underline] = applyEffect('underline', effects_1.UnderlineEffect[effects_1.EffectKey.Underline]),
_a[effects_1.EffectKey.BlinkSlow] = applyEffect('blink', effects_1.BlinkEffect[effects_1.EffectKey.BlinkSlow]),
_a[effects_1.EffectKey.BlinkRapid] = applyEffect('blink', effects_1.BlinkEffect[effects_1.EffectKey.BlinkRapid]),
_a[effects_1.EffectKey.NegativeImage] = applyEffect('inverted', effects_1.NegativeEffect[effects_1.EffectKey.NegativeImage]),
_a[effects_1.EffectKey.ConcealedCharacters] = applyEffect('concealed', effects_1.ConcealedEffect[effects_1.EffectKey.ConcealedCharacters]),
_a[effects_1.EffectKey.CrossedOut] = applyEffect('crossedOut', effects_1.CrossedOutEffect[effects_1.EffectKey.CrossedOut]),
_a[effects_1.EffectKey.DoublyUnderlined] = applyEffect('underline', effects_1.UnderlineEffect[effects_1.EffectKey.DoublyUnderlined]),
_a[effects_1.EffectKey.NormalColorAndWeight] = setEffects({
weight: effects_1.TextWeightEffect.Default,
foreground: effects_1.ColorEffect.Default,
background: effects_1.ColorEffect.Default,
foregroundMode: effects_1.ColorModeEffect.Default,
backgroundMode: effects_1.ColorModeEffect.Default,
}),
_a[effects_1.EffectKey.NotItalic] = applyEffect('italic', effects_1.ItalicEffect[effects_1.EffectKey.NotItalic]),
_a[effects_1.EffectKey.NotUnderlined] = applyEffect('underline', effects_1.UnderlineEffect[effects_1.EffectKey.NotUnderlined]),
_a[effects_1.EffectKey.Steady] = applyEffect('blink', effects_1.BlinkEffect[effects_1.EffectKey.Steady]),
_a[effects_1.EffectKey.PositiveImage] = applyEffect('inverted', effects_1.NegativeEffect[effects_1.EffectKey.PositiveImage]),
_a[effects_1.EffectKey.RevealedCharacters] = applyEffect('concealed', effects_1.ConcealedEffect[effects_1.EffectKey.RevealedCharacters]),
_a[effects_1.EffectKey.NotCrossedOut] = applyEffect('crossedOut', effects_1.CrossedOutEffect[effects_1.EffectKey.NotCrossedOut]),
_a[effects_1.EffectKey.Foreground] = setEffectsSetter(createParseColorResultMapper('foreground')),
_a[effects_1.EffectKey.BrightForeground] = setEffectsSetter(createParseColorResultMapper('foreground')),
_a[effects_1.EffectKey.Background] = setEffectsSetter(createParseColorResultMapper('background')),
_a[effects_1.EffectKey.BrightBackground] = setEffectsSetter(createParseColorResultMapper('background')),
_a);
//# sourceMappingURL=commands.js.map
;