live 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ *.swp
5
+ *.swo
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in live.gemspec
4
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,36 @@
1
+ Live
2
+ ====
3
+
4
+ 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.
5
+ 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.
6
+
7
+ Usage
8
+ -----
9
+
10
+ $ live
11
+
12
+ open a new terminal
13
+
14
+ $ echo 'hello' > /tmp/live-rb
15
+
16
+ You will see the code evaluated in the first terminal
17
+
18
+ $ echo 'bind_key(:a){ 'key a was pressed' }' > /tmp/live-rb
19
+
20
+ When you press the key 'a' with focus on the first terminal it will call the block
21
+
22
+ Vim
23
+ ---
24
+
25
+ To use from vim paste this in your .vimrc file
26
+
27
+ function EvalLiveRuby() range
28
+ let text = [join(getline(a:firstline, a:lastline), ';')]
29
+ return writefile(text, '/tmp/live-rb')
30
+ endfunction
31
+
32
+ map <Leader>x :call EvalLiveRuby()<enter>
33
+
34
+ Then by pressing leader and x you will execute the file or range in the live session
35
+
36
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/bin/live ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
3
+
4
+ require 'live'
5
+
6
+ Live::Session.new
data/lib/ext/object.rb ADDED
@@ -0,0 +1,21 @@
1
+ class Object
2
+ # Outputs an ANSI colored string with the object representation
3
+ def colored_inspect
4
+ case self
5
+ when Exception
6
+ "\e[41;33m#{self.inspect}\e[0m"
7
+ when Numeric, Symbol, TrueClass, FalseClass, NilClass
8
+ "\e[35m#{self.inspect}\e[0m"
9
+ when Live::Notice
10
+ "\e[42;30m#{self}\e[0m"
11
+ when String
12
+ "\e[32m#{self.inspect}\e[0m"
13
+ when Array
14
+ "[#{ self.collect{ |i| i.colored_inspect}.join(', ') }]"
15
+ when Hash
16
+ "{#{ self.collect{ |i| i.collect{|e| e.colored_inspect}.join(' => ') }.join(', ') }}"
17
+ else
18
+ "\e[36m#{self}\e[0m"
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module Live
2
+ VERSION = "0.1.0"
3
+ end
data/lib/live.rb ADDED
@@ -0,0 +1,90 @@
1
+ require 'tempfile'
2
+ require 'highline'
3
+
4
+ $LOAD_PATH.unshift File.dirname(__FILE__)
5
+ require 'ext/object'
6
+
7
+ module Live
8
+ class Notice < String; end
9
+
10
+ class Context
11
+ def initialize session
12
+ @session = session
13
+ @binding = binding
14
+ end
15
+
16
+ def eval string
17
+ begin
18
+ super string, @binding
19
+ rescue Exception => exception
20
+ SystemExit === exception ? quit! : exception
21
+ end
22
+ end
23
+
24
+ # Binds a proc to a keystroke
25
+ def bind_key key, &block
26
+ @session.key_bindings[key.to_s.unpack('c').first] = block
27
+ Notice.new "Key '#{key}' is bound to an action" if block
28
+ end
29
+
30
+ def quit!
31
+ @session.quit!
32
+ end
33
+
34
+ def reset!
35
+ @session.key_bindings.clear
36
+ @session.new_context
37
+ end
38
+ end
39
+
40
+ class Session
41
+ include HighLine::SystemExtensions
42
+ attr_reader :path, :key_bindings
43
+
44
+ # 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
45
+ def initialize path = "#{Dir.tmpdir}/live-rb"
46
+ raise Exception.new("Another session sems to be running: #{path}") if File.exist? path
47
+ puts Notice.new("Live Session: #{path}")
48
+
49
+ %x{mkfifo #{path}}
50
+ @pipe, @path, @key_bindings = File.open(path, 'r+'), path, {}
51
+
52
+ begin
53
+ new_context and key_listen and run!
54
+ ensure
55
+ File.delete(path) if File.exists? path
56
+ end
57
+ end
58
+
59
+ def quit!
60
+ File.delete(@path) && exit
61
+ end
62
+
63
+ def puts obj
64
+ super obj.colored_inspect
65
+ # Hackish solution for cursor position
66
+ super "\e[200D"
67
+ super "\e[2A"
68
+ end
69
+
70
+ def new_context
71
+ @context = Context.new(self)
72
+ end
73
+
74
+ private
75
+ # Starts a loop that checks the named pipe and evaluate its contents, will be called on initialize
76
+ def run!
77
+ loop { puts @context.eval(@pipe.gets) }
78
+ end
79
+
80
+ # Listen for keystrokes and calls bound procs
81
+ def key_listen
82
+ Thread.new do
83
+ loop do
84
+ block = @key_bindings[get_character]
85
+ puts block.call if block
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
data/live.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "live/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "live"
7
+ s.version = Live::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Macario Ortega"]
10
+ s.email = ["macarui@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Live is like IRC only it is meant to be used from a text editor}
13
+ s.description = %q{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.
14
+ 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.
15
+ }
16
+
17
+ s.add_dependency 'highline'
18
+
19
+ s.rubyforge_project = "live"
20
+ s.files = `git ls-files`.split("\n")
21
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
23
+ s.require_paths = ["lib"]
24
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: live
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Macario Ortega
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-03-14 00:00:00 -06:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: highline
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ description: |
34
+ 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.
35
+ 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.
36
+
37
+ email:
38
+ - macarui@gmail.com
39
+ executables:
40
+ - live
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - .gitignore
47
+ - Gemfile
48
+ - README.rdoc
49
+ - Rakefile
50
+ - bin/live
51
+ - lib/ext/object.rb
52
+ - lib/live.rb
53
+ - lib/live/version.rb
54
+ - live.gemspec
55
+ has_rdoc: true
56
+ homepage: ""
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options: []
61
+
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ requirements: []
81
+
82
+ rubyforge_project: live
83
+ rubygems_version: 1.3.7
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Live is like IRC only it is meant to be used from a text editor
87
+ test_files: []
88
+