forked from cabal-club/cabal-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.js
104 lines (100 loc) · 2.73 KB
/
commands.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
var through = require('through2')
function Commander (view, cabal) {
if (!(this instanceof Commander)) return new Commander(view, cabal)
this.cabal = cabal
this.channel = 'default'
this.view = view
this.pattern = (/^\/(\w*)\s*(.*)/)
this.history = []
var self = this
this.commands = {
nick: {
help: () => 'change your display name',
call: (arg) => {
if (arg === '') return
self.cabal.username = arg
self.view.writeLine("* you're now known as " + arg)
}
},
names: {
help: () => 'display the names of the currently logged in users',
call: (arg) => {
var users = Object.keys(self.cabal.users)
self.view.writeLine('* currently connected users:')
users.map((u) => self.view.writeLine.bind(self.view)(` ${u}`))
}
},
channels: {
help: () => "display the cabal's channels",
call: (arg) => {
self.cabal.getChannels((err, channels) => {
if (err) return
self.view.writeLine('* channels:')
channels.map((m) => {
self.view.writeLine.bind(self.view)(` ${m}`)
})
})
}
},
join: {
help: () => 'join a new channel',
call: (arg) => {
if (arg === '') arg = 'default'
self.channel = arg
self.view.loadChannel(arg)
}
},
clear: {
help: () => 'clear the current backscroll',
call: (arg) => {
self.view.clear()
}
},
help: {
help: () => 'display this help message',
call: (arg) => {
for (var key in self.commands) {
self.view.writeLine(`/${key}\n ${self.commands[key].help()}`)
}
}
},
debug: {
help: () => 'debug hyperdb keys',
call: (arg) => {
var stream = self.cabal.db.createHistoryStream()
stream.pipe(through.obj(function (chunk, enc, next) {
if (chunk.key.indexOf(arg) > -1) {
self.view.writeLine.bind(self.view)(chunk.key + ': ' + JSON.stringify(chunk.value))
}
next()
}))
}
},
quit: {
help: () => 'exit the cabal process',
call: (arg) => {
process.exit(0)
}
}
}
}
Commander.prototype.process = function (line) {
var self = this
self.history.push(line)
if (self.history.length > 1000) self.history.shift()
var match = self.pattern.exec(line)
var cmd = match ? match[1] : ''
var arg = match ? match[2] : ''
arg = arg.trim()
if (cmd in self.commands) {
self.commands[cmd].call(arg)
} else if (cmd) {
self.view.writeLine(`${cmd} is not a command, type /help for commands`)
} else {
line = line.trim()
if (line !== '') {
self.cabal.message(self.channel, line)
}
}
}
module.exports = Commander