-
Notifications
You must be signed in to change notification settings - Fork 0
/
autocomplete-fixed.js
121 lines (104 loc) · 3.33 KB
/
autocomplete-fixed.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
'use strict';
const { Select } = require('enquirer');
const highlight = (input, color) => {
let val = input.toLowerCase();
return (str, highlightIndices) => {
if (highlightIndices) {
return highlightIndices.flat().map((h, i) => (i % 2 === 0) ? h : h + 1).map((h, i, a) => {
switch (i) {
case 0:
return [str.slice(0, h), str.slice(h, a[1])];
case a.length - 1:
return str.slice(h);
default:
return str.slice(h, a[i + 1]);
}
}).flat().map((s, i) => (i % 2 === 0) ? s : color(s)).join('');
}
let s = str.toLowerCase();
let i = s.indexOf(val);
let colored = color(str.slice(i, i + val.length));
return i >= 0 ? str.slice(0, i) + colored + str.slice(i + val.length) : str;
};
};
class AutoComplete extends Select {
constructor(options) {
super(options);
this.cursorShow();
}
moveCursor(n) {
this.state.cursor += n;
}
dispatch(ch) {
return this.append(ch);
}
space(ch) {
return this.options.multiple ? super.space(ch) : this.append(ch);
}
append(ch) {
let { cursor, input } = this.state;
this.input = input.slice(0, cursor) + ch + input.slice(cursor);
this.moveCursor(1);
return this.complete();
}
delete() {
let { cursor, input } = this.state;
if (!input) return this.alert();
this.input = input.slice(0, cursor - 1) + input.slice(cursor);
this.moveCursor(-1);
return this.complete();
}
deleteForward() {
let { cursor, input } = this.state;
if (input[cursor] === void 0) return this.alert();
this.input = `${input}`.slice(0, cursor) + `${input}`.slice(cursor + 1);
return this.complete();
}
async complete() {
this.completing = true;
this.choices = await this.suggest(this.input, this.state._choices.map(ch => { ch.highlightIndices = undefined; return ch; }));
this.state.limit = void 0; // allow getter/setter to reset limit
this.index = Math.min(Math.max(this.visible.length - 1, 0), this.index);
await this.render();
this.completing = false;
}
suggest(input = this.input, choices = this.state._choices) {
if (typeof this.options.suggest === 'function') {
return this.options.suggest.call(this, input, choices);
}
let str = input.toLowerCase();
return choices.filter(ch => ch.message.toLowerCase().includes(str));
}
pointer() {
return '';
}
format() {
if (!this.focused) return this.input;
if (this.options.multiple && this.state.submitted) {
return this.selected.map(ch => this.styles.primary(ch.message)).join(', ');
}
if (this.state.submitted) {
let value = this.value = this.input = this.focused.value;
return this.styles.primary(value);
}
return this.input;
}
async render() {
if (this.state.status !== 'pending') return super.render();
let style = this.options.highlight
? this.options.highlight.bind(this)
: this.styles.placeholder;
let color = highlight(this.input, style);
let choices = this.choices;
this.choices = choices.map(ch => ({ ...ch, message: color(ch.message, ch.highlightIndices) }));
await super.render();
this.choices = choices;
}
submit() {
if (this.options.multiple) {
this.value = this.selected.map(ch => ch.name);
}
return super.submit();
}
}
module.exports = AutoComplete;