Permalink
Browse files

added README file

  • Loading branch information...
1 parent be348eb commit e71ca2752040ded28f2bbf9cf3d5a1225ab4fd00 @maca committed Mar 14, 2011
Showing with 80 additions and 28 deletions.
  1. +1 −0 .gitignore
  2. +36 −0 README.rdoc
  3. +43 −28 lib/live.rb
View
1 .gitignore
@@ -2,4 +2,5 @@ pkg/*
*.gem
.bundle
*.swp
+*.swo
View
36 README.rdoc
@@ -0,0 +1,36 @@
+Live
+====
+
+Live is like IRC only it is meant to be used from a text editor, it essentially polls a *nix pipe and evaluates its contents.
+It was devised for audiovisual livecoding (ruby-processing and scruby), a block can be bound to a key and can be called later by a keystroke.
+
+Usage
+-----
+
+ $ live
+
+open a new terminal
+
+ $ echo 'hello' > /tmp/live-rb
+
+You will see the code evaluated in the first terminal
+
+ $ echo 'bind_key(:a){ 'key a was pressed' }' > /tmp/live-rb
+
+When you press the key 'a' with focus on the first terminal it will call the block
+
+Vim
+---
+
+To use from vim paste this in your .vimrc file
+
+ function EvalLiveRuby() range
+ let text = [join(getline(a:firstline, a:lastline), ';')]
+ return writefile(text, '/tmp/live-rb')
+ endfunction
+
+ map <Leader>x :call EvalLiveRuby()<enter>
+
+Then by pressing leader and x you will execute the file or range in the live session
+
+
View
71 lib/live.rb
@@ -6,10 +6,40 @@
module Live
class Notice < String; end
+
+ class Context
+ def initialize session
+ @session = session
+ @binding = binding
+ end
+
+ def eval string
+ begin
+ super string, @binding
+ rescue Exception => exception
+ SystemExit === exception ? quit! : exception
+ end
+ end
+
+ # Binds a proc to a keystroke
+ def bind_key key, &block
+ @session.key_bindings[key.to_s.unpack('c').first] = block
+ Notice.new "Key '#{key}' is bound to an action" if block
+ end
+
+ def quit!
+ @session.quit!
+ end
+
+ def reset!
+ @session.key_bindings.clear
+ @session.new_context
+ end
+ end
class Session
include HighLine::SystemExtensions
- attr_reader :path
+ attr_reader :path, :key_bindings
# Starts a live session using a named pipe to receive code from a remote source and evaluates it within a context, a bit like an IRB session but evaluates code sent from a text editor
def initialize path = "#{Dir.tmpdir}/live-rb"
@@ -20,28 +50,15 @@ def initialize path = "#{Dir.tmpdir}/live-rb"
@pipe, @path, @key_bindings = File.open(path, 'r+'), path, {}
begin
- get_binding and key_listen and run!
+ new_context and key_listen and run!
ensure
- quit!
- end
- end
-
- def safe_eval *args
- begin
- eval *args
- rescue Exception => exception
- SystemExit === exception ? quit! : exception
+ File.delete(path) if File.exists? path
end
end
def quit!
File.delete(@path) && exit
end
-
- # Starts a loop that checks the named pipe and evaluate its contents, will be called on initialize
- def run!
- loop { puts safe_eval(@pipe.gets, @context) }
- end
def puts obj
super obj.colored_inspect
@@ -50,6 +67,16 @@ def puts obj
super "\e[2A"
end
+ def new_context
+ @context = Context.new(self)
+ end
+
+ private
+ # Starts a loop that checks the named pipe and evaluate its contents, will be called on initialize
+ def run!
+ loop { puts @context.eval(@pipe.gets) }
+ end
+
# Listen for keystrokes and calls bound procs
def key_listen
Thread.new do
@@ -59,17 +86,5 @@ def key_listen
end
end
end
-
- # Binds a proc to a keystroke
- def bind_key key, &block
- @key_bindings[key.to_s.unpack('c').first] = block
- Notice.new "Key '#{key}' is bound to an action" if block
- end
-
- def get_binding
- @context = binding
- end
-
- alias :reload! :get_binding
end
end

0 comments on commit e71ca27

Please sign in to comment.