Solved

Using colors in terminal with 'watch' command

Posted on 2004-09-10
4
215 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now