Skip to content

Commit

Permalink
If we can identify the terminal as iTerm2 or as tmux, we can be sure
Browse files Browse the repository at this point in the history
they support 256 and RGB colours, so set those flags too.
  • Loading branch information
nicm committed Jan 28, 2020
1 parent 84995ae commit a6129e9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 32 deletions.
1 change: 1 addition & 0 deletions tmux.h
Expand Up @@ -1171,6 +1171,7 @@ struct tty_term {
#define TERM_NOXENL 0x2
#define TERM_DECSLRM 0x4
#define TERM_DECFRA 0x8
#define TERM_RGBCOLOURS 0x10
int flags;

LIST_ENTRY(tty_term) entry;
Expand Down
4 changes: 3 additions & 1 deletion tty-keys.c
Expand Up @@ -1117,7 +1117,9 @@ tty_keys_device_status_report(struct tty *tty, const char *buf, size_t len,

/* Set terminal flags. */
if (strncmp(tmp, "ITERM2 ", 7) == 0)
flags |= TERM_DECSLRM;
flags |= (TERM_DECSLRM|TERM_256COLOURS|TERM_RGBCOLOURS);
if (strncmp(tmp, "TMUX ", 5) == 0)
flags |= (TERM_256COLOURS|TERM_RGBCOLOURS);
log_debug("%s: received DSR %.*s", c->name, (int)*size, buf);

tty_set_flags(tty, flags);
Expand Down
28 changes: 9 additions & 19 deletions tty-term.c
Expand Up @@ -522,11 +522,16 @@ tty_term_find(char *name, int fd, char **cause)
goto error;
}

/* Figure out if we have 256 colours (or more). */
if (tty_term_number(term, TTYC_COLORS) >= 256 ||
tty_term_has(term, TTYC_RGB))
/* Set flag if terminal has 256 colours. */
if (tty_term_number(term, TTYC_COLORS) >= 256)
term->flags |= TERM_256COLOURS;

/* Set flag if terminal has RGB colours. */
if ((tty_term_flag(term, TTYC_TC) || tty_term_has(term, TTYC_RGB)) ||
(tty_term_has(term, TTYC_SETRGBF) &&
tty_term_has(term, TTYC_SETRGBB)))
term->flags |= TERM_RGBCOLOURS;

/*
* Terminals without xenl (eat newline glitch) wrap at at $COLUMNS - 1
* rather than $COLUMNS (the cursor can never be beyond $COLUMNS - 1).
Expand Down Expand Up @@ -561,22 +566,7 @@ tty_term_find(char *name, int fd, char **cause)
code->type = TTYCODE_STRING;
}

/*
* On terminals with RGB colour (Tc or RGB), fill in setrgbf and
* setrgbb if they are missing.
*/
if ((tty_term_flag(term, TTYC_TC) || tty_term_flag(term, TTYC_RGB)) &&
!tty_term_has(term, TTYC_SETRGBF) &&
!tty_term_has(term, TTYC_SETRGBB)) {
code = &term->codes[TTYC_SETRGBF];
code->value.string = xstrdup("\033[38;2;%p1%d;%p2%d;%p3%dm");
code->type = TTYCODE_STRING;
code = &term->codes[TTYC_SETRGBB];
code->value.string = xstrdup("\033[48;2;%p1%d;%p2%d;%p3%dm");
code->type = TTYCODE_STRING;
}

/* Log it. */
/* Log the capabilities. */
for (i = 0; i < tty_term_ncodes(); i++)
log_debug("%s%s", name, tty_term_describe(term, i));

Expand Down
27 changes: 15 additions & 12 deletions tty.c
Expand Up @@ -2386,11 +2386,10 @@ tty_check_fg(struct tty *tty, struct window_pane *wp, struct grid_cell *gc)
/* Is this a 24-bit colour? */
if (gc->fg & COLOUR_FLAG_RGB) {
/* Not a 24-bit terminal? Translate to 256-colour palette. */
if (!tty_term_has(tty->term, TTYC_SETRGBF)) {
colour_split_rgb(gc->fg, &r, &g, &b);
gc->fg = colour_find_rgb(r, g, b);
} else
if ((tty->term->flags|tty->term_flags) & TERM_RGBCOLOURS)
return;
colour_split_rgb(gc->fg, &r, &g, &b);
gc->fg = colour_find_rgb(r, g, b);
}

/* How many colours does this terminal have? */
Expand Down Expand Up @@ -2436,11 +2435,10 @@ tty_check_bg(struct tty *tty, struct window_pane *wp, struct grid_cell *gc)
/* Is this a 24-bit colour? */
if (gc->bg & COLOUR_FLAG_RGB) {
/* Not a 24-bit terminal? Translate to 256-colour palette. */
if (!tty_term_has(tty->term, TTYC_SETRGBB)) {
colour_split_rgb(gc->bg, &r, &g, &b);
gc->bg = colour_find_rgb(r, g, b);
} else
if ((tty->term->flags|tty->term_flags) & TERM_RGBCOLOURS)
return;
colour_split_rgb(gc->bg, &r, &g, &b);
gc->bg = colour_find_rgb(r, g, b);
}

/* How many colours does this terminal have? */
Expand Down Expand Up @@ -2617,15 +2615,14 @@ tty_try_colour(struct tty *tty, int colour, const char *type)
}

if (colour & COLOUR_FLAG_RGB) {
colour_split_rgb(colour & 0xffffff, &r, &g, &b);
if (*type == '3') {
if (!tty_term_has(tty->term, TTYC_SETRGBF))
return (-1);
colour_split_rgb(colour & 0xffffff, &r, &g, &b);
goto fallback_rgb;
tty_putcode3(tty, TTYC_SETRGBF, r, g, b);
} else {
if (!tty_term_has(tty->term, TTYC_SETRGBB))
return (-1);
colour_split_rgb(colour & 0xffffff, &r, &g, &b);
goto fallback_rgb;
tty_putcode3(tty, TTYC_SETRGBB, r, g, b);
}
return (0);
Expand All @@ -2638,6 +2635,12 @@ tty_try_colour(struct tty *tty, int colour, const char *type)
log_debug("%s: 256 colour fallback: %s", tty->client->name, s);
tty_puts(tty, s);
return (0);

fallback_rgb:
xsnprintf(s, sizeof s, "\033[%s;2;%d;%d;%dm", type, r, g, b);
log_debug("%s: RGB colour fallback: %s", tty->client->name, s);
tty_puts(tty, s);
return (0);
}

static void
Expand Down

0 comments on commit a6129e9

Please sign in to comment.