-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterm.hpp
More file actions
80 lines (67 loc) · 2.62 KB
/
term.hpp
File metadata and controls
80 lines (67 loc) · 2.62 KB
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
#pragma once
#include <cstdlib>
#include <string>
#include <string_view>
#include <unistd.h>
namespace cfbox::term {
namespace detail {
struct ColorState {
bool auto_detected = false;
bool auto_value = false;
bool override_set = false;
bool override_value = false;
};
inline auto color_state() noexcept -> ColorState& {
static ColorState state;
return state;
}
} // namespace detail
[[nodiscard]] inline auto color_enabled() noexcept -> bool {
auto& s = detail::color_state();
if (s.override_set) return s.override_value;
if (!s.auto_detected) {
s.auto_detected = true;
s.auto_value = (std::getenv("NO_COLOR") == nullptr) && (isatty(STDOUT_FILENO) != 0);
}
return s.auto_value;
}
inline void set_color_enabled(bool enabled) noexcept {
auto& s = detail::color_state();
s.override_set = true;
s.override_value = enabled;
}
inline void reset_color_enabled() noexcept {
auto& s = detail::color_state();
s.override_set = false;
s.auto_detected = false;
}
namespace detail {
[[nodiscard]] inline auto sv(const char* code) noexcept -> std::string_view {
return color_enabled() ? std::string_view{code} : std::string_view{};
}
} // namespace detail
// Foreground colors
[[nodiscard]] inline auto red() noexcept -> std::string_view { return detail::sv("\033[31m"); }
[[nodiscard]] inline auto green() noexcept -> std::string_view { return detail::sv("\033[32m"); }
[[nodiscard]] inline auto yellow() noexcept -> std::string_view { return detail::sv("\033[33m"); }
[[nodiscard]] inline auto blue() noexcept -> std::string_view { return detail::sv("\033[34m"); }
[[nodiscard]] inline auto magenta() noexcept -> std::string_view { return detail::sv("\033[35m"); }
[[nodiscard]] inline auto cyan() noexcept -> std::string_view { return detail::sv("\033[36m"); }
// Attributes
[[nodiscard]] inline auto bold() noexcept -> std::string_view { return detail::sv("\033[1m"); }
[[nodiscard]] inline auto dim() noexcept -> std::string_view { return detail::sv("\033[2m"); }
[[nodiscard]] inline auto underline() noexcept -> std::string_view { return detail::sv("\033[4m"); }
// Reset
[[nodiscard]] inline auto reset() noexcept -> std::string_view { return detail::sv("\033[0m"); }
// Utility: wrap text with a color and reset
inline auto colored(std::string_view text, std::string_view color_code) -> std::string {
if (!color_enabled()) return std::string{text};
auto r = reset();
std::string result;
result.reserve(color_code.size() + text.size() + r.size());
result.append(color_code);
result.append(text);
result.append(r);
return result;
}
} // namespace cfbox::term