-
-
Notifications
You must be signed in to change notification settings - Fork 305
/
text.js
103 lines (84 loc) · 2.08 KB
/
text.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
const color = require('clorox');
const Prompt = require('./prompt');
const { cursor } = require('sisteransi');
const { style: stl, clear } = require('../util');
class TextPrompt extends Prompt {
constructor({ message, cursor, initial, style = 'default' }) {
super();
this.msg = message;
this.value = initial || '';
this.initialValue = this.value;
this.transform = stl.render(style);
this.rendered = this.transform(this.value);
this.cursor = cursor || this.rendered.length;
this.clear = clear('');
this.render(true);
}
setValue(v) {
this.value = v;
this.rendered = this.transform(v);
this.fire();
}
reset() {
this.setValue(this.initialValue);
this.fire();
this.render();
}
abort() {
this.done = this.aborted = true;
this.fire();
this.render();
this.out.write('\n');
this.close();
}
submit() {
this.done = true;
this.aborted = false;
this.fire();
this.render();
this.out.write('\n');
this.close();
}
_(c, key) {
this.setValue(this.value.slice(0, this.cursor) + c + this.value.slice(this.cursor));
this.cursor++;
this.render();
}
delete() {
if (this.value.length == 0) return this.bell();
if(this.cursor == 0 ) return this.bell();
this.cursor--;
this.setValue(this.value.slice(0, this.cursor) + this.value.slice(this.cursor+1));
this.render();
}
first() {
this.cursor = 0;
this.render();
}
last() {
this.cursor = this.rendered.length;
this.render();
}
left() {
if (this.cursor <= 0) return this.bell();
this.cursor--;
this.render();
}
right() {
if (this.cursor >= this.rendered.length) return this.bell();
this.cursor++;
this.render();
}
render() {
const prompt = [
stl.symbol(this.done, this.aborted),
color.bold(this.msg),
stl.delimiter(this.done),
this.rendered
].join(' ');
this.out.write(this.clear + prompt);
this.out.write(cursor.move(-this.rendered.length + this.cursor));
this.clear = clear(prompt);
}
}
module.exports = TextPrompt;