Skip to content

Commit

Permalink
Add readline functionality to monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
greeneca committed Jan 10, 2017
1 parent 6a24084 commit 91af4fd
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
roku_builder (3.10.7)
roku_builder (3.11.0)
faraday (~> 0.10)
faraday-digestauth (~> 0.2)
git (~> 1.3)
Expand Down
2 changes: 2 additions & 0 deletions lib/roku_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
require 'nokogiri'
#navigator
require 'io/console'
#monitor
require 'readline'

require "roku_builder/controller"
require "roku_builder/controller_commands"
Expand Down
47 changes: 38 additions & 9 deletions lib/roku_builder/monitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def init()
taskX: 8093,
profiler: 8080,
}
@show_prompt = false
end

# Monitor a development log on the Roku device
Expand All @@ -41,19 +42,47 @@ def monitor(type:)
end
end
}
# setup readline

libedit = false
begin
Readline.vi_editing_mode
rescue NotImplementedError
libedit = true
end

commands = [
"bsc", "bscs", "brkd", "bt", "classes", "cont", "cont", "down", "d",
"exit", "gc", "help", "last", "list", "next", "print", "p", "?", "step",
"s", "t", "over", "out", "up", "u", "var", "q"
].sort
commands.collect { |i| i += ' ' } if libedit

comp = proc { |s| commands.grep( /^#{Regexp.escape(s)}/ ) }

Readline.completion_append_character = " "
Readline.completion_proc = comp


running = true
@logger.unknown "Q to exit"
while running
begin
command = gets.chomp
case command
when "q"
thread.exit
running = false
when "stop"
thread[:connection].puts("\C-c")
if @show_prompt
command = Readline.readline('BrightScript Debugger> ', true)
if command =~ /^\s*$/ or Readline::HISTORY.to_a[-2] == command
Readline::HISTORY.pop
end
case command
when "q"
thread.exit
running = false
else
@show_prompt = false
thread[:connection].puts(command)
end
else
thread[:connection].puts(command)
sleep 0.01
end
rescue SystemExit, Interrupt
thread[:connection].puts("\C-c")
Expand All @@ -73,7 +102,7 @@ def manage_text(all_text:, txt:)
puts line if !line.strip.empty?
end
if all_text.downcase == "BrightScript Debugger> ".downcase
print all_text
@show_prompt = true
all_text = ""
end
all_text
Expand Down
10 changes: 5 additions & 5 deletions lib/roku_builder/navigator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ def interactive
if char == "\u0003"
running = false
else
Thread.new(char) {|char|
if @mappings[char.to_sym] != nil
nav(commands:[@mappings[char.to_sym][0].to_sym])
elsif char.inspect.force_encoding("UTF-8").ascii_only?
type(text: char)
Thread.new(char) {|character|
if @mappings[character.to_sym] != nil
nav(commands:[@mappings[character.to_sym][0].to_sym])
elsif character.inspect.force_encoding("UTF-8").ascii_only?
type(text: character)
end
}
end
Expand Down
2 changes: 1 addition & 1 deletion lib/roku_builder/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

module RokuBuilder
# Version of the RokuBuilder Gem
VERSION = "3.10.7"
VERSION = "3.11.0"
end
42 changes: 28 additions & 14 deletions tests/roku_builder/monitor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ def test_monitor_monit
logger: Logger.new("/dev/null")
}
monitor = RokuBuilder::Monitor.new(**device_config)
monitor.instance_variable_set(:@show_prompt, true)

connection.expect(:waitfor, nil) do |config|
assert_equal(/./, config['Match'])
assert_equal(false, config['Timeout'])
end

def monitor.gets
readline = proc {
sleep(0.1)
"q"
end
}

Net::Telnet.stub(:new, connection) do
monitor.monitor(type: :main)
Readline.stub(:readline, readline) do
Net::Telnet.stub(:new, connection) do
monitor.monitor(type: :main)
end
end

connection.verify
Expand All @@ -39,6 +42,7 @@ def test_monitor_monit_and_manage
logger: Logger.new("/dev/null")
}
monitor = RokuBuilder::Monitor.new(**device_config)
monitor.instance_variable_set(:@show_prompt, true)

connection.expect(:waitfor, nil) do |config, &blk|
assert_equal(/./, config['Match'])
Expand All @@ -47,14 +51,16 @@ def test_monitor_monit_and_manage
blk.call(txt) == ""
end

def monitor.gets
readline = proc {
sleep(0.1)
"q"
end
}

Net::Telnet.stub(:new, connection) do
monitor.stub(:manage_text, "") do
monitor.monitor(type: :main)
Readline.stub(:readline, readline) do
Net::Telnet.stub(:new, connection) do
monitor.stub(:manage_text, "") do
monitor.monitor(type: :main)
end
end
end

Expand All @@ -70,14 +76,19 @@ def test_monitor_monit_input
logger: Logger.new("/dev/null")
}
monitor = RokuBuilder::Monitor.new(**device_config)
monitor.instance_variable_set(:@show_prompt, true)

connection.expect(:waitfor, nil) do |config|
assert_equal(/./, config['Match'])
assert_equal(false, config['Timeout'])
end
connection.expect(:puts, nil, ["text"])
connection.expect(:puts, nil) do |text|
assert_equal("text", text)
monitor.instance_variable_set(:@show_prompt, true)
end


def monitor.gets
readline = proc {
@count ||= 0
sleep(0.1)
case @count
Expand All @@ -87,10 +98,12 @@ def monitor.gets
else
"q"
end
end
}

Net::Telnet.stub(:new, connection) do
monitor.monitor(type: :main)
Readline.stub(:readline, readline) do
Net::Telnet.stub(:new, connection) do
monitor.monitor(type: :main)
end
end

connection.verify
Expand All @@ -105,6 +118,7 @@ def test_monitor_manage_text
logger: Logger.new("/dev/null")
}
monitor = RokuBuilder::Monitor.new(**device_config)
monitor.instance_variable_set(:@show_prompt, true)
monitor.instance_variable_set(:@mock, mock)

def monitor.puts(input)
Expand Down

0 comments on commit 91af4fd

Please sign in to comment.