-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.mjs
305 lines (255 loc) · 7.82 KB
/
console.mjs
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import { TRUE } from "./coreJs.mjs"
import { SoftEvent } from "./coreJs.mjs"
import { UNDEFINED } from "./coreJs.mjs"
import { isString } from "./coreJs.mjs"
export const console_colors = {
black: 0,
red: 1,
green: 2,
yellow: 3,
blueDark: 4,
pink: 5,
blue: 6,
white: 7,
none: 8,
}
export const console_brightnessBackground = {
'0': 100, // light
'1': 40 // dark
}
export const console_brightnessText = {
'0': 90, // light
'1': 30 // dark
}
export const console_fontStyle = {
normal: 0,
bold: 1,
thin: 2,
italic: 3,
underline: 4,
blink: 5,
inverted: 7,
invisible: 8,
crossed: 9,
cool: 20,
digits: 52,
}
export const console_brightness = {
light: 0, // light
dark: 1 // dark
}
const processProperty = (propContent) => {
if (isString(propContent)) {
const obj = {}
propContent = propContent.split(' ').filter(a => a).forEach(element => {
if (console_colors[element] !== UNDEFINED) {
obj.color = element;
} else if (console_brightness[element] !== UNDEFINED) {
obj.brightness = element;
} else {
obj[element] = true;
}
});
propContent = obj;
}
if (propContent.color !== UNDEFINED) {
if (isString(propContent.color)) {
propContent.color = console_colors[propContent.color] || 0;
}
}
if (propContent.brightness !== UNDEFINED) {
if (isString(propContent.brightness)) {
propContent.brightness = console_brightness[propContent.brightness] || 0;
}
}
return propContent;
}
export const console_style = ({text, background}) => {
let styleText = '';
const style = (text) => `\x1b[${styleText}m${text}\x1b[0m`;
style.isConsoleStyle = TRUE;
const stylesList = [];
if (text !== UNDEFINED) {
text = processProperty(text);
for (let name in text) {
const value = text[name];
if (name === 'color') {
if (text.brightness !== UNDEFINED) {
stylesList.push(value + console_brightnessText[text.brightness])
} else {
stylesList.push(value + console_brightnessText['1'])
}
} else {
const fontStyle = console_fontStyle[name];
if (fontStyle !== UNDEFINED) {
stylesList.push(fontStyle)
}
}
}
}
if (background !== UNDEFINED) {
background = processProperty(background);
for (let name in background) {
const value = background[name];
if (name === 'color') {
if (background.brightness !== UNDEFINED) {
stylesList.push(value + console_brightnessBackground[background.brightness])
} else {
stylesList.push(value + console_brightnessBackground['1'])
}
}
}
}
styleText = stylesList.join(';');
console_log(styleText, text, background)
return style;
}
const
right = '\x1B[C',
up = '\x1B[A',
left = '\x1B[D',
down = '\x1B[B',
home = '\x1B[H',
newLine = '\n'
// -------------------------------------------------------------------
const write = (text) => process.stdout.write(text);
export const console_newline = () => write('\n');
export const console_moveToTopLeft = () => write(home);
export const console_resize = (x, y) => write(`\x1B[8;${x};${y}t`);
export const console_moveBy = (x, y) => {
if (x) {
if (x < 0) {
x = -x;
write(`\x1B[${x > 1 ? x : ''}A`)
} else {
write(`\x1B[${x > 1 ? x : ''}B`)
}
}
if (y) {
if (y < 0) {
y = -y;
write(`\x1B[${y > 1 ? y : ''}D`)
} else {
write(`\x1B[${y > 1 ? y : ''}C`)
}
}
}
export const console_moveTo = (x = 0, y = 0) => {
if (!x && !y) {
console_moveToTopLeft()
} else {
write(`\x1B[${y + 1};${x + 1}H`)
}
}
export const console_moveToBottomRight = () => {
console_moveTo(999, 999)
}
let console_getPosition_isProcessing = 0;
export const console_getPosition = (callback) => {
if (!console_getPosition_isProcessing) write(`\x1B[6n`);
console_getPosition_isProcessing++;
const event = (x, y) => {
console_getPosition_isProcessing--;
console_onPosition.del(event); // just do it once
callback(x, y);
};
console_onPosition.add(event);
}
// -------------------------------------------------------------------
export const console_inline = (...args) => {
for (let i = 0; i < args.length; i++) {
write(args[i]);
}
}
export const console_line = (...args) => {
console_inline(...args, newLine);
}
export const console_noSpaces = {};
export const console_spaces = {};
export const console_noNewLine = {};
export const console_log = (...args) => {
let spacesInbetween = TRUE;
let notFirst, noNewLine, styleToApply;
const writeText = text => {
if (notFirst && spacesInbetween) write(' ')
if (styleToApply) {
write(styleToApply(text))
styleToApply = UNDEFINED;
} else {
write(text);
}
notFirst = TRUE;
}
for (let i = 0; i < args.length; i++) {
let arg = args[i];
if (arg === console_spaces) {
spacesInbetween = TRUE;
} else if (arg === console_noSpaces) {
spacesInbetween = FALSE;
} else if (arg === console_noNewLine) {
noNewLine = TRUE;
} else if (arg === UNDEFINED) {
writeText('undefined');
} else if (arg === null) {
writeText('null');
} else if (arg.isConsoleStyle) {
styleToApply = arg;
} else if (isString(arg)) {
writeText(arg);
} else {
arg = JSON.stringify(arg, null, 3);
writeText(arg);
}
}
if (!noNewLine) write('\n');
}
// -------------------------------------------------------------------
// on cursor position received
export const console_onPosition = SoftEvent();
// -------------------------------------------------------------------
const twoCharsCommandContent = (data) => data.substring(2, data.length - 1);
const twoCharsCommands = {
'\x1B[': {
'': (data) => {
console_log(data)
},
R: (data) => {
const [y, x] = twoCharsCommandContent(data).split(';')
console_onPosition.raise(parseInt(y-1), parseInt(x-1));
}
}
}
const fullDataCommands = {
'': (data) => {
console_log(data)
},
'\u0003': (data) => {
process.exit();
}
}
// -------------------------------------------------------------------
let console_input;
export const console_enablaDataInput = () => {
if (!console_input) {
console_input = process.stdin;
// without this, we would only get streams once enter is pressed
console_input.setRawMode( true );
// resume stdin in the parent process (node app won't quit all by itself
// unless an error or process.exit() happens)
console_input.resume();
// i don't want binary, do you?
console_input.setEncoding( 'utf8' );
console_input.on('data', ( data ) => {
const twoChars = data.substr(0, 2);
const cmdGroup = twoCharsCommands[twoChars];
if (cmdGroup) {
const lastChar = data[data.length - 1];
const cmd = cmdGroup[lastChar] || cmdGroup[''];
cmd(data);
return;
}
const fullDataCmd = fullDataCommands[data] || fullDataCommands[''];
fullDataCmd(data);
});
}
}