Class: BaseShell

Inherits:
Object
  • Object
show all
Defined in:
lib/rhcp_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.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rhcp_shell/base_shell.rb', line 7

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.



5
6
7
# File 'lib/rhcp_shell/base_shell.rb', line 5

def backend
  @backend
end

Instance Method Details

#consoleObject



77
78
79
# File 'lib/rhcp_shell/base_shell.rb', line 77

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

#runObject



81
82
83
84
85
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
111
# File 'lib/rhcp_shell/base_shell.rb', line 81

def run
  backend.show_banner
  loop do      
    @thread = Thread.new {
        break unless line = console.readline
  
        if line then
          @logger.debug "got : #{line}"
        else
          @logger.debug "got an empty line"      
        end

        backend.process_input line
    }
    begin
      @thread.join
    rescue
      error = $!
      if error == "exit"
        puts "exiting"
        Kernel.exit
      else
        puts "got an error in @thread.join: #{error}"
        puts error.backtrace.join("\n")
      end
      
    end
    
  end
  $stderr.puts "Exiting shell..."      
end