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 <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
  • Loading branch information
BridgeAR committed Apr 4, 2018
1 parent 1b71522 commit ceaeee0
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 ceaeee0

Please sign in to comment.