Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

256-color terminal support #4

Open
laanwj opened this Issue · 27 comments

4 participants

@laanwj

Very nice project, I've been pondering abut something like this for a while, and existing Python console support projects seem to have died a quiet death.

A small feature request: Most terminals these days support 256 color (gnome, konsole, even old xterm/mrxvt). It'd be awesome if you could use these colors in Clint (for example, by setting colors as RGB and choosing the closest color). We're now in 2011 and 8-color text is old-fashioned :)

I might get around to implementing this myself and submitting a patch. I have code for this in one of my other projects. (As for lists of R/G/B values, and/or color names, IPython could be an inspiration as it has a module for it)

@kennethreitz

Thanks! :)

I've been considering something like this for a while now, but wasn't sure about cross platform support. Does the windows terminal support 256?

Possible API:

from clint.textui import colored

colored.rgb('3f3f3f', 'Special colored text')
@laanwj

That API looks good.

I'm not sure about the Windows terminal, it's probably too wired to the old DOS-age (Putty does seem to support it with some settings enabled -- http://blog.cutsnake.net/2009/03/256-colours-with-vim-and-putty.html ).

But indeed, fact is that not all terminals support it, so it either needs detection (preferable) and/or a user-specified flag to enabled it.

In case of 8/16 colors, a fallback is needed to choose the closest color available.

Edit: found a pretty recent negative w/o Windows console 256 color support: http://cygwin.com/ml/cygwin/2010-04/msg00877.html

@kennethreitz

API:

colored.rgb('#3f3f3f', 'Special colored text')
colored.rgb('3f3f3f', 'Special colored text')
colored.rgb(('3f', '3f', '3f'), 'Special colored text')
colored.rgb((100, 24, 244), 'Special colored text')
@kennethreitz

Also, standard html colors support:

colored.rgb('lightcyan', 'Cyan Text')
@laanwj

It looks like the HTML colors map pretty well to xterm256 colors:

Screenshot

I've now put the code in some self-contained modules. Conversion of RGB from/to HTML color:
http://pastebin.com/bGfJEyQ6

Conversion of RGB from/to xterm256 color:
http://pastebin.com/LtFehhfz

Both use the same colorconv.<xxx>.from_rgb / to_rgb interface. I'll make a similar module for ansi, as fallback. This is simple, but ambiguous, as ANSI colors are not fixed in RGB space as the HTML/xterm256 ones.

Then I'll go integrate this into Clint.

@kennethreitz
Owner

Beautiful!

You, sir, are a gentleman and a scholar. :cake:

@kennethreitz
Owner

btw, are those clint columns? :P

@laanwj

Not yet, at the moment they are improvised columns :)

@laanwj

I've implemented the API support in my fork:

https://github.com/laanwj/clint

I've added an example colors_all_html.py to showcase it (using Clint columns :P).

Open issue

  • Is it possible to detect terminal color support? Currently it defaults to ANSI16 unless the user sets otherwise. For xterm/putty/GNOME it could pick 256 colors, and for konsole 24 bit RGB.
@laanwj laanwj was assigned
@kennethreitz

Fantastic! I'm playing with it now.

Can you send a pull request from your xterm256 branch to my feature/256 branch?

@kennethreitz

Hmm, this breaks the regular colored.red('string') capability for some reason.

@laanwj

Couldn't send a pull request, I think you already synced it.

OK strange I'll take a look.

@laanwj

Solved in my branch, I had forgotten a getattr.

@kennethreitz

@laanwj thank you sir!

As soon as we can figure out how to detect a terminal's color capabilities and disable 256 when it's not available, I'll merge this in.

@laanwj

Did a little research on that,

https://docs.google.com/document/d/1xPuad1f8yM7NvBzXmuaSWVEyZXDAdQTOB5yZ4V7YBXk/edit?hl=en&authkey=CKOo7_oD

To conclude: it's complicated (except on Windows).

@kennethreitz

I'm thinking of building an internal table of supported terminal emulators.

$TERM

@laanwj
@laanwj

I see you removed the colorama intitialisation, why is that? I think colorama init would be a good place to put the TERM-based detection?

@kennethreitz

@laanwj, the colorama.init call tells colorama to automatically send a reset color after anything is printed, which is unnecessary because ColoredString already ends them with a reset.

@jdevera

I read the linked doc but I don't know why tput colors is not a good method.

The “tput colors” command shows the amount of colors specified in the terminfo entry specief by TERM.

So this means in theory there would not be any need to keep a list of supported TERMs. If tput colors says 8, it's up to the user to configure the terminal to display more colors, and clint should probably do what other programs do, fall back to an approximate value within those available colors.

What is really blockiing this merge?

@laanwj

Yes, in theory you're right. But if you want it to work out of the box in for example Ubuntu (gnome terminal), just taking the tput value for granted is not enough. It returns 8... :(

@jdevera

That's right, but what I mean is that we still should rely on what tput says. Other programs like Vim or Tmux use the 8 color approximations until I set the TERM properly. It's a pain, but the bug is in the distros or the terminals, I guess. It shouldn't be a blocker for Clint, I think.

@laanwj

Fine with me, the checks for TERM/COLORTERM can always be added in later.

@jdevera

Can we please retake this?

@jdevera

@kennethreitz @laanwj Is this code anywhere still? I'd like to take a look and try to make it work based on what tput says, but I could find no PR or branch with it. I'm so looking forward to outputting crazy colours! :)

@laanwj

@jdevera I never submitted a pull request somehow. But the code is still here:
https://github.com/laanwj/clint/tree/xterm256

@jpiper
Collaborator

I'm going to pick this back up and hopefully get it in clint soon :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.