JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
puttin' it all together
authorJason Woofenden <jason@jasonwoof.com>
Wed, 30 Jan 2013 05:34:06 +0000 (00:34 -0500)
committerJason Woofenden <jason@jasonwoof.com>
Wed, 30 Jan 2013 05:34:06 +0000 (00:34 -0500)
index.html
input-test.coffee [deleted file]
parse.coffee [deleted file]
publish-my-session.coffee [new file with mode: 0644]
server.js [deleted file]
terminal.coffee [new file with mode: 0644]

index e55e8c0..daa26ae 100644 (file)
@@ -1,9 +1,7 @@
 <script src="/socket.io/socket.io.js"></script>
 <script>
   var socket = io.connect('http://localhost');
-  socket.on('news', function (data) {
+  socket.on('write', function (data) {
     console.log(data);
-    socket.emit('my other event', { my: 'data' });
-    socket.emit('jason', { is: 'large and in charge' });
   });
 </script>
diff --git a/input-test.coffee b/input-test.coffee
deleted file mode 100644 (file)
index fbb2a6e..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-child_process = require 'child_process'
-
-process.stdin.resume()
-process.stdin.setEncoding 'utf8'
-
-process.stdin.on 'data', (data) ->
-       child_process.spawn 'notify-send', ['--expire-time=1000', "length: #{data.length}", data]
-
-process.stdin.on 'end', -> process.exit()
diff --git a/parse.coffee b/parse.coffee
deleted file mode 100644 (file)
index b3dfed6..0000000
+++ /dev/null
@@ -1,63 +0,0 @@
-async = require 'async'
-fs = require 'fs'
-
-class Terminal
-       # public:
-       constructor: (width, height) ->
-               @width = 1
-               @height = 1
-               @text = [""]
-               @attributes = [0]
-               @x = 0
-               @y = 0
-               @partial = ''
-               @resize width, height
-
-       resize: (width, height) ->
-               # FIXME: write a version that retains some of the data
-               @text = []
-               @attributes = []
-               for i in [0...height]
-                       @text[i] = ""
-                       @attributes[i] = new Array(width)
-
-       # pass data from stdout
-       update: (data) ->
-               return unless data?.length > 0
-               if @partial.length > 0
-                       data = @partial + data
-                       @partial = ''
-               parts = data.split(/\x1b\[/)
-               if parts.length > 1
-                       if -1 is @escape_sequence_length parts[parts.length - 1]
-                               @partial = parts.pop()
-               if parts.length > 0
-                       for i in [0...parts.length]
-                               if i is 0
-                                       @update_text parts[i]
-                               else
-                                       @update_sequence_then_text parts[i]
-               return
-
-       # str has no escape sequences
-       update_text: (str) ->
-               return unless str.length > 0
-               console.log "text: \"#{str}\"" # FIXME
-
-       # str is the whole escape sequence (minus the esc[ prefix)
-       update_sequence: (str) ->
-               console.log "sequence: \"#{str}\"" # FIXME
-
-       update_sequence_then_text: (str) ->
-               len = @escape_sequence_length str
-               if len is -1
-                       console.log "couldn't find escape sequence here: #{str.substr 0, 25}"
-                       @update_text "ESC[" + str
-               else
-                       @update_sequence str.substr 0, len
-                       @update_text str.substr len
-
-       escape_sequence_length: (str) ->
-               parts = str.match(/^[0-9;?]{0,25}./)
-               return -1 unless parts?
-               return parts[0].length
diff --git a/publish-my-session.coffee b/publish-my-session.coffee
new file mode 100644 (file)
index 0000000..bd2cd30
--- /dev/null
@@ -0,0 +1,35 @@
+handler = (req, res) ->
+       fs.readFile __dirname + '/index.html', (err, data) ->
+               if err
+                       res.writeHead(500)
+                       return res.end('Error loading index.html')
+
+               res.writeHead(200)
+               res.end(data)
+
+app = require('http').createServer(handler)
+io = require('socket.io').listen(app)
+fs = require('fs')
+terminal = require('./terminal.coffee')
+
+# SETTINGS
+app.listen(9293)
+term = terminal.new(105, 66)
+
+io.sockets.on 'connection', (socket) ->
+       # FIXME socket.emit 'write', term.getState()
+       term.on 'sequence', (data) ->
+               socket.emit 'write', "sequence: #{data}"
+       term.on 'text', (data) ->
+               socket.emit 'write', data
+       socket.on 'disconnect', ->
+               # FIXME stop term update callback
+               console.log 'client disconnected'
+
+process.stdin.resume()
+process.stdin.setEncoding 'utf8'
+
+process.stdin.on 'data', (data) ->
+       term.update data
+
+process.stdin.on 'end', -> process.exit()
diff --git a/server.js b/server.js
deleted file mode 100644 (file)
index 9936453..0000000
--- a/server.js
+++ /dev/null
@@ -1,28 +0,0 @@
-var app = require('http').createServer(handler)
-  , io = require('socket.io').listen(app)
-  , fs = require('fs')
-
-app.listen(9293);
-
-function handler (req, res) {
-  fs.readFile(__dirname + '/index.html',
-  function (err, data) {
-    if (err) {
-      res.writeHead(500);
-      return res.end('Error loading index.html');
-    }
-
-    res.writeHead(200);
-    res.end(data);
-  });
-}
-
-io.sockets.on('connection', function (socket) {
-  setInterval(function() { socket.emit('news', { hello: 'world' });}, 2000);
-  socket.on('my other event', function (data) {
-    console.log(data);
-  });
-  socket.on('jason', function (data) {
-    console.log({jason_is_here: data});
-  });
-});
diff --git a/terminal.coffee b/terminal.coffee
new file mode 100644 (file)
index 0000000..94be906
--- /dev/null
@@ -0,0 +1,75 @@
+async = require 'async'
+fs = require 'fs'
+
+class Terminal
+       # public:
+       constructor: (width, height) ->
+               @width = 1
+               @height = 1
+               @text = [""]
+               @attributes = [0]
+               @x = 0
+               @y = 0
+               @partial = ''
+               @resize width, height
+               @text_callbacks = []
+               @sequence_callbacks = []
+       
+       on: (evt, callback) ->
+               @["#{evt}_callbacks"].push callback
+
+       resize: (width, height) ->
+               # FIXME: write a version that retains some of the data
+               @text = []
+               @attributes = []
+               for i in [0...height]
+                       @text[i] = ""
+                       @attributes[i] = new Array(width)
+
+       # pass data from stdout
+       update: (data) ->
+               return unless data?.length > 0
+               if @partial.length > 0
+                       data = @partial + data
+                       @partial = ''
+               parts = data.split(/\x1b\[/)
+               if parts.length > 1
+                       if -1 is @escape_sequence_length parts[parts.length - 1]
+                               @partial = parts.pop()
+               if parts.length > 0
+                       for i in [0...parts.length]
+                               if i is 0
+                                       @update_text parts[i]
+                               else
+                                       @update_sequence_then_text parts[i]
+               return
+
+       # str has no escape sequences
+       update_text: (str) ->
+               return unless str.length > 0
+               for c in @text_callbacks
+                       c str
+               console.log "text: \"#{str}\"" # FIXME
+
+       # str is the whole escape sequence (minus the esc[ prefix)
+       update_sequence: (str) ->
+               for c in @sequence_callbacks
+                       c str
+               console.log "sequence: \"#{str}\"" # FIXME
+
+       update_sequence_then_text: (str) ->
+               len = @escape_sequence_length str
+               if len is -1
+                       console.log "couldn't find escape sequence here: #{str.substr 0, 25}"
+                       @update_text "ESC[" + str
+               else
+                       @update_sequence str.substr 0, len
+                       @update_text str.substr len
+
+       escape_sequence_length: (str) ->
+               parts = str.match(/^[0-9;?]{0,25}./)
+               return -1 unless parts?
+               return parts[0].length
+
+exports.new = (width, height) ->
+       return new Terminal width, height