Skip to content

Commit

Permalink
Make color escape sequences conform to terminfo entry
Browse files Browse the repository at this point in the history
img2xterm had been combining multiple 256color rendition changes in the
same SGR sequence, e.g., `ESC [ 48;5;179; 38;5;100 m` to change both the
background and foreground color.

The xterm-256color terminfo entry requires (and mosh enforces) that
these be in separate escapes. This commit makes the img2xterm output
render correctly in mosh.

Example: `echo -e "setab 179\nsetaf 100" | tput -S | od -c` generates
`ESC [ 48;5;179 m ESC [ 38;5;100 m`.

* Olivia5k/doge#48
* mobile-shell/mosh#519
* mobile-shell/mosh#110 (comment)
  • Loading branch information
keithw committed May 11, 2014
1 parent 219eee6 commit 6b23da1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ Converting images
Known issues
------------

* Doesn't work in [mosh] [7].
* There is something wrong with the implementation of [CIE94 delta-E] [8].
* ImageMagick can be glitchy. Perhaps using libpng would be a better idea.

[7]: https://github.com/keithw/mosh
[8]: https://en.wikipedia.org/wiki/Color_difference#CIE94
15 changes: 4 additions & 11 deletions img2xterm.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,19 +251,12 @@ void bifurcate(FILE* file, unsigned long color1, unsigned long color2, char* bst
if (bg != oldbg)
{
if (bg == color_transparent)
fputs(cowheader ? "\\e[49" : "\e[49", file);
fputs(cowheader ? "\\e[49m" : "\e[49m", file);
else
fprintf(file, cowheader ? "\\e[48;5;%lu" : "\e[48;5;%lu", bg);

if (fg != oldfg)
if (fg == color_undef)
fputs(";39m", file);
else
fprintf(file, ";38;5;%lum", fg);
else
fputc('m', file);
fprintf(file, cowheader ? "\\e[48;5;%lum" : "\e[48;5;%lum", bg);
}
else if (fg != oldfg)

if (fg != oldfg)
{
if (fg == color_undef)
fputs(cowheader ? "\\e[39m" : "\e[39m", file);
Expand Down

0 comments on commit 6b23da1

Please sign in to comment.