ruby - Capturing $stdout and writing to console loses color in bash console

Viewed 54

Writing directly to console maintains color codes

I have a command that writes to the console. The image shows the bash color codes working. create is green

RailsAppGenerator::AppGenerator.start(opts, destination_root: destination_root)

enter image description here

Writing to console via a captured string, looses the color codes

If I capture the console to a variable and then write the variable back to $stdout, the console does not show colors.

enter image description here

capture_console do
  RailsAppGenerator::AppGenerator.start(opts, destination_root: destination_root)
end

def capture_console
  $stdout = StringIO.new
  yield
  @console_output = $stdout.string
  $stdout.close
  STDOUT.puts @console_output
ensure
  $stdout = STDOUT # save_stdout
end

0 Answers
Related