Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture original terminal colors #11

Open
oblique opened this issue Aug 8, 2012 · 26 comments
Open

Capture original terminal colors #11

oblique opened this issue Aug 8, 2012 · 26 comments

Comments

@oblique
Copy link

@oblique oblique commented Aug 8, 2012

you can get the colors with some console codes.. i wrote an example here https://github.com/oblique/configs/blob/master/.zsh.d/get_term_rgb_color.py
screen, tmux, and virtual console do not support this operation.

@oblique
Copy link
Author

@oblique oblique commented Aug 8, 2012

also some programs can change there colors, for example cmatrixreloaded
this is an example how they do it https://github.com/oblique/configs/blob/master/.zshrc#L160
actually see my ncmpcpp() and wicd-curses() functions

@sickill
Copy link
Contributor

@sickill sickill commented Aug 29, 2012

I tried your script but I get following error:

~ % get_term_rgb_color.py 1
Traceback (most recent call last):
  File "/home/kill/bin/get_term_rgb_color.py", line 30, in <module>
    os.write(fd, bytes("\033]4;%d;?\033\\" % int(x), "UTF-8"))
TypeError: str() takes at most 1 argument (2 given)

@oblique
Copy link
Author

@oblique oblique commented Aug 31, 2012

the script works in python3.

for python2 change the line to:
os.write(fd, "\033]4;%d;?\033" % int(x))

@sickill
Copy link
Contributor

@sickill sickill commented Oct 11, 2012

I was thinking about sth like this for some time. Thanks for bringing this on.
Unfortunatelly this works only under (u)rxvt and xterm. On gnome-terminal, screen, tmux it does not. So it wouldn't much of use. If there's maybe other (or additional) way to get this info I'd be happy to use it.
Or maybe there's some way to get just the info "are we under light fg/dark bg terminal or under dark fg/light bg".

@losingkeys
Copy link
Contributor

@losingkeys losingkeys commented Oct 11, 2012

I haven't looked into it... but maybe something from here? (originally form this stackoverflow answer)

@oblique
Copy link
Author

@oblique oblique commented Oct 18, 2012

It does work in gnome-terminal but it doesn't in konsole.
I will try to find any other way.

@oblique
Copy link
Author

@oblique oblique commented Oct 18, 2012

I found that with tmux you can forward console codes to the terminal and I modified my script https://github.com/oblique/configs/blob/master/.zsh.d/get_term_rgb_color.py
I didn't manage to do it with screen.
If you have any questions please ask.

@oblique
Copy link
Author

@oblique oblique commented Oct 20, 2012

Sorry, I forgot to push my changes in ncmpcpp() and wicd-curses() functions, now it's updated.

@sickill
Copy link
Contributor

@sickill sickill commented Nov 18, 2012

I just checked it under urxvt, xterm, gnome-terminal and tmux. Only under gnome-terminal it's not working.
Do you think this could also work on some OSX terminal like Terminal.app or ITerm2 ?

At the moment I'm refactoring the recorder a little (here: https://github.com/sickill/ascii.io-cli/tree/split). Once I'm done with refactoring I'll try to use the code you provided here.

Thanks!

@oblique
Copy link
Author

@oblique oblique commented Dec 1, 2012

I don't know about Terminal.app or ITerm2, if they support color changing it will work..

@sickill
Copy link
Contributor

@sickill sickill commented Oct 8, 2013

Just for the reference, I have also found this : https://github.com/dranjan/termcolors
Not sure if it gives us anything more than the @oblique's code though.

@losingkeys
Copy link
Contributor

@losingkeys losingkeys commented Oct 10, 2013

It looks like https://github.com/dranjan/termcolors depends on select.poll, which doesn't seem to work in OSX.

Scratch that, I was wrong. @sigmavirus24 figured out that there was an issue with the tool passing -1*2 around as a timeout value instead of just -1. More investigation needed... but this might end up working.

@sigmavirus24
Copy link

@sigmavirus24 sigmavirus24 commented Oct 17, 2013

Also note that when I tried this on OSX+iTerm2 it didn't actually pick up the term colors properly. So even with that fixed, it's still not the most optimal since the underlying C API that termcolors uses (select/select.poll) is not as flexible as the other *nix flavors which will accept any negative value.

@losingkeys
Copy link
Contributor

@losingkeys losingkeys commented Dec 11, 2013

So I worked on this with @sigmavirus24 and came up with this: https://gist.github.com/joshuarh/7904813. The following still needs to be done/checked:

  1. Send the color info up to the server
  2. Remove the default colors? or use them as a fallback?
  3. Make sure the background/foreground colors are correct. We're using "magic numbers" here, but I think they're constant, at least for xresources/xdefaults colorschemes.
  4. Make sure the web player can handle what's recorded.
    • I thought I read the player the site uses is vt100 compatible, or something to that effect. So can we send it escape codes and have it "just work"? Or do we need to convert the colors to hex first? If it's the latter, then we have some work to do. I know some colors are laid out with the rgb info right in there, so that's not hard to parse, but mine on urxvt look like this: '\x1b[30m', so I'm not sure how to convert that to something we can use on the web.
  5. Test it in various terminals. I know some of the other solutions didn't work on various terminals (VTE-based ones, Terminal.app, iTerm, rxvt, etc), so we should make sure this works before using it heavily.

@mrabault
Copy link

@mrabault mrabault commented Feb 11, 2014

It doesn't seem easy to obtain the terminal's colorscheme. Instead, could we allow the user to specify her/his colorscheme in a JSON file ? If the implementation is done generically enough, it'll be a breeze to switch to any new technique to capture the terminal colors we'd discover later on.

@losingkeys
Copy link
Contributor

@losingkeys losingkeys commented Feb 11, 2014

It doesn't seem easy, but I'm not convinced that it's not doable. I think we can record the escape codes, we just need to convert those back to xterm colors.

OTOH, if we allowed users to specify which colors they wanted, that brings up interesting possible features and bugs. We could let people choose which scheme they want to see when browsing, but depending on the foreground colors that were recorded, this could be a problem (for instance, here's the default theme with solarized foreground colors in fallback mode: https://asciinema.org/a/2214).

@mrabault
Copy link

@mrabault mrabault commented Feb 11, 2014

What I really meant is that it should be to the uploader to specify the colorscheme. As to the rest, I don't know and I'm not really able to judge.

@sickill
Copy link
Contributor

@sickill sickill commented Feb 12, 2014

@losingkeys we ARE already recording the escape color codes. The thing is they are just single numbers like 1, 2 or 3. They mean red, green and yellow. And so on. People use different color schemes in their terminal but most of the schemes are close to the "original" xterm colors so it looks "ok" in majority of cases.

@losingkeys Allowing the user to specify his color scheme (let's say in .Xresources format) is an easy thing to do and I'm considering doing just that. So if you use solarized or some other terminal theme that completely alters the meaning of each color it's gonna be a solution for wrong colors.

@mrabault the ideal solution would be to use recorder to detect and upload terminal scheme, yeah. But we haven't found (yet!) a good enough solution that would work for majority of terminal emulators across different OSes.

@blueyed
Copy link

@blueyed blueyed commented Nov 12, 2014

Has there been made progress in this regard, e.g. on the go branch?
Would querying xrdb for colors be an option? This could pick up a custom theme (e.g. solarized) automatically.

@sickill
Copy link
Contributor

@sickill sickill commented Nov 12, 2014

@blueyed no progress so far.

Querying xrdb would only make sense for xterm/rxvt/other-terminal-using-x-resources. People use libvte based terms (Gnome Terminal, XFCE Terminal, Terminator,...) and non-Linux terminals (iTerm, Terminal.app on OS X) too.

@sickill
Copy link
Contributor

@sickill sickill commented Nov 12, 2014

@blueyed btw, you can change a theme for your asciicasts, either account-wide (on your account settings page) or per asciicast (on asciicast edit page). For ex this is your asciicast in solarized-dark: https://asciinema.org/a/13855?theme=solarized-dark

@daGrevis
Copy link

@daGrevis daGrevis commented Jun 11, 2016

This would be a killer feature!

@sickill
Copy link
Contributor

@sickill sickill commented Apr 20, 2017

We're discussing version 2 of asciicast format in #196, and I think adding optional colors object to meta-data, containing text foreground/background color and the palette for all 8(16) colors would be a nice addition. As @mrabault said, we can use and update a technique of obtaining the colors, as long as the palette representation in JSON stays the same/compatible with the web player.

As for the actual technique, this issue is very old one and the linked solutions seem to be gone. I wanted something that works on all platforms, and I was hesitating to settle on non-100% working solution. Now I think it's better to just support what we can now, treat the colors palette as optional, and fallback to player's default theme when it's not in JSON (for any reason).

Anyone with something working on Python 3.3+?

@josegonzalez
Copy link

@josegonzalez josegonzalez commented Sep 1, 2017

@timofonic please never again just mention literally everyone who has been in contact with a repository. I'm not even a contributor here and I wouldn't have any "news" for you.

As an aside, if anyone had "news" on this - or the other 2 issues you mentioned myself and many others on - it would have been posted here as either a comment or a pull request that closes this issue. If you don't see anything relevant to the issue, assume that there is "no news" for you.

@XANi
Copy link

@XANi XANi commented Sep 1, 2017

@timofonic die in hell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
10 participants