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

tty: improve tty color detection #19730

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
tty: add color support for more terminals
This adds support for a couple more terminals.
  • Loading branch information
BridgeAR committed Apr 1, 2018
commit 1cff59190911c0ef1de417905198142ef10100cf
50 changes: 48 additions & 2 deletions lib/internal/tty.js
Original file line number Diff line number Diff line change
@@ -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.
@@ -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)