Skip to content

Commit d1a31c0

Browse files
committed
Toml: enable colorization
1 parent 9c0318b commit d1a31c0

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/Rustify/Result.hpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#pragma once
22

3-
#include <concepts>
3+
#include "../TermColor.hpp"
4+
45
#include <exception>
56
#include <fmt/core.h>
67
#include <memory>
78
#include <mitama/anyhow/anyhow.hpp>
89
#include <mitama/result/result.hpp>
910
#include <string>
11+
#include <string_view>
1012
#include <type_traits>
1113
#include <utility>
1214

@@ -57,16 +59,29 @@ namespace toml {
5759
template <typename T, typename... U>
5860
inline Result<T>
5961
try_find(const toml::value& v, const U&... u) noexcept {
60-
using namespace std::string_view_literals; // NOLINT
62+
using std::string_view_literals::operator""sv;
63+
64+
if (cabin::shouldColorStderr()) {
65+
color::enable();
66+
} else {
67+
color::disable();
68+
}
6169

6270
try {
6371
return Ok(toml::find<T>(v, u...));
6472
} catch (const std::exception& e) {
6573
std::string what = e.what();
66-
// TODO: make the same fix on upstream
67-
if (what.starts_with("[error] ")) {
68-
what = what.substr("[error] "sv.size());
74+
75+
static constexpr std::size_t errorPrefixSize = "[error] "sv.size();
76+
static constexpr std::size_t colorErrorPrefixSize =
77+
"\033[31m\033[01m[error]\033[00m "sv.size();
78+
79+
if (cabin::shouldColorStderr()) {
80+
what = what.substr(colorErrorPrefixSize);
81+
} else {
82+
what = what.substr(errorPrefixSize);
6983
}
84+
7085
if (what.back() == '\n') {
7186
what.pop_back(); // remove the last '\n' since logger::error adds one.
7287
}

0 commit comments

Comments
 (0)