Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

engine: make output Readline-friendly #536

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/foreman/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,16 @@ def handle_io(readers)
if reader.eof?
@readers.delete_if { |key, value| value == reader }
else
data = reader.gets
output_with_mutex name_for(@readers.invert[reader]), data
$stdout.print marker(name_for(@readers.invert[reader]))

loop do
ch = reader.getc

$stdout.print ch
$stdout.flush

break if ch == "\n"
end
end
end
end
Expand Down
15 changes: 8 additions & 7 deletions lib/foreman/engine/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,19 @@ def startup

def output(name, data)
data.to_s.lines.map(&:chomp).each do |message|
output = ""
output += $stdout.color(@colors[name.split(".").first].to_sym)
output += "#{Time.now.strftime("%H:%M:%S")} #{pad_process_name(name)} | "
output += $stdout.color(:reset)
output += message
$stdout.puts output
$stdout.flush
$stdout.puts(marker(name) + message)
end
rescue Errno::EPIPE
terminate_gracefully
end

def marker(name)
output = ""
output += $stdout.color(@colors[name.split(".").first].to_sym)
output += "#{Time.now.strftime("%H:%M:%S")} #{pad_process_name(name)} | "
output += $stdout.color(:reset)
end

def shutdown
end

Expand Down
17 changes: 17 additions & 0 deletions spec/foreman/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@
end

describe "with a valid Procfile" do
describe "Readline applications" do
it "outputs prompt" do
without_fakefs do
output = foreman("start irb -f #{resource_path("Procfile.IRB")}")
expect(output).to match(/irb\(main\):001:0>/)
end
end

it "outputs prompt and responds to input" do
without_fakefs do
output = foreman("start irbinput -f #{resource_path("Procfile.IRB")}")
expect(output).to match(/# leet foreman/)
expect(output).to match(/=> 74331/)
end
end
end

it "can run a single command" do
without_fakefs do
output = foreman("start env -f #{resource_path("Procfile")}")
Expand Down
2 changes: 2 additions & 0 deletions spec/resources/Procfile.IRB
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
irb: bin/irb
irbinput: bin/irbinput
5 changes: 5 additions & 0 deletions spec/resources/bin/irb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env ruby
require 'irb'
Thread.new { IRB.start }
sleep 1
abort
16 changes: 16 additions & 0 deletions spec/resources/bin/irbinput
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env ruby
require 'pty'
require 'irb'

PTY.spawn('irb') do |reader, writer|
Thread.new do
sleep 1
writer.puts '0xf013 + 0x3248 # leet foreman'
end

loop do
sleep 2
print reader.sysread(256)
abort
end
end