dietrb 0.4.7 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE CHANGED
@@ -1,10 +1,16 @@
1
1
  Copyright (c) <2010> Eloy Duran, <eloy.de.enige@gmail.com>
2
2
 
3
- Portions Copyright (c) 2006-2010 Ben Bleything <ben@bleything.net> (history utils)
4
- Portions Copyright (c) 2006-2010 Paul Duncan <pabs@pablotron.org> (Wirble)
5
- Portions Copyright (c) 2009-2010 Jens Wille <jens.wille@gmail.com> (Wirble)
6
- Portions Copyright (c) 2006-2010 Giles Bowkett (light background color scheme)
7
- Portions Copyright (c) 2006-2010 Revieworld Ltd. <http://github.com/floehopper/mocha> (class/address object inspect)
3
+ Portions Copyright (c):
4
+ * 2006-2010 Ben Bleything <ben@bleything.net> (history utils)
5
+ * 2006-2010 Paul Duncan <pabs@pablotron.org> (Wirble)
6
+ * 2009-2010 Jens Wille <jens.wille@gmail.com> (Wirble)
7
+ * 2006-2010 Giles Bowkett (light background color scheme)
8
+ * 2006-2010 Revieworld Ltd. <http://github.com/floehopper/mocha>
9
+ (class/address object inspect)
10
+ * 2008-2010 Vincent Isambart <http://github.com/vincentisambart/hotconsole>
11
+ (output redirection on different threads)
12
+ * 2010 James Tucker <http://github.com/raggi/ir>
13
+ (basis of driver code and inspiration)
8
14
 
9
15
  This software is available under the Ruby license:
10
16
 
data/Rakefile CHANGED
@@ -7,12 +7,12 @@ task :default => :run
7
7
 
8
8
  desc "Run the specs (run it with a rake installed on the Ruby version you want to run the specs on)"
9
9
  task :spec do
10
- sh "#{ruby_bin} -r #{FileList['./spec/**/*_spec.rb'].join(' -r ')} -e ''"
10
+ sh "mspec -t #{ruby_bin} spec"
11
11
  end
12
12
 
13
13
  desc "Run dietrb"
14
14
  task :run do
15
- sh "#{ruby_bin} -I lib ./bin/dietrb -r irb/ext/colorize -r pp"
15
+ sh "#{ruby_bin} -I lib ./bin/dietrb -d -r irb/ext/colorize -r pp"
16
16
  end
17
17
 
18
18
  namespace :macruby do
@@ -33,16 +33,19 @@ namespace :macruby do
33
33
  desc "Merge source into the MacRuby repo"
34
34
  task :merge do
35
35
  if (repo = ENV['macruby_repo']) && File.exist?(repo)
36
- bin = File.join(repo, 'bin/irb')
37
- lib = File.join(repo, 'lib')
36
+ bin = File.join(repo, 'bin/irb')
37
+ lib = File.join(repo, 'lib')
38
+ spec = File.join(repo, 'spec/dietrb')
38
39
 
39
- rm_f bin
40
- rm_f File.join(lib, 'irb.rb')
40
+ rm_f bin
41
+ rm_f File.join(lib, 'irb.rb')
41
42
  rm_rf File.join(lib, 'irb')
43
+ rm_rf spec
42
44
 
43
- cp 'bin/dietrb', bin
44
- cp 'lib/irb.rb', lib
45
- cp_r 'lib/irb', lib
45
+ cp 'bin/dietrb', bin
46
+ cp 'lib/irb.rb', lib
47
+ cp_r 'lib/irb', lib
48
+ cp_r 'spec', spec
46
49
  else
47
50
  puts "[!] Set the `macruby_repo' env variable to point to the MacRuby repo checkout"
48
51
  exit 1
@@ -51,6 +54,7 @@ namespace :macruby do
51
54
  end
52
55
 
53
56
  begin
57
+ require 'rubygems'
54
58
  require 'jeweler'
55
59
  require File.expand_path('../lib/irb/version', __FILE__)
56
60
  Jeweler::Tasks.new do |gemspec|
data/TODO CHANGED
@@ -1,3 +1,4 @@
1
+ * Make puts/print use the proper IO when used from user code
1
2
  * Configurable history file? (:HISTORY_FILE) Configurable number of saved history lines? (:SAVE_HISTORY)
2
3
  * Make sure the following formatters work: hirb, awesome_print, and looksee
3
4
  * Make sure the majority of the utils in utility_belt work
data/bin/dietrb CHANGED
@@ -2,41 +2,55 @@
2
2
 
3
3
  require 'irb'
4
4
 
5
- unless ARGV.empty?
6
- require 'optparse'
7
-
8
- ignore_irbrc = false
9
-
10
- OptionParser.new do |opt|
11
- bin = File.basename($0)
12
- opt.banner = "Usage: #{bin} [options] [programfile] [arguments]"
13
- opt.on("-f", "Ignore ~/.irbrc") { |ignore| ignore_irbrc = ignore }
14
- opt.on("-r load-lib", "Loads the given library (same as `ruby -r')") { |lib| require lib }
15
- opt.on("-d", "Set $DEBUG to true (same as `ruby -d')") { $DEBUG = true }
16
- opt.on("-I path", "Add path to $LOAD_PATH") { |path| $LOAD_PATH.unshift(path) }
17
- opt.on("--noinspect", "Don't use inspect for output") { IRB.formatter.inspect = false }
18
- opt.on("--simple-prompt", "Simple prompt mode") { IRB.formatter.prompt = :simple }
19
- opt.on("--noprompt", "No prompt mode") { IRB.formatter.prompt = nil }
20
- opt.on("-v", "--version", "Print the version of #{bin}") do
21
- puts IRB::VERSION::DESCRIPTION
22
- exit
23
- end
24
- end.parse!(ARGV)
25
- end
26
-
27
- unless ignore_irbrc
28
- irbrc = File.expand_path("~/.irbrc")
29
- load(irbrc) if File.exist?(irbrc)
30
- end
31
-
32
- IRB.formatter.filter_from_backtrace << /^#{__FILE__}/
5
+ IRB_CONTEXT_TOPLEVEL_ARGS = [self, TOPLEVEL_BINDING.dup]
33
6
 
34
- if ARGV.empty?
35
- irb(self, TOPLEVEL_BINDING.dup)
36
- else
37
- path = ARGV.shift
38
- context = IRB::Context.new(self, TOPLEVEL_BINDING.dup)
39
- File.open(path, 'r') do |file|
40
- file.each_line { |line| context.input_line(line) }
7
+ module IRB
8
+ # Just a namespace so not to pollute the toplevel namespace with lvars.
9
+ module Bin
10
+ unless ARGV.empty?
11
+ require 'optparse'
12
+
13
+ driver = nil
14
+ ignore_irbrc = false
15
+
16
+ OptionParser.new do |opt|
17
+ bin = File.basename($0)
18
+ opt.banner = "Usage: #{bin} [options] [programfile] [arguments]"
19
+ opt.on("-f", "Ignore ~/.irbrc") { |i| ignore_irbrc = i }
20
+ opt.on("-r load-lib", "Loads the given library (same as `ruby -r')") { |l| require l }
21
+ opt.on("-d", "Set $DEBUG to true (same as `ruby -d')") { $DEBUG = true }
22
+ opt.on("-I path", "Add path to $LOAD_PATH") { |p| $LOAD_PATH.unshift(p) }
23
+ opt.on("--driver name", "As driver, use one of: tty, readline, or socket") { |d| driver = d }
24
+ opt.on("--noinspect", "Don't use inspect for output") { IRB.formatter.inspect = false }
25
+ opt.on("--simple-prompt", "Simple prompt mode") { IRB.formatter.prompt = :simple }
26
+ opt.on("--noprompt", "No prompt mode") { IRB.formatter.prompt = nil }
27
+ opt.on("-v", "--version", "Print the version of #{bin}") do
28
+ $stdout.puts IRB::VERSION::DESCRIPTION
29
+ exit
30
+ end
31
+ end.parse!(ARGV)
32
+ end
33
+
34
+ unless ignore_irbrc
35
+ irbrc = File.expand_path("~/.irbrc")
36
+ load(irbrc) if File.exist?(irbrc)
37
+ end
38
+
39
+ IRB.formatter.filter_from_backtrace << /^#{__FILE__}/
40
+
41
+ if ARGV.empty?
42
+ driver ||= begin
43
+ require 'readline'
44
+ 'readline'
45
+ rescue LoadError
46
+ 'tty'
47
+ end
48
+ require "irb/driver/#{driver}"
49
+ irb(*IRB_CONTEXT_TOPLEVEL_ARGS)
50
+ else
51
+ path = ARGV.shift
52
+ context = IRB::Context.new(*IRB_CONTEXT_TOPLEVEL_ARGS)
53
+ File.open(path, 'r') { |f| f.each_line { |line| context.input_line(line) } }
54
+ end
41
55
  end
42
56
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{dietrb}
8
- s.version = "0.4.7"
8
+ s.version = "0.5.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Eloy Duran"]
12
- s.date = %q{2010-07-02}
12
+ s.date = %q{2010-07-24}
13
13
  s.default_executable = %q{dietrb}
14
14
  s.description = %q{IRB on a diet, for MacRuby / Ruby 1.9}
15
15
  s.email = %q{eloy.de.enige@gmail.com}
@@ -30,6 +30,10 @@ Gem::Specification.new do |s|
30
30
  "lib/irb/completion.rb",
31
31
  "lib/irb/context.rb",
32
32
  "lib/irb/deprecated.rb",
33
+ "lib/irb/driver.rb",
34
+ "lib/irb/driver/readline.rb",
35
+ "lib/irb/driver/socket.rb",
36
+ "lib/irb/driver/tty.rb",
33
37
  "lib/irb/ext/colorize.rb",
34
38
  "lib/irb/ext/completion.rb",
35
39
  "lib/irb/ext/history.rb",
@@ -37,11 +41,14 @@ Gem::Specification.new do |s|
37
41
  "lib/irb/formatter.rb",
38
42
  "lib/irb/source.rb",
39
43
  "lib/irb/version.rb",
40
- "spec/colorize_spec.rb",
41
- "spec/completion_spec.rb",
42
44
  "spec/context_spec.rb",
45
+ "spec/driver/readline_spec.rb",
46
+ "spec/driver/tty_spec.rb",
47
+ "spec/driver_spec.rb",
48
+ "spec/ext/colorize_spec.rb",
49
+ "spec/ext/completion_spec.rb",
50
+ "spec/ext/history_spec.rb",
43
51
  "spec/formatter_spec.rb",
44
- "spec/history_spec.rb",
45
52
  "spec/regression/context_spec.rb",
46
53
  "spec/source_spec.rb",
47
54
  "spec/spec_helper.rb"
@@ -50,14 +57,17 @@ Gem::Specification.new do |s|
50
57
  s.rdoc_options = ["--charset=UTF-8"]
51
58
  s.require_paths = ["lib"]
52
59
  s.required_ruby_version = Gem::Requirement.new("~> 1.9")
53
- s.rubygems_version = %q{1.3.7}
60
+ s.rubygems_version = %q{1.3.5}
54
61
  s.summary = %q{IRB on a diet, for MacRuby / Ruby 1.9}
55
62
  s.test_files = [
56
- "spec/colorize_spec.rb",
57
- "spec/completion_spec.rb",
58
- "spec/context_spec.rb",
63
+ "spec/context_spec.rb",
64
+ "spec/driver/readline_spec.rb",
65
+ "spec/driver/tty_spec.rb",
66
+ "spec/driver_spec.rb",
67
+ "spec/ext/colorize_spec.rb",
68
+ "spec/ext/completion_spec.rb",
69
+ "spec/ext/history_spec.rb",
59
70
  "spec/formatter_spec.rb",
60
- "spec/history_spec.rb",
61
71
  "spec/regression/context_spec.rb",
62
72
  "spec/source_spec.rb",
63
73
  "spec/spec_helper.rb"
@@ -67,7 +77,7 @@ Gem::Specification.new do |s|
67
77
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
68
78
  s.specification_version = 3
69
79
 
70
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
80
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
71
81
  else
72
82
  end
73
83
  else
data/lib/irb.rb CHANGED
@@ -10,9 +10,6 @@ require 'irb/version'
10
10
 
11
11
  require 'irb/deprecated'
12
12
 
13
- require 'irb/ext/history'
14
- require 'irb/ext/completion'
15
-
16
13
  # if !ENV['SPECCING'] && defined?(RUBY_ENGINE) && RUBY_ENGINE == "macruby"
17
14
  # require 'irb/ext/macruby'
18
15
  # end
@@ -1 +1 @@
1
- IRB.deprecated "The file `irb/completion' has moved to `irb/ext/ecompletion' and is loaded by default.", caller
1
+ IRB.deprecated "The file `irb/completion' has moved to `irb/ext/completion' and is loaded by default.", caller
@@ -5,28 +5,13 @@
5
5
  # Copyright (C) 2009-2010, Eloy Duran <eloy.de.enige@gmail.com>
6
6
 
7
7
  require 'irb/formatter'
8
- require 'readline'
9
8
 
10
9
  module IRB
11
10
  class Context
12
- class << self
13
- attr_accessor :current
14
-
15
- def make_current(context)
16
- # Messing with a current context is gonna bite me in the ass when we
17
- # get to multi-threading, but we'll it when we get there.
18
- before, @current = @current, context
19
- yield
20
- ensure
21
- @current = before
22
- end
23
-
24
- def processors
25
- @processors ||= []
26
- end
27
- end
11
+ IGNORE_RESULT = :irb_ignore_result
28
12
 
29
- attr_reader :object, :binding, :line, :source, :processors
13
+ attr_reader :object, :binding, :line, :source
14
+ attr_accessor :formatter
30
15
 
31
16
  def initialize(object, explicit_binding = nil)
32
17
  @object = object
@@ -34,8 +19,8 @@ module IRB
34
19
  @line = 1
35
20
  clear_buffer
36
21
 
37
- @underscore_assigner = __evaluate__("_ = nil; proc { |val| _ = val }")
38
- @processors = self.class.processors.map { |processor| processor.new(self) }
22
+ @last_result_assigner = __evaluate__("_ = nil; proc { |val| _ = val }")
23
+ @exception_assigner = __evaluate__("e = exception = nil; proc { |ex| e = exception = ex }")
39
24
  end
40
25
 
41
26
  def __evaluate__(source, file = __FILE__, line = __LINE__)
@@ -44,31 +29,14 @@ module IRB
44
29
 
45
30
  def evaluate(source)
46
31
  result = __evaluate__(source.to_s, '(irb)', @line - @source.buffer.size + 1)
47
- store_result(result)
48
- puts formatter.result(result)
49
- result
32
+ unless result == IGNORE_RESULT
33
+ store_result(result)
34
+ puts(formatter.result(result))
35
+ result
36
+ end
50
37
  rescue Exception => e
51
38
  store_exception(e)
52
- puts formatter.exception(e)
53
- end
54
-
55
- # Reads input and passes it to all processors.
56
- def readline
57
- input = Readline.readline(formatter.prompt(self), true)
58
- @processors.each { |processor| input = processor.input(input) }
59
- input
60
- rescue Interrupt
61
- clear_buffer
62
- ""
63
- end
64
-
65
- def run
66
- self.class.make_current(self) do
67
- while line = readline
68
- continue = process_line(line)
69
- break unless continue
70
- end
71
- end
39
+ puts(formatter.exception(e))
72
40
  end
73
41
 
74
42
  # Returns whether or not the user wants to continue the current runloop.
@@ -88,7 +56,7 @@ module IRB
88
56
  return false if @source.terminate?
89
57
 
90
58
  if @source.syntax_error?
91
- puts formatter.syntax_error(@line, @source.syntax_error)
59
+ puts(formatter.syntax_error(@line, @source.syntax_error))
92
60
  @source.pop
93
61
  elsif @source.code_block?
94
62
  evaluate(@source)
@@ -99,13 +67,17 @@ module IRB
99
67
  true
100
68
  end
101
69
 
70
+ def prompt
71
+ formatter.prompt(self)
72
+ end
73
+
102
74
  def input_line(line)
103
- puts formatter.prompt(self) + line
75
+ puts(formatter.prompt(self) + line)
104
76
  process_line(line)
105
77
  end
106
78
 
107
79
  def formatter
108
- IRB.formatter
80
+ @formatter ||= IRB.formatter
109
81
  end
110
82
 
111
83
  def clear_buffer
@@ -113,20 +85,11 @@ module IRB
113
85
  end
114
86
 
115
87
  def store_result(result)
116
- @underscore_assigner.call(result)
88
+ @last_result_assigner.call(result)
117
89
  end
118
90
 
119
91
  def store_exception(exception)
120
- $e = $EXCEPTION = exception
92
+ @exception_assigner.call(exception)
121
93
  end
122
94
  end
123
- end
124
-
125
- module Kernel
126
- # Creates a new IRB::Context with the given +object+ and runs it.
127
- def irb(object, binding = nil)
128
- IRB::Context.new(object, binding).run
129
- end
130
-
131
- private :irb
132
- end
95
+ end
@@ -0,0 +1,61 @@
1
+ module IRB
2
+ module Driver
3
+ class << self
4
+ def current=(driver)
5
+ ThreadGroup.new.add(Thread.current)
6
+ Thread.current[:irb_driver] = driver
7
+ end
8
+
9
+ def current
10
+ current_thread = Thread.current
11
+ current_thread[:irb_driver] ||= begin
12
+ if group = current_thread.group
13
+ driver = nil
14
+ group.list.each do |thread|
15
+ break if driver = thread[:irb_driver]
16
+ end
17
+ driver
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ class OutputRedirector
24
+ def self.target
25
+ if driver = IRB::Driver.current
26
+ driver.output
27
+ else
28
+ $stderr
29
+ end
30
+ end
31
+
32
+ # A standard output object has only one mandatory method: write.
33
+ # It returns the number of characters written
34
+ def write(object)
35
+ string = object.respond_to?(:to_str) ? object : object.to_s
36
+ send_to_target :write, string
37
+ string.length
38
+ end
39
+
40
+ # if puts is not there, Ruby will automatically use the write
41
+ # method when calling Kernel#puts, but defining it has 2 advantages:
42
+ # - if puts is not defined, you cannot of course use $stdout.puts directly
43
+ # - (objc) when Ruby emulates puts, it calls write twice
44
+ # (once for the string and once for the carriage return)
45
+ # but here we send the calls to another thread so it's nice
46
+ # to be able to save up one (slow) interthread call
47
+ def puts(*args)
48
+ send_to_target :puts, *args
49
+ nil
50
+ end
51
+
52
+ # Override this if for your situation you need to dispatch from a thread
53
+ # in a special manner.
54
+ #
55
+ # TODO: for macruby send to main thread
56
+ def send_to_target(method, *args)
57
+ self.class.target.__send__(method, *args)
58
+ end
59
+ end
60
+ end
61
+ end