Class: BaseShell

Inherits:
Object
  • Object
show all
Defined in:
lib/vop/shell/base_shell.rb

Overview

This class is an abstract implementation of a command shell It handles command completion and history functions. For the actual business logic, you need to pass it an implementation of ShellBackend

Defined Under Namespace

Classes: ReadlineConsole, SimpleConsole

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backend) ⇒ BaseShell

Returns a new instance of BaseShell.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/vop/shell/base_shell.rb', line 9

def initialize(backend)
  @logger = $logger
  @backend = backend

  at_exit { console.close }

  trap("INT") {
    Thread.kill(@thread)
    @backend.process_ctrl_c
  }
end

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



7
8
9
# File 'lib/vop/shell/base_shell.rb', line 7

def backend
  @backend
end

Instance Method Details

#consoleObject



82
83
84
# File 'lib/vop/shell/base_shell.rb', line 82

def console
  @console ||= $stdin.tty? ? ReadlineConsole.new(self) : SimpleConsole.new
end

#runObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/vop/shell/base_shell.rb', line 86

def run
  backend.show_banner
  loop do
    @thread = Thread.new {
      line = console.readline

      if line
        backend.process_input line
      end
    }
    begin
      @thread.join
    rescue
      error = $!
      if error == "exit"
        Kernel.exit
      else
        $stderr.puts "error: >>#{error}<<"
        $stderr.puts error.backtrace.join("\n")
      end
    end

  end
  $stderr.puts "Exiting shell..."
end