11cb0ef41Sopenharmony_ci// MIT License
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a copy
61cb0ef41Sopenharmony_ci// of this software and associated documentation files (the "Software"), to deal
71cb0ef41Sopenharmony_ci// in the Software without restriction, including without limitation the rights
81cb0ef41Sopenharmony_ci// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
91cb0ef41Sopenharmony_ci// copies of the Software, and to permit persons to whom the Software is
101cb0ef41Sopenharmony_ci// furnished to do so, subject to the following conditions:
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included in
131cb0ef41Sopenharmony_ci// all copies or substantial portions of the Software.
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
161cb0ef41Sopenharmony_ci// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
171cb0ef41Sopenharmony_ci// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
181cb0ef41Sopenharmony_ci// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
191cb0ef41Sopenharmony_ci// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
201cb0ef41Sopenharmony_ci// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
211cb0ef41Sopenharmony_ci// SOFTWARE.
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci'use strict';
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciconst {
261cb0ef41Sopenharmony_ci  ArrayPrototypeSome,
271cb0ef41Sopenharmony_ci  RegExpPrototypeExec,
281cb0ef41Sopenharmony_ci  StringPrototypeSplit,
291cb0ef41Sopenharmony_ci  StringPrototypeToLowerCase,
301cb0ef41Sopenharmony_ci} = primordials;
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ciconst { validateInteger } = require('internal/validators');
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_cilet OSRelease;
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciconst COLORS_2 = 1;
371cb0ef41Sopenharmony_ciconst COLORS_16 = 4;
381cb0ef41Sopenharmony_ciconst COLORS_256 = 8;
391cb0ef41Sopenharmony_ciconst COLORS_16m = 24;
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci// Some entries were taken from `dircolors`
421cb0ef41Sopenharmony_ci// (https://linux.die.net/man/1/dircolors). The corresponding terminals might
431cb0ef41Sopenharmony_ci// support more than 16 colors, but this was not tested for.
441cb0ef41Sopenharmony_ci//
451cb0ef41Sopenharmony_ci// Copyright (C) 1996-2016 Free Software Foundation, Inc. Copying and
461cb0ef41Sopenharmony_ci// distribution of this file, with or without modification, are permitted
471cb0ef41Sopenharmony_ci// provided the copyright notice and this notice are preserved.
481cb0ef41Sopenharmony_ciconst TERM_ENVS = {
491cb0ef41Sopenharmony_ci  'eterm': COLORS_16,
501cb0ef41Sopenharmony_ci  'cons25': COLORS_16,
511cb0ef41Sopenharmony_ci  'console': COLORS_16,
521cb0ef41Sopenharmony_ci  'cygwin': COLORS_16,
531cb0ef41Sopenharmony_ci  'dtterm': COLORS_16,
541cb0ef41Sopenharmony_ci  'gnome': COLORS_16,
551cb0ef41Sopenharmony_ci  'hurd': COLORS_16,
561cb0ef41Sopenharmony_ci  'jfbterm': COLORS_16,
571cb0ef41Sopenharmony_ci  'konsole': COLORS_16,
581cb0ef41Sopenharmony_ci  'kterm': COLORS_16,
591cb0ef41Sopenharmony_ci  'mlterm': COLORS_16,
601cb0ef41Sopenharmony_ci  'mosh': COLORS_16m,
611cb0ef41Sopenharmony_ci  'putty': COLORS_16,
621cb0ef41Sopenharmony_ci  'st': COLORS_16,
631cb0ef41Sopenharmony_ci  // https://github.com/da-x/rxvt-unicode/tree/v9.22-with-24bit-color
641cb0ef41Sopenharmony_ci  'rxvt-unicode-24bit': COLORS_16m,
651cb0ef41Sopenharmony_ci  // https://gist.github.com/XVilka/8346728#gistcomment-2823421
661cb0ef41Sopenharmony_ci  'terminator': COLORS_16m,
671cb0ef41Sopenharmony_ci};
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ciconst TERM_ENVS_REG_EXP = [
701cb0ef41Sopenharmony_ci  /ansi/,
711cb0ef41Sopenharmony_ci  /color/,
721cb0ef41Sopenharmony_ci  /linux/,
731cb0ef41Sopenharmony_ci  /^con[0-9]*x[0-9]/,
741cb0ef41Sopenharmony_ci  /^rxvt/,
751cb0ef41Sopenharmony_ci  /^screen/,
761cb0ef41Sopenharmony_ci  /^xterm/,
771cb0ef41Sopenharmony_ci  /^vt100/,
781cb0ef41Sopenharmony_ci];
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_cilet warned = false;
811cb0ef41Sopenharmony_cifunction warnOnDeactivatedColors(env) {
821cb0ef41Sopenharmony_ci  if (warned)
831cb0ef41Sopenharmony_ci    return;
841cb0ef41Sopenharmony_ci  let name = '';
851cb0ef41Sopenharmony_ci  if (env.NODE_DISABLE_COLORS !== undefined)
861cb0ef41Sopenharmony_ci    name = 'NODE_DISABLE_COLORS';
871cb0ef41Sopenharmony_ci  if (env.NO_COLOR !== undefined) {
881cb0ef41Sopenharmony_ci    if (name !== '') {
891cb0ef41Sopenharmony_ci      name += "' and '";
901cb0ef41Sopenharmony_ci    }
911cb0ef41Sopenharmony_ci    name += 'NO_COLOR';
921cb0ef41Sopenharmony_ci  }
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci  if (name !== '') {
951cb0ef41Sopenharmony_ci    process.emitWarning(
961cb0ef41Sopenharmony_ci      `The '${name}' env is ignored due to the 'FORCE_COLOR' env being set.`,
971cb0ef41Sopenharmony_ci      'Warning',
981cb0ef41Sopenharmony_ci    );
991cb0ef41Sopenharmony_ci    warned = true;
1001cb0ef41Sopenharmony_ci  }
1011cb0ef41Sopenharmony_ci}
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci// The `getColorDepth` API got inspired by multiple sources such as
1041cb0ef41Sopenharmony_ci// https://github.com/chalk/supports-color,
1051cb0ef41Sopenharmony_ci// https://github.com/isaacs/color-support.
1061cb0ef41Sopenharmony_cifunction getColorDepth(env = process.env) {
1071cb0ef41Sopenharmony_ci  // Use level 0-3 to support the same levels as `chalk` does. This is done for
1081cb0ef41Sopenharmony_ci  // consistency throughout the ecosystem.
1091cb0ef41Sopenharmony_ci  if (env.FORCE_COLOR !== undefined) {
1101cb0ef41Sopenharmony_ci    switch (env.FORCE_COLOR) {
1111cb0ef41Sopenharmony_ci      case '':
1121cb0ef41Sopenharmony_ci      case '1':
1131cb0ef41Sopenharmony_ci      case 'true':
1141cb0ef41Sopenharmony_ci        warnOnDeactivatedColors(env);
1151cb0ef41Sopenharmony_ci        return COLORS_16;
1161cb0ef41Sopenharmony_ci      case '2':
1171cb0ef41Sopenharmony_ci        warnOnDeactivatedColors(env);
1181cb0ef41Sopenharmony_ci        return COLORS_256;
1191cb0ef41Sopenharmony_ci      case '3':
1201cb0ef41Sopenharmony_ci        warnOnDeactivatedColors(env);
1211cb0ef41Sopenharmony_ci        return COLORS_16m;
1221cb0ef41Sopenharmony_ci      default:
1231cb0ef41Sopenharmony_ci        return COLORS_2;
1241cb0ef41Sopenharmony_ci    }
1251cb0ef41Sopenharmony_ci  }
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ci  if (env.NODE_DISABLE_COLORS !== undefined ||
1281cb0ef41Sopenharmony_ci      // See https://no-color.org/
1291cb0ef41Sopenharmony_ci      env.NO_COLOR !== undefined ||
1301cb0ef41Sopenharmony_ci      // The "dumb" special terminal, as defined by terminfo, doesn't support
1311cb0ef41Sopenharmony_ci      // ANSI color control codes.
1321cb0ef41Sopenharmony_ci      // See https://invisible-island.net/ncurses/terminfo.ti.html#toc-_Specials
1331cb0ef41Sopenharmony_ci      env.TERM === 'dumb') {
1341cb0ef41Sopenharmony_ci    return COLORS_2;
1351cb0ef41Sopenharmony_ci  }
1361cb0ef41Sopenharmony_ci
1371cb0ef41Sopenharmony_ci  if (process.platform === 'win32') {
1381cb0ef41Sopenharmony_ci    // Lazy load for startup performance.
1391cb0ef41Sopenharmony_ci    if (OSRelease === undefined) {
1401cb0ef41Sopenharmony_ci      const { release } = require('os');
1411cb0ef41Sopenharmony_ci      OSRelease = StringPrototypeSplit(release(), '.');
1421cb0ef41Sopenharmony_ci    }
1431cb0ef41Sopenharmony_ci    // Windows 10 build 10586 is the first Windows release that supports 256
1441cb0ef41Sopenharmony_ci    // colors. Windows 10 build 14931 is the first release that supports
1451cb0ef41Sopenharmony_ci    // 16m/TrueColor.
1461cb0ef41Sopenharmony_ci    if (+OSRelease[0] >= 10) {
1471cb0ef41Sopenharmony_ci      const build = +OSRelease[2];
1481cb0ef41Sopenharmony_ci      if (build >= 14931)
1491cb0ef41Sopenharmony_ci        return COLORS_16m;
1501cb0ef41Sopenharmony_ci      if (build >= 10586)
1511cb0ef41Sopenharmony_ci        return COLORS_256;
1521cb0ef41Sopenharmony_ci    }
1531cb0ef41Sopenharmony_ci
1541cb0ef41Sopenharmony_ci    return COLORS_16;
1551cb0ef41Sopenharmony_ci  }
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_ci  if (env.TMUX) {
1581cb0ef41Sopenharmony_ci    return COLORS_256;
1591cb0ef41Sopenharmony_ci  }
1601cb0ef41Sopenharmony_ci
1611cb0ef41Sopenharmony_ci  if (env.CI) {
1621cb0ef41Sopenharmony_ci    if ([
1631cb0ef41Sopenharmony_ci      'APPVEYOR',
1641cb0ef41Sopenharmony_ci      'BUILDKITE',
1651cb0ef41Sopenharmony_ci      'CIRCLECI',
1661cb0ef41Sopenharmony_ci      'DRONE',
1671cb0ef41Sopenharmony_ci      'GITHUB_ACTIONS',
1681cb0ef41Sopenharmony_ci      'GITLAB_CI',
1691cb0ef41Sopenharmony_ci      'TRAVIS',
1701cb0ef41Sopenharmony_ci    ].some((sign) => sign in env) || env.CI_NAME === 'codeship') {
1711cb0ef41Sopenharmony_ci      return COLORS_256;
1721cb0ef41Sopenharmony_ci    }
1731cb0ef41Sopenharmony_ci    return COLORS_2;
1741cb0ef41Sopenharmony_ci  }
1751cb0ef41Sopenharmony_ci
1761cb0ef41Sopenharmony_ci  if ('TEAMCITY_VERSION' in env) {
1771cb0ef41Sopenharmony_ci    return RegExpPrototypeExec(/^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/, env.TEAMCITY_VERSION) !== null ?
1781cb0ef41Sopenharmony_ci      COLORS_16 : COLORS_2;
1791cb0ef41Sopenharmony_ci  }
1801cb0ef41Sopenharmony_ci
1811cb0ef41Sopenharmony_ci  switch (env.TERM_PROGRAM) {
1821cb0ef41Sopenharmony_ci    case 'iTerm.app':
1831cb0ef41Sopenharmony_ci      if (!env.TERM_PROGRAM_VERSION ||
1841cb0ef41Sopenharmony_ci        RegExpPrototypeExec(/^[0-2]\./, env.TERM_PROGRAM_VERSION) !== null
1851cb0ef41Sopenharmony_ci      ) {
1861cb0ef41Sopenharmony_ci        return COLORS_256;
1871cb0ef41Sopenharmony_ci      }
1881cb0ef41Sopenharmony_ci      return COLORS_16m;
1891cb0ef41Sopenharmony_ci    case 'HyperTerm':
1901cb0ef41Sopenharmony_ci    case 'MacTerm':
1911cb0ef41Sopenharmony_ci      return COLORS_16m;
1921cb0ef41Sopenharmony_ci    case 'Apple_Terminal':
1931cb0ef41Sopenharmony_ci      return COLORS_256;
1941cb0ef41Sopenharmony_ci  }
1951cb0ef41Sopenharmony_ci
1961cb0ef41Sopenharmony_ci  if (env.COLORTERM === 'truecolor' || env.COLORTERM === '24bit') {
1971cb0ef41Sopenharmony_ci    return COLORS_16m;
1981cb0ef41Sopenharmony_ci  }
1991cb0ef41Sopenharmony_ci
2001cb0ef41Sopenharmony_ci  if (env.TERM) {
2011cb0ef41Sopenharmony_ci    if (RegExpPrototypeExec(/^xterm-256/, env.TERM) !== null) {
2021cb0ef41Sopenharmony_ci      return COLORS_256;
2031cb0ef41Sopenharmony_ci    }
2041cb0ef41Sopenharmony_ci
2051cb0ef41Sopenharmony_ci    const termEnv = StringPrototypeToLowerCase(env.TERM);
2061cb0ef41Sopenharmony_ci
2071cb0ef41Sopenharmony_ci    if (TERM_ENVS[termEnv]) {
2081cb0ef41Sopenharmony_ci      return TERM_ENVS[termEnv];
2091cb0ef41Sopenharmony_ci    }
2101cb0ef41Sopenharmony_ci    if (ArrayPrototypeSome(TERM_ENVS_REG_EXP,
2111cb0ef41Sopenharmony_ci                           (term) => RegExpPrototypeExec(term, termEnv) !== null)) {
2121cb0ef41Sopenharmony_ci      return COLORS_16;
2131cb0ef41Sopenharmony_ci    }
2141cb0ef41Sopenharmony_ci  }
2151cb0ef41Sopenharmony_ci  // Move 16 color COLORTERM below 16m and 256
2161cb0ef41Sopenharmony_ci  if (env.COLORTERM) {
2171cb0ef41Sopenharmony_ci    return COLORS_16;
2181cb0ef41Sopenharmony_ci  }
2191cb0ef41Sopenharmony_ci  return COLORS_2;
2201cb0ef41Sopenharmony_ci}
2211cb0ef41Sopenharmony_ci
2221cb0ef41Sopenharmony_cifunction hasColors(count, env) {
2231cb0ef41Sopenharmony_ci  if (env === undefined &&
2241cb0ef41Sopenharmony_ci      (count === undefined || (typeof count === 'object' && count !== null))) {
2251cb0ef41Sopenharmony_ci    env = count;
2261cb0ef41Sopenharmony_ci    count = 16;
2271cb0ef41Sopenharmony_ci  } else {
2281cb0ef41Sopenharmony_ci    validateInteger(count, 'count', 2);
2291cb0ef41Sopenharmony_ci  }
2301cb0ef41Sopenharmony_ci
2311cb0ef41Sopenharmony_ci  return count <= 2 ** getColorDepth(env);
2321cb0ef41Sopenharmony_ci}
2331cb0ef41Sopenharmony_ci
2341cb0ef41Sopenharmony_cimodule.exports = {
2351cb0ef41Sopenharmony_ci  getColorDepth,
2361cb0ef41Sopenharmony_ci  hasColors,
2371cb0ef41Sopenharmony_ci};
238