One possibility, if the terminal supports it, is to use the terminal's Change Color escape sequence. Apparently konsole doesn't support it though. From the Xterm control sequence document (ctlseqs):
OSC Ps ; Pt BEL
Ps = 4 ; c ; spec -> Change Color Number c to the color specified by spec, i.e., a name or RGB specification as per XParseColor. Any number of c name pairs may be given. The color numbers correspond to the ANSI colors 0-7, their bright versions 8-15, and if supported, the remainder of the 88-color or 256-color table.
What this means is that the control sequence \e]4;NUMBER;VALUE\a
will change the appearance of color NUMBER. NUMBER is a color number (0–7 for the eight basic colors, 8–15 for the bright versions, and more if the terminal supports more colors). The VALUE is something that XParseColor understands, such as an RGB specification #123456
or an X color name (look for rgb.txt
on your machine, or use xcolors
to see the possibilities).
For example, the following command changes the basic blue color (color 4) and its bright variant (4+8) to contain some green:
printf '\033]4;4;#004080;12;#0040ff\007'
Note that this changes every character currently displayed in this particular color in the window. There is no way to change the meaning of a color only for subsequently displayed characters; if that's what you want, you'll have to configure each program displaying inside the terminal to use different color numbers when talking to the terminal.
Having this happen exactly when you're typing in an ssh session will be very complicated, but handling the common cases is reasonably simple: use a wrapper around ssh that changes the color palette, then runs ssh, and finally changes the color palette back. Examples of cases this won't handle are suspending the ssh process and running ssh inside screen or tmux.
I encountered the 'color-stealing' issues with watch, it's because watch uses a simple /bin/sh output style, which clobbers any color, aliases, shortcuts... the whole shebang!
So I cobbled this together, placed in my .bashrc, allows me to use all my own aliases, shortcuts and whatnot:
Usage:
watcher 20 'somecommand | apipe | grep'
The number is the delay between updates, in seconds, the command can include anything you can quote.
###### 'watch' workalike, lets me use my aliases and such
function watcher() { WATCHERTIME=$1 ; WATCHERFILE=/tmp/watcher$$ ; shift ; while true ; do WATCHERHEIGHT=$(($LINES - 5)) ; ( eval $* ) | tail -n ${WATCHERHEIGHT} > ${WATCHERFILE} 2>/dev/null; clear ; /bin/echo -n "Every ${WATCHERTIME} seconds - " ; date ; /bin/echo ; cat ${WATCHERFILE} ; \rm -f ${WATCHERFILE} ; /bin/echo ; /bin/echo "==" ; sleep ${WATCHERTIME} ; done ; }
To break it down:
function watcher()
{
WATCHERTIME=$1
WATCHERFILE=/tmp/watcher$$
shift
while true; do
WATCHERHEIGHT=$(($LINES - 5))
( eval $* ) | tail -n ${WATCHERHEIGHT} > ${WATCHERFILE} 2>/dev/null
clear
/bin/echo -n "Every ${WATCHERTIME} seconds - "
date
/bin/echo
cat ${WATCHERFILE}
\rm -f ${WATCHERFILE}
/bin/echo
/bin/echo "=="
sleep ${WATCHERTIME}
done
}
It determines the current screen height in lines, subtracts out enough for its own output, then repeatedly executes the command given, then clears the screen, displays the output and waits for the next loop. It displays a short '==' at the bottom to signify that's where the output ended. Sometime's it's helpful to know this.
I did it this way so there was smallest possible delay when displaying. if you don't capture output and then display, you get a long pause, then the output... icky.
Since it doesn't clobber colors, you get everything you're used to getting. Enjoy!
Best Answer
You might want to have a look at
toilet
. The following has been incorporated in the banner of one of the servers at my lab:You can install it on Debian based systems with
toilet
works perfectly well with ASCII art:I have written a little Perl script to highlight specific regexes in text. If you use
.
as the regex, it will color everything a specific color:The script (use
-h
for a tiny help message):