From 43d1a5103e38037dd305b3f465e9cd4c65fecd58 Mon Sep 17 00:00:00 2001 From: David Dollar Date: Sat, 3 Aug 2024 19:31:17 -0400 Subject: [PATCH] better handling of prefix injection --- lib/foreman/engine.rb | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/foreman/engine.rb b/lib/foreman/engine.rb index 360a547b..9de49345 100644 --- a/lib/foreman/engine.rb +++ b/lib/foreman/engine.rb @@ -36,6 +36,7 @@ def initialize(options={}) @env = {} @mutex = Mutex.new @names = {} + @prefixed = {} @processes = [] @running = {} @readers = {} @@ -378,6 +379,7 @@ def spawn_processes writer.puts "unknown command: #{process.command}" end @buffers[reader] = Buffer.new + @prefixed[reader] = false @running[pid] = [process, n] @readers[pid] = reader end @@ -414,8 +416,7 @@ def handle_io(readers) def handle_io_interactive(reader) done = false name = name_for(@readers.invert[reader]) - - output_partial prefix(name) + indent = prefix(name).gsub(ANSI_TOKEN, "").length loop do @buffers[reader].write(reader.read_nonblock(10)) @@ -423,13 +424,18 @@ def handle_io_interactive(reader) @buffers[reader].each_token do |token| case token when /^\e\[(\d+)G$/ - output_partial "\e[#{::Regexp.last_match(1).to_i + prefix(name).gsub(ANSI_TOKEN, "").length}G" + output_partial "\e[#{Regexp.last_match(1).to_i + indent}G" when ANSI_TOKEN output_partial token when "\n" output_partial token - output_partial prefix(name) + @prefixed[reader] = false else + unless @prefixed[reader] + output_partial "\e[1G" + output_partial prefix(name) + @prefixed[reader] = true + end output_partial token end done = (token == "\n") @@ -450,6 +456,11 @@ def handle_io_noninteractive(reader) end end + def output_prefix(reader) + output_partial prefix(name_for(@readers.invert[reader])) + @prefixed[reader] = true + end + def watch_for_output Thread.new do begin