Capturing and displaying a command's output at the same time

Hi there!

I'm currently making an application where at some point I want to let a long command running and display its output in the terminal, while still being able when the command ends to retrieve all its output in a string.

How can I do this? Given the command will use escape sequences to display colors and reset lines in the terminal, I need something that would work a bit like an Stdio::inherit_and_pipe().

Thanks in advance for your help!