2

I am looking for a way to execute a terminal command and get the colored output of it. In that below post, they discuss how to get the output of an executed command which is partially what I need.

But the thing is terminal will not output with ANSI color codes, which as I understand it, programs know if they output to the terminal or not so they give a colored output when printing to the terminal and non-colored for else. Even though some commands support forced-colored outputs (like grep --color=auto, ls --color) not all of them do.

I also tried the command script which is said it saves colors but did not work for me.

How do I execute a command and get the output of the command within C++ using POSIX?

yunusemregul
  • 179
  • 3
  • 11
  • So you want to know which ANSI color codes were used in a command like `ls --color` ? – Andreas DM Jan 18 '20 at 01:46
  • Most commands don't output colours if they detect they aren't running in a tty terminal, you'll have to fool it into thinking it is somehow – Alan Birtles Jan 18 '20 at 08:11
  • `unbuffer` sounds promising https://superuser.com/a/751809 – Alan Birtles Jan 18 '20 at 08:17
  • Thank you for your research, @AlanBirtles. The command `unbuffer` is not something that comes by default. My primary goal was to achieve this by using system libraries, still thanks. – yunusemregul Jan 18 '20 at 19:00
  • Yes, @AndreasDM. I am trying to get executed command output with ANSI color codes as some commands do that with `--color` option. – yunusemregul Jan 18 '20 at 19:02

0 Answers0