tput doesn't set color in the terminal - tput

I wish to colorize my terminal while using interactive shell, namely the command prompt. I managed to do this using escape sequences:
PS1="\e[93m prompt> \e[0m"
I want to use tput instead of escape sequences. Like that:
PS1="$(tput setaf 1) prompt> "
However, this doesn't yeld any color.
I tried other tput subcommands in different ways. None of them gave any effect except of tput cols and tput lines which gave appropriate output.
How should I use tput properly in this case?

Related

Linux Bash Trying to write checklist progress "bar" of sorts. Replace the same line

Sorry if I am not giving you enough info, this is my first time posting here.
I am trying to make this in a bash script.
Downloading...............
"run bash commands and when they are done, replace the "Downloading..." text with the text bellow in the same line aka space."
Downloading............... DONE!
go to next line and show
Installing................
"run bash commands again and when they are done, replace the "Installing..." text with the text bellow in the same line aka space."
Installing................ DONE!
I hope you get what I mean. Thanks in advance.
I've tried:
#/bin/bash
tput sc # save cursor
printf "Something that I made up for this string"
sleep 1
tput rc;tput el # rc = restore cursor, el = erase to end of line
printf "Another message for testing"
sleep 1
tput rc;tput el
printf "Yet another one"
sleep 1
tput rc;tput el
But it doesn't make new lines, it just uses one line to show all text.
I'm assuming you pulled the tput code from somewhere, and I'm guessing that 'somewhere' also explained that tput is being used to overwrite the same line multiple times (as your script actually does).
From your description it doesn't sound like you need to overwrite any lines so using tput is the wrong solution.
If I understand your description correctly you should be able to do everything you want with some (relatively) simple printf commands, eg:
printf "Downloading .... " # no '\n' in the output so cursor remains at end of current line
# run your bash commands here
printf "DONE!\n" # append to end of current line and then add a new line (\n)
printf "Installing .... " # no '\n' in the output so cursor remains at end of current line
# run more bash commands here
printf "DONE!\n" # append to end of the current line and then add a new line (\n)
Keep in mind that if any of your 'bash commands' generate any output then the cursor will be moved (probably to a new line) thus messing up your output. Net result is that you'll need to make sure your 'bash commands' do not generate any output to stdout/stderr (alternatively, make sure all output - stdout/stderr - is redirected to files).
If your requirement is to have the 'bash commands' send output to the terminal then you may need to go back to using tput ... but that's going to depend on exactly how you want the output to appear.
NOTE: If this (above) does not meet your requirement then please update the question with more details.

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"

Colourful makefile info command

Usually I am using echo -e "\e[1:32mMessage\e[0m" to print colourful messages out of the makefile. But now I want to print message inside of the ifndef block, so I am using $(info Message) style. Is it possible to make this kind of message colourful ?
Yes. You can use a tool like tput to output the literal escape sequences needed instead of using echo -e (which isn't a good idea anyway) to do the same thing.
For example:
$(info $(shell tput setaf 1)Message$(shell tput sgr0))
Though that requires two shells to be spawned and two external commands to be run as opposed to the echo (or similar) methods in a recipe context so that's comparatively more expensive.
You could (and should if you plan on using the colors in more than one place) save the output from tput in a variable and then just re-use that.
red:=$(shell tput setaf 1)
reset:=$(shell tput sgr0)
$(info $(red)Message$(reset))
$(info $(red)Message$(reset))

Make color escape codes work in cgdb

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.

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