-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
number.ts
247 lines (215 loc) 路 6.21 KB
/
number.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import type { KeyCode } from "../keycode/key_code.ts";
import { GenericPrompt } from "./_generic_prompt.ts";
import {
GenericSuggestions,
GenericSuggestionsKeys,
GenericSuggestionsOptions,
GenericSuggestionsSettings,
} from "./_generic_suggestions.ts";
import { parseNumber } from "./_utils.ts";
import { blue, yellow } from "./deps.ts";
import { Figures } from "./figures.ts";
/** Number key options. */
export interface NumberKeys extends GenericSuggestionsKeys {
increaseValue?: string[];
decreaseValue?: string[];
}
type UnsupportedOptions = "files";
/** Number prompt options. */
export interface NumberOptions
extends Omit<GenericSuggestionsOptions<number, string>, UnsupportedOptions> {
min?: number;
max?: number;
float?: boolean;
round?: number;
keys?: NumberKeys;
}
/** Number prompt settings. */
interface NumberSettings extends GenericSuggestionsSettings<number, string> {
min: number;
max: number;
float: boolean;
round: number;
keys?: NumberKeys;
}
/** Number prompt representation. */
export class Number extends GenericSuggestions<number, string, NumberSettings> {
/** Execute the prompt and show cursor on end. */
public static prompt(options: string | NumberOptions): Promise<number> {
if (typeof options === "string") {
options = { message: options };
}
return new this({
pointer: blue(Figures.POINTER_SMALL),
prefix: yellow("? "),
indent: " ",
listPointer: blue(Figures.POINTER),
maxRows: 8,
min: -Infinity,
max: Infinity,
float: false,
round: 2,
...options,
files: false,
keys: {
increaseValue: ["up", "u", "+"],
decreaseValue: ["down", "d", "-"],
...(options.keys ?? {}),
},
}).prompt();
}
/**
* Inject prompt value. Can be used for unit tests or pre selections.
* @param value Input value.
*/
public static inject(value: string): void {
GenericPrompt.inject(value);
}
protected success(value: number): string | undefined {
this.saveSuggestions(value);
return super.success(value);
}
/**
* Handle user input event.
* @param event Key event.
*/
protected async handleEvent(event: KeyCode): Promise<void> {
switch (true) {
case this.settings.suggestions &&
this.isKey(this.settings.keys, "next", event):
if (this.settings.list) {
this.selectPreviousSuggestion();
} else {
this.selectNextSuggestion();
}
break;
case this.settings.suggestions &&
this.isKey(this.settings.keys, "previous", event):
if (this.settings.list) {
this.selectNextSuggestion();
} else {
this.selectPreviousSuggestion();
}
break;
case this.isKey(this.settings.keys, "increaseValue", event):
this.increaseValue();
break;
case this.isKey(this.settings.keys, "decreaseValue", event):
this.decreaseValue();
break;
default:
await super.handleEvent(event);
}
}
/** Increase input number. */
public increaseValue() {
this.manipulateIndex(false);
}
/** Decrease input number. */
public decreaseValue() {
this.manipulateIndex(true);
}
/** Decrease/increase input number. */
protected manipulateIndex(decrease?: boolean) {
if (this.inputValue[this.inputIndex] === "-") {
this.inputIndex++;
}
if (
this.inputValue.length && (this.inputIndex > this.inputValue.length - 1)
) {
this.inputIndex--;
}
const decimalIndex: number = this.inputValue.indexOf(".");
const [abs, dec] = this.inputValue.split(".");
if (dec && this.inputIndex === decimalIndex) {
this.inputIndex--;
}
const inDecimal: boolean = decimalIndex !== -1 &&
this.inputIndex > decimalIndex;
let value: string = (inDecimal ? dec : abs) || "0";
const oldLength: number = this.inputValue.length;
const index: number = inDecimal
? this.inputIndex - decimalIndex - 1
: this.inputIndex;
const increaseValue = Math.pow(10, value.length - index - 1);
value = (parseInt(value) + (decrease ? -increaseValue : increaseValue))
.toString();
this.inputValue = !dec
? value
: (this.inputIndex > decimalIndex
? abs + "." + value
: value + "." + dec);
if (this.inputValue.length > oldLength) {
this.inputIndex++;
} else if (
this.inputValue.length < oldLength &&
this.inputValue[this.inputIndex - 1] !== "-"
) {
this.inputIndex--;
}
this.inputIndex = Math.max(
0,
Math.min(this.inputIndex, this.inputValue.length - 1),
);
}
/**
* Add char to input.
* @param char Char.
*/
protected addChar(char: string): void {
if (isNumeric(char)) {
super.addChar(char);
} else if (
this.settings.float &&
char === "." &&
this.inputValue.indexOf(".") === -1 &&
(this.inputValue[0] === "-" ? this.inputIndex > 1 : this.inputIndex > 0)
) {
super.addChar(char);
}
}
/**
* Validate input value.
* @param value User input value.
* @return True on success, false or error message on error.
*/
protected validate(value: string): boolean | string {
if (!isNumeric(value)) {
return false;
}
const val: number = parseFloat(value);
if (val > this.settings.max) {
return `Value must be lower or equal than ${this.settings.max}`;
}
if (val < this.settings.min) {
return `Value must be greater or equal than ${this.settings.min}`;
}
return true;
}
/**
* Map input value to output value.
* @param value Input value.
* @return Output value.
*/
protected transform(value: string): number | undefined {
const val: number = parseFloat(value);
if (this.settings.float) {
return parseFloat(val.toFixed(this.settings.round));
}
return val;
}
/**
* Format output value.
* @param value Output value.
*/
protected format(value: number): string {
return value.toString();
}
/** Get input input. */
protected getValue(): string {
return this.inputValue;
}
}
function isNumeric(value: string | number): value is (number | string) {
return typeof value === "number" || (!!value && !isNaN(parseNumber(value)));
}