From 71ee5bf95d653ffddccd44fc62a42f42ac97638a Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Sun, 1 Apr 2018 11:37:13 +0200 Subject: [PATCH] tty: add color support for more terminals This adds support for a couple more terminals. PR-URL: https://github.com/nodejs/node/pull/19730 Reviewed-By: Anna Henningsen Reviewed-By: Richard Lau Reviewed-By: Matteo Collina Reviewed-By: James M Snell Reviewed-By: Trivikram Kamat --- lib/internal/tty.js | 50 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/lib/internal/tty.js b/lib/internal/tty.js index a581ac312f5327..c4edab24fb87ab 100644 --- a/lib/internal/tty.js +++ b/lib/internal/tty.js @@ -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)