1

I am using the termcolor library with python3 to output colored text in my terminal.

I have a few issues though. It seems to overwrite more global settings, as words on which I do not use termcolor have their color changed too.

Better to look at the image: Image

In all the lines, I only use termcolor to color the last column. The beginning of the first line till the previous to last column looks like a standard line in my terminal (konsole). The last column should be red, but somehow is orangish-red... The 2nd row is fully colored changed, yet I only applied it to the last column... (which this time is red, as wanted). The 3rd row is the same as the 2nd, minus the fact that the last one should be yellow but is orange instead.

If I run my script in xterm instead of Konsole, the output is exactly what I would like, but again it changes the color (/ highlight?) of some text I did not ask it for. (The output of the script looks indeed like what I'd want, but the original xterm colors are not to my liking and somehow termcolor changed them all again like with Konsole, although the end is more pleasing it is still not the desired behavior)

Is this something to configure in my python script? Or in my terminal?

Here is a code sample:

if col1 == "?":
    coloredCol    = colored(col2, "yellow")
elif col2 > 1:
    coloredCol    = colored(col2, 'green')
elif col2 < 1:
    coloredCol    = colored(col2, 'red')
else:
    coloredCol    = colored(col2, 'white')

print (col1, col3, col4, col5, col6, coloredCol)

col1, col3, col4, col5, col6 are just "simple" "non-termcolored" variables.

Thank you!

gee
  • 13
  • 4
  • Can you post some code? Also when using color codes, its best to explicitly state the color/style before any printing and not rely on the style resetting on newlines. – kalhartt Oct 25 '13 at 02:48
  • @kalhartt I have added a sample thank you. As for being explicit, that may be a good idea. I am not yet sure how to get the same color/effect as original though. – gee Oct 25 '13 at 03:19
  • Looks like Termcolor resets the default color properly, ie. `repr(colored('a', 'red')) == '\x1b[31ma\x1b[0m'`. So being explicit is good practice but not strictly necessary. I can't reproduce this in the consoles I have available, will check back when I can install konsole. – kalhartt Oct 25 '13 at 03:42
  • Can't reproduce the problem in konsole either, [this code](https://gist.github.com/kalhartt/7149518) gives me [this result](http://imgur.com/lmRasua), which works as expected. Check/post the ansi codes generated using `print(repr(colX))` – kalhartt Oct 25 '13 at 04:47
  • Indeed it looks fine in your code, it might be setup related? Here are 3 rows: http://pastebin.com/tn7Traf3, the first column is for the result of repr of the original column before we color it, the 2nd one is repr of the colored column, the 3rd column is the result of repr for the column before the one in color (so should be unchanged). – gee Oct 25 '13 at 05:03
  • It might be setup related, does your `python -m termcolor` look like [this](http://imgur.com/FYO2wlp). – kalhartt Oct 25 '13 at 05:36
  • Not quite, [that](http://imgur.com/jRdX0aK). I will look into my terminal setup then and assume termcolor is doing as expected. Thank you! – gee Oct 25 '13 at 19:51

0 Answers0