Permalink
Browse files

Merge pull request #192 from github/repl

Convert shell adapter into proper REPL
  • Loading branch information...
2 parents 8f8165c + 1637f4d commit 612e8bd3545dc483fa39439261a1f6038c901d45 Tom Bell committed Nov 24, 2011
Showing with 30 additions and 19 deletions.
  1. +30 −19 src/adapters/shell.coffee
@@ -1,29 +1,40 @@
-Robot = require '../robot'
-Adapter = require '../adapter'
+Readline = require 'readline'
+
+Robot = require '../robot'
+Adapter = require '../adapter'
class Shell extends Adapter
send: (user, strings...) ->
- for str in strings
- console.log str
+ unless process.platform is "win32"
+ console.log "\033[01;32m#{str}\033[0m" for str in strings
+ else
+ console.log "#{str}" for str in strings
+ @repl.prompt()
reply: (user, strings...) ->
- for str in strings
- @send user, "#{user.name}: #{str}"
+ @send user, strings...
run: ->
- user = @userForId('1', { name: "Shell" })
-
- process.stdin.resume()
- process.stdin.on 'data', (txt) =>
- txt.toString().split("\n").forEach (line) =>
- return if line.length is 0
- @receive new Robot.TextMessage user, line
-
- setTimeout =>
- user = @userForId('1', { name: "Shell" })
- atmos = @userForId('2', { name: "atmos" })
- holman = @userForId('3', { name: "Zach Holman" })
- , 3000
+ stdin = process.openStdin()
+ stdout = process.stdout
+
+ process.on "uncaughtException", (err) =>
+ @robot.logger.error "#{err}"
+
+ @repl = Readline.createInterface stdin, stdout, null
+
+ @repl.on "close", =>
+ stdin.destroy()
+ process.exit 0
+
+ @repl.on "line", (buffer) =>
+ @repl.close() if buffer.toLowerCase() is "exit"
+ @repl.prompt()
+ user = @userForId '1', name: "Shell"
+ @receive new Robot.TextMessage user, buffer
+
+ @repl.setPrompt "#{@robot.name}> "
+ @repl.prompt()
exports.use = (robot) ->
new Shell robot

0 comments on commit 612e8bd

Please sign in to comment.