Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. const ESC = '\u001B[';
  2. const OSC = '\u001B]';
  3. const BEL = '\u0007';
  4. const SEP = ';';
  5. const isTerminalApp = process.env.TERM_PROGRAM === 'Apple_Terminal';
  6. const ansiEscapes = {};
  7. ansiEscapes.cursorTo = (x, y) => {
  8. if (typeof x !== 'number') {
  9. throw new TypeError('The `x` argument is required');
  10. }
  11. if (typeof y !== 'number') {
  12. return ESC + (x + 1) + 'G';
  13. }
  14. return ESC + (y + 1) + ';' + (x + 1) + 'H';
  15. };
  16. ansiEscapes.cursorMove = (x, y) => {
  17. if (typeof x !== 'number') {
  18. throw new TypeError('The `x` argument is required');
  19. }
  20. let returnValue = '';
  21. if (x < 0) {
  22. returnValue += ESC + (-x) + 'D';
  23. } else if (x > 0) {
  24. returnValue += ESC + x + 'C';
  25. }
  26. if (y < 0) {
  27. returnValue += ESC + (-y) + 'A';
  28. } else if (y > 0) {
  29. returnValue += ESC + y + 'B';
  30. }
  31. return returnValue;
  32. };
  33. ansiEscapes.cursorUp = (count = 1) => ESC + count + 'A';
  34. ansiEscapes.cursorDown = (count = 1) => ESC + count + 'B';
  35. ansiEscapes.cursorForward = (count = 1) => ESC + count + 'C';
  36. ansiEscapes.cursorBackward = (count = 1) => ESC + count + 'D';
  37. ansiEscapes.cursorLeft = ESC + 'G';
  38. ansiEscapes.cursorSavePosition = isTerminalApp ? '\u001B7' : ESC + 's';
  39. ansiEscapes.cursorRestorePosition = isTerminalApp ? '\u001B8' : ESC + 'u';
  40. ansiEscapes.cursorGetPosition = ESC + '6n';
  41. ansiEscapes.cursorNextLine = ESC + 'E';
  42. ansiEscapes.cursorPrevLine = ESC + 'F';
  43. ansiEscapes.cursorHide = ESC + '?25l';
  44. ansiEscapes.cursorShow = ESC + '?25h';
  45. ansiEscapes.eraseLines = count => {
  46. let clear = '';
  47. for (let i = 0; i < count; i++) {
  48. clear += ansiEscapes.eraseLine + (i < count - 1 ? ansiEscapes.cursorUp() : '');
  49. }
  50. if (count) {
  51. clear += ansiEscapes.cursorLeft;
  52. }
  53. return clear;
  54. };
  55. ansiEscapes.eraseEndLine = ESC + 'K';
  56. ansiEscapes.eraseStartLine = ESC + '1K';
  57. ansiEscapes.eraseLine = ESC + '2K';
  58. ansiEscapes.eraseDown = ESC + 'J';
  59. ansiEscapes.eraseUp = ESC + '1J';
  60. ansiEscapes.eraseScreen = ESC + '2J';
  61. ansiEscapes.scrollUp = ESC + 'S';
  62. ansiEscapes.scrollDown = ESC + 'T';
  63. ansiEscapes.clearScreen = '\u001Bc';
  64. ansiEscapes.clearTerminal = process.platform === 'win32' ?
  65. `${ansiEscapes.eraseScreen}${ESC}0f` :
  66. // 1. Erases the screen (Only done in case `2` is not supported)
  67. // 2. Erases the whole screen including scrollback buffer
  68. // 3. Moves cursor to the top-left position
  69. // More info: https://www.real-world-systems.com/docs/ANSIcode.html
  70. `${ansiEscapes.eraseScreen}${ESC}3J${ESC}H`;
  71. ansiEscapes.beep = BEL;
  72. ansiEscapes.link = (text, url) => {
  73. return [
  74. OSC,
  75. '8',
  76. SEP,
  77. SEP,
  78. url,
  79. BEL,
  80. text,
  81. OSC,
  82. '8',
  83. SEP,
  84. SEP,
  85. BEL
  86. ].join('');
  87. };
  88. ansiEscapes.image = (buffer, options = {}) => {
  89. let returnValue = `${OSC}1337;File=inline=1`;
  90. if (options.width) {
  91. returnValue += `;width=${options.width}`;
  92. }
  93. if (options.height) {
  94. returnValue += `;height=${options.height}`;
  95. }
  96. if (options.preserveAspectRatio === false) {
  97. returnValue += ';preserveAspectRatio=0';
  98. }
  99. return returnValue + ':' + buffer.toString('base64') + BEL;
  100. };
  101. ansiEscapes.iTerm = {
  102. setCwd: (cwd = process.cwd()) => `${OSC}50;CurrentDir=${cwd}${BEL}`,
  103. annotation: (message, options = {}) => {
  104. let returnValue = `${OSC}1337;`;
  105. const hasX = typeof options.x !== 'undefined';
  106. const hasY = typeof options.y !== 'undefined';
  107. if ((hasX || hasY) && !(hasX && hasY && typeof options.length !== 'undefined')) {
  108. throw new Error('`x`, `y` and `length` must be defined when `x` or `y` is defined');
  109. }
  110. message = message.replace(/\|/g, '');
  111. returnValue += options.isHidden ? 'AddHiddenAnnotation=' : 'AddAnnotation=';
  112. if (options.length > 0) {
  113. returnValue +=
  114. (hasX ?
  115. [message, options.length, options.x, options.y] :
  116. [options.length, message]).join('|');
  117. } else {
  118. returnValue += message;
  119. }
  120. return returnValue + BEL;
  121. }
  122. };
  123. export default ansiEscapes;