index.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. 'use strict';
  2. var ESC = '\u001b[';
  3. var x = module.exports;
  4. x.cursorTo = function (x, y) {
  5. if (arguments.length === 0) {
  6. return ESC + 'H';
  7. }
  8. if (arguments.length === 1) {
  9. return ESC + (x + 1) + 'G';
  10. }
  11. return ESC + (y + 1) + ';' + (x + 1) + 'H';
  12. };
  13. x.cursorMove = function (x, y) {
  14. var ret = '';
  15. if (x < 0) {
  16. ret += ESC + (-x) + 'D';
  17. } else if (x > 0) {
  18. ret += ESC + x + 'C';
  19. }
  20. if (y < 0) {
  21. ret += ESC + (-y) + 'A';
  22. } else if (y > 0) {
  23. ret += ESC + y + 'B';
  24. }
  25. return ret;
  26. };
  27. x.cursorUp = function (count) {
  28. return ESC + (typeof count === 'number' ? count : 1) + 'A';
  29. };
  30. x.cursorDown = function (count) {
  31. return ESC + (typeof count === 'number' ? count : 1) + 'B';
  32. };
  33. x.cursorForward = function (count) {
  34. return ESC + (typeof count === 'number' ? count : 1) + 'C';
  35. };
  36. x.cursorBackward = function (count) {
  37. return ESC + (typeof count === 'number' ? count : 1) + 'D';
  38. };
  39. x.cursorLeft = ESC + '1000D';
  40. x.cursorSavePosition = ESC + 's';
  41. x.cursorRestorePosition = ESC + 'u';
  42. x.cursorGetPosition = ESC + '6n';
  43. x.cursorNextLine = ESC + 'E';
  44. x.cursorPrevLine = ESC + 'F';
  45. x.cursorHide = ESC + '?25l';
  46. x.cursorShow = ESC + '?25h';
  47. x.eraseLines = function (count) {
  48. var clear = '';
  49. for (var i = 0; i < count; i++) {
  50. clear += x.cursorLeft + x.eraseEndLine + (i < count - 1 ? x.cursorUp() : '');
  51. }
  52. return clear;
  53. };
  54. x.eraseEndLine = ESC + 'K';
  55. x.eraseStartLine = ESC + '1K';
  56. x.eraseLine = ESC + '2K';
  57. x.eraseDown = ESC + 'J';
  58. x.eraseUp = ESC + '1J';
  59. x.eraseScreen = ESC + '2J';
  60. x.scrollUp = ESC + 'S';
  61. x.scrollDown = ESC + 'T';
  62. x.clearScreen = '\u001bc';
  63. x.beep = '\u0007';
  64. x.image = function (buf, opts) {
  65. opts = opts || {};
  66. var ret = '\u001b]1337;File=inline=1';
  67. if (opts.width) {
  68. ret += ';width=' + opts.width;
  69. }
  70. if (opts.height) {
  71. ret += ';height=' + opts.height;
  72. }
  73. if (opts.preserveAspectRatio === false) {
  74. ret += ';preserveAspectRatio=0';
  75. }
  76. return ret + ':' + buf.toString('base64') + '\u0007';
  77. };
  78. x.iTerm = {};
  79. x.iTerm.setCwd = function (cwd) {
  80. return '\u001b]50;CurrentDir=' + (cwd || process.cwd()) + '\u0007';
  81. };