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.

confirm.js 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. const color = require('kleur');
  2. const Prompt = require('./prompt');
  3. const { style, clear } = require('../util');
  4. const { erase, cursor } = require('sisteransi');
  5. /**
  6. * ConfirmPrompt Base Element
  7. * @param {Object} opts Options
  8. * @param {String} opts.message Message
  9. * @param {Boolean} [opts.initial] Default value (true/false)
  10. * @param {Stream} [opts.stdin] The Readable stream to listen to
  11. * @param {Stream} [opts.stdout] The Writable stream to write readline data to
  12. * @param {String} [opts.yes] The "Yes" label
  13. * @param {String} [opts.yesOption] The "Yes" option when choosing between yes/no
  14. * @param {String} [opts.no] The "No" label
  15. * @param {String} [opts.noOption] The "No" option when choosing between yes/no
  16. */
  17. class ConfirmPrompt extends Prompt {
  18. constructor(opts={}) {
  19. super(opts);
  20. this.msg = opts.message;
  21. this.value = opts.initial;
  22. this.initialValue = !!opts.initial;
  23. this.yesMsg = opts.yes || 'yes';
  24. this.yesOption = opts.yesOption || '(Y/n)';
  25. this.noMsg = opts.no || 'no';
  26. this.noOption = opts.noOption || '(y/N)';
  27. this.render();
  28. }
  29. reset() {
  30. this.value = this.initialValue;
  31. this.fire();
  32. this.render();
  33. }
  34. exit() {
  35. this.abort();
  36. }
  37. abort() {
  38. this.done = this.aborted = true;
  39. this.fire();
  40. this.render();
  41. this.out.write('\n');
  42. this.close();
  43. }
  44. submit() {
  45. this.value = this.value || false;
  46. this.done = true;
  47. this.aborted = false;
  48. this.fire();
  49. this.render();
  50. this.out.write('\n');
  51. this.close();
  52. }
  53. _(c, key) {
  54. if (c.toLowerCase() === 'y') {
  55. this.value = true;
  56. return this.submit();
  57. }
  58. if (c.toLowerCase() === 'n') {
  59. this.value = false;
  60. return this.submit();
  61. }
  62. return this.bell();
  63. }
  64. render() {
  65. if (this.closed) return;
  66. if (this.firstRender) this.out.write(cursor.hide);
  67. else this.out.write(clear(this.outputText, this.out.columns));
  68. super.render();
  69. this.outputText = [
  70. style.symbol(this.done, this.aborted),
  71. color.bold(this.msg),
  72. style.delimiter(this.done),
  73. this.done ? (this.value ? this.yesMsg : this.noMsg)
  74. : color.gray(this.initialValue ? this.yesOption : this.noOption)
  75. ].join(' ');
  76. this.out.write(erase.line + cursor.to(0) + this.outputText);
  77. }
  78. }
  79. module.exports = ConfirmPrompt;