File: ColorUtils.cpp

package info (click to toggle)
vecgeom 1.2.8%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 24,016 kB
  • sloc: cpp: 88,803; ansic: 6,888; python: 1,035; sh: 582; sql: 538; makefile: 23
file content (101 lines) | stat: -rw-r--r-- 2,555 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//----------------------------------*-C++-*----------------------------------//
// Copyright 2020-2023 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file ColorUtils.cpp
//---------------------------------------------------------------------------//
#include "VecGeom/management/ColorUtils.h"

#include <cstdio>
#include <cstdlib>
#include <string>
#include <unistd.h>

#include "VecGeom/management/Environment.h"

namespace vecgeom {

//---------------------------------------------------------------------------//
/*!
 * Whether colors are enabled (currently read-only).
 */
bool use_color()
{
  const static bool result = [] {
    FILE *stream          = stderr;
    std::string color_str = vecgeom::getenv("VECGEOM_COLOR");
    if (color_str.empty()) {
      // Don't use "getenv" to check gtest variable, to avoid
      // adding it to the list of exposed variables
      if (const char *color_cstr = std::getenv("GTEST_COLOR")) {
        color_str = std::string(color_cstr);
      }
    }
    if (color_str == "0") {
      // Color is explicitly disabled
      return false;
    }
    if (!color_str.empty()) {
      // Color is explicitly enabled
      return true;
    }
    if (!isatty(fileno(stream))) {
      // This stream is not a user-facing terminal
      return false;
    }
    if (const char *term_str = std::getenv("TERM")) {
      if (std::string{term_str}.find("xterm") != std::string::npos) {
        // 'xterm' is in the TERM type, so assume it uses colors
        return true;
      }
    }

    return false;
  }();

  return result;
}

//---------------------------------------------------------------------------//
/*!
 * Get an ANSI color codes if colors are enabled.
 *
 *  - [b]lue
 *  - [g]reen
 *  - [y]ellow
 *  - [r]ed
 *  - [x] gray
 *  - [R]ed bold
 *  - [W]hite bold
 *  - [ ] default (reset color)
 */
char const *color_code(char abbrev)
{
  if (!use_color()) return "";

  switch (abbrev) {
  case 'g':
    return "\033[32m";
  case 'b':
    return "\033[34m";
  case 'r':
    return "\033[31m";
  case 'x':
    return "\033[37;2m";
  case 'y':
    return "\033[33m";
  case 'R':
    return "\033[31;1m";
  case 'W':
    return "\033[37;1m";
  case ' ':
    return "\033[0m";
  }

  // Unknown color code: ignore
  return "";
}

//---------------------------------------------------------------------------//
} // namespace vecgeom