CSIHelper.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * CSIHelper, or "Control Sequence Introducer Helper" is the translator of actions into
  3. * sequence of ANSI characters that will trigger some action in the Terminal.
  4. *
  5. * Note: CSI is not from the TV Series.
  6. *
  7. * More info:
  8. * - https://en.wikipedia.org/wiki/ANSI_escape_code
  9. * - http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html
  10. * - http://ascii-table.com/ansi-escape-sequences-vt-100.php
  11. */
  12. var CSIHelper = module.exports = {}
  13. const ESC = CSIHelper.ESC = '\u001b'
  14. /*
  15. * Save current cursor position
  16. */
  17. CSIHelper.save = function save() {
  18. return '\u001b7'
  19. }
  20. /*
  21. * Restore cursor position
  22. */
  23. CSIHelper.restore = function restore() {
  24. return '\u001b8'
  25. }
  26. /*
  27. * Move cursor up `n` lines. Default is 1
  28. */
  29. CSIHelper.up = function up(n) {
  30. n = typeof n === 'number' ? n : 1
  31. return n > 0 ? ESC + '[' + n + 'A' : ''
  32. }
  33. /*
  34. * Move cursor down `n` lines. Default is 1
  35. */
  36. CSIHelper.down = function down(n) {
  37. n = typeof n === 'number' ? n : 1
  38. return n > 0 ? ESC + '[' + n + 'B' : ''
  39. }
  40. CSIHelper.clearLine = function clearLine() {
  41. return ESC + '[2K' + ESC + '[1G'
  42. }