Skip to content

Commit

Permalink
tty: add color support for more terminals
Browse files Browse the repository at this point in the history
This adds support for a couple more terminals.

PR-URL: #19730
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
  • Loading branch information
BridgeAR authored and targos committed Apr 6, 2018
1 parent 2b5a89b commit 71ee5bf
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions lib/internal/tty.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,41 @@ const COLORS_16 = 4;
const COLORS_256 = 8;
const COLORS_16m = 24;

// Some entries were taken from `dircolors`
// (https://linux.die.net/man/1/dircolors). The corresponding terminals might
// support more than 16 colors, but this was not tested for.
//
// Copyright (C) 1996-2016 Free Software Foundation, Inc. Copying and
// distribution of this file, with or without modification, are permitted
// provided the copyright notice and this notice are preserved.
const TERM_ENVS = [
'Eterm',
'cons25',
'console',
'cygwin',
'dtterm',
'gnome',
'hurd',
'jfbterm',
'konsole',
'kterm',
'mlterm',
'putty',
'st',
'terminator'
];

const TERM_ENVS_REG_EXP = [
/ansi/,
/color/,
/linux/,
/^con[0-9]*x[0-9]/,
/^rxvt/,
/^screen/,
/^xterm/,
/^vt100/
];

// The `getColorDepth` API got inspired by multiple sources such as
// https://github.com/chalk/supports-color,
// https://github.com/isaacs/color-support.
Expand Down Expand Up @@ -89,8 +124,19 @@ function getColorDepth(env = process.env) {
if (env.TERM) {
if (/^xterm-256/.test(env.TERM))
return COLORS_256;
if (/^screen|^xterm|^vt100|color|ansi|cygwin|linux/i.test(env.TERM))
return COLORS_16;

const termEnv = env.TERM.toLowerCase();

for (const term of TERM_ENVS) {
if (termEnv === term) {
return COLORS_16;
}
}
for (const term of TERM_ENVS_REG_EXP) {
if (term.test(termEnv)) {
return COLORS_16;
}
}
}

if (env.COLORTERM)
Expand Down

0 comments on commit 71ee5bf

Please sign in to comment.