2 const ansiEscapes = module.exports;
3 // TODO: remove this in the next major version
4 module.exports.default = ansiEscapes;
10 const isTerminalApp = process.env.TERM_PROGRAM === 'Apple_Terminal';
12 ansiEscapes.cursorTo = (x, y) => {
13 if (typeof x !== 'number') {
14 throw new TypeError('The `x` argument is required');
17 if (typeof y !== 'number') {
18 return ESC + (x + 1) + 'G';
21 return ESC + (y + 1) + ';' + (x + 1) + 'H';
24 ansiEscapes.cursorMove = (x, y) => {
25 if (typeof x !== 'number') {
26 throw new TypeError('The `x` argument is required');
32 ret += ESC + (-x) + 'D';
38 ret += ESC + (-y) + 'A';
46 ansiEscapes.cursorUp = (count = 1) => ESC + count + 'A';
47 ansiEscapes.cursorDown = (count = 1) => ESC + count + 'B';
48 ansiEscapes.cursorForward = (count = 1) => ESC + count + 'C';
49 ansiEscapes.cursorBackward = (count = 1) => ESC + count + 'D';
51 ansiEscapes.cursorLeft = ESC + 'G';
52 ansiEscapes.cursorSavePosition = isTerminalApp ? '\u001B7' : ESC + 's';
53 ansiEscapes.cursorRestorePosition = isTerminalApp ? '\u001B8' : ESC + 'u';
54 ansiEscapes.cursorGetPosition = ESC + '6n';
55 ansiEscapes.cursorNextLine = ESC + 'E';
56 ansiEscapes.cursorPrevLine = ESC + 'F';
57 ansiEscapes.cursorHide = ESC + '?25l';
58 ansiEscapes.cursorShow = ESC + '?25h';
60 ansiEscapes.eraseLines = count => {
63 for (let i = 0; i < count; i++) {
64 clear += ansiEscapes.eraseLine + (i < count - 1 ? ansiEscapes.cursorUp() : '');
68 clear += ansiEscapes.cursorLeft;
74 ansiEscapes.eraseEndLine = ESC + 'K';
75 ansiEscapes.eraseStartLine = ESC + '1K';
76 ansiEscapes.eraseLine = ESC + '2K';
77 ansiEscapes.eraseDown = ESC + 'J';
78 ansiEscapes.eraseUp = ESC + '1J';
79 ansiEscapes.eraseScreen = ESC + '2J';
80 ansiEscapes.scrollUp = ESC + 'S';
81 ansiEscapes.scrollDown = ESC + 'T';
83 ansiEscapes.clearScreen = '\u001Bc';
85 ansiEscapes.clearTerminal = process.platform === 'win32' ?
86 `${ansiEscapes.eraseScreen}${ESC}0f` :
87 // 1. Erases the screen (Only done in case `2` is not supported)
88 // 2. Erases the whole screen including scrollback buffer
89 // 3. Moves cursor to the top-left position
90 // More info: https://www.real-world-systems.com/docs/ANSIcode.html
91 `${ansiEscapes.eraseScreen}${ESC}3J${ESC}H`;
93 ansiEscapes.beep = BEL;
95 ansiEscapes.link = (text, url) => {
112 ansiEscapes.image = (buffer, options = {}) => {
113 let ret = `${OSC}1337;File=inline=1`;
116 ret += `;width=${options.width}`;
119 if (options.height) {
120 ret += `;height=${options.height}`;
123 if (options.preserveAspectRatio === false) {
124 ret += ';preserveAspectRatio=0';
127 return ret + ':' + buffer.toString('base64') + BEL;
130 ansiEscapes.iTerm = {
131 setCwd: (cwd = process.cwd()) => `${OSC}50;CurrentDir=${cwd}${BEL}`,
133 annotation: (message, options = {}) => {
134 let ret = `${OSC}1337;`;
136 const hasX = typeof options.x !== 'undefined';
137 const hasY = typeof options.y !== 'undefined';
138 if ((hasX || hasY) && !(hasX && hasY && typeof options.length !== 'undefined')) {
139 throw new Error('`x`, `y` and `length` must be defined when `x` or `y` is defined');
142 message = message.replace(/\|/g, '');
144 ret += options.isHidden ? 'AddHiddenAnnotation=' : 'AddAnnotation=';
146 if (options.length > 0) {
149 [message, options.length, options.x, options.y] :
150 [options.length, message]).join('|');