wishthis/node_modules/ansi-escapes/index.js

158 lines
3.7 KiB
JavaScript
Raw Normal View History

2022-01-21 08:28:41 +00:00
'use strict';
2022-06-08 10:36:39 +00:00
const ansiEscapes = module.exports;
// TODO: remove this in the next major version
module.exports.default = ansiEscapes;
2022-01-21 08:28:41 +00:00
const ESC = '\u001B[';
const OSC = '\u001B]';
const BEL = '\u0007';
const SEP = ';';
const isTerminalApp = process.env.TERM_PROGRAM === 'Apple_Terminal';
2022-06-08 10:36:39 +00:00
ansiEscapes.cursorTo = (x, y) => {
2022-01-21 08:28:41 +00:00
if (typeof x !== 'number') {
throw new TypeError('The `x` argument is required');
}
if (typeof y !== 'number') {
return ESC + (x + 1) + 'G';
}
return ESC + (y + 1) + ';' + (x + 1) + 'H';
};
2022-06-08 10:36:39 +00:00
ansiEscapes.cursorMove = (x, y) => {
2022-01-21 08:28:41 +00:00
if (typeof x !== 'number') {
throw new TypeError('The `x` argument is required');
}
let ret = '';
if (x < 0) {
ret += ESC + (-x) + 'D';
} else if (x > 0) {
ret += ESC + x + 'C';
}
if (y < 0) {
ret += ESC + (-y) + 'A';
} else if (y > 0) {
ret += ESC + y + 'B';
}
return ret;
};
2022-06-08 10:36:39 +00:00
ansiEscapes.cursorUp = (count = 1) => ESC + count + 'A';
ansiEscapes.cursorDown = (count = 1) => ESC + count + 'B';
ansiEscapes.cursorForward = (count = 1) => ESC + count + 'C';
ansiEscapes.cursorBackward = (count = 1) => ESC + count + 'D';
ansiEscapes.cursorLeft = ESC + 'G';
ansiEscapes.cursorSavePosition = isTerminalApp ? '\u001B7' : ESC + 's';
ansiEscapes.cursorRestorePosition = isTerminalApp ? '\u001B8' : ESC + 'u';
ansiEscapes.cursorGetPosition = ESC + '6n';
ansiEscapes.cursorNextLine = ESC + 'E';
ansiEscapes.cursorPrevLine = ESC + 'F';
ansiEscapes.cursorHide = ESC + '?25l';
ansiEscapes.cursorShow = ESC + '?25h';
ansiEscapes.eraseLines = count => {
2022-01-21 08:28:41 +00:00
let clear = '';
for (let i = 0; i < count; i++) {
2022-06-08 10:36:39 +00:00
clear += ansiEscapes.eraseLine + (i < count - 1 ? ansiEscapes.cursorUp() : '');
2022-01-21 08:28:41 +00:00
}
if (count) {
2022-06-08 10:36:39 +00:00
clear += ansiEscapes.cursorLeft;
2022-01-21 08:28:41 +00:00
}
return clear;
};
2022-06-08 10:36:39 +00:00
ansiEscapes.eraseEndLine = ESC + 'K';
ansiEscapes.eraseStartLine = ESC + '1K';
ansiEscapes.eraseLine = ESC + '2K';
ansiEscapes.eraseDown = ESC + 'J';
ansiEscapes.eraseUp = ESC + '1J';
ansiEscapes.eraseScreen = ESC + '2J';
ansiEscapes.scrollUp = ESC + 'S';
ansiEscapes.scrollDown = ESC + 'T';
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
ansiEscapes.clearScreen = '\u001Bc';
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
ansiEscapes.clearTerminal = process.platform === 'win32' ?
`${ansiEscapes.eraseScreen}${ESC}0f` :
2022-01-21 08:28:41 +00:00
// 1. Erases the screen (Only done in case `2` is not supported)
// 2. Erases the whole screen including scrollback buffer
// 3. Moves cursor to the top-left position
// More info: https://www.real-world-systems.com/docs/ANSIcode.html
2022-06-08 10:36:39 +00:00
`${ansiEscapes.eraseScreen}${ESC}3J${ESC}H`;
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
ansiEscapes.beep = BEL;
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
ansiEscapes.link = (text, url) => {
2022-01-21 08:28:41 +00:00
return [
OSC,
'8',
SEP,
SEP,
url,
BEL,
text,
OSC,
'8',
SEP,
SEP,
BEL
].join('');
};
2022-06-08 10:36:39 +00:00
ansiEscapes.image = (buffer, options = {}) => {
let ret = `${OSC}1337;File=inline=1`;
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
if (options.width) {
ret += `;width=${options.width}`;
2022-01-21 08:28:41 +00:00
}
2022-06-08 10:36:39 +00:00
if (options.height) {
ret += `;height=${options.height}`;
2022-01-21 08:28:41 +00:00
}
2022-06-08 10:36:39 +00:00
if (options.preserveAspectRatio === false) {
2022-01-21 08:28:41 +00:00
ret += ';preserveAspectRatio=0';
}
2022-06-08 10:36:39 +00:00
return ret + ':' + buffer.toString('base64') + BEL;
2022-01-21 08:28:41 +00:00
};
2022-06-08 10:36:39 +00:00
ansiEscapes.iTerm = {
setCwd: (cwd = process.cwd()) => `${OSC}50;CurrentDir=${cwd}${BEL}`,
annotation: (message, options = {}) => {
let ret = `${OSC}1337;`;
const hasX = typeof options.x !== 'undefined';
const hasY = typeof options.y !== 'undefined';
if ((hasX || hasY) && !(hasX && hasY && typeof options.length !== 'undefined')) {
throw new Error('`x`, `y` and `length` must be defined when `x` or `y` is defined');
}
message = message.replace(/\|/g, '');
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
ret += options.isHidden ? 'AddHiddenAnnotation=' : 'AddAnnotation=';
if (options.length > 0) {
ret +=
(hasX ?
[message, options.length, options.x, options.y] :
[options.length, message]).join('|');
} else {
ret += message;
}
return ret + BEL;
}
};