Class: ReblShell

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

Overview

Static class that contains constants and functions for the rebl shell.

Constant Summary

@@histfile =
File::expand_path("~/.rebl_history")
@@maxhistsize =
100
@@escape_char =
'/'
@@commands =
{"tick" => [lambda {|lib,argv| lib.tick(argv[1].nil? ? 1 : Integer(argv[1]))},
 "tick [x]:\texecutes x (or 1) timesteps"],

    "run" => [lambda {|lib,_| lib.run},
              "run\ttick until quiescence, a breakpoint, or #{@@escape_char}stop"],

    "stop" => [lambda {|lib,_| lib.stop},
"stop\tstop ticking"],

    "lsrules" => [lambda {|lib,_| lib.rules.sort{|a,b| a[0] <=> b[0]}.each {|k,v| puts "#{k}: "+v}},
   "lsrules\tlist rules"],

    "rmrule" => [lambda {|lib,argv| lib.del_rule(Integer(argv[1]))},
  "rmrule x\tremove rule number x"],

    "lscollections" => [lambda {|lib,_| lib.state.sort{|a,b| a[0] <=> b[0]}.each {|k,v| puts "#{k}: "+v}},
         "lscollections\tlist collections"],

    "dump" => [lambda {|lib,argv| lib.dump(argv[1])},
"dump c\tdump contents of collection c"],

    "exit" => [lambda {|_,_| do_exit}, "exit\texit rebl"],

    "quit" => [lambda {|_,_| do_exit}, "quit\texit rebl"],

    "help" => [lambda {|_,_| pretty_help},
"help\tprint this help message"]}
@@abbrevs =
@@commands.keys.abbrev
@@exit_message =
"Rebellion quashed."

Class Method Summary (collapse)

Class Method Details

+ (Object) command(c)

lookup full command from abbreviation



144
145
146
# File 'lib/bud/rebl.rb', line 144

def self.command(c)
  return @@abbrevs[c].nil? ? nil : @@commands[@@abbrevs[c]][0]
end

+ (Object) rebl_loop(lib, noreadline = false)

One step of the rebl shell loop: processes one rebl shell line from stdin and returns. May raise an Exception.



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
# File 'lib/bud/rebl.rb', line 98

def self.rebl_loop(lib, noreadline=false)
  begin
    if noreadline
      line = gets
    else
      line = Readline::readline('rebl> ')
    end
    do_exit if line.nil?
    line.strip!
    return if line.empty?
    Readline::HISTORY.push(line) unless noreadline
    split_line = line.split(" ")
    if line[0..0] == @@escape_char then
      # Command
      split_line[0].slice! 0
      if command split_line[0]
        command(split_line[0]).call(lib, split_line)
      else
        puts "invalid command or ambiguous command prefix"
      end
    elsif is_collection? split_line[0]
      # Collection
      lib.add_collection(line)
    else
      # Rule
      lib.add_rule(line)
    end
  rescue Interrupt
    abort(do_exit)
  end
end

+ (Object) run

Starts a rebl shell. – This function is not covered by testcases, but setup and rebl_loop are. ++



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/bud/rebl.rb', line 62

def self.run
  lib = setup
  loop do
    begin
      rebl_loop(lib)
    rescue Exception => e
      puts "exception: #{e}"
      #puts e.backtrace
    end
  end
end

+ (Object) setup

Performs setup as part of starting a rebl shell, and returns the instance of LibRebl that is created; testcases call this directly.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/bud/rebl.rb', line 76

def self.setup
  Signal.trap("INT") {do_exit}
  Signal.trap("TERM") {do_exit}

  ipport = ARGV[0] ? ARGV[0].split(":") : []
  lib = LibRebl.new(*[(ipport[0] or "localhost"), (ipport[1] or 0)])
  setup_history

  comp = proc do |s|
    @@commands.keys.map do |c|
      @@escape_char+c
    end.grep( /^#{Regexp.escape(s)}/ )
  end
  Readline.completion_append_character = ' '
  Readline.completion_proc = comp

  welcome
  return lib
end

+ (Object) setup_history

Reads permanent history from @@histfile. This code is pretty much the same as irb's code.



132
133
134
135
136
137
138
139
140
141
# File 'lib/bud/rebl.rb', line 132

def self.setup_history
  begin
    if File::exists?(@@histfile)
      lines = IO::readlines(@@histfile).collect { |line| line.chomp }
      Readline::HISTORY.push(*lines)
    end
  rescue Exception
    puts "Error when configuring permanent history: #{$!}"
  end
end