-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
116 lines (89 loc) · 2.68 KB
/
index.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
104
105
106
107
108
109
110
111
112
113
114
115
116
const readline = require('readline');
const server = require('./src/server');
const client = require('./src/client');
const jsonClient = require('./src/json_client');
const grabClient = require('./src/grab_client');
// Set up argumnets
const argv = require('yargs')
.usage('Usage: termchat [options] [message] \n \
Lightweight CLI chat client and server. If message is included, the process \
will send the message and immediately exit. If omitted, the process will stay open \
in interactive mode.')
.boolean('server')
.alias('s', 'server')
.describe('s', 'Run the chat server for clients to join')
.default('port', 2797)
.alias('p', 'port')
.describe('p', 'Port to connect to on server')
.default('channel', 'general')
.alias('c', 'channel')
.describe('c', 'Channel (room) to connect to join')
.default('host', 'http://alexrowe.net')
.alias('h', 'host')
.describe('h', 'Hostname of server')
.default('user', process.env.USER || 'anonymous')
.alias('u', 'user')
.describe('u', 'Your nickname in server chat')
.boolean('json')
.alias('j', 'json')
.describe('j', 'Output server state in JSON and exit')
.boolean('background')
.alias('b', 'background')
.describe('b', 'Backround mode, do not expect any stdin')
.default('history', 5)
.alias('i', 'history')
.describe('i', 'Number of previous messages to show on login')
.default('grab', 0)
.alias('g', 'grab')
.describe('g', 'Grab Last message')
.boolean('version')
.describe('version', 'Display Version')
.help()
.argv
// Show version of package
if (argv.version) {
console.log(require('./package.json').version);
process.exit();
}
// One-off message to send to server
let message = argv._.join(' ');
// Readline Variable
let readLine;
// If server is set, start server
if (argv.server) {
server.startServer(argv.port);
} else if (argv.json){
jsonClient.startClient(argv, message);
} else if (argv.grab){
grabClient.startClient(argv);
// Otherwise, start the client socket
} else {
if (!argv.background) {
startReadline();
}
client.startClient(argv, message);
}
// Set up read line
function startReadline () {
readLine = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: true
});
let message = '';
readLine.on('line', function(line){
// we are only interested in non-empty lines
if (!line) return;
// Move cursor up one line and clear line
process.stdout.moveCursor(0, -1);
process.stdout.clearLine();
// If Start of message, make a multi long message
if (!message) {
setTimeout(function (){
client.sendMessage(message);
message = '';
}, 4);
}
message += line;
});
}