source: trip-planner-front/node_modules/ansi-escapes/index.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.7 KB
Line 
1'use strict';
2const ansiEscapes = module.exports;
3// TODO: remove this in the next major version
4module.exports.default = ansiEscapes;
5
6const ESC = '\u001B[';
7const OSC = '\u001B]';
8const BEL = '\u0007';
9const SEP = ';';
10const isTerminalApp = process.env.TERM_PROGRAM === 'Apple_Terminal';
11
12ansiEscapes.cursorTo = (x, y) => {
13 if (typeof x !== 'number') {
14 throw new TypeError('The `x` argument is required');
15 }
16
17 if (typeof y !== 'number') {
18 return ESC + (x + 1) + 'G';
19 }
20
21 return ESC + (y + 1) + ';' + (x + 1) + 'H';
22};
23
24ansiEscapes.cursorMove = (x, y) => {
25 if (typeof x !== 'number') {
26 throw new TypeError('The `x` argument is required');
27 }
28
29 let ret = '';
30
31 if (x < 0) {
32 ret += ESC + (-x) + 'D';
33 } else if (x > 0) {
34 ret += ESC + x + 'C';
35 }
36
37 if (y < 0) {
38 ret += ESC + (-y) + 'A';
39 } else if (y > 0) {
40 ret += ESC + y + 'B';
41 }
42
43 return ret;
44};
45
46ansiEscapes.cursorUp = (count = 1) => ESC + count + 'A';
47ansiEscapes.cursorDown = (count = 1) => ESC + count + 'B';
48ansiEscapes.cursorForward = (count = 1) => ESC + count + 'C';
49ansiEscapes.cursorBackward = (count = 1) => ESC + count + 'D';
50
51ansiEscapes.cursorLeft = ESC + 'G';
52ansiEscapes.cursorSavePosition = isTerminalApp ? '\u001B7' : ESC + 's';
53ansiEscapes.cursorRestorePosition = isTerminalApp ? '\u001B8' : ESC + 'u';
54ansiEscapes.cursorGetPosition = ESC + '6n';
55ansiEscapes.cursorNextLine = ESC + 'E';
56ansiEscapes.cursorPrevLine = ESC + 'F';
57ansiEscapes.cursorHide = ESC + '?25l';
58ansiEscapes.cursorShow = ESC + '?25h';
59
60ansiEscapes.eraseLines = count => {
61 let clear = '';
62
63 for (let i = 0; i < count; i++) {
64 clear += ansiEscapes.eraseLine + (i < count - 1 ? ansiEscapes.cursorUp() : '');
65 }
66
67 if (count) {
68 clear += ansiEscapes.cursorLeft;
69 }
70
71 return clear;
72};
73
74ansiEscapes.eraseEndLine = ESC + 'K';
75ansiEscapes.eraseStartLine = ESC + '1K';
76ansiEscapes.eraseLine = ESC + '2K';
77ansiEscapes.eraseDown = ESC + 'J';
78ansiEscapes.eraseUp = ESC + '1J';
79ansiEscapes.eraseScreen = ESC + '2J';
80ansiEscapes.scrollUp = ESC + 'S';
81ansiEscapes.scrollDown = ESC + 'T';
82
83ansiEscapes.clearScreen = '\u001Bc';
84
85ansiEscapes.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`;
92
93ansiEscapes.beep = BEL;
94
95ansiEscapes.link = (text, url) => {
96 return [
97 OSC,
98 '8',
99 SEP,
100 SEP,
101 url,
102 BEL,
103 text,
104 OSC,
105 '8',
106 SEP,
107 SEP,
108 BEL
109 ].join('');
110};
111
112ansiEscapes.image = (buffer, options = {}) => {
113 let ret = `${OSC}1337;File=inline=1`;
114
115 if (options.width) {
116 ret += `;width=${options.width}`;
117 }
118
119 if (options.height) {
120 ret += `;height=${options.height}`;
121 }
122
123 if (options.preserveAspectRatio === false) {
124 ret += ';preserveAspectRatio=0';
125 }
126
127 return ret + ':' + buffer.toString('base64') + BEL;
128};
129
130ansiEscapes.iTerm = {
131 setCwd: (cwd = process.cwd()) => `${OSC}50;CurrentDir=${cwd}${BEL}`,
132
133 annotation: (message, options = {}) => {
134 let ret = `${OSC}1337;`;
135
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');
140 }
141
142 message = message.replace(/\|/g, '');
143
144 ret += options.isHidden ? 'AddHiddenAnnotation=' : 'AddAnnotation=';
145
146 if (options.length > 0) {
147 ret +=
148 (hasX ?
149 [message, options.length, options.x, options.y] :
150 [options.length, message]).join('|');
151 } else {
152 ret += message;
153 }
154
155 return ret + BEL;
156 }
157};
Note: See TracBrowser for help on using the repository browser.