Echo Writes Code

environment.cpp

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
102
103
#include "crucible/core/environment.hpp"

#include "crucible/core/iterators.hpp"
#include "crucible/core/memory.hpp"
#include "crucible/core/string.hpp"

#ifdef CRUCIBLE_PLATFORM_IS_POSIX_LIKE
#include <unistd.h>
#endif // CRUCIBLE_PLATFORM_IS_POSIX_LIKE

#include <cstdio>
#include <cstdlib>
#include <mutex>

namespace crucible::core
{
	namespace
	{
		[[nodiscard]] auto stdout_supports_color() -> bool
		{

#ifdef CRUCIBLE_PLATFORM_IS_POSIX_LIKE
			return ::isatty(::fileno(stdout));
#endif // CRUCIBLE_PLATFORM_IS_POSIX_LIKE

#ifdef CRUCIBLE_PLATFORM_IS_WINDOWS_LIKE
			return false;
#endif // CRUCIBLE_PLATFORM_IS_WINDOWS_LIKE

		}
	}

	struct Environment_Data
	{};

	Environment::Environment(std::unique_ptr<Environment_Data> data) :
		m_data { std::move(data) }
	{
	}

	auto Environment::initialize() -> void
	{
		initialize_colors();
	}

	auto Environment::initialize_colors() -> void
	{
		// See: https://no-color.org
		char const *no_color { std::getenv("NO_COLOR") };
		if (no_color != nullptr && no_color[0] != '\0') {
			m_colors = false;
			return;
		}

		// See: https://bixense.com/clicolors/
		char const *clicolor_force { std::getenv("CLICOLOR_FORCE") };
		if (clicolor_force != nullptr && clicolor_force[0] != '\0') {
			m_colors = true;
			return;
		}

		// Our own colorization mechanism
		char const *crucible_colors_data { std::getenv("CRUCIBLE_COLORS") };
		if (crucible_colors_data != nullptr) {
			std::size_t const crucible_colors_size { std::strlen(crucible_colors_data) };
			String_View const crucible_colors { crucible_colors_data, crucible_colors_size };

			String_View const off_values[] { "0", "never", "NEVER", "false", "FALSE", "no", "NO", "off", "OFF" };
			if (iterate(off_values).contains(crucible_colors)) {
				m_colors = false;
				return;
			}

			String_View const on_values[] { "1", "always", "ALWAYS", "true", "TRUE", "yes", "YES", "on", "ON" };
			if (iterate(on_values).contains(crucible_colors)) {
				m_colors = true;
				return;
			}

			String_View const auto_values[] { "auto", "AUTO", "automatic", "AUTOMATIC", "detect", "DETECT" };
			if (iterate(auto_values).contains(crucible_colors)) {
				m_colors = stdout_supports_color();
				return;
			}

			// Ideally we would print an error here, but it causes infinite recursion due to
			// `print_error_line()` trying to initialize us again (since we're technically not done the
			// constructor yet). In this case it's safer to assume no colors, since we can't know what
			// was actually desired.
			m_colors = false;
			return;
		}

		// If there's no color configuration, default to auto
		m_colors = stdout_supports_color();
	}

	auto environment() -> Environment &
	{
		static Environment instance { std::make_unique<Environment_Data>() };
		return instance;
	}
}