summaryrefslogtreecommitdiff
path: root/vendor/bandit/bandit/reporters/colorizer.h
blob: 217bdddf79a01c43f93d742954310ba0b3d3d100 (plain)
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
#ifndef BANDIT_REPORTERS_COLORIZER_H
#define BANDIT_REPORTERS_COLORIZER_H

#ifdef _WIN32
  #ifndef MINGW32
    #define NOMINMAX
  #endif

  #define WIN32_LEAN_AND_MEAN
  #include <windows.h>
#endif

namespace bandit { namespace detail {

#ifdef _WIN32
  struct colorizer
  {
    colorizer(bool colors_enabled = true)
      : colors_enabled_(colors_enabled),
		stdout_handle_(GetStdHandle(STD_OUTPUT_HANDLE))
    {
		original_color_ = get_console_color();
	}

    const char* green() const
    {
      if(colors_enabled_)
	  {
		  set_console_color(FOREGROUND_GREEN);
	  }
	  return "";
    }

    const char* red() const
    {
      if(colors_enabled_)
	  {
		  set_console_color(FOREGROUND_RED);
	  }
	  return "";
    }

    const char* reset() const
    {
      if(colors_enabled_)
	  {
		  set_console_color(original_color_);
	  }
	  return "";
    }

  private:
	  WORD get_console_color() const
	  {
		  CONSOLE_SCREEN_BUFFER_INFO info = {0};
		  GetConsoleScreenBufferInfo(stdout_handle_, &info);
		  return info.wAttributes;
	  }

	  void set_console_color(WORD color) const
	  {
		  SetConsoleTextAttribute(stdout_handle_, color);
	  }

    private:
    bool colors_enabled_;
	HANDLE stdout_handle_;
	WORD original_color_;
  };

#else
  struct colorizer
  {
    colorizer(bool colors_enabled = true)
      : colors_enabled_(colors_enabled)
    {}

    const char* green() const
    {
      return colors_enabled_ ? "\033[1;32m" : "";
    }

    const char* red() const
    {
      return colors_enabled_ ? "\033[1;31m" : "";
    }

    const char* reset() const
    {
      return colors_enabled_ ? "\033[0m" : "";
    }

    private:
    bool colors_enabled_;
  };
#endif
}}

#endif