Basic terminal output using C++ - Questions

2019-08-27 15:50发布

问题:

Well, the question may sound a bit too vague but here's 2 things I need to do and I'd definitely need some input on this :

  • Output something (e.g. using cout) with color (note: My TERM environment variable is set to xterm-color if that makes any difference; also, is there any uniform way to output colored text that's compatible with both pure mac and *nix terminals in general, so that the code is portable)

  • Output something at the same position on the terminal screen. OK, this may sound confusing too. Let's take a terminal app which simply outputs a progress percentage. It normally won't start a new line for that. The new value is shown at the very same spot. How is this doable? (Being a once Borland Pascal guy from the good old DOS days, the only thing I could think of is something to do with accessing video memory directly... or not?)

So... any ideas?

回答1:

You probably want to use ncurses library. And ANSI escape codes can also be used for coloring.



回答2:

1)

You can try Color cout , but that is not protable. I tried (ANSI escape codes) something like

cout << "\033[1;31mbold red text\033[0m\n";

cout << "\33[0;31m" << "Enter Your String here" << "\33[0m" << std::endl ;

You can also look at How do I output coloured text to a Linux terminal?

2)

Are you looking for something like watch or top like app which are showing output at the same spot.