Class: Rpremote::Shell
- Inherits:
-
Object
- Object
- Rpremote::Shell
- Defined in:
- lib/rpremote/shell.rb,
sig/rpremote.rbs
Defined Under Namespace
Classes: CommandError, Error, ProtocolError, TimeoutError
Constant Summary collapse
- DEFAULT_TIMEOUT =
20.0- PROMPT_START =
"\e[?25l\e[1G$> \e[0K".b
- PROMPT_END =
"\e[?25h".b
- ECHO_START =
"\e[?25l\e[1G$> ".b
- ERASE_LINE =
"\e[0K".b
- CURSOR_POSITION_QUERY =
"\e[6n".b
- CURSOR_POSITION_RESPONSE =
"\e[1;1R".b
- RUBY_EXCEPTION_STATUS =
"\x1eR2P2:RUBY_EXCEPTION\x1f".b
Instance Attribute Summary collapse
-
#io ⇒ Object
readonly
Returns the value of attribute io.
-
#timeout ⇒ Float
readonly
Returns the value of attribute timeout.
Class Method Summary collapse
Instance Method Summary collapse
- #execute(command, output: nil) ⇒ String
-
#initialize(io, timeout: DEFAULT_TIMEOUT) ⇒ Shell
constructor
A new instance of Shell.
- #send_command(command) ⇒ nil
- #synchronize! ⇒ nil
Constructor Details
#initialize(io, timeout: DEFAULT_TIMEOUT) ⇒ Shell
Returns a new instance of Shell.
32 33 34 35 36 37 |
# File 'lib/rpremote/shell.rb', line 32 def initialize(io, timeout: DEFAULT_TIMEOUT) raise ArgumentError, "timeout must be positive" unless timeout.positive? @io = io @timeout = timeout end |
Instance Attribute Details
#io ⇒ Object (readonly)
Returns the value of attribute io.
30 31 32 |
# File 'lib/rpremote/shell.rb', line 30 def io @io end |
#timeout ⇒ Float (readonly)
Returns the value of attribute timeout.
30 31 32 |
# File 'lib/rpremote/shell.rb', line 30 def timeout @timeout end |
Class Method Details
.quote_argument(argument) ⇒ String
21 22 23 24 25 26 27 28 |
# File 'lib/rpremote/shell.rb', line 21 def self.quote_argument(argument) argument = String(argument) raise ArgumentError, "shell argument contains a control character" if argument.match?(/[\x00-\x1f\x7f]/) return "'#{argument}'" unless argument.include?("'") return %("#{argument}") unless argument.include?('"') raise ArgumentError, "shell argument cannot contain both quote characters" end |
Instance Method Details
#execute(command, output: nil) ⇒ String
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rpremote/shell.rb', line 46 def execute(command, output: nil) command = validate_command(command) send_command(command) echo = ECHO_START + command.b + ERASE_LINE stream_state = {} response = read_until_prompt(after: echo) { |buffer| stream_output(output, buffer, echo, stream_state) } command_output = extract_output(response, command) ruby_exception = command_output.include?(RUBY_EXCEPTION_STATUS) command_output = remove_ruby_exception_status(command_output) raise CommandError, "Ruby exception reported by R2P2" if ruby_exception command_output rescue TimeoutError interrupt raise end |
#send_command(command) ⇒ nil
63 64 65 66 67 |
# File 'lib/rpremote/shell.rb', line 63 def send_command(command) command = validate_command(command) write_all("#{command}\r".b) nil end |
#synchronize! ⇒ nil
39 40 41 42 43 44 |
# File 'lib/rpremote/shell.rb', line 39 def synchronize! drain_input write_all("\r".b) read_until_prompt nil end |