index.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. 'use strict'
  2. // These tables borrowed from `ansi`
  3. var prefix = '\x1b['
  4. exports.up = function up (num) {
  5. return prefix + (num || '') + 'A'
  6. }
  7. exports.down = function down (num) {
  8. return prefix + (num || '') + 'B'
  9. }
  10. exports.forward = function forward (num) {
  11. return prefix + (num || '') + 'C'
  12. }
  13. exports.back = function back (num) {
  14. return prefix + (num || '') + 'D'
  15. }
  16. exports.nextLine = function nextLine (num) {
  17. return prefix + (num || '') + 'E'
  18. }
  19. exports.previousLine = function previousLine (num) {
  20. return prefix + (num || '') + 'F'
  21. }
  22. exports.horizontalAbsolute = function horizontalAbsolute (num) {
  23. if (num == null) throw new Error('horizontalAboslute requires a column to position to')
  24. return prefix + num + 'G'
  25. }
  26. exports.eraseData = function eraseData () {
  27. return prefix + 'J'
  28. }
  29. exports.eraseLine = function eraseLine () {
  30. return prefix + 'K'
  31. }
  32. exports.goto = function (x, y) {
  33. return prefix + y + ';' + x + 'H'
  34. }
  35. exports.gotoSOL = function () {
  36. return '\r'
  37. }
  38. exports.beep = function () {
  39. return '\x07'
  40. }
  41. exports.hideCursor = function hideCursor () {
  42. return prefix + '?25l'
  43. }
  44. exports.showCursor = function showCursor () {
  45. return prefix + '?25h'
  46. }
  47. var colors = {
  48. reset: 0,
  49. // styles
  50. bold: 1,
  51. italic: 3,
  52. underline: 4,
  53. inverse: 7,
  54. // resets
  55. stopBold: 22,
  56. stopItalic: 23,
  57. stopUnderline: 24,
  58. stopInverse: 27,
  59. // colors
  60. white: 37,
  61. black: 30,
  62. blue: 34,
  63. cyan: 36,
  64. green: 32,
  65. magenta: 35,
  66. red: 31,
  67. yellow: 33,
  68. bgWhite: 47,
  69. bgBlack: 40,
  70. bgBlue: 44,
  71. bgCyan: 46,
  72. bgGreen: 42,
  73. bgMagenta: 45,
  74. bgRed: 41,
  75. bgYellow: 43,
  76. grey: 90,
  77. brightBlack: 90,
  78. brightRed: 91,
  79. brightGreen: 92,
  80. brightYellow: 93,
  81. brightBlue: 94,
  82. brightMagenta: 95,
  83. brightCyan: 96,
  84. brightWhite: 97,
  85. bgGrey: 100,
  86. bgBrightBlack: 100,
  87. bgBrightRed: 101,
  88. bgBrightGreen: 102,
  89. bgBrightYellow: 103,
  90. bgBrightBlue: 104,
  91. bgBrightMagenta: 105,
  92. bgBrightCyan: 106,
  93. bgBrightWhite: 107
  94. }
  95. exports.color = function color (colorWith) {
  96. if (arguments.length !== 1 || !Array.isArray(colorWith)) {
  97. colorWith = Array.prototype.slice.call(arguments)
  98. }
  99. return prefix + colorWith.map(colorNameToCode).join(';') + 'm'
  100. }
  101. function colorNameToCode (color) {
  102. if (colors[color] != null) return colors[color]
  103. throw new Error('Unknown color or style name: ' + color)
  104. }