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

#include "crucible/core/console.hpp"
#include "crucible/core/iterator.hpp"
#include "crucible/core/memory.hpp"
#include "crucible/core/string.hpp"

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

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

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

#if CRUCIBLE_PLATFORM_IS_POSIX_LIKE
			return ::isatty(::fileno(stdout));
#else
			return false; // Play it safe.
#endif // CRUCIBLE_PLATFORM_IS_POSIX_LIKE

		}
	}

	struct environment_data
	{
		std::mutex mtx;
	};

	environment_handle::environment_handle(std::unique_ptr<environment_data> data) :
		m_data { std::move(data) }
	{}

	auto environment_handle::initialize() -> void
	{
		auto locker = lock();
		initialize_colors();
	}

	auto environment_handle::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_handle::lock() -> std::unique_lock<std::mutex>
	{
		return std::unique_lock<std::mutex> { m_data->mtx };
	}

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