问题
Is there any software that will automagically add terminal colors to terminal-based apps that don't support colors?
How would it work? Well, the software could at least try to identify source code in the same way that syntax highlighters do (albeit most syntax highlighters would have problems with parts of the source tree being off-screen), and this general method can be extended to lots of other more-human forms of data; for example, dates, times, money, email addrs, etc are easy to automatically identify.
回答1:
There are two parts to the question:
1) How do I add colors in general? Those are signaled via ANSI Color Codes
For example:
$ echo -e "plain \033[0;36mcyan\033[0;0m"
You should see plain cyan, with "cyan" colored.
2) How do I add colors to my specific app? For a standard command-line utility that writes to standard output, you can pipe it to a script that inserts the color codes:
$ echo "plain cyan" | sed 's/cyan/\\033[0;36mcyan\\033[0;0m/'
plain \033[0;36mcyan\033[0;0m
$ echo "plain cyan" | sed 's/cyan/\\\\033[0;36mcyan\\\\033[0;0m/' | while read x; do echo -e "$x"; done
plain cyan
回答2:
Depends what you mean by a terminal based app. If it's just writing to stdout then in theory you could pipe it to a filter that does what you suggest: "parse the input and modify the output" e.g. if it sees a keyword wrap it in "color tags" as required by your terminal.
If it's using something like "curses" to address the terminal screen, I think this becomes close to impossible. It might be ok on a single terminal type, but I suspect it would be a major effort.
来源:https://stackoverflow.com/questions/18905470/colorize-bw-terminal-based-apps