Class: JSH::REPL

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

Constant Summary collapse

DEFAULT_PROMPT =
"[{:number_of_times:}] jsh) "
PROMPT_SYNTAX =
/\{:(.+?):\}/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, options = {}) ⇒ REPL



15
16
17
18
19
20
21
# File 'lib/jsh/repl.rb', line 15

def initialize(base, options = {})
  @base = base
  @noprompt = options.fetch(:noprompt, false)
  @readline = options.fetch(:readline, true)
  @options = options
  config[:number_of_times] = 0
end

Instance Attribute Details

#baseObject

Returns the value of attribute base.



9
10
11
# File 'lib/jsh/repl.rb', line 9

def base
  @base
end

#promptObject

Returns the value of attribute prompt.



9
10
11
# File 'lib/jsh/repl.rb', line 9

def prompt
  @prompt
end

Class Method Details

.start(options) ⇒ Object



11
12
13
# File 'lib/jsh/repl.rb', line 11

def self.start(options)
  new(Base.new, options).start
end

Instance Method Details

#noprompt?Boolean



43
44
45
# File 'lib/jsh/repl.rb', line 43

def noprompt?
  @noprompt
end

#readline?Boolean



47
48
49
# File 'lib/jsh/repl.rb', line 47

def readline?
  @readline
end

#replObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/jsh/repl.rb', line 29

def repl
  catch :halt do
    loop do
      input = read
      if command = commands[input.to_sym]
        command.arity != 0 ? command.call(base) : command.call
      else
        output.puts((input.nil? or input.empty?) ? "" : base.context.eval(input))
      end
      config[:number_of_times] += 1
    end
  end
end

#startObject



23
24
25
26
27
# File 'lib/jsh/repl.rb', line 23

def start
  call_hook(:before)
  repl
  call_hook(:after)
end