Can one capture what a command prints on the REPL in a variable of type String?

I want to display on a list of the packages I am using or importing. I know this command

Pkg.status()

wll print the list on the screen, but with other items I am not interested in. So I would like to capture what is printed on the screen as a string, clean it up, and then store it in a String variable.

I tried to do this

captured_string = "$(Pkg.status())"

but captured_string turned out to be the string “nothing.”

Is there a way to capture what is printed by this command?

Note: the question is not specific to Pkg.status(). I would like to be able to capture what is printed on the screen by any Julia command.

Try Suppressor.jl.

2 Likes

You can do this with sprint and any function that takes an I/O stream argument to print into.
For Pkg.status:

sprint(io->Pkg.status(;io))
1 Like

Thanks. Exactly what I needed.