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.
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
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.
Related
TL;DR?
How can I change every instance of one character (e.g. 'E') in a terminal window to another string of characters (e.g. '~E'), moving all other characters along in the window in the process? So:
abcdEfghij
becomes:
abcd~Efghij
This should work in the gnome-terminal and work with whatever output is on that terminal, from whatever program. Ideally it will be a script or other program I can run within that terminal emulator.
The context
I am using a Braille display (the Canute 360) with a Braille screen-reader (brltty), which at present does not support capital letters. They show up in Braille the same as lower-case letters. Until this is developed for BRLTTY I sometimes need to be able to force showing which letters are capitalised in order for me to, for example, write code.
The proposed solution
N.B. The below proposed solution is not intended to be elegant; it is a quick and dirty way of letting me and other Braille-using developers continue to program with this display for our work until the proper solution is forthcoming in the screen-reader proper.
I am trying to essentially 'wrap' the output of the terminal emulator (in this case gnome-terminal to force a certain character in front of every capital letter so on the Braille display it can be identified. I am going to assume that character is tilde (~). I am not concerned about this breaking vertical alignment, or forcing the line off the edge of the display. I am assuming a 40 character wide terminal (width of the Canute).
So this normal output:
$ echo ${string}
A Quick Brown Fox
Jumps over the lazy
Dog. Etc.
$
Becomes this:
$ echo ${string}
~A ~Quick ~Brown ~Fox
~Jumps over the lazy
~Dog. ~Etc.
$
That would then be viewable on the Canute (in US Computer Braille) as this:
$ echo ${string}
~a ~quick ~brown ~fox
~jumps over the lazy
~dog. ~etc.
$
It is fine for this to be a command that has to be called first, like screen. So:
$ caps-hack
$ nano
[doing my thing, then quit nano]
$
[ctrl-x to quit caps-hack]
$
Or alternatively it could launch a new terminal window, or it could be tied to specific TUI applications (like nano). I will primarily be using it for working inside vi, nano, micro and other editors, so if it cannot capture all terminal output the solution is still valuable.
Example use case: Micro/nano text editor
When I need to see capitals whilst editing text using micro or nano I would first launch caps-hack, then I could use the TUI editor, exit it, be back on the terminal, then cancel caps-hack if I wanted to revert to usual behaviour.
This is what nano normally looks like:
GNU nano 4.8 New Buffer Modified
This is a nonsense file for
Stackoverflow.
^G Get Hel^O Write O^W Where I^K Cut Tex
^X Exit ^R Read Fi^\ Replace^U Paste T
I'm looking for a solution that would then make it look like this:
~G~N~U nano 4.8 ~New ~Buffer ~Modi
~This is a nonsense file for
~Stackoverflow.
^~G Get ~Hel^~O ~Write ~O^~W ~Where ~I^~
^~X ~Exit ^~R ~Read ~Fi^\ ~Replace^~U
(Note that I have cut it off at 40 characters.)
The effect would be the same (inserting tildes, cutting off at 40 characters) whether I was in the terminal itself, in mc, or watching a ping stream.
This active use case means that so far as I can see I cannot simply pipe the output of programs to a bash script (like the one below), as that wouldn't work with a TUI. So I cannot do: $ nano myfile.txt | caps-hack
What I have tried so far
I have not worked out how to essentially capture the output, modify it and write it back to the terminal window. However I have the following shell snippet which I believe could be used for it, if I know where to put it.
# Repeat for all visible lines in the terminal
moddedline1=$( echo ${originalLine1} | sed -E -e 's/\([A-Z]\)/~\1/g' )
moddedline1="${moddedline1:0:40}"
tput cup 1 0 && printf "${moddedline1}"
My goal is to be able to colorize (or set underlining, etc) terminal window output from any source (I figure if ls works, all the rest will too) on an as-needed / as-desired basis via command line scripts. ...It's a given here that SOME programs may very well override this in their output, and insofar as that's an easy to address aspect of this question, that's in there, too - as in, I'd sort of like to know how that may pertain to any answer(s).
I already understand a heck of a lot of the background information as I use color in my shell scripts now. The issue is they don't persist when their shell exits; my focus in this question is on making functional a set of a few command line utilities that can let the user select from a set of known named colors, like Blue or Red, or even just give the correct code to do that, and leave all output to that terminal window in that situation until a reset is given, via a command line script, though surely if another command utility, like 'reset' were to reset it, so be it!
I believe this is likely possible through use of what for me at least are rarely touched elements of the environment like PS1.
I've touched that stuff so rarely, I really don't know what I'm doing there!
On Linux, if you are using a scrolled terminal that responds to the SGR standard, colours instructions sent to that display stream will remain (persist) until you flush that display stream with the "clear" command.
The execution of the below script will illustrate that with one set of FG/BG colours, then the terminal defaults, then another set of FG/BG colours:
#!/bin/sh
### Useful References:
### https://www.ecma-international.org/publications-and-standards/standards/ecma-48/
### https://en.wikipedia.org/wiki/ANSI_escape_code
###
### https://www.htmlcsscolor.com/hex/FFFF80
### https://www.w3schools.com/colors/colors_picker.asp?colorhex=FFFF00
### https://www.colorabout.com/color/hex/ffff80/
### https://www.color-name.com/hex/ffff80
### https://www.cpcwiki.eu/index.php/CPC_Palette
### https://hexcolor.co/hex/ffff80
### https://www.colorxs.com/color/hex-ffff80
### https://encycolorpedia.com/ffff80
### https://www.color-hex.com/color/ffff80
### https://www.colorcodehex.com/ffff80.html
### http://html-color.org/FFFF80
cat >sample_text.txt <<"EnDoFiNpUt"
The original specification only had 8 colors, and just gave them names. The SGR parameters 30–37 selected the foreground color, while 40–47 selected the background. Quite a few terminals implemented "bold" (SGR code 1) as a brighter color rather than a different font, thus providing 8 additional foreground colors. Usually you could not get these as background colors, though sometimes inverse video (SGR code 7) would allow that.
EnDoFiNpUt
FG_color="\e[91m" ### Red
BG_color="\e[47m" ### Gray
echo "${FG_color}${BG_color}"
cat sample_text.txt
echo "\e[0m" ### Environment Default
cat sample_text.txt
FG_color="\e[30m" ### Black
BG_color="\e[102m" ### Green
echo "${FG_color}${BG_color}"
cat sample_text.txt
echo "\e[0m"
That will appear as follows (on my terminal):
I'm having troubles in doing simple stuff with grep in my Mac. I'd like to rename a file, and for that I use grep and sed.
If I don't use grep, it works fine.
If I use grep, it doesn't work: mv shows a weird error.
For what I understand, the reason may be the colored grep I am using, and then mv uses not the filename, but the code for displaying the colored filename in the console (as elm content is a colored yum.txt, instead of a normal one).
Is this the reason? What can I do?
You are right, those strange characters are indeed color codes. You can search for the --color= option either in GREP_OPTIONS environment variable (printenv) and/or your user defined aliases.
Then just change --color=always to --color=auto; with the --color=auto option set, grep should display color codes only when the standard output is connected to a terminal and plain text otherwise.
How do you display Regional Status Indicators (things like country flags) in the Linux Terminal? Is this even possible?
I'm programming a VPN client and would like to include some flags for the countries the client includes. For example, I would like to display the Japan flag (🇯🇵). I pasted the two Unicode symbols 🇯 and 🇵 next to each other in a Bash script, but upon running the script in the Terminal, I just got the two symbols next to each other (they didn't "merge"). For example purposes I have part of the file below (note there is a space between the symbols so they don't "merge" in the browser, the space is not there in the real script).
#!/bin/bash
echo "Please choose a server:"
echo -e "🇯 🇵 Japan (1)" # in the real script there is no space here.
echo -e "..."
read -p "> " choice
...
And upon running:
$ ./script.sh
Please choose a server:
🇯 🇵 Japan (1) [ with no space in between ]
...
I understand the concept of Regional Status Indicators, but my question is if it's possible to use them in the terminal.
Edit: I noted that some answerers were having trouble understanding my problem, so I provided a screenshot of what my terminal looks like when I run this script.
Not sure if you copied the correct byte sequences, but you can simply use the correct escapes instead:
$ echo -e "\U1f1ef\U1f1f5 Japan (1)"
🇯🇵 Japan (1)
It may also be an issue with your terminal understanding the Unicode sequence and rendering it properly, rather than a problem with your script.
I have a program that I run through the command line and I wanted to print out bold or styled text similar to how the man pages are bold (I can't think of a styled example offhand).
How do I style text sent to the terminal?
If it makes a difference, I'm running a MacOSX terminal.
I believe you want to use the ncurses library to accomplish this.
You can have a look at this SO question: Colored grep? which shows a simple way to color output for VT100 terminals (works great on MacOSX)
Another useful SO Question is: Apply formatting to unix shell, with a link to ANSI escape codes, and examples from a shell.
You can do this from any shell script using the tput program to output terminfo codes. Oddly, there's a code to turn vold on but not off---you have to turn everything off. Reverse video can be turned on and off with tput smso and tput rmso.
Here's an example for bold (/bin/ksh):
print -n "This word is "; tput bold; print -n "bold"; tput sgr0; print "!"
In most programming languages it is easier to fork a process and call tput than it is to bother with the ncurses library (to which tput is a command-line interface).