index.js 3.7 KB

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