dietrb 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE CHANGED
@@ -1,3 +1,8 @@
1
1
  Copyright (c) <2010> Eloy Duran, <eloy.de.enige@gmail.com>
2
2
 
3
+ Portions Copyright (c) 2006-2010 Ben Bleything (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
+
3
8
  This software is available under the Ruby license.
data/README.rdoc CHANGED
@@ -11,6 +11,39 @@ not grow too much more. For now my things to-do are .irbrc support, completion,
11
11
  and investigate what else people really really need. After that it's time to
12
12
  polish.
13
13
 
14
+ == Extensions
15
+
16
+ * irb/ext/colorize.rb, adds support for colorizing the prompt and result. The
17
+ code was based upon Wirble's implementation, so your custom Wirble themes
18
+ should still work.
19
+
20
+ Configure it with:
21
+
22
+ * IRB.formatter.color_scheme = scheme, where scheme can be: :dark_background,
23
+ :light_background, or :fresh. Defaults to :dark_background.
24
+
25
+ * IRB.formatter.colors returns the hash of current token-type to color
26
+ mappings.
27
+
28
+ * irb/ext/completion.rb, adds, as the name implies, autocompletion for
29
+ constants, variables, methods, etc.
30
+
31
+ * irb/ext/history.rb, stores/loads the history in and from the history file,
32
+ which is located at ~/.irb_history.
33
+
34
+ It provides the following API:
35
+
36
+ * Kernel#history(N), or Kernel#h, will show N number of the most recent history
37
+ entries. Defaults to 50.
38
+
39
+ * Kernel#history!(entry_or_range), or Kernel#h!, will execute the specified
40
+ history entry, or entries if a range is given.
41
+
42
+ * Kernel#clear_history! will clear the history and the history file.
43
+
44
+ * irb/ext/macruby.rb, adds support for Cocoa development by starting an
45
+ NSRunloop. Loaded by default on MacRuby.
46
+
14
47
  == Differences
15
48
 
16
49
  * This IRB version specifically targets MacRuby, for now, and allows Cocoa
data/Rakefile CHANGED
@@ -2,7 +2,8 @@ task :default => :run
2
2
 
3
3
  desc "Run the specs"
4
4
  task :spec do
5
- sh "macbacon #{FileList['spec/**/*_spec.rb'].join(' ')}"
5
+ # sh "macbacon #{FileList['spec/**/*_spec.rb'].join(' ')}"
6
+ sh "bacon19 #{FileList['spec/**/*_spec.rb'].join(' ')}"
6
7
  end
7
8
 
8
9
  desc "Run specs with Kicker"
@@ -12,7 +13,7 @@ end
12
13
 
13
14
  desc "Run dietrb with ruby19"
14
15
  task :run do
15
- sh "ruby19 -Ilib ./bin/dietrb -r irb/ext/completion"
16
+ sh "ruby19 -Ilib ./bin/dietrb -r irb/ext/completion -r irb/ext/colorize -r pp"
16
17
  end
17
18
 
18
19
  begin
@@ -25,6 +26,7 @@ begin
25
26
  gemspec.authors = ["Eloy Duran"]
26
27
 
27
28
  gemspec.required_ruby_version = ::Gem::Requirement.new("~> 1.9")
29
+ gemspec.files.reject! { |file| file =~ /^extensions/ }
28
30
  end
29
31
  rescue LoadError
30
32
  end
data/TODO ADDED
@@ -0,0 +1,7 @@
1
+ * Add history manager support
2
+ * Add colored output, possibly based on the Wirble code
3
+ * Make sure the following formatters work: hirb, awesome_print, and looksee
4
+ * Make sure the majority of the utils in utility_belt work
5
+ * Possibly add copy-paste support as an ext
6
+ * Make sure ported/used code is attributed and contact authors about Ruby license
7
+ * Decide whether or not we need more control for colorizing which could be done with Ripper::SexpBuilder
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
data/bin/dietrb CHANGED
@@ -20,6 +20,9 @@ unless ARGV.empty?
20
20
  end.parse!(ARGV)
21
21
  end
22
22
 
23
+ irbrc = File.expand_path("~/.irbrc")
24
+ load(irbrc) if File.exist?(irbrc)
25
+
23
26
  IRB.formatter.filter_from_backtrace << /^#{__FILE__}/
24
27
 
25
28
  if ARGV.empty?
@@ -28,9 +31,6 @@ else
28
31
  path = ARGV.shift
29
32
  context = IRB::Context.new(self, TOPLEVEL_BINDING.dup)
30
33
  File.open(path, 'r') do |file|
31
- file.each_line do |line|
32
- puts IRB.formatter.prompt(context) + line
33
- context.process_line(line)
34
- end
34
+ file.each_line { |line| context.input_line(line) }
35
35
  end
36
36
  end
data/dietrb.gemspec CHANGED
@@ -5,37 +5,42 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{dietrb}
8
- s.version = "0.3.0"
8
+ s.version = "0.4.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-02-24}
12
+ s.date = %q{2010-04-18}
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}
16
16
  s.executables = ["dietrb"]
17
17
  s.extra_rdoc_files = [
18
18
  "LICENSE",
19
- "README.rdoc"
19
+ "README.rdoc",
20
+ "TODO"
20
21
  ]
21
22
  s.files = [
22
23
  ".gitignore",
23
24
  "LICENSE",
24
25
  "README.rdoc",
25
26
  "Rakefile",
27
+ "TODO",
26
28
  "VERSION",
27
29
  "bin/dietrb",
28
30
  "dietrb.gemspec",
29
31
  "lib/irb.rb",
30
32
  "lib/irb/context.rb",
33
+ "lib/irb/ext/colorize.rb",
31
34
  "lib/irb/ext/completion.rb",
35
+ "lib/irb/ext/history.rb",
32
36
  "lib/irb/ext/macruby.rb",
33
37
  "lib/irb/formatter.rb",
34
38
  "lib/irb/source.rb",
39
+ "spec/colorize_spec.rb",
35
40
  "spec/completion_spec.rb",
36
41
  "spec/context_spec.rb",
37
42
  "spec/formatter_spec.rb",
38
- "spec/irb_spec.rb",
43
+ "spec/history_spec.rb",
39
44
  "spec/source_spec.rb",
40
45
  "spec/spec_helper.rb"
41
46
  ]
@@ -46,10 +51,11 @@ Gem::Specification.new do |s|
46
51
  s.rubygems_version = %q{1.3.5}
47
52
  s.summary = %q{IRB on a diet, for MacRuby / Ruby 1.9}
48
53
  s.test_files = [
49
- "spec/completion_spec.rb",
54
+ "spec/colorize_spec.rb",
55
+ "spec/completion_spec.rb",
50
56
  "spec/context_spec.rb",
51
57
  "spec/formatter_spec.rb",
52
- "spec/irb_spec.rb",
58
+ "spec/history_spec.rb",
53
59
  "spec/source_spec.rb",
54
60
  "spec/spec_helper.rb"
55
61
  ]
data/lib/irb/context.rb CHANGED
@@ -7,20 +7,28 @@ module IRB
7
7
  attr_accessor :current
8
8
 
9
9
  def make_current(context)
10
+ # Messing with a current context is gonna bite me in the ass when we
11
+ # get to multi-threading, but we'll it when we get there.
10
12
  before, @current = @current, context
11
13
  yield
12
14
  ensure
13
15
  @current = before
14
16
  end
17
+
18
+ def processors
19
+ @processors ||= []
20
+ end
15
21
  end
16
22
 
17
- attr_reader :object, :binding, :line, :source
23
+ attr_reader :object, :binding, :line, :source, :processors
18
24
 
19
25
  def initialize(object, explicit_binding = nil)
20
26
  @object = object
21
27
  @binding = explicit_binding || object.instance_eval { binding }
22
28
  @line = 1
23
29
  clear_buffer
30
+
31
+ @processors = self.class.processors.map { |processor| processor.new(self) }
24
32
  end
25
33
 
26
34
  def __evaluate__(source, file = __FILE__, line = __LINE__)
@@ -35,8 +43,11 @@ module IRB
35
43
  puts formatter.exception(e)
36
44
  end
37
45
 
46
+ # Reads input and passes it to all processors.
38
47
  def readline
39
- Readline.readline(formatter.prompt(self), true)
48
+ input = Readline.readline(formatter.prompt(self), true)
49
+ @processors.each { |processor| input = processor.input(input) }
50
+ input
40
51
  rescue Interrupt
41
52
  clear_buffer
42
53
  ""
@@ -79,7 +90,10 @@ module IRB
79
90
  true
80
91
  end
81
92
 
82
- private
93
+ def input_line(line)
94
+ puts formatter.prompt(self) + line
95
+ process_line(line)
96
+ end
83
97
 
84
98
  def formatter
85
99
  IRB.formatter
@@ -89,4 +103,13 @@ module IRB
89
103
  @source = Source.new
90
104
  end
91
105
  end
106
+ end
107
+
108
+ module Kernel
109
+ # Creates a new IRB::Context with the given +object+ and runs it.
110
+ def irb(object, binding = nil)
111
+ IRB::Context.new(object, binding).run
112
+ end
113
+
114
+ private :irb
92
115
  end
@@ -0,0 +1,177 @@
1
+ module IRB
2
+ # This code is based upon Wirble (http://pablotron.org/software/wirble/) by
3
+ # Paul Duncan and Jens Wille.
4
+ class ColoredFormatter < Formatter
5
+ TYPE_ALIASES = {
6
+ :on_comma => :comma,
7
+ :refers => :operator, # Wirble compat
8
+ :on_op => :operator,
9
+
10
+ :on_lbrace => :open_hash,
11
+ :on_rbrace => :close_hash,
12
+ :on_lbracket => :open_array,
13
+ :on_rbracket => :close_array,
14
+
15
+ :on_ident => :symbol,
16
+ :on_symbeg => :symbol_prefix,
17
+
18
+ :on_tstring_beg => :open_string,
19
+ :on_tstring_content => :string,
20
+ :on_tstring_end => :close_string,
21
+
22
+ :on_int => :number,
23
+ :on_kw => :keyword,
24
+ :on_const => :constant,
25
+ :class => :constant # Wirble compat
26
+ }
27
+
28
+ # # object colors
29
+ # :open_object => :dark_gray,
30
+ # :object_class => :purple,
31
+ # :object_addr_prefix => :blue,
32
+ # :object_line_prefix => :blue,
33
+ # :close_object => :dark_gray,
34
+
35
+ COLOR_SCHEMES = {
36
+ :dark_background => {
37
+ # :prompt => :green,
38
+ # :result_prefix => :light_purple,
39
+
40
+ :comma => :blue,
41
+ :operator => :blue,
42
+
43
+ :open_hash => :green,
44
+ :close_hash => :green,
45
+ :open_array => :green,
46
+ :close_array => :green,
47
+
48
+ :symbol_prefix => :yellow, # hmm ident...
49
+ :symbol => :yellow,
50
+
51
+ :open_string => :red,
52
+ :string => :cyan,
53
+ :close_string => :red,
54
+
55
+ :number => :cyan,
56
+ :keyword => :yellow,
57
+ :constant => :light_green
58
+ },
59
+ :light_background => {
60
+ :comma => :purple,
61
+ :operator => :blue,
62
+
63
+ :open_hash => :red,
64
+ :close_hash => :red,
65
+ :open_array => :red,
66
+ :close_array => :red,
67
+
68
+ :symbol_prefix => :black,
69
+ :symbol => :light_gray,
70
+
71
+ :open_string => :blue,
72
+ :string => :dark_gray,
73
+ :close_string => :blue,
74
+
75
+ :number => :black,
76
+ :keyword => :brown,
77
+ :constant => :red
78
+ },
79
+ :fresh => {
80
+ :prompt => :green,
81
+ :result_prefix => :light_purple,
82
+
83
+ :comma => :red,
84
+ :operator => :red,
85
+
86
+ :open_hash => :blue,
87
+ :close_hash => :blue,
88
+ :open_array => :green,
89
+ :close_array => :green,
90
+
91
+ :symbol_prefix => :yellow,
92
+ :symbol => :yellow,
93
+
94
+ :number => :cyan,
95
+ :string => :cyan,
96
+ :keyword => :white
97
+ }
98
+ }
99
+
100
+ #
101
+ # Terminal escape codes for colors.
102
+ #
103
+ module Color
104
+ COLORS = {
105
+ :nothing => '0;0',
106
+ :black => '0;30',
107
+ :red => '0;31',
108
+ :green => '0;32',
109
+ :brown => '0;33',
110
+ :blue => '0;34',
111
+ :cyan => '0;36',
112
+ :purple => '0;35',
113
+ :light_gray => '0;37',
114
+ :dark_gray => '1;30',
115
+ :light_red => '1;31',
116
+ :light_green => '1;32',
117
+ :yellow => '1;33',
118
+ :light_blue => '1;34',
119
+ :light_cyan => '1;36',
120
+ :light_purple => '1;35',
121
+ :white => '1;37',
122
+ }
123
+
124
+ #
125
+ # Return the escape code for a given color.
126
+ #
127
+ def self.escape(name)
128
+ COLORS.key?(name) && "\e[#{COLORS[name]}m"
129
+ end
130
+
131
+ CLEAR = escape(:nothing)
132
+ end
133
+
134
+ attr_reader :colors, :color_scheme
135
+
136
+ def initialize
137
+ super
138
+ self.color_scheme = :dark_background
139
+ end
140
+
141
+ def color_scheme=(scheme)
142
+ @colors = COLOR_SCHEMES[scheme].dup
143
+ @color_scheme = scheme
144
+ end
145
+
146
+ def color(type)
147
+ type = TYPE_ALIASES[type] if TYPE_ALIASES.has_key?(type)
148
+ @colors[type]
149
+ end
150
+
151
+ def colorize_token(type, token)
152
+ if color = color(type)
153
+ "#{Color.escape(color)}#{token}#{Color::CLEAR}"
154
+ else
155
+ token
156
+ end
157
+ end
158
+
159
+ def colorize(str)
160
+ Ripper.lex(str).map { |_, type, token| colorize_token(type, token) }.join
161
+ end
162
+
163
+ def prompt(context)
164
+ colorize_token(:prompt, super)
165
+ end
166
+
167
+ def result_prefix
168
+ colorize_token(:result_prefix, Formatter::RESULT_PREFIX)
169
+ end
170
+
171
+ def result(object)
172
+ "#{result_prefix} #{colorize(inspect_object(object))}"
173
+ end
174
+ end
175
+ end
176
+
177
+ IRB.formatter = IRB::ColoredFormatter.new
@@ -0,0 +1,90 @@
1
+ module IRB
2
+ # The util methods, history and history!, were taken from Ben Bleything's
3
+ # command_history.rb in utility_belt.
4
+ class History
5
+ class << self
6
+ attr_accessor :file, :max_entries_in_overview
7
+
8
+ def current
9
+ IRB::Context.current.processors.find do |processor|
10
+ processor.is_a?(IRB::History)
11
+ end
12
+ end
13
+ end
14
+
15
+ def initialize(context)
16
+ @context = context
17
+
18
+ to_a.each do |source|
19
+ Readline::HISTORY.push(source)
20
+ end if Readline::HISTORY.to_a.empty?
21
+ end
22
+
23
+ def input(source)
24
+ File.open(self.class.file, "a") { |f| f.puts(source) }
25
+ source
26
+ end
27
+
28
+ def to_a
29
+ File.read(self.class.file).split("\n")
30
+ end
31
+
32
+ def clear!
33
+ File.open(self.class.file, "w") { |f| f << "" }
34
+ Readline::HISTORY.clear
35
+ end
36
+
37
+ def history(number_of_entries = max_entries_in_overview)
38
+ history_size = Readline::HISTORY.size
39
+ start_index = 0
40
+
41
+ # always remove one extra, because that's the `history' command itself
42
+ if history_size <= number_of_entries
43
+ end_index = history_size - 2
44
+ else
45
+ end_index = history_size - 2
46
+ start_index = history_size - number_of_entries - 1
47
+ end
48
+
49
+ start_index.upto(end_index) do |i|
50
+ puts "#{i}: #{Readline::HISTORY[i]}"
51
+ end
52
+ end
53
+
54
+ def history!(entry_or_range)
55
+ # we don't want to execute history! again
56
+ @context.clear_buffer
57
+
58
+ if entry_or_range.is_a?(Range)
59
+ entry_or_range.to_a.each do |i|
60
+ @context.input_line(Readline::HISTORY[i])
61
+ end
62
+ else
63
+ @context.input_line(Readline::HISTORY[entry_or_range])
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ module Kernel
70
+ def history(number_of_entries = IRB::History.max_entries_in_overview)
71
+ IRB::History.current.history(number_of_entries)
72
+ nil
73
+ end
74
+ alias_method :h, :history
75
+
76
+ def history!(entry_or_range)
77
+ IRB::History.current.history!(entry_or_range)
78
+ nil
79
+ end
80
+ alias_method :h!, :history!
81
+
82
+ def clear_history!
83
+ IRB::History.current.clear!
84
+ nil
85
+ end
86
+ end
87
+
88
+ IRB::Context.processors << IRB::History
89
+ IRB::History.file = File.expand_path("~/.irb_history")
90
+ IRB::History.max_entries_in_overview = 50
data/lib/irb/formatter.rb CHANGED
@@ -1,12 +1,13 @@
1
1
  module IRB
2
- def self.formatter
3
- @formatter ||= Formatter.new
2
+ class << self
3
+ attr_accessor :formatter
4
4
  end
5
5
 
6
6
  class Formatter
7
7
  DEFAULT_PROMPT = "irb(%s):%03d:%d> "
8
8
  SIMPLE_PROMPT = ">> "
9
9
  NO_PROMPT = ""
10
+ RESULT_PREFIX = "=>"
10
11
  SYNTAX_ERROR = "SyntaxError: compile error\n(irb):%d: %s"
11
12
  SOURCE_ROOT = /^#{File.expand_path('../../../', __FILE__)}/
12
13
 
@@ -27,8 +28,12 @@ module IRB
27
28
  end
28
29
  end
29
30
 
31
+ def inspect_object(object)
32
+ object.respond_to?(:pretty_inspect) ? object.pretty_inspect : object.inspect
33
+ end
34
+
30
35
  def result(object)
31
- "=> #{object.inspect}"
36
+ "#{RESULT_PREFIX} #{inspect_object(object)}"
32
37
  end
33
38
 
34
39
  def syntax_error(line, message)
@@ -46,4 +51,6 @@ module IRB
46
51
  end
47
52
  end
48
53
  end
49
- end
54
+ end
55
+
56
+ IRB.formatter = IRB::Formatter.new
data/lib/irb.rb CHANGED
@@ -1,15 +1,8 @@
1
1
  require 'irb/context'
2
2
  require 'irb/source'
3
3
 
4
+ require 'irb/ext/history'
5
+
4
6
  if !ENV['SPECCING'] && defined?(RUBY_ENGINE) && RUBY_ENGINE == "macruby"
5
7
  require 'irb/ext/macruby'
6
8
  end
7
-
8
- module Kernel
9
- # Creates a new IRB::Context with the given +object+ and runs it.
10
- def irb(object, binding = nil)
11
- IRB::Context.new(object, binding).run
12
- end
13
-
14
- private :irb
15
- end
@@ -0,0 +1,53 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+ require 'irb/ext/colorize'
3
+
4
+ main = self
5
+
6
+ describe "IRB::ColoredFormatter" do
7
+ before do
8
+ @formatter = IRB::ColoredFormatter.new
9
+ @context = IRB::Context.new(main)
10
+ end
11
+
12
+ it "colorizes a constant" do
13
+ @formatter.result(Hash).should == "=> \e[1;32mHash\e[0;0m"
14
+ end
15
+
16
+ # Not Wirble compliant
17
+ it "colorizes a Range" do
18
+ # @formatter.result(1..3).should == "=> \e[0;36m1\e[0;0m\e[0;31m..\e[0;0m\e[0;36m3\e[0;0m"
19
+ @formatter.result(1..3).should == "=> \e[0;36m1\e[0;0m\e[0;34m..\e[0;0m\e[0;36m3\e[0;0m"
20
+ end
21
+
22
+ it "colorizes a String" do
23
+ @formatter.result("foo bar").should == "=> \e[0;31m\"\e[0;0m\e[0;36mfoo bar\e[0;0m\e[0;31m\"\e[0;0m"
24
+ end
25
+
26
+ it "colorizes a Hash" do
27
+ @formatter.result(:ok => :foo).should == "=> \e[0;32m{\e[0;0m\e[1;33m:\e[0;0m\e[1;33mok\e[0;0m\e[0;34m=>\e[0;0m\e[1;33m:\e[0;0m\e[1;33mfoo\e[0;0m\e[0;32m}\e[0;0m"
28
+ end
29
+
30
+ it "colorizes an Array" do
31
+ @formatter.result([1, 2]).should == "=> \e[0;32m[\e[0;0m\e[0;36m1\e[0;0m\e[0;34m,\e[0;0m \e[0;36m2\e[0;0m\e[0;32m]\e[0;0m"
32
+ end
33
+
34
+ it "colorizes the prompt" do
35
+ @formatter.colors[:prompt] = :light_green
36
+ @formatter.prompt(@context).should == "\e[1;32m#{IRB::Formatter.new.prompt(@context)}\e[0;0m"
37
+ end
38
+
39
+ it "colorizes the result prefix" do
40
+ @formatter.colors[:result_prefix] = :light_red
41
+ @formatter.result("").should == "\e[1;31m=>\e[0;0m \e[0;31m\"\e[0;0m\e[0;31m\"\e[0;0m"
42
+ end
43
+
44
+ it "uses the dark_background color scheme by default" do
45
+ @formatter.color_scheme.should == :dark_background
46
+ end
47
+
48
+ it "changes color scheme" do
49
+ @formatter.color_scheme = :fresh
50
+ @formatter.color_scheme.should == :fresh
51
+ @formatter.colors[:result_prefix].should == :light_purple
52
+ end
53
+ end
data/spec/context_spec.rb CHANGED
@@ -17,6 +17,12 @@ def stub_Readline
17
17
  end
18
18
  stub_Readline
19
19
 
20
+ class TestProcessor
21
+ def input(s)
22
+ s * 2
23
+ end
24
+ end
25
+
20
26
  main = self
21
27
 
22
28
  describe "IRB::Context" do
@@ -44,6 +50,17 @@ describe "IRB::Context" do
44
50
  @context.source.to_s.should == ""
45
51
  end
46
52
 
53
+ it "initializes with an instance of each processor" do
54
+ before = IRB::Context.processors.dup
55
+ begin
56
+ IRB::Context.processors << TestProcessor
57
+ @context = IRB::Context.new(main)
58
+ @context.processors.last.should.be.instance_of TestProcessor
59
+ ensure
60
+ IRB::Context.processors.replace(before)
61
+ end
62
+ end
63
+
47
64
  it "does not use the same binding copy of the top level object" do
48
65
  lambda { eval("x", @context.binding) }.should.raise NameError
49
66
  end
@@ -62,8 +79,8 @@ end
62
79
  describe "IRB::Context, when evaluating source" do
63
80
  before do
64
81
  @context = IRB::Context.new(main)
65
- def @context.puts(string); @printed = string; end
66
- def @context.printed; @printed; end
82
+ def @context.printed; @printed ||= '' end
83
+ def @context.puts(string); printed << "#{string}\n" end
67
84
  end
68
85
 
69
86
  it "evaluates code with the object's binding" do
@@ -72,8 +89,7 @@ describe "IRB::Context, when evaluating source" do
72
89
 
73
90
  it "prints the result" do
74
91
  @context.evaluate("Hash[:foo, :foo]")
75
- printed = @context.instance_variable_get(:@printed)
76
- printed.should == "=> {:foo=>:foo}"
92
+ @context.printed.should == "=> {:foo=>:foo}\n"
77
93
  end
78
94
 
79
95
  it "assigns the result to the local variable `_'" do
@@ -108,6 +124,12 @@ describe "IRB::Context, when evaluating source" do
108
124
  @context.process_line("end")
109
125
  @context.printed.should.match /\(irb\):3:in.+\(irb\):2:in/m
110
126
  end
127
+
128
+ it "inputs a line to be processed, skipping readline" do
129
+ expected = "#{@context.formatter.prompt(@context)}2 * 21\n=> 42\n"
130
+ @context.input_line("2 * 21")
131
+ @context.printed.should == expected
132
+ end
111
133
  end
112
134
 
113
135
  describe "IRB::Context, when receiving input" do
@@ -121,6 +143,12 @@ describe "IRB::Context, when receiving input" do
121
143
  Readline.received.should == ["irb(main):001:0> ", true]
122
144
  end
123
145
 
146
+ it "passes the input to all processors, which may return a new value" do
147
+ @context.processors << TestProcessor.new
148
+ Readline.stub_input("foo")
149
+ @context.readline.should == "foofoo"
150
+ end
151
+
124
152
  it "processes the output" do
125
153
  Readline.stub_input("def foo")
126
154
  def @context.process_line(line); @received = line; false; end
@@ -196,4 +224,13 @@ describe "IRB::Context, when receiving input" do
196
224
  @context.run
197
225
  @context.instance_variable_get(:@received).should.not == "def foo"
198
226
  end
227
+ end
228
+
229
+ describe "Kernel::irb" do
230
+ it "creates a new context for the given object and runs it" do
231
+ Readline.stub_input("::IRBRan = self")
232
+ o = Object.new
233
+ irb(o)
234
+ IRBRan.should == o
235
+ end
199
236
  end
@@ -54,6 +54,12 @@ describe "IRB::Formatter" do
54
54
  @formatter.result(:foo => :foo).should == "=> {:foo=>:foo}"
55
55
  end
56
56
 
57
+ it "prints the result with object#pretty_inspect, if it responds to it" do
58
+ object = Object.new
59
+ def object.pretty_inspect; "foo"; end
60
+ @formatter.result(object).should == "foo"
61
+ end
62
+
57
63
  it "prints that a syntax error occurred on the last line and reset the buffer to the previous line" do
58
64
  @formatter.syntax_error(2, "syntax error, unexpected '}'").should ==
59
65
  "SyntaxError: compile error\n(irb):2: syntax error, unexpected '}'"
@@ -0,0 +1,171 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+ require "tempfile"
3
+
4
+ describe "IRB::History" do
5
+ it "stores the history by default in ~/.irb_history" do
6
+ IRB::History.file.should == File.expand_path("~/.irb_history")
7
+ end
8
+
9
+ before do
10
+ @file = Tempfile.new("irb_history.txt")
11
+ IRB::History.file = @file.path
12
+ @history = IRB::History.new(nil)
13
+ end
14
+
15
+ after do
16
+ @file.close
17
+ end
18
+
19
+ it "adds input to the history file" do
20
+ @history.input "puts :ok"
21
+ @file.rewind; @file.read.should == "puts :ok\n"
22
+ @history.input "foo(x)"
23
+ @file.rewind; @file.read.should == "puts :ok\nfoo(x)\n"
24
+ end
25
+
26
+ it "returns the same input value" do
27
+ @history.input("foo(x)").should == "foo(x)"
28
+ end
29
+
30
+ it "returns the contents of the history file as an array of lines" do
31
+ @history.input "puts :ok"
32
+ @history.to_a.should == ["puts :ok"]
33
+ @history.input "foo(x)"
34
+ @history.to_a.should == ["puts :ok", "foo(x)"]
35
+ end
36
+
37
+ it "stores the contents of the history file in Readline::HISTORY once" do
38
+ Readline::HISTORY.clear
39
+
40
+ @history.input "puts :ok"
41
+ @history.input "foo(x)"
42
+
43
+ IRB::History.new(nil)
44
+ IRB::History.new(nil)
45
+
46
+ Readline::HISTORY.to_a.should == ["puts :ok", "foo(x)"]
47
+ end
48
+
49
+ it "clears the history and history file" do
50
+ @history.input "puts :ok"
51
+ @history.input "foo(x)"
52
+ @history.clear!
53
+
54
+ @file.rewind; @file.read.should.be.empty
55
+ Readline::HISTORY.to_a.should.be.empty
56
+ end
57
+ end
58
+
59
+ class IRB::History
60
+ def printed
61
+ @printed ||= ""
62
+ end
63
+
64
+ def print(s)
65
+ printed << s
66
+ end
67
+
68
+ def puts(s)
69
+ printed << "#{s}\n"
70
+ end
71
+
72
+ def clear!
73
+ @cleared = true
74
+ end
75
+ def cleared?
76
+ @cleared
77
+ end
78
+ end
79
+
80
+ describe "IRB::History, concerning the user api" do
81
+ it "shows by default a maximum of 50 history entries" do
82
+ IRB::History.max_entries_in_overview.should == 50
83
+ end
84
+
85
+ before do
86
+ sources = [
87
+ "puts :ok",
88
+ "x = foo(x)",
89
+ "class AAA",
90
+ " def bar",
91
+ " :ok",
92
+ " end",
93
+ "end",
94
+ "THIS LINE REPRESENTS THE ENTERED COMMAND AND SHOULD BE OMITTED!"
95
+ ]
96
+
97
+ Readline::HISTORY.clear
98
+ sources.each { |source| Readline::HISTORY.push(source) }
99
+
100
+ IRB::History.max_entries_in_overview = 5
101
+
102
+ @context = IRB::Context.new(Object.new)
103
+ IRB::Context.current = @context
104
+
105
+ @history = @context.processors.find { |p| p.is_a?(IRB::History) }
106
+ end
107
+
108
+ after do
109
+ IRB::Context.current = nil
110
+ end
111
+
112
+ it "returns nil so that IRB doesn't cache some arbitrary line number" do
113
+ history.should == nil
114
+ end
115
+
116
+ it "prints a formatted list with, by default IRB::History.max_entries_in_overview, number of history entries" do
117
+ history
118
+
119
+ @history.printed.should == %{
120
+ 2: class AAA
121
+ 3: def bar
122
+ 4: :ok
123
+ 5: end
124
+ 6: end
125
+ }.sub(/\n/, '')
126
+ end
127
+
128
+ it "prints a formatted list of N most recent history entries" do
129
+ history(7)
130
+
131
+ @history.printed.should == %{
132
+ 0: puts :ok
133
+ 1: x = foo(x)
134
+ 2: class AAA
135
+ 3: def bar
136
+ 4: :ok
137
+ 5: end
138
+ 6: end
139
+ }.sub(/\n/, '')
140
+ end
141
+
142
+ it "prints a formatted list of all history entries if the request number of entries is more than there is" do
143
+ history(777)
144
+
145
+ @history.printed.should == %{
146
+ 0: puts :ok
147
+ 1: x = foo(x)
148
+ 2: class AAA
149
+ 3: def bar
150
+ 4: :ok
151
+ 5: end
152
+ 6: end
153
+ }.sub(/\n/, '')
154
+ end
155
+
156
+ it "evaluates the history entry specified" do
157
+ @context.__evaluate__("x = 2; def foo(x); x * 2; end")
158
+ history! 1
159
+ @context.__evaluate__("x").should == 4
160
+ end
161
+
162
+ it "evaluates the history entries specified by a range" do
163
+ history! 2..6
164
+ @context.__evaluate__("AAA.new.bar").should == :ok
165
+ end
166
+
167
+ it "clears the history and history file" do
168
+ clear_history!
169
+ @history.should.be.cleared
170
+ end
171
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dietrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eloy Duran
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-24 00:00:00 +01:00
12
+ date: 2010-04-18 00:00:00 +02:00
13
13
  default_executable: dietrb
14
14
  dependencies: []
15
15
 
@@ -22,24 +22,29 @@ extensions: []
22
22
  extra_rdoc_files:
23
23
  - LICENSE
24
24
  - README.rdoc
25
+ - TODO
25
26
  files:
26
27
  - .gitignore
27
28
  - LICENSE
28
29
  - README.rdoc
29
30
  - Rakefile
31
+ - TODO
30
32
  - VERSION
31
33
  - bin/dietrb
32
34
  - dietrb.gemspec
33
35
  - lib/irb.rb
34
36
  - lib/irb/context.rb
37
+ - lib/irb/ext/colorize.rb
35
38
  - lib/irb/ext/completion.rb
39
+ - lib/irb/ext/history.rb
36
40
  - lib/irb/ext/macruby.rb
37
41
  - lib/irb/formatter.rb
38
42
  - lib/irb/source.rb
43
+ - spec/colorize_spec.rb
39
44
  - spec/completion_spec.rb
40
45
  - spec/context_spec.rb
41
46
  - spec/formatter_spec.rb
42
- - spec/irb_spec.rb
47
+ - spec/history_spec.rb
43
48
  - spec/source_spec.rb
44
49
  - spec/spec_helper.rb
45
50
  has_rdoc: true
@@ -71,9 +76,10 @@ signing_key:
71
76
  specification_version: 3
72
77
  summary: IRB on a diet, for MacRuby / Ruby 1.9
73
78
  test_files:
79
+ - spec/colorize_spec.rb
74
80
  - spec/completion_spec.rb
75
81
  - spec/context_spec.rb
76
82
  - spec/formatter_spec.rb
77
- - spec/irb_spec.rb
83
+ - spec/history_spec.rb
78
84
  - spec/source_spec.rb
79
85
  - spec/spec_helper.rb
data/spec/irb_spec.rb DELETED
@@ -1,10 +0,0 @@
1
- require File.expand_path('../spec_helper', __FILE__)
2
-
3
- describe "Kernel::irb" do
4
- it "creates a new context for the given object and runs it" do
5
- Readline.stub_input("::IRBRan = self")
6
- o = Object.new
7
- irb(o)
8
- IRBRan.should == o
9
- end
10
- end