toggle.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. "use strict";
  2. const color = require('kleur');
  3. const Prompt = require('./prompt');
  4. const _require = require('../util'),
  5. style = _require.style,
  6. clear = _require.clear;
  7. const _require2 = require('sisteransi'),
  8. cursor = _require2.cursor,
  9. erase = _require2.erase;
  10. /**
  11. * TogglePrompt Base Element
  12. * @param {Object} opts Options
  13. * @param {String} opts.message Message
  14. * @param {Boolean} [opts.initial=false] Default value
  15. * @param {String} [opts.active='no'] Active label
  16. * @param {String} [opts.inactive='off'] Inactive label
  17. * @param {Stream} [opts.stdin] The Readable stream to listen to
  18. * @param {Stream} [opts.stdout] The Writable stream to write readline data to
  19. */
  20. class TogglePrompt extends Prompt {
  21. constructor(opts = {}) {
  22. super(opts);
  23. this.msg = opts.message;
  24. this.value = !!opts.initial;
  25. this.active = opts.active || 'on';
  26. this.inactive = opts.inactive || 'off';
  27. this.initialValue = this.value;
  28. this.render();
  29. }
  30. reset() {
  31. this.value = this.initialValue;
  32. this.fire();
  33. this.render();
  34. }
  35. exit() {
  36. this.abort();
  37. }
  38. abort() {
  39. this.done = this.aborted = true;
  40. this.fire();
  41. this.render();
  42. this.out.write('\n');
  43. this.close();
  44. }
  45. submit() {
  46. this.done = true;
  47. this.aborted = false;
  48. this.fire();
  49. this.render();
  50. this.out.write('\n');
  51. this.close();
  52. }
  53. deactivate() {
  54. if (this.value === false) return this.bell();
  55. this.value = false;
  56. this.render();
  57. }
  58. activate() {
  59. if (this.value === true) return this.bell();
  60. this.value = true;
  61. this.render();
  62. }
  63. delete() {
  64. this.deactivate();
  65. }
  66. left() {
  67. this.deactivate();
  68. }
  69. right() {
  70. this.activate();
  71. }
  72. down() {
  73. this.deactivate();
  74. }
  75. up() {
  76. this.activate();
  77. }
  78. next() {
  79. this.value = !this.value;
  80. this.fire();
  81. this.render();
  82. }
  83. _(c, key) {
  84. if (c === ' ') {
  85. this.value = !this.value;
  86. } else if (c === '1') {
  87. this.value = true;
  88. } else if (c === '0') {
  89. this.value = false;
  90. } else return this.bell();
  91. this.render();
  92. }
  93. render() {
  94. if (this.closed) return;
  95. if (this.firstRender) this.out.write(cursor.hide);else this.out.write(clear(this.outputText, this.out.columns));
  96. super.render();
  97. this.outputText = [style.symbol(this.done, this.aborted), color.bold(this.msg), style.delimiter(this.done), this.value ? this.inactive : color.cyan().underline(this.inactive), color.gray('/'), this.value ? color.cyan().underline(this.active) : this.active].join(' ');
  98. this.out.write(erase.line + cursor.to(0) + this.outputText);
  99. }
  100. }
  101. module.exports = TogglePrompt;