@eagleoutice/flowr-dev
Version:
Static Dataflow Analyzer and Program Slicer for the R Programming Language
156 lines • 6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatter = exports.ansiFormatter = exports.escape = exports.markdownFormatter = exports.voidFormatter = exports.ColorEffect = void 0;
exports.italic = italic;
exports.bold = bold;
exports.color = color;
exports.faint = faint;
exports.ansiInfo = ansiInfo;
exports.stripAnsi = stripAnsi;
exports.supportsHyperlinks = supportsHyperlinks;
exports.setFormatter = setFormatter;
// noinspection JSUnusedGlobalSymbols
var ColorEffect;
(function (ColorEffect) {
ColorEffect[ColorEffect["Foreground"] = 30] = "Foreground";
ColorEffect[ColorEffect["Background"] = 40] = "Background";
})(ColorEffect || (exports.ColorEffect = ColorEffect = {}));
exports.voidFormatter = new class {
format(input) {
return input;
}
getFormatString(_options) {
return '';
}
reset() {
return '';
}
hyperlink(text, url, force = false) {
return force ? text : url;
}
}();
exports.markdownFormatter = new class {
format(input, options) {
if (options && 'style' in options) {
if (options.style === 1 /* FontStyles.Bold */) {
input = `**${input}**`;
}
else if (options.style === 3 /* FontStyles.Italic */ || options.style === 2 /* FontStyles.Faint */) {
input = `_${input}_`;
}
else {
throw new Error(`Unsupported font style: ${String(options.style)}`);
}
}
let source = input.replaceAll(/\\"/g, '\'').replaceAll(/\\/g, '\\\\').replaceAll(/\n/g, '\\\n');
/* repeatedly replace all spaces but only at the beginning of a line */
let target = source;
do {
source = target;
/* or replace back to front */
target = source.replace(/^(?<leading>( )*) /m, '$<leading> ');
} while (target !== source);
return target;
}
getFormatString(_options) {
return '';
}
reset() {
return '';
}
hyperlink(text, url, _force = false) {
return `[${text}](${url})`;
}
}();
/**
* This does not work if the {@link setFormatter|formatter} is void. Tries to format the text with a bold font weight.
*/
function italic(s, f = exports.formatter, options) {
return f.format(s, { style: 3 /* FontStyles.Italic */, ...options });
}
/**
* This does not work if the {@link setFormatter|formatter} is void. Tries to format the text with an italic font shape.
*/
function bold(s, f = exports.formatter, options) {
return f.format(s, { style: 1 /* FontStyles.Bold */, ...options });
}
/**
* Color the text in the given foreground color.
*/
function color(s, c, f = exports.formatter, opts) {
return f.format(s, { color: c, effect: ColorEffect.Foreground, ...opts });
}
/** Faint/dim ("grayed out") text. */
function faint(s, f = exports.formatter, options) {
return f.format(s, { style: 2 /* FontStyles.Faint */, ...options });
}
/**
* This does not work if the {@link setFormatter|formatter} is void. Tries to format the text as informational message.
*/
function ansiInfo(s, f = exports.formatter) {
return f.format(s, { color: 7 /* Colors.White */, effect: ColorEffect.Foreground, style: 3 /* FontStyles.Italic */ });
}
exports.escape = '\x1b[';
/** every {@link escape} sequence, so a place that cannot colour (a browser, a log file) can drop them */
// eslint-disable-next-line no-control-regex -- the escape character is what an escape sequence starts with
const AnsiSequence = /\x1b\[[\d;]*m/g;
/** The text without any colouring, for wherever an escape sequence would only show up as `[0m`. */
function stripAnsi(text) {
return text.replaceAll(AnsiSequence, '');
}
const colorSuffix = 'm';
let hyperlinkSupport;
/** best-effort, env-based OSC 8 hyperlink support (`FORCE_HYPERLINK`/`NO_HYPERLINK` override; unknown terminals are treated as unsupported) */
function supportsHyperlinks() {
if (hyperlinkSupport !== undefined) {
return hyperlinkSupport;
}
const env = process.env;
if (env.FORCE_HYPERLINK !== undefined) {
return (hyperlinkSupport = env.FORCE_HYPERLINK !== '' && env.FORCE_HYPERLINK !== '0');
}
if (env.NO_HYPERLINK !== undefined || !process.stdout.isTTY) {
return (hyperlinkSupport = false);
}
const known = Boolean(env.WT_SESSION || env.KITTY_WINDOW_ID || env.KONSOLE_VERSION || env.DOMTERM)
|| (env.TERM_PROGRAM !== undefined && ['iTerm.app', 'WezTerm', 'vscode', 'ghostty', 'Hyper', 'rio'].includes(env.TERM_PROGRAM))
|| (env.VTE_VERSION !== undefined && Number(env.VTE_VERSION) >= 5000)
|| (env.TERM !== undefined && /kitty|wezterm|ghostty/i.test(env.TERM));
return (hyperlinkSupport = known);
}
exports.ansiFormatter = {
reset() {
return `${exports.escape}0${colorSuffix}`;
},
hyperlink(text, url, force = false) {
if (supportsHyperlinks()) {
return `\x1b]8;;${url}\x07${text}\x1b]8;;\x07`;
}
return force ? text : url;
},
format(input, options) {
return `${this.getFormatString(options)}${input}${this.reset()}`;
},
getFormatString(options) {
if (options === undefined) {
return '';
}
const params = [];
if ('style' in options) {
const styles = Array.isArray(options.style) ? options.style : [options.style];
params.push(...styles);
}
if ('color' in options) {
params.push((options.bright ? options.effect + 60 : options.effect) + options.color);
}
return params.length === 0 ? '' : `${exports.escape}${params.join(';')}${colorSuffix}`;
}
};
exports.formatter = exports.ansiFormatter;
/**
* (Globally) sets the output formatter used by the utility functions in this module.
*/
function setFormatter(setFormatter) {
exports.formatter = setFormatter;
}
//# sourceMappingURL=ansi.js.map