-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChat.js
More file actions
239 lines (205 loc) · 5.1 KB
/
Copy pathChat.js
File metadata and controls
239 lines (205 loc) · 5.1 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
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
const blessed = require('blessed');
class Chat {
/**
* @constructor
*
* @param {String} - The client's user username
* @param {String} - The client's user tag
*
* @example
*
* let chat = new Chat('UserA', '4234');
*
*/
constructor(username = 'UserA', userTag = '000000000000000000000000', quitCallback = () => {}) {
this.quitCallback = quitCallback;
this.screen = blessed.screen({
smartCSR:true,
alwaysScroll:true,
scrollable: true,
scrollbar: true,
cursor: {
artificial: true,
shape: 'line',
blink: true,
color: null
}
});
this.screen.key('9', () => {
this.scrollUp(-1);
});
this.screen.key('8', () => {
this.scrollUp(1);
});
this.screen.key('q', function () {
quitCallback();
this.destroy();
process.exit();
});
this.form = blessed.form({
parent: this.screen,
width: '100%',
left: 'center',
keys: true,
vi: true
});
this.messageForm = blessed.form({
parent: this.form,
width: '100%',
left: 'center',
bottom: 4
});
this.username = username;
this.userTag = userTag;
this.prompt;
this.hasFooter = false;
this.userHistory = [];
this.messages = [];
this.scroll = 0;
this.color = Chat.colorFromName(username);
this.messageCount = 0;
this.streak = 1;
}
/**
* Add a message to the chat box interface
*
* @param {String} username - The user name to display
* @param {String} content - The content of the message to display
* @param {String} [userTag] - The user tag (display's next to the name in brackets)
*
*
* @example
*
* chat.addMessage('UserB', 'Hello world back!', '1241');
*
*/
addMessage(username, content, userTag = Math.round(Math.random(1000, 9999))) {
if (content.length === 0) return;
this.messageCount++;
const color = Chat.colorFromName(username);
this.userHistory.push({ username, userTag });
const showUsername = this.isDifferentUser();
this.scrollUp(showUsername ? 3 : 1);
let user = showUsername ? `\x1b[${color}m@${username}\x1b[0m [\x1b[32m${userTag}\x1b[0m]\n` : '';
if (showUsername) {
this.streak = 0;
} else {
this.streak= 0;
}
this.messages.push(blessed.text({
parent: this.messageForm,
content: `${user}${content}`,
bottom: this.hasFooter+(this.streak),
left: 1
}))
}
/**
* add a chat prompt
*
* @param {Function} sendMessage - Callback with a `Message` object as argument
*
*
* @example
*
* // add a prompt and do nothing on enter
* chat.addPrompt(() => {});
*
* // add a prompt and `console.log` data on enter
* chat.addPrompt((msg) => {
* console.log(msg);
* // on enter event
* })
*
*/
addPrompt(sendMessage) {
this.prompt = blessed.textbox({
parent:this.form,
bottom: 0,
left: 0,
height: 3,
content: 'message',
inputOnFocus: true,
border: {
type: 'line'
},
focus: {
fg: 'blue'
},
index: 10
});
this.prompt.focus();
this.screen.key('enter', ()=>{
this.form.submit();
})
this.form.on('submit', ({ textbox }) => {
sendMessage({
user: this.username,
userTag: this.userTag,
message: textbox
});
this.addMessage(this.username, textbox, this.userTag);
this.clear();
this.render();
});
}
/**
* Add a footer (Text at the bottom of the screen
*
* @param {String} content - The text writtern in the footer
*
* @example
*
* chat.addFooter('Press ESC-q to quit!');
*
*/
addFooter(content) {
this.prompt.bottom++;
this.scrollUp(1);
this.hasFooter = true;
const text = blessed.text({
parent: this.form,
bottom: 0,
left: 0,
width: '100%',
content
});
}
getLastUser() {
return this.userHistory[this.userHistory.length - 1];
}
focusScroll() {
this.scrollUp(this.scroll)
}
scrollUp(n) {
this.scroll += n;
this.messages.forEach((m,i) => {
m.bottom += n;
});
}
isDifferentUser() {
if (this.userHistory.length <= 1) return true;
return !(
this.userHistory[this.userHistory.length - 2].username === this.userHistory[this.userHistory.length - 1].username &&
this.userHistory[this.userHistory.length - 2].userTag === this.userHistory[this.userHistory.length - 1].userTag
)
}
clear() {
this.form.reset();
this.prompt.focus();
}
render() {
this.screen.render();
}
static palette = ['31', '32', '33', '34', '35', '36', '96', '94', '91', '92', '93', '95']
static colorFromName(name) {
let sum = 0;
for (let i = 0; i < name.length; i++) {
if (("0123456789").includes(name[i]))
sum += +name[i];
else sum += name.charCodeAt(i)/8;
if (Math.floor(sum) >= Chat.palette.length) sum -= Chat.palette.length+1;
}
return Chat.palette[Math.max(0, Math.min(Chat.palette.length, Math.floor(sum-1)))];
}
};
module.exports = Chat;