-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm.js
More file actions
150 lines (130 loc) · 2.47 KB
/
Copy pathForm.js
File metadata and controls
150 lines (130 loc) · 2.47 KB
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
const blessed = require('blessed');
class Form {
/**
*
* @constructor
*
* @example
*
* let form = new Form();
*
*/
constructor() {
this.screen = blessed.screen({
smartCSR:true,
cursor: {
artificial: true,
shape: 'line',
blink: true,
color: null
}
});
this.form = blessed.form({
parent: this.screen,
width: '80%',
top: 'center',
left: 'center',
bottom: -2,
keys: true,
vi: true,
});
this.rowCount = 0;
this.callback = () => {};
this.values = {};
this.screen.key('q', function () {
this.destroy();
process.exit();
});
}
/**
*
* Create a form text input
*
* @param {String} label - The label of the input prompt
*
* @example
*
* form.addPrompt('Username');
* form.addPrompt('Password');
* form.addPrompt('Favorite Animal');
*
*/
addPrompt(label) {
const lbl = blessed.text({
parent: this.form,
content: label,
top: this.rowCount+1
})
const textbox = blessed.textbox({
parent:this.form,
name: label,
height: 3,
left: 'center',
top: this.rowCount,
content: 'message',
inputOnFocus: true,
border: {
type: 'line'
},
focus: {
fg: 'blue'
},
index: 10
});
this.rowCount += 3;
}
/**
*
* Add a submit button & event to the form
*
* @param {Function} callback - The submit event callback
*
* @example
*
* form.addPrompt('myTextArea');
*
* form.addSubmit((data) => {
* console.log(data.myTextArea);
* });
*
*/
addSubmit(callback = () => {}) {
const btn = blessed.button({
parent: this.form,
content: 'CHAT',
border: {
type: 'line'
},
style: {
focus: {
bg: 'white',
fg: 'black'
},
},
top: this.rowCount + 2,
width: 8,
height: 3,
left: 'center',
})
btn.on('press', ()=>{
this.form.submit();
})
this.callback = callback.bind(this);
this.form.on('submit', (data)=>{
let values = Object.values(data);
for (let i = 0; i < values.length; i++) {
if(values[i].length === 0) {
return;
};
}
this.callback(data);
})
}
clear() {
this.screen.destroy();
}
render() {
this.screen.render();
}
}
module.exports = Form;