3

I have a simple python script that prints text with color, using colorama package.

test.py

#!/usr/bin/env python
from colorama import Fore

text = "Test"

print(Fore.RED + text)

Problem is - when I try to put it into watch command when connected via SSH, it doesn't show the colors.

when connected via SSH I run

watch -c test.py

And it doesn't show the colors.

I searched a lot about this issue, but watch via SSH won't show colors for some reason. (also when using the -c flag).

What is the problem here?

EDIT:

Asked another question that reproduces the issue - linux - watch command not showing colors via ssh

Community
  • 1
  • 1
Ofek Agmon
  • 5,040
  • 14
  • 57
  • 101
  • Possible duplicate of [Colors with unix command "watch"?](http://stackoverflow.com/questions/3793126/colors-with-unix-command-watch) – languitar Apr 18 '17 at 15:42
  • Confirmed! Using colorama with watch and color flag - like you are supposed to - results in uncolored text, see answer for workaround. – Fabian N. Apr 18 '17 at 15:47
  • 1
    Sorry, forgot to mention that I am connected via SSH and it doesn't work. Edited the question. – Ofek Agmon Apr 18 '17 at 15:51
  • Sry can't reproduce, that is most likely a problem with your terminal. I just sshed into my home server and run the script there with `watch -c "python test.py"` and I got colors.... – Fabian N. Apr 18 '17 at 16:13
  • asked another question that reproduces the problem here - http://stackoverflow.com/questions/43495237/linux-watch-command-not-showing-colors-via-ssh – Ofek Agmon Apr 19 '17 at 12:04

1 Answers1

2

Update: after William Russells comment I looked a bit around in the colorama github repo and found the wrap parameter of the init() function

By telling colorama to act like stdout is not wrapped by calling init(wrap=False):

test.py

# !/usr/bin/env python
import colorama
from colorama import Fore

colorama.init(wrap=False)

text = "Test"

print(Fore.RED + text)

you get proper colors inside the watch command too.


Update 2: For colors over ssh in general see How to get coloured terminal over ssh?


Original answer:

Ok I don't have any idea why but when I remove the colorama.init() it works:

test.py

#!/usr/bin/env python
from colorama import Fore

text = "Test"

print(Fore.RED + text)

Called with watch -c test.py results in red "Test"...

I was copy pasting their sample from the python package index and forgot the init() and surprise I got colored watch output o.O

Fabian N.
  • 3,807
  • 2
  • 23
  • 46
  • 2
    init() is probably checking if it is writing to a tty and disabling color if it is not. When running through watch, the output of the wrapped program is not a tty, so init() disables the color. – William Pursell Apr 18 '17 at 15:48
  • Sorry, forgot to mention that I am connected via SSH and it doesn't work. Edited the question. – Ofek Agmon Apr 18 '17 at 15:52
  • asked another question that reproduces the problem here - http://stackoverflow.com/questions/43495237/linux-watch-command-not-showing-colors-via-ssh – Ofek Agmon Apr 19 '17 at 12:05