Make color escape codes work in cgdb - colors

I have the following function residing in ~/.gdbinit:
define foo
echo \033[34m
echo testing...\n
echo \033[0m
end
When running foo in gdb it prints testing... in blue, however, when running it in cgdb the result is:
[34mtesting...
[0m
How can I enable color escape codes in cgdb?

Unfortunately, the gdb "window" in cgdb is not a full-fledged terminal... it can handle basic text I/O but it will not render any terminal escape sequences such as colors, cursor movement, etc.

Related

Where and How Bash convert strings to colors

I am working on Bash 5.0 from GNU repository. I wanted to find the place where Bash reads a string with ASCII colors and convert it to colors, like in the following case where it convert "Hello" to red:
root#ubuntu:~/Desktop/bash-5.0# ./bash
root#ubuntu:~/Desktop/bash-5.0# echo $BASH_VERSION
5.0.0(8)-release
root#ubuntu:~/Desktop/bash-5.0# ./bash -c 'echo -e "\033[31mHello\e[0m World"'
Hello World
I searched inside the source code and found two files that seems to be related:
bash-5.0/lib/readline/colors.c - link
bash-5.0/lib/readline/parse-colors.c - link
But they are not, they work only on the first time I load Bash and you need to write the following rows in the file ~/.inputrc for it to work:
set colored-completion-prefix on
set colored-stats on
Any idea where in the code Bash takes string like that "\033[31mHello" and convert it to red?
It's not the shell that's converting anything to colors, it is your terminal. The shell only outputs ANSI escape codes which are then picked up by the terminal.
Depending on your point of view and philosophical interpretations, \033[31mHello already is a colored string (for the shell, at least, it is)

ASCII underline escape code for Linux terminal capabilities

In a Linux terminal emulator with xterm capabilities, printing the escape code \x1b[4m will make the following characters print with an underline.
In the Linux console with linux term capabilities, printing the escape code \x1b[4m causes the following characters to be printed in blue.
How do I print underlined text in a Linux console?
When I mention "linux" or "xterm" term capabilities I'm talking about the output of $echo $TERM.
Instead of \x1b[4m, try $(tput smul)
printf "%s\n" "$(tput smul)This is underlined"

Sending Ctrl-L via dbus to terminal emulator

One can send text via dbus to the terminal emulator konsole as followed:
qdbus org.kde.konsole /Sessions/1 sendText "hello"
However I want to remotely clear the screen of the specified terminal window.
So I tried:
qdbus org.kde.konsole /Sessions/1 runCommand "clear"
Does partly what I want. Only problem: The screen doesn't get cleared when there is a process running.
In the terminal emulator, in this case the key combination "Ctrl + L" would do the job.
So I'm trying to send a string with escape characters for this shortcut.
Is this going to work? This, however doesn't do;
qdbus org.kde.konsole /Sessions/1 sendText "\033[2J"
(runCommand neither)
This is working for me:
qdbus org.kde.konsole /Sessions/1 sendText $'\014'
First, to produce a character from its octal code, the syntax "\033" would work in C but not in Bash.
Second, while "ESC [ 2 J" is a VT100 code to Erase Screen, it work for me only if I echo $'\033[2J', and that will not work if a command is running.
Third, Ctrl-L will work if a program is expecting input from a terminal (like irb or python do), but it will not work for a while sleep 1; do echo Still running; done loop.

Printing a "Hello World" in bourne shell without directly using white space char

I'm currently trying to solve a tricky/silly challenge and i've come to a dead end.
The challenge is basically to form a one-liner /bin/sh compatible command line
which essentially outputs "Hello World" without directly typing White space or Tab characters in the command itself.
for example something like -
echo Hello World
would be invalid since we used white space twice in the command line.
Any ideas?
Assuming that IFS by default is set to space:
# echo${IFS}a${IFS}b
a b
Tested on Solaris 10 sh.
Cheating a little, but it gives the correct effect (superficially) in bash:
PS1=hello$'\x20'world$'\n'"$PS1"
for example,
$ PS1=hello$'\x20'world$'\n'"$PS1"
hello world
$
The problem is that it will print hello world after every command in future :-)

Is it possible to make stdout and stderr output be of different colors in XTerm or Konsole?

Is it even achievable?
I would like the output from a command’s stderr to be rendered in a different color than stdout (for example, in red).
I need such a modification to work with the Bash shell in the Konsole, XTerm, or GNOME Terminal terminal emulators on Linux.
Here's a solution that combines some of the good ideas already presented.
Create a function in a bash script:
color() ( set -o pipefail; "$#" 2>&1>&3 | sed $'s,.*,\e[31m&\e[m,' >&2 ) 3>&1
Use it like this:
$ color command -program -args
It will show the command's stderr in red.
Keep reading for an explanation of how it works. There are some interesting features demonstrated by this command.
color()... — Creates a bash function called color.
set -o pipefail — This is a shell option that preserves the error return code of a command whose output is piped into another command. This is done in a subshell, which is created by the parentheses, so as not to change the pipefail option in the outer shell.
"$#" — Executes the arguments to the function as a new command. "$#" is equivalent to "$1" "$2" ...
2>&1 — Redirects the stderr of the command to stdout so that it becomes sed's stdin.
>&3 — Shorthand for 1>&3, this redirects stdout to a new temporary file descriptor 3. 3 gets routed back into stdout later.
sed ... — Because of the redirects above, sed's stdin is the stderr of the executed command. Its function is to surround each line with color codes.
$'...' A bash construct that causes it to understand backslash-escaped characters
.* — Matches the entire line.
\e[31m — The ANSI escape sequence that causes the following characters to be red
& — The sed replace character that expands to the entire matched string (the entire line in this case).
\e[m — The ANSI escape sequence that resets the color.
>&2 — Shorthand for 1>&2, this redirects sed's stdout to stderr.
3>&1 — Redirects the temporary file descriptor 3 back into stdout.
Here's an extension of the same concept that also makes STDOUT green:
function stdred() (
set -o pipefail;
(
"$#" 2>&1>&3 |
sed $'s,.*,\e[31m&\e[m,' >&2
) 3>&1 |
sed $'s,.*,\e[32m&\e[m,'
)
You can also check out stderred: https://github.com/sickill/stderred
I can't see that there is any way for the terminal emulator to do this.
The interface between the terminal emulator and the shell/app is via a pseudo-tty, where the terminal emulator is on the master side and the shell/app on the other. The shell/app have both stdout and stderr connected to the same pty, so when the terminal emulator reads from the pty for the shell/app output it can no longer tell which was written to stdout and which to stderr.
You will have to use one of the solutions that intercepts the data between the application and the slave-pty and inserts escape codes to control the terminal output colo(u)r.
Here is a little Awk script that will print everything you pass it in red.
#!/usr/bin/awk -f
{ printf("%c[%dm%s%c[0m\n", 0x1B, 31, $0, 0x1B); fflush() }
It simply prints each line it receives on stdin within the necessary escape codes to display it in red. It is followed by an escape code to reset the terminal.
(If you need a different color, change the second argument in the above printf call from 31 to the number corresponding to the desired color.)
Save it to colr.awk, do a chmod a+x, and use it like so:
$ my_program | ./colr.awk
It has the drawback that lines may not be displayed in order, because stderr goes directly to the console, while stdout is piped through an additional process.
A simple solution to color stdout in red is to pipe it through grep:
program | grep .
This should not require installing anything, as grep should be already installed everywhere.
Taken from Dennis’s comment on superuser.com.
I think you should use the standard escape sequences on stderr. Have a look at this.
Hilite will do this. It's a lightweight solution, but you have to invoke it for each command, eg. hilite gcc myprog.c. A more radical approach is built in to my experimental shell Gush which shows stderr from all commands run in red, stdout in black. Either way is very useful for software builds where you have lots of output with a few error messages that could easily be missed if not highlighted.

Resources