class ReblShell

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

Public Class Methods

command(c) click to toggle source

lookup full command from abbreviation

# File lib/bud/rebl.rb, line 144
def self.command(c)
  return @@abbrevs[c].nil? ? nil : @@commands[@@abbrevs[c]][0]
end
rebl_loop(lib, noreadline=false) click to toggle source

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

# 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
run() click to toggle source

Starts a rebl shell.

# 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
setup() click to toggle source

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

# 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
setup_history() click to toggle source

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

# 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

Private Class Methods

do_exit() click to toggle source

Called on exit. Writes the session's history to @@histfile, and stops the bud instance from listening.

# File lib/bud/rebl.rb, line 180
def self.do_exit
  begin
    lines = Readline::HISTORY.to_a.reverse.uniq.reverse
    lines = lines[-@@maxhistsize, @@maxhistsize] if lines.size > @@maxhistsize
    File::open(@@histfile, File::WRONLY|File::CREAT|File::TRUNC) do |io|
      io.puts lines.join("\n")
    end
  rescue Exception
    puts "Error when saving permanent history: #{$!}"
  end
  @rebl_class_inst.stop if @rebl_class_inst
  puts "\n" + @@exit_message
  exit!
end
is_collection?(c) click to toggle source

Checks if a given string refers to a collection type (one of the builtin collection types or a wrapper_name for a lattice).

# File lib/bud/rebl.rb, line 197
def self.is_collection?(c)
  TABLE_TYPES.include?(c) || Bud::Lattice.lattice_kinds.has_key?(c.to_sym)
end
pretty_help() click to toggle source

pretty-printed help

# File lib/bud/rebl.rb, line 150
def self.pretty_help
  puts "rebl commands are prefixed by '#{@@escape_char}'"
  puts "other input is interpreted as Bloom code."
  puts
  puts "rebl commands:"
  maxlen = @@commands.keys.sort{|a,b| b.size - a.size}.first.size
  cmd_list = @@commands.keys.sort
  cmd_list.each do |c|
    v = @@commands[c]
    puts @@escape_char +
      v[1].gsub(/\t/, " "*(maxlen + 4 - v[1].split("\t")[0].size))
  end
  puts "\nbreakpoints:"
  puts "a breakpoint is a rule with the 'breakpoint' scratch on the left of "+
    "a '<=' operator.\n'#{@@escape_char}run' will stop ticking at the end of a "+
    "timestep where a 'breakpoint' tuple exists."
end
welcome() click to toggle source
# File lib/bud/rebl.rb, line 169
def self.welcome
  puts "Welcome to rebl, the interactive Bloom terminal."
  puts
  puts "Type: " + @@escape_char + "h for help"
  puts "      " + @@escape_char + "q to quit"
  puts
end