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.

Prompt.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _constants() {
  7. const data = require('../constants');
  8. _constants = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _defineProperty(obj, key, value) {
  14. if (key in obj) {
  15. Object.defineProperty(obj, key, {
  16. value: value,
  17. enumerable: true,
  18. configurable: true,
  19. writable: true
  20. });
  21. } else {
  22. obj[key] = value;
  23. }
  24. return obj;
  25. }
  26. class Prompt {
  27. constructor() {
  28. _defineProperty(this, '_entering', void 0);
  29. _defineProperty(this, '_value', void 0);
  30. _defineProperty(this, '_onChange', void 0);
  31. _defineProperty(this, '_onSuccess', void 0);
  32. _defineProperty(this, '_onCancel', void 0);
  33. _defineProperty(this, '_offset', void 0);
  34. _defineProperty(this, '_promptLength', void 0);
  35. _defineProperty(this, '_selection', void 0);
  36. _defineProperty(this, '_onResize', () => {
  37. this._onChange();
  38. });
  39. // Copied from `enter` to satisfy TS
  40. this._entering = true;
  41. this._value = '';
  42. this._selection = null;
  43. this._offset = -1;
  44. this._promptLength = 0;
  45. this._onChange = () => {};
  46. this._onSuccess = () => {};
  47. this._onCancel = () => {};
  48. }
  49. enter(onChange, onSuccess, onCancel) {
  50. this._entering = true;
  51. this._value = '';
  52. this._onSuccess = onSuccess;
  53. this._onCancel = onCancel;
  54. this._selection = null;
  55. this._offset = -1;
  56. this._promptLength = 0;
  57. this._onChange = () =>
  58. onChange(this._value, {
  59. max: 10,
  60. offset: this._offset
  61. });
  62. this._onChange();
  63. process.stdout.on('resize', this._onResize);
  64. }
  65. setPromptLength(length) {
  66. this._promptLength = length;
  67. }
  68. setPromptSelection(selected) {
  69. this._selection = selected;
  70. }
  71. put(key) {
  72. switch (key) {
  73. case _constants().KEYS.ENTER:
  74. this._entering = false;
  75. this._onSuccess(this._selection || this._value);
  76. this.abort();
  77. break;
  78. case _constants().KEYS.ESCAPE:
  79. this._entering = false;
  80. this._onCancel(this._value);
  81. this.abort();
  82. break;
  83. case _constants().KEYS.ARROW_DOWN:
  84. this._offset = Math.min(this._offset + 1, this._promptLength - 1);
  85. this._onChange();
  86. break;
  87. case _constants().KEYS.ARROW_UP:
  88. this._offset = Math.max(this._offset - 1, -1);
  89. this._onChange();
  90. break;
  91. case _constants().KEYS.ARROW_LEFT:
  92. case _constants().KEYS.ARROW_RIGHT:
  93. break;
  94. case _constants().KEYS.CONTROL_U:
  95. this._value = '';
  96. this._offset = -1;
  97. this._selection = null;
  98. this._onChange();
  99. break;
  100. default:
  101. this._value =
  102. key === _constants().KEYS.BACKSPACE
  103. ? this._value.slice(0, -1)
  104. : this._value + key;
  105. this._offset = -1;
  106. this._selection = null;
  107. this._onChange();
  108. break;
  109. }
  110. }
  111. abort() {
  112. this._entering = false;
  113. this._value = '';
  114. process.stdout.removeListener('resize', this._onResize);
  115. }
  116. isEntering() {
  117. return this._entering;
  118. }
  119. }
  120. exports.default = Prompt;