Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

toggle.js 2.5KB

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