/
_generic_input.ts
155 lines (140 loc) 路 4.15 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
148
149
150
151
152
153
154
155
import type { KeyCode } from "../keycode/key_code.ts";
import {
GenericPrompt,
GenericPromptKeys,
GenericPromptOptions,
GenericPromptSettings,
} from "./_generic_prompt.ts";
import { blue, dim, stripColor, underline } from "./deps.ts";
/** Input keys options. */
export interface GenericInputKeys extends GenericPromptKeys {
moveCursorLeft?: string[];
moveCursorRight?: string[];
deleteCharLeft?: string[];
deleteCharRight?: string[];
}
/** Generic input prompt options. */
export interface GenericInputPromptOptions<T, V>
extends GenericPromptOptions<T, V> {
keys?: GenericInputKeys;
}
/** Generic input prompt settings. */
export interface GenericInputPromptSettings<T, V>
extends GenericPromptSettings<T, V> {
keys?: GenericInputKeys;
}
/** Generic input prompt representation. */
export abstract class GenericInput<
T,
V,
S extends GenericInputPromptSettings<T, V>,
> extends GenericPrompt<T, V, S> {
protected inputValue = "";
protected inputIndex = 0;
/**
* Prompt constructor.
* @param settings Prompt settings.
*/
protected constructor(settings: S) {
super({
...settings,
keys: {
moveCursorLeft: ["left"],
moveCursorRight: ["right"],
deleteCharLeft: ["backspace"],
deleteCharRight: ["delete"],
...(settings.keys ?? {}),
},
});
}
protected getCurrentInputValue(): string {
return this.inputValue;
}
protected message(): string {
const message: string = super.message() + " " + this.settings.pointer + " ";
this.cursor.x = stripColor(message).length + this.inputIndex + 1;
return message + this.input();
}
protected input(): string {
return underline(this.inputValue);
}
protected highlight(
value: string | number,
color1: (val: string) => string = dim,
color2: (val: string) => string = blue,
): string {
value = value.toString();
const inputLowerCase = this.getCurrentInputValue().toLowerCase();
const valueLowerCase = value.toLowerCase();
const index = valueLowerCase.indexOf(inputLowerCase);
const matched: string = value.slice(index, index + inputLowerCase.length);
return index >= 0
? color1(value.slice(0, index)) + color2(matched) +
color1(value.slice(index + inputLowerCase.length))
: value;
}
/**
* Handle user input event.
* @param event Key event.
*/
protected async handleEvent(event: KeyCode): Promise<void> {
switch (true) {
case event.name === "c" && event.ctrl:
this.clear();
this.tty.cursorShow();
Deno.exit(130);
return;
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):
await this.submit();
break;
default:
if (event.sequence && !event.meta && !event.ctrl) {
this.addChar(event.sequence);
}
}
}
/** Add character to current input. */
protected addChar(char: string): void {
this.inputValue = this.inputValue.slice(0, this.inputIndex) + char +
this.inputValue.slice(this.inputIndex);
this.inputIndex++;
}
/** Move prompt cursor left. */
protected moveCursorLeft(): void {
if (this.inputIndex > 0) {
this.inputIndex--;
}
}
/** Move prompt cursor right. */
protected moveCursorRight(): void {
if (this.inputIndex < this.inputValue.length) {
this.inputIndex++;
}
}
/** Delete char left. */
protected deleteChar(): void {
if (this.inputIndex > 0) {
this.inputIndex--;
this.deleteCharRight();
}
}
/** Delete char right. */
protected deleteCharRight(): void {
if (this.inputIndex < this.inputValue.length) {
this.inputValue = this.inputValue.slice(0, this.inputIndex) +
this.inputValue.slice(this.inputIndex + 1);
}
}
}