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.

boolean.js 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. 'use strict';
  2. const Prompt = require('../prompt');
  3. const { isPrimitive, hasColor } = require('../utils');
  4. class BooleanPrompt extends Prompt {
  5. constructor(options) {
  6. super(options);
  7. this.cursorHide();
  8. }
  9. async initialize() {
  10. let initial = await this.resolve(this.initial, this.state);
  11. this.input = await this.cast(initial);
  12. await super.initialize();
  13. }
  14. dispatch(ch) {
  15. if (!this.isValue(ch)) return this.alert();
  16. this.input = ch;
  17. return this.submit();
  18. }
  19. format(value) {
  20. let { styles, state } = this;
  21. return !state.submitted ? styles.primary(value) : styles.success(value);
  22. }
  23. cast(input) {
  24. return this.isTrue(input);
  25. }
  26. isTrue(input) {
  27. return /^[ty1]/i.test(input);
  28. }
  29. isFalse(input) {
  30. return /^[fn0]/i.test(input);
  31. }
  32. isValue(value) {
  33. return isPrimitive(value) && (this.isTrue(value) || this.isFalse(value));
  34. }
  35. async hint() {
  36. if (this.state.status === 'pending') {
  37. let hint = await this.element('hint');
  38. if (!hasColor(hint)) {
  39. return this.styles.muted(hint);
  40. }
  41. return hint;
  42. }
  43. }
  44. async render() {
  45. let { input, size } = this.state;
  46. let prefix = await this.prefix();
  47. let sep = await this.separator();
  48. let msg = await this.message();
  49. let hint = this.styles.muted(this.default);
  50. let promptLine = [prefix, msg, hint, sep].filter(Boolean).join(' ');
  51. this.state.prompt = promptLine;
  52. let header = await this.header();
  53. let value = this.value = this.cast(input);
  54. let output = await this.format(value);
  55. let help = (await this.error()) || (await this.hint());
  56. let footer = await this.footer();
  57. if (help && !promptLine.includes(help)) output += ' ' + help;
  58. promptLine += ' ' + output;
  59. this.clear(size);
  60. this.write([header, promptLine, footer].filter(Boolean).join('\n'));
  61. this.restore();
  62. }
  63. set value(value) {
  64. super.value = value;
  65. }
  66. get value() {
  67. return this.cast(super.value);
  68. }
  69. }
  70. module.exports = BooleanPrompt;