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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "crucible/core/environment.hpp"

#include "crucible/core/console.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
	{
		std::mutex mtx;
	};

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

	auto Environment::initialize() -> void
	{
		auto locker = lock();
		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 && crucible_colors_data[0] != '\0') {
			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;
			}

			console()
				.format_err_line("Unrecognized CRUCIBLE_COLORS value `{}`", crucible_colors)
				.format_err_line("")
				.format_err_line("To disable colors, do ONE of the following:")
				.format_err_line("  * Set the NO_COLOR environment variable to any nonempty value")
				.format_err_line("  * Set the CRUCIBLE_COLORS environment variable to 0, no, off, false, or never")
				.format_err_line("")
				.format_err_line("To enable colors, do ONE of the following:")
				.format_err_line("  * Set the CLICOLOR_FORCE environment variable to any nonempty value")
				.format_err_line("  * Set the CRUCIBLE_COLORS environment variable to 1, yes, on, true, or always")
				.format_err_line("")
				.format_err_line("To autodetect whether or not to use colors:")
				.format_err_line("  * Set the CRUCIBLE_COLORS environment variable to auto, automatic, or detect")
				.format_err_line("")
				.format_err_line("Colors are DISABLED due to this error")
				.format_err_line("");

			m_colors = false;
			return;
		}

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

	auto Environment::lock() -> std::unique_lock<std::mutex>
	{
		return std::unique_lock<std::mutex> { m_data->mtx };
	}

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