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.

55 lines
1.3 KiB

4 years ago
  1. 'use strict';
  2. const Prompt = require('../types/string');
  3. const completer = require('../completer');
  4. class Input extends Prompt {
  5. constructor(options) {
  6. super(options);
  7. let history = this.options.history;
  8. if (history && history.store) {
  9. let initial = history.values || this.initial;
  10. this.autosave = !!history.autosave;
  11. this.store = history.store;
  12. this.data = this.store.get('values') || { past: [], present: initial };
  13. this.initial = this.data.present || this.data.past[this.data.past.length - 1];
  14. }
  15. }
  16. completion(action) {
  17. if (!this.store) return this.alert();
  18. this.data = completer(action, this.data, this.input);
  19. if (!this.data.present) return this.alert();
  20. this.input = this.data.present;
  21. this.cursor = this.input.length;
  22. return this.render();
  23. }
  24. altUp() {
  25. return this.completion('prev');
  26. }
  27. altDown() {
  28. return this.completion('next');
  29. }
  30. prev() {
  31. this.save();
  32. return super.prev();
  33. }
  34. save() {
  35. if (!this.store) return;
  36. this.data = completer('save', this.data, this.input);
  37. this.store.set('values', this.data);
  38. }
  39. submit() {
  40. if (this.store && this.autosave === true) {
  41. this.save();
  42. }
  43. return super.submit();
  44. }
  45. }
  46. module.exports = Input;