-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
_generic_input.ts
147 lines (121 loc) 路 3.46 KB
/
_generic_input.ts
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
import type { KeyEvent } from "../keycode/key_event.ts";
import {
GenericPrompt,
GenericPromptOptions,
GenericPromptSettings,
} from "./_generic_prompt.ts";
import { stripColor, underline } from "./deps.ts";
export interface GenericInputKeys {
moveCursorLeft?: string[];
moveCursorRight?: string[];
deleteCharLeft?: string[];
deleteCharRight?: string[];
submit?: string[];
}
export interface GenericInputPromptOptions<T>
extends GenericPromptOptions<T, string> {
keys?: GenericInputKeys;
}
export interface GenericInputPromptSettings<T>
extends GenericPromptSettings<T, string> {
keys?: GenericInputKeys;
}
export abstract class GenericInput<T, S extends GenericInputPromptSettings<T>>
extends GenericPrompt<T, string, S> {
protected input = "";
protected index = 0;
public static inject(value: string): void {
GenericPrompt.inject(value);
}
protected constructor(settings: S) {
super({
...settings,
keys: {
moveCursorLeft: ["left"],
moveCursorRight: ["right"],
deleteCharLeft: ["backspace"],
deleteCharRight: ["delete"],
submit: ["enter", "return"],
...(settings.keys ?? {}),
},
});
}
protected setPrompt(message: string) {
message += " " + this.settings.pointer + " ";
const length = new TextEncoder().encode(stripColor(message)).length;
message += underline(this.input);
this.write(message);
this.screen.cursorTo(length - 1 + this.index);
}
protected async handleEvent(event: KeyEvent): Promise<boolean> {
switch (true) {
case event.name === "c":
if (event.ctrl) {
this.screen.cursorShow();
return Deno.exit(0);
}
if (event.sequence) {
this.addChar(event.sequence);
}
break;
// case "up": // scroll history?
// break;
//
// case "down": // scroll history?
// break;
case this.isKey(this.settings.keys, "moveCursorLeft", event):
this.moveCursorLeft();
break;
case this.isKey(this.settings.keys, "moveCursorRight", event):
this.moveCursorRight();
break;
case this.isKey(this.settings.keys, "deleteCharRight", event):
this.deleteCharRight();
break;
case this.isKey(this.settings.keys, "deleteCharLeft", event):
this.deleteChar();
break;
case this.isKey(this.settings.keys, "submit", event):
return true;
default:
if (event.sequence && !event.meta && !event.ctrl) {
this.addChar(event.sequence);
}
break;
}
return false;
}
protected addChar(char: string): void {
this.input = this.input.slice(0, this.index) + char +
this.input.slice(this.index);
this.index++;
}
protected moveCursorLeft(): void {
if (this.index > 0) {
this.index--;
}
}
protected moveCursorRight(): void {
if (this.index < this.input.length) {
const index = this.input.indexOf(" ", this.index);
this.index++;
}
}
protected deleteChar(): void {
if (this.index > 0) {
this.index--;
this.screen.cursorBackward(1);
this.input = this.input.slice(0, this.index) +
this.input.slice(this.index + 1);
}
}
protected deleteCharRight(): void {
if (this.index < this.input.length) {
this.input = this.input.slice(0, this.index) +
this.input.slice(this.index + 1);
}
}
protected getValue(): string {
return this.input;
}
}