Class: Rubic::Rubic

Inherits:
Object
  • Object
show all
Defined in:
lib/rubic.rb

Overview

The REPL itself

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Rubic

Returns a new instance of Rubic.



82
83
84
85
# File 'lib/rubic.rb', line 82

def initialize
  @workspace = IRB::WorkSpace.new(TOPLEVEL_BINDING.dup)
  @lines = LineGroup.new
end

Instance Method Details

#eval_forever ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/rubic.rb', line 87

def eval_forever
  # so we can reset settings after
  # todo need to put more in here (maybe? IRB doesn't...)
  history_len = Reline::HISTORY.size
  og_completion_proc = Reline.completion_proc
  og_autocompletion = Reline.autocompletion

  Reline.completion_proc = lambda { |target, preposting, postposting|
    completion(target, preposting, postposting)
  }

  Reline.autocompletion = true
  Reline.completer_quote_characters = ""
  Reline.output_modifier_proc = proc do |input, complete:|
    output_modifier(input, complete)
  end

  loop do
    line = Reline.readline("> ")
    break if line.nil?

    Reline::HISTORY.push(line) if line.strip != ""

    cmd, args = line.split(" ", 2)
    case cmd
    when "list"
      @lines.lines.each do |line_num, line|
        puts output_modifier("#{line_num} #{line}", false)
      end
    when "run"
      run(@lines.stringify(false))
    when "clear"
      @lines.clear
    when "load"
      if args.nil? || args.empty?
        puts "Usage: load filename.rub"
      else
        @lines.load(args, true)
      end
    when "save"
      if args.nil? || args.empty?
        puts "Usage: save filename.rub"
      else
        @lines.save(args, true)
      end
    when "import"
      if args.nil? || args.empty?
        puts "Usage: import filename.rb"
      else
        @lines.load(args, false)
      end
    when "export"
      if args.nil? || args.empty?
        puts "Usage: export filename.rb"
      else
        @lines.save(args, false)
      end
    when "squish"
      @lines.resize(1)
    when "widen"
      @lines.resize(10)
    when "help"
      print_help
    when "exit", "bye"
      break
    else
      line_num, expr = line.split(" ", 2)
      line_num = begin
        Integer(line_num)
      rescue StandardError
        nil
      end

      if line_num.nil?
        run(line) if line != ""
      else
        @lines.update(line_num, expr)
      end
    end
  rescue Interrupt
    puts "^C"
  end

  # so that the shell is put on a new line
  # if we exit with ctrl+d
  puts
ensure
  # reset history
  delta = Reline::HISTORY.size - history_len
  (0...delta).each do
    Reline::HISTORY.pop
  end

  Reline.completion_proc = og_completion_proc
  Reline.autocompletion = og_autocompletion
end