Solved

Using colors in terminal with 'watch' command

Posted on 2004-09-10
4
232 Views
Last Modified: 2010-04-21

We are monitoring an application remotely (over ssh) by viewing changes in files that it creates.  Some files are  rewritten by the application continually, and we watch them using:

watch -d -n1 cat [filename]

The "-d" option highlights the changes occuring, and provides a simple but effective monitoring screen.

Is there any way to incorporate colors into this?  The application could insert special characters into the files.

In the xterm, typing "ls --color" highlights some files in different colors.  But "watch ls --color" doesn't show the colors.  Why is that?

Any creative ideas?  Alternatives to watch?  

thanks,
Dan Evison
0
Comment
Question by:devison
  • 2
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
aib_42 earned 500 total points
ID: 12037539
Apparently, watch strips the ANSI escape character from its output.
(watch ls --color shows a lot of "[xxm" strings, which is the ANSI coloring sequence minus the starting \033)
I think this is because watch itself uses ANSI sequences (to highlight the differences and also clear the screen), and doesn't want programs to interfere with. Unfortunately, there seems to be no "--no-strip" parameter, either.

One simple alternative is to use

while true; do clear; cat [filename]; sleep 1; done

which doesn't really highlight the differences. Highlighting could be done by tee'ing the output to a file, comparing it to the last output and somehow color-drawing the differences onto the screen, but I think the best (and the easiest) alternative is to modify watch itself.

Actually I might do that myself. Not right now, though; I will post again tomorrow.
0
 

Author Comment

by:devison
ID: 12072603
Thanks for that information - I'm going to accept your answer.  I'd like to avoid the loop you suggested partly because I think watch is more efficient using the ncurses refresh function and perhaps only sending information that has changed.
I've had a look at the source code for watch myself, and I can see how I could change it to recognize specific escape sequences, then translate those back to ncurses function calls.  That doesn't seem ideal though.  
Any further thoughts on this would be appreciated.

Dan
0
 
LVL 7

Expert Comment

by:aib_42
ID: 12072997
> I will post again tomorrow.

D'oh, it's been 5 days. You can see how good of a week I'm having :).

Anyway, why wouldn't a modified watch be an ideal solution? What if it had a switch to preserve color? What if it was modified to have a switch to preserve color? :)
0
 

Author Comment

by:devison
ID: 12114206
Sorry, I missed your post...

A modified watch would be an ideal solution!  (The loop I mentioned I'd like to avoid was the while loop).  

I'm just wondering if watch can be modified without checking explicitly for every escape sequence and translating those back to ncurses function calls.  That's what I think may need to be done.

Of course it would be so much better to just pass any escape sequences over to the terminal, but because of the way watch writes character by character to specific coordinates I'm not sure that is possible.

What do you think?

thanks
Dan


0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question