index.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.beep = BEL;
  66. x.link = (text, url) => {
  67. return [
  68. OSC,
  69. '8',
  70. SEP,
  71. SEP,
  72. url,
  73. BEL,
  74. text,
  75. OSC,
  76. '8',
  77. SEP,
  78. SEP,
  79. BEL
  80. ].join('');
  81. };
  82. x.image = (buf, opts) => {
  83. opts = opts || {};
  84. let ret = OSC + '1337;File=inline=1';
  85. if (opts.width) {
  86. ret += `;width=${opts.width}`;
  87. }
  88. if (opts.height) {
  89. ret += `;height=${opts.height}`;
  90. }
  91. if (opts.preserveAspectRatio === false) {
  92. ret += ';preserveAspectRatio=0';
  93. }
  94. return ret + ':' + buf.toString('base64') + BEL;
  95. };
  96. x.iTerm = {};
  97. x.iTerm.setCwd = cwd => OSC + '50;CurrentDir=' + (cwd || process.cwd()) + BEL;