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.

text.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. "use strict";
  2. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
  3. function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
  4. const color = require('kleur');
  5. const Prompt = require('./prompt');
  6. const _require = require('sisteransi'),
  7. erase = _require.erase,
  8. cursor = _require.cursor;
  9. const _require2 = require('../util'),
  10. style = _require2.style,
  11. clear = _require2.clear,
  12. lines = _require2.lines,
  13. figures = _require2.figures;
  14. /**
  15. * TextPrompt Base Element
  16. * @param {Object} opts Options
  17. * @param {String} opts.message Message
  18. * @param {String} [opts.style='default'] Render style
  19. * @param {String} [opts.initial] Default value
  20. * @param {Function} [opts.validate] Validate function
  21. * @param {Stream} [opts.stdin] The Readable stream to listen to
  22. * @param {Stream} [opts.stdout] The Writable stream to write readline data to
  23. * @param {String} [opts.error] The invalid error label
  24. */
  25. class TextPrompt extends Prompt {
  26. constructor(opts = {}) {
  27. super(opts);
  28. this.transform = style.render(opts.style);
  29. this.scale = this.transform.scale;
  30. this.msg = opts.message;
  31. this.initial = opts.initial || ``;
  32. this.validator = opts.validate || (() => true);
  33. this.value = ``;
  34. this.errorMsg = opts.error || `Please Enter A Valid Value`;
  35. this.cursor = Number(!!this.initial);
  36. this.cursorOffset = 0;
  37. this.clear = clear(``, this.out.columns);
  38. this.render();
  39. }
  40. set value(v) {
  41. if (!v && this.initial) {
  42. this.placeholder = true;
  43. this.rendered = color.gray(this.transform.render(this.initial));
  44. } else {
  45. this.placeholder = false;
  46. this.rendered = this.transform.render(v);
  47. }
  48. this._value = v;
  49. this.fire();
  50. }
  51. get value() {
  52. return this._value;
  53. }
  54. reset() {
  55. this.value = ``;
  56. this.cursor = Number(!!this.initial);
  57. this.cursorOffset = 0;
  58. this.fire();
  59. this.render();
  60. }
  61. exit() {
  62. this.abort();
  63. }
  64. abort() {
  65. this.value = this.value || this.initial;
  66. this.done = this.aborted = true;
  67. this.error = false;
  68. this.red = false;
  69. this.fire();
  70. this.render();
  71. this.out.write('\n');
  72. this.close();
  73. }
  74. validate() {
  75. var _this = this;
  76. return _asyncToGenerator(function* () {
  77. let valid = yield _this.validator(_this.value);
  78. if (typeof valid === `string`) {
  79. _this.errorMsg = valid;
  80. valid = false;
  81. }
  82. _this.error = !valid;
  83. })();
  84. }
  85. submit() {
  86. var _this2 = this;
  87. return _asyncToGenerator(function* () {
  88. _this2.value = _this2.value || _this2.initial;
  89. _this2.cursorOffset = 0;
  90. _this2.cursor = _this2.rendered.length;
  91. yield _this2.validate();
  92. if (_this2.error) {
  93. _this2.red = true;
  94. _this2.fire();
  95. _this2.render();
  96. return;
  97. }
  98. _this2.done = true;
  99. _this2.aborted = false;
  100. _this2.fire();
  101. _this2.render();
  102. _this2.out.write('\n');
  103. _this2.close();
  104. })();
  105. }
  106. next() {
  107. if (!this.placeholder) return this.bell();
  108. this.value = this.initial;
  109. this.cursor = this.rendered.length;
  110. this.fire();
  111. this.render();
  112. }
  113. moveCursor(n) {
  114. if (this.placeholder) return;
  115. this.cursor = this.cursor + n;
  116. this.cursorOffset += n;
  117. }
  118. _(c, key) {
  119. let s1 = this.value.slice(0, this.cursor);
  120. let s2 = this.value.slice(this.cursor);
  121. this.value = `${s1}${c}${s2}`;
  122. this.red = false;
  123. this.cursor = this.placeholder ? 0 : s1.length + 1;
  124. this.render();
  125. }
  126. delete() {
  127. if (this.isCursorAtStart()) return this.bell();
  128. let s1 = this.value.slice(0, this.cursor - 1);
  129. let s2 = this.value.slice(this.cursor);
  130. this.value = `${s1}${s2}`;
  131. this.red = false;
  132. if (this.isCursorAtStart()) {
  133. this.cursorOffset = 0;
  134. } else {
  135. this.cursorOffset++;
  136. this.moveCursor(-1);
  137. }
  138. this.render();
  139. }
  140. deleteForward() {
  141. if (this.cursor * this.scale >= this.rendered.length || this.placeholder) return this.bell();
  142. let s1 = this.value.slice(0, this.cursor);
  143. let s2 = this.value.slice(this.cursor + 1);
  144. this.value = `${s1}${s2}`;
  145. this.red = false;
  146. if (this.isCursorAtEnd()) {
  147. this.cursorOffset = 0;
  148. } else {
  149. this.cursorOffset++;
  150. }
  151. this.render();
  152. }
  153. first() {
  154. this.cursor = 0;
  155. this.render();
  156. }
  157. last() {
  158. this.cursor = this.value.length;
  159. this.render();
  160. }
  161. left() {
  162. if (this.cursor <= 0 || this.placeholder) return this.bell();
  163. this.moveCursor(-1);
  164. this.render();
  165. }
  166. right() {
  167. if (this.cursor * this.scale >= this.rendered.length || this.placeholder) return this.bell();
  168. this.moveCursor(1);
  169. this.render();
  170. }
  171. isCursorAtStart() {
  172. return this.cursor === 0 || this.placeholder && this.cursor === 1;
  173. }
  174. isCursorAtEnd() {
  175. return this.cursor === this.rendered.length || this.placeholder && this.cursor === this.rendered.length + 1;
  176. }
  177. render() {
  178. if (this.closed) return;
  179. if (!this.firstRender) {
  180. if (this.outputError) this.out.write(cursor.down(lines(this.outputError, this.out.columns) - 1) + clear(this.outputError, this.out.columns));
  181. this.out.write(clear(this.outputText, this.out.columns));
  182. }
  183. super.render();
  184. this.outputError = '';
  185. this.outputText = [style.symbol(this.done, this.aborted), color.bold(this.msg), style.delimiter(this.done), this.red ? color.red(this.rendered) : this.rendered].join(` `);
  186. if (this.error) {
  187. this.outputError += this.errorMsg.split(`\n`).reduce((a, l, i) => a + `\n${i ? ' ' : figures.pointerSmall} ${color.red().italic(l)}`, ``);
  188. }
  189. this.out.write(erase.line + cursor.to(0) + this.outputText + cursor.save + this.outputError + cursor.restore + cursor.move(this.cursorOffset, 0));
  190. }
  191. }
  192. module.exports = TextPrompt;