github-pages-deploy-action/node_modules/ansi-escapes/index.js

132 lines
2.7 KiB
JavaScript
Raw Normal View History

2020-01-28 13:07:56 +08:00
'use strict';
2020-03-22 05:13:25 +08:00
const x = module.exports;
2020-01-28 13:07:56 +08:00
const ESC = '\u001B[';
const OSC = '\u001B]';
const BEL = '\u0007';
const SEP = ';';
const isTerminalApp = process.env.TERM_PROGRAM === 'Apple_Terminal';
2020-03-22 05:13:25 +08:00
x.cursorTo = (x, y) => {
2020-01-28 13:07:56 +08: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';
};
2020-03-22 05:13:25 +08:00
x.cursorMove = (x, y) => {
2020-01-28 13:07:56 +08: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;
};
2020-03-22 05:13:25 +08:00
x.cursorUp = count => ESC + (typeof count === 'number' ? count : 1) + 'A';
x.cursorDown = count => ESC + (typeof count === 'number' ? count : 1) + 'B';
x.cursorForward = count => ESC + (typeof count === 'number' ? count : 1) + 'C';
x.cursorBackward = count => ESC + (typeof count === 'number' ? count : 1) + 'D';
x.cursorLeft = ESC + 'G';
x.cursorSavePosition = ESC + (isTerminalApp ? '7' : 's');
x.cursorRestorePosition = ESC + (isTerminalApp ? '8' : 'u');
x.cursorGetPosition = ESC + '6n';
x.cursorNextLine = ESC + 'E';
x.cursorPrevLine = ESC + 'F';
x.cursorHide = ESC + '?25l';
x.cursorShow = ESC + '?25h';
x.eraseLines = count => {
2020-01-28 13:07:56 +08:00
let clear = '';
for (let i = 0; i < count; i++) {
2020-03-22 05:13:25 +08:00
clear += x.eraseLine + (i < count - 1 ? x.cursorUp() : '');
2020-01-28 13:07:56 +08:00
}
if (count) {
2020-03-22 05:13:25 +08:00
clear += x.cursorLeft;
2020-01-28 13:07:56 +08:00
}
return clear;
};
2020-03-22 05:13:25 +08:00
x.eraseEndLine = ESC + 'K';
x.eraseStartLine = ESC + '1K';
x.eraseLine = ESC + '2K';
x.eraseDown = ESC + 'J';
x.eraseUp = ESC + '1J';
x.eraseScreen = ESC + '2J';
x.scrollUp = ESC + 'S';
x.scrollDown = ESC + 'T';
2020-01-28 13:07:56 +08:00
2020-03-22 05:13:25 +08:00
x.clearScreen = '\u001Bc';
2020-01-28 13:07:56 +08:00
2020-03-22 05:13:25 +08:00
x.clearTerminal = process.platform === 'win32' ?
`${x.eraseScreen}${ESC}0f` :
2020-01-28 13:07:56 +08: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
2020-03-22 05:13:25 +08:00
`${x.eraseScreen}${ESC}3J${ESC}H`;
2020-01-28 13:07:56 +08:00
2020-03-22 05:13:25 +08:00
x.beep = BEL;
2020-01-28 13:07:56 +08:00
2020-03-22 05:13:25 +08:00
x.link = (text, url) => {
2020-01-28 13:07:56 +08:00
return [
OSC,
'8',
SEP,
SEP,
url,
BEL,
text,
OSC,
'8',
SEP,
SEP,
BEL
].join('');
};
2020-03-22 05:13:25 +08:00
x.image = (buf, opts) => {
opts = opts || {};
let ret = OSC + '1337;File=inline=1';
2020-01-28 13:07:56 +08:00
2020-03-22 05:13:25 +08:00
if (opts.width) {
ret += `;width=${opts.width}`;
2020-01-28 13:07:56 +08:00
}
2020-03-22 05:13:25 +08:00
if (opts.height) {
ret += `;height=${opts.height}`;
2020-01-28 13:07:56 +08:00
}
2020-03-22 05:13:25 +08:00
if (opts.preserveAspectRatio === false) {
2020-01-28 13:07:56 +08:00
ret += ';preserveAspectRatio=0';
}
2020-03-22 05:13:25 +08:00
return ret + ':' + buf.toString('base64') + BEL;
2020-01-28 13:07:56 +08:00
};
2020-03-22 05:13:25 +08:00
x.iTerm = {};
2020-01-28 13:07:56 +08:00
2020-03-22 05:13:25 +08:00
x.iTerm.setCwd = cwd => OSC + '50;CurrentDir=' + (cwd || process.cwd()) + BEL;