Class: MimickIRB

Inherits:
RubyLex
  • Object
show all
Defined in:
lib/shoes/shoes_irb.rb

Defined Under Namespace

Classes: Continue, Empty

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMimickIRB



10
11
12
13
14
15
16
17
# File 'lib/shoes/shoes_irb.rb', line 10

def initialize
  super
  $stdout = StringIO.new
  set_input(StringIO.new)
  @started = Time.now
  @history = []
  @echo = true
end

Instance Attribute Details

#echoObject

Returns the value of attribute echo



5
6
7
# File 'lib/shoes/shoes_irb.rb', line 5

def echo
  @echo
end

#historyObject

Returns the value of attribute history



5
6
7
# File 'lib/shoes/shoes_irb.rb', line 5

def history
  @history
end

#startedObject

Returns the value of attribute started



5
6
7
# File 'lib/shoes/shoes_irb.rb', line 5

def started
  @started
end

Instance Method Details

#run(str) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/shoes/shoes_irb.rb', line 19

def run(str)
  obj = nil
  @io << str
  @io.rewind
  unless l = lex
    raise Empty if @line == ''
  else
    case l.strip
    when "reset"
      @line = ""
    when "time"
      @line = "puts %{This IRB session started #{Time.at(Time.now.getutc - @started.getutc.to_f).strftime('%H:%M:%S')} ago.}"
    when /^echo( (.*))?/
      case $2
      when "on"
         @echo = true
         @line = "puts %{echo is toggled on}"
      when "off"
         @echo = false
         @line = "puts %{echo is toggled off}"
      else
         @line = "puts %{echo is toggled #{@echo ? "on" : "off"}}"
      end
    when "history"
      @line = "puts %{#{@history.join("\n")}}"
    when "save"
      filename = ask_save_file
      if filename
         File.open(filename, "w") { |f| f.write(@history.join("\n")) }
         @line = "puts %{Command history saved to #{filename.inspect}...}"
      else
        @line = ""
      end
    when "help"
      message = <<-END.gsub(/^ {12}/, '')
          help\tdisplay this help.
          echo\techo, echo on and echo off to toggle object output. 
          time\tdisplay time since the IRB session started.
          history\tlist command history.
          save\tsave command history to a file.
          reset\treset terminal.
      END
      @line = "puts \"#{message}\""
    else
      @line << l << "\n"
      if @ltype or @continue or @indent > 0
        raise Continue
      end
    end
  end
  unless @line.empty?
    obj = eval @line, TOPLEVEL_BINDING, "(irb)", @line_no
  end
  @line_no += @line.scan(/\n/).length
  @line = ''
  @exp_line_no = @line_no

  @indent = 0
  @indent_stack = []

  $stdout.rewind
  output = $stdout.read
  $stdout.truncate(0)
  $stdout.rewind
  [output, obj]
rescue Object => e
  case e when Empty, Continue
  else @line = ""
  end
  raise e
ensure
  set_input(StringIO.new)
end