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