Class: Brush::Shell

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

Constant Summary collapse

DEFAULT_EXEC_MATCH =
%r"^(\S.*)"
DEFAULT_RUBY_MATCH =
%r"^\s+(.*)"
DEFAULT_PS1 =
"% "
DEFAULT_PS2 =
"%-"
DEFAULT_RPS1 =
"\#=> "
DEFAULT_RPS2 =
"\# > "

Instance Method Summary collapse

Constructor Details

#initialize(inp = $stdin, out = $stdout, err = $stderr, binding = nil) ⇒ Shell

Returns a new instance of Shell.



15
16
17
18
19
20
21
22
23
24
# File 'lib/brush.rb', line 15

def initialize (inp = $stdin, out = $stdout, err = $stderr, binding = nil)
  @in, @out, @err = inp, out, err
  @exec_match = DEFAULT_EXEC_MATCH
  @ruby_match = DEFAULT_RUBY_MATCH
  @ps1, @ps2 = DEFAULT_PS1, DEFAULT_PS2
  @rps1, @rps2 = DEFAULT_RPS1, DEFAULT_RPS2
  @binding ||= binding
  @out.sync = true
  @err.sync = true
end

Instance Method Details

#repl(code) ⇒ Object



40
41
42
43
# File 'lib/brush.rb', line 40

def repl (code)
  result = eval(code, @binding)
  @out.puts "#{@rps1}#{result.inspect}"
end

#startObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/brush.rb', line 26

def start
  while not @in.eof?
    @out.print @ps1
    line = @in.gets.chomp
    if line =~ @ruby_match
      repl line[@ruby_match, 1]
    elsif line =~ @exec_match
      system line[@exec_match, 1]
    else
      repl line
    end
  end
end