index.js 2.7 KB

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