-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandParser.js
195 lines (189 loc) · 5.01 KB
/
CommandParser.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const AutoCompleter = require("./AutoCompleter.js");
const CommandOverwritten = require("./CommandOverwritten.js");
class CommandList {
constructor() {
this.commands = new Map();
this.aliases = new Map();
}
addCommand(command) {
var name = command.name;
if (this.commands.has(name)) {
let existingCmd = this.commands.get(name);
console.warn("Command %O already registered while trying to add Command %O with name %s.", existingCmd, command, name);
/*var overridden = new CommandOverwritten(existingCmd);
for(let alias of existingCmd.aliases)
{
this.aliases.set(alias, overridden);
}*/
}
this.commands.set(name, command);
for (let alias of command.aliases) {
if (this.aliases.has(alias)) {
this.aliases.get(alias).push(name);
} else {
this.aliases.set(alias, [name]);
}
}
}
removeCommand(name) {
if (this.commands.has(name)) {
for (let alias of this.commands.get(name).aliases) {
let arr = this.aliases.get(alias).filter(a => a != name);
if (arr.length < 1) {
this.aliases.delete(alias);
} else {
this.aliases.set(alias, arr);
}
}
this.commands.delete(name);
}
}
getCommand(name) {
if (this.commands.has(name)) {
return this.commands.get(name);
}
if (this.aliases.has(name)) {
let aliasArr = this.aliases.get(name);
if (aliasArr.length == 1) {
return getCommand(aliasArr[0]);
}
//return alias instanceof Command ? alias : this.getCommand(alias);
}
return null;
}
* keys() {
let keys = [...this.commands.keys(), ...this.aliases.keys()];
keys.sort();
for (let key of keys) {
yield key;
}
}
* values() {
yield* this.commands.values();
}
* entries() {
let keys = [...this.commands.keys(), ...this.aliases.keys()];
keys.sort();
for (let key of keys) {
yield [key, this.getCommand(key)];
}
}
* validCommands() {
let vals = [...this.commands.keys()];
for (let [alias, aliasArr] of this.aliases) {
if (aliasArr.length == 1) {
vals.push(alias);
}
}
vals.sort();
yield* vals;
}
*[Symbol.iterator]() {
yield* this.validCommands();
}
}
const shorthandMap = new WeakMap();
function hotkeySubmit(event, text, parser){
event.preventDefault();
parser.submit();
}
function hotkeyAutoComplete(event, text, parser){
event.preventDefault();
parser.autocomplete(event.shiftKey);
}
hotkeyAutoComplete.noDisableAutocomplete = true;
function hotkeyClear(event, text, parser){
event.preventDefault();
parser.clearText();
}
function shorthand (parser, func) {
if(!shorthandMap.has(parser))
{
shorthandMap.set(parser, new Map([
[parser.submit, hotkeySubmit],
[parser.autocomplete, hotkeyAutoComplete],
[parser.clearText, hotkeyClear]
]));
}
let map = shorthandMap.get(parser);
return map.has(func) ? map.get(func) : func;
}
class CommandParser {
constructor(inputText) {
this.inputText = inputText;
this.active = true;
this.hotkeys = {};
this.autocompleter = new AutoCompleter(this);
this.addHotkey("Enter", this.submit);
this.addHotkey("Escape", this.clearText);
this.addHotkey("Tab", this.autocomplete);
this.inputText.addEventListener("keydown", this.__handleKeyDown.bind(this));
this.commands = new CommandList();
}
activate() {
this.active = true;
return this;
}
deactivate() {
this.active = false;
return this;
}
get text() {
return this.inputText.value
}
set text(value) {
this.inputText.value = value
}
addHotkey(key, callback) {
if (key in this.hotkeys) {
console.log("Replacing keybind \"%s\": %O with: %O", key, this.hotkeys[key], callback);
}
this.hotkeys[key] = shorthand(this, callback);
return this; //For chaining
}
addCommand(command) {
this.commands.addCommand(command);
return this; //For chaining
}
/**
* Use this to clear all text from the entry.
* Optional argument is keyEvent (for convenience with addHotkey). If passed in, event.preventDefault() will be called.
*/
clearText(parser) {
this.text = "";
}
/**
* Use this to autocomplete the entry.
* Optional argument is boolean to reverse direction.
*/
autocomplete(reverse) {
this.autocompleter.fillNext(reverse);
}
submit() {
console.log(this.text);
let handled = true; //TODO
if (handled) {
this.clearText();
}
return handled;
}
get usage() {
//TODO: Usage
}
__handleKeyDown(e) {
if (!this.active) {
return;
}
if (e.key in this.hotkeys) {
let callback = this.hotkeys[e.key];
if (!callback.noDisableAutocomplete) {
this.autocompleter.disable();
}
var result = callback(e, this.text, this);
}
}
__getAutoCompletionOptions() {
return Array.from(this.commands.validCommands());
}
}
module.exports = CommandParser