Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 278
  • Last Modified:

Using colors in terminal with 'watch' command


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
devison
Asked:
devison
  • 2
  • 2
1 Solution
 
aib_42Commented:
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
 
devisonAuthor Commented:
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
 
aib_42Commented:
> 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
 
devisonAuthor Commented:
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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now