Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cliMain: colorize anyhow error #1093

Merged
merged 2 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/Cabin.cc
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#include "Cabin.hpp"

#include "Algos.hpp"
#include "Cli.hpp"
#include "Cmd.hpp"
#include "Rustify/Result.hpp"
#include "TermColor.hpp"

#include <cstdlib>
#include <exception>
#include <fmt/core.h>
#include <span>
#include <string>
#include <string_view>
#include <vector>

Expand Down Expand Up @@ -62,8 +65,8 @@ getCli() noexcept {
return cli;
}

Result<void>
cliMain(const std::span<char* const> args) noexcept {
static Result<void>
parseArgs(const std::span<char* const> args) noexcept {
// Parse arguments (options should appear before the subcommand, as the help
// message shows intuitively)
// cabin --verbose run --release help --color always --verbose
Expand Down Expand Up @@ -107,4 +110,21 @@ cliMain(const std::span<char* const> args) noexcept {
return getCli().printHelp({});
}

static std::string
colorizeAnyhowError(std::string s) {
// `Caused by:` leaves a trailing newline
if (s.find("Caused by:") != std::string::npos) {
replaceAll(s, "Caused by:", Yellow("Caused by:").toErrStr());
replaceAll(s, "\n", "");
}
return s;
}

Result<void, std::string>
cliMain(const std::span<char* const> args) noexcept {
return parseArgs(args).map_err([](const auto& e) {
return colorizeAnyhowError(e->what());
});
}

} // namespace cabin
3 changes: 2 additions & 1 deletion src/Cabin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
#include "Rustify/Result.hpp"

#include <span>
#include <string>

namespace cabin {

Result<void> cliMain(std::span<char* const> args) noexcept;
Result<void, std::string> cliMain(std::span<char* const> args) noexcept;

} // namespace cabin
6 changes: 4 additions & 2 deletions src/Rustify/Result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ namespace thiserror = mitama::thiserror;
} \
} while (false)

template <typename T, typename E = void>
struct UseAnyhow {};

template <typename T, typename E = UseAnyhow>
using Result = std::conditional_t<
std::is_void_v<E>, anyhow::result<T>, mitama::result<T, E>>;
std::is_same_v<E, UseAnyhow>, anyhow::result<T>, mitama::result<T, E>>;

template <typename... Args>
inline auto
Expand Down
2 changes: 1 addition & 1 deletion src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
int
main(int argc, char* argv[]) {
return cabin::cliMain(std::span<char* const>(argv + 1, argv + argc))
.map_err([](const auto& e) { cabin::logger::error("{}", e->what()); })
.map_err([](const auto& e) { cabin::logger::error("{}", e); })
.is_err();
}
Loading