Colors with Unix Command "Watch"

Colors with unix command watch?

Some newer versions of watch now support color.

For example watch --color ls -ahl --color.

Related.

linux watch -c (with color parameter), but commands display colorless output. Why?

ls and others see that they are run from a script so they go to the default mode (without colors):

With --color=auto, ls emits color codes only when standard output is connected to a terminal.

try forcing the color output:

watch -c ls --color

bash-program watch and ANSI escape sequences in the output

The --color option does the trick for me.

watch --color -n 1 "echo -e '\033[36mHello World\033[0m'"

Or, in the absence of the color option, how about a home-grown watch:

while [ 1 ]; do clear; echo -e '\033[36mHello World\033[0m'; sleep 1; done

Simple clone of unix watch using Ruby (with coloured output)

You're correct that the commands you are running switch to a non-colour output when invoked from a script. They detect that their standard output is not a terminal and modify their output accordingly.

Fortunately, you should be able to trick the commands into thinking they are outputting to a terminal by using a pseudo-terminal. You can do this using the PTY module in Ruby.

I took the following from this answer and tested it on Ruby 1.9.3-p392.

require 'pty'

PTY.spawn('ls --color=auto') do |stdin, stdout, pid|
begin
stdin.each {|line| print line}
rescue Errno::EIO
# End of input
end
end

How can I make the watch command interpret vt100 sequences?

Edit:

More recent versions of watch support color. You will need to use an extra level of quoting to preserve the quotes and escapes in the particular situation of the example in the question:

watch 'echo -e "\033[31mHello World\033[0m"'

From man watch:

  -c, --color
Interpret ANSI color sequences.

Previously:

From man watch:

Non-printing characters are stripped from program output. Use "cat -v"
as part of the command pipeline if you want to see them.

But they don't get interpreted, so I don't think there's any way.

Read Command : Display the prompt in color (or enable interpretation of backslash escapes)

read won't process any special escapes in the argument to -p, so you need to specify them literally. bash's ANSI-quoted strings are useful for this:

read -p $'\e[31mFoobar\e[0m: ' foo

You should also be able to type a literal escape character with Control-v Escape, which will show up as ^[ in the terminal:

read -p '^[[31mFoobar^[[0m: ' foo

OS X Bash, 'watch' command

You can emulate the basic functionality with the shell loop:

while :; do clear; your_command; sleep 2; done

That will loop forever, clear the screen, run your command, and wait two seconds - the basic watch your_command implementation.

You can take this a step further and create a watch.sh script that can accept your_command and sleep_duration as parameters:

#!/bin/bash
# usage: watch.sh <your_command> <sleep_duration>

while :;
do
clear
date
$1
sleep $2
done


Related Topics



Leave a reply



Submit