-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
95 lines (84 loc) · 2.33 KB
/
test.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
process.stdout.write("\x1Bc");
var readline = require('readline'),
util = require('util');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
completer: completer
});
rl.setPrompt("> ", 2);
rl.on("line", function(line) {
if (line == "help") { console.log("no command.") }
rl.prompt();
});
rl.on('close', function() {
return process.exit(1);
});
rl.on("SIGINT", function() {
rl.clearLine();
rl.question("Confirm exit : ", function(answer) {
return (answer.match(/^o(ui)?$/i) || answer.match(/^y(es)?$/i)) ? process.exit(1) : rl.output.write("> ");
});
});
rl.prompt();
var fu = function(type, args) {
var t = Math.ceil((rl.line.length + 3) / process.stdout.columns);
var text = util.format.apply(console, args);
rl.output.write("\n\x1B[" + t + "A\x1B[0J");
rl.output.write(text + "\n");
rl.output.write(Array(t).join("\n\x1B[E"));
rl._refreshLine();
};
console.log = function() {
fu("log", arguments);
};
console.warn = function() {
fu("warn", arguments);
};
console.info = function() {
fu("info", arguments);
};
console.error = function() {
fu("error", arguments);
};
console.log(">> Readline : Ok.");
function completer(line) {
var completions = ["help", "command1", "command2", "login", "check", "ping"];
var hits = completions.filter(function(c) {
return c.indexOf(line) == 0;
});
if (hits.length == 1) {
return [hits, line];
} else {
console.log("Suggest :");
var list = "",
l = 0,
c = "",
t = hits.length ? hits : completions;
for (var i = 0; i < t.length; i++) {
c = t[i].replace(/(\s*)$/g, "")
if (list != "") {
list += ", ";
}
if (((list + c).length + 4 - l) > process.stdout.columns) {
list += "\n";
l = list.length;
}
list += c;
}
console.log(list + "\n");
return [hits, line];
}
}
//-----------------------------
function main() {
var ___i=0;
setInterval(function () {
var num = function () { return Math.floor(Math.random() * 255) + 1; };
console.log(num()+"."+num()+"."+num()+" user connected.");
}, 1000);
//your code.
}
setTimeout(function () {
main();
}, 10);