-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
85 lines (79 loc) · 2.58 KB
/
Copy pathindex.js
File metadata and controls
85 lines (79 loc) · 2.58 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
#!/usr/bin/env node
const inquirer = require('inquirer')
const Hyperchat = require('../hyperchat')
const setupQuestions = [{
type: 'input',
name: 'name',
message: 'What is your name?'
}]
const openInput = {
type: 'input',
name: 'input',
message: '>>>'
}
let chat
inquirer.prompt(setupQuestions)
.then((answers) => {
// start chatclient
if (answers.name) {
chat = new Hyperchat(answers.name)
chat.on('ready', () => console.log(chat.name, 'now available on public key:', chat.key))
chat.on('connection', () => console.log('i am connected to someone'))
chat.on('listening', data => console.log('i am listening to', data.key))
chat.on('disconnecting', key => console.log('disconnecting from', key))
chat.on('disconnected', key => console.log('disconnected from', key))
chat.on('destroyed', () => console.log('Hyperchat is destroyed'))
chat.on('started', data => console.log(data.name, 'joined conversation'))
chat.on('ended', data => console.log(data.name, 'exited conversation'))
chat.on('heard', data => console.log(data.name, 'heard', data.who, '-', data.index))
chat.on('message', data => console.log(`${data.name}:`, data.message))
return chatLoop()
}
})
function chatLoop () {
return inquirer.prompt([openInput])
.then(actOnInput)
}
function actOnInput (answer) {
const { input } = answer
const command = input.match(/:(\w+)(?:\s(.+))?/)
if (command) {
switch (command[1]) {
case 'q':
case 'quit':
chat.disconnect(() => console.log('DESTROYED'))
console.log('QUIT')
return
case 'c':
case 'connect':
if (command[2]) {
const key = command[2].trim()
console.log('attempt to connect to', key)
chat.add(key)
}
break
case 'w':
case 'whoami':
console.log('your public key is:', chat.key)
break
case 'd':
case 'disconnect':
const key = command[2].trim()
console.log('attempt to disconnect from', key)
chat.remove(key)
break
case 'h':
case 'help':
console.log(':h or :help - prints out the commants the chat cli accepts')
console.log(':w or :whoami - logs out your address')
console.log(':c [key] or :connect [key] - listens to conversation at key')
console.log(':d [key] or :disconnect [key] - stops listnening to conversation at key')
console.log(':q or :quit [key] - stops sharing and kills all connections')
break
}
} else {
// append chat
chat.chat(input)
}
chatLoop()
}