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

refactor: replace some ostream use with fmt #1131

Merged
merged 7 commits into from
Jan 30, 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
4 changes: 4 additions & 0 deletions src/Cabin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ getCli() noexcept {

static std::string
colorizeAnyhowError(std::string s) {
if (!shouldColorStderr()) {
return s;
}

if (s.find("Caused by:") != std::string::npos) {
replaceAll(s, "Caused by:", Yellow("Caused by:").toErrStr());
// `Caused by:` leaves a trailing newline, FIXME: upstream this
Expand Down
18 changes: 9 additions & 9 deletions src/Cli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <fmt/core.h>
#include <functional>
#include <iostream>
#include <span>
#include <string>
#include <string_view>
Expand Down Expand Up @@ -195,16 +195,16 @@ Subcmd::setGlobalOpts(const Opts& globalOpts) noexcept {
return *this;
}
std::string
Subcmd::formatUsage(std::ostream& os) const noexcept {
std::string str = Bold(Green("Usage: ")).toStr(os);
str += Bold(Cyan(cmdName)).toStr(os);
Subcmd::formatUsage(FILE* file) const noexcept {
std::string str = Bold(Green("Usage: ")).toStr(file);
str += Bold(Cyan(cmdName)).toStr(file);
str += ' ';
str += Bold(Cyan(name)).toStr(os);
str += Bold(Cyan(name)).toStr(file);
str += ' ';
str += Cyan("[OPTIONS]").toStr(os);
str += Cyan("[OPTIONS]").toStr(file);
if (!arg.name.empty()) {
str += ' ';
str += Cyan(arg.getLeft()).toStr(os);
str += Cyan(arg.getLeft()).toStr(file);
}
return str;
}
Expand All @@ -229,7 +229,7 @@ Subcmd::noSuchArg(std::string_view arg) const {
"{}"
"{}\n\n"
"For more information, try '{}'",
Bold(Yellow(arg)).toErrStr(), suggestion, formatUsage(std::cerr),
Bold(Yellow(arg)).toErrStr(), suggestion, formatUsage(stderr),
Bold(Cyan("--help")).toErrStr()
);
}
Expand Down Expand Up @@ -273,7 +273,7 @@ Subcmd::formatHelp() const noexcept {

std::string str = std::string(desc);
str += "\n\n";
str += formatUsage(std::cout);
str += formatUsage(stdout);
str += "\n\n";
str += formatHeader("Options:");
if (globalOpts.has_value()) {
Expand Down
4 changes: 2 additions & 2 deletions src/Cli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

#include "Rustify/Result.hpp"

#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <iterator>
#include <optional>
#include <span>
Expand Down Expand Up @@ -206,7 +206,7 @@ class Subcmd : public CliBase<Subcmd>, public ShortAndHidden<Subcmd> {
}

Subcmd& setGlobalOpts(const Opts& globalOpts) noexcept;
std::string formatUsage(std::ostream& os) const noexcept;
std::string formatUsage(FILE* file) const noexcept;
std::string formatHelp() const noexcept;
std::string format(std::size_t maxOffset) const noexcept;

Expand Down
33 changes: 17 additions & 16 deletions src/Cmd/Version.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "../Rustify/Result.hpp"

#include <cstdlib>
#include <iostream>
#include <fmt/format.h>
#include <string_view>

#ifndef CABIN_CABIN_PKG_VERSION
Expand Down Expand Up @@ -50,13 +50,13 @@ const Subcmd VERSION_CMD = //
static consteval std::string_view
commitInfo() noexcept {
if (sizeof(COMMIT_SHORT_HASH) <= 1 && sizeof(COMMIT_DATE) <= 1) {
return "\n";
return "";
} else if (sizeof(COMMIT_SHORT_HASH) <= 1) {
return " (" COMMIT_DATE ")\n";
return " (" COMMIT_DATE ")";
} else if (sizeof(COMMIT_DATE) <= 1) {
return " (" COMMIT_SHORT_HASH ")\n";
return " (" COMMIT_SHORT_HASH ")";
} else {
return " (" COMMIT_SHORT_HASH " " COMMIT_DATE ")\n";
return " (" COMMIT_SHORT_HASH " " COMMIT_DATE ")";
}
}

Expand Down Expand Up @@ -147,18 +147,19 @@ versionMain(const CliArgsView args) noexcept {
return VERSION_CMD.noSuchArg(arg);
}

std::cout << "cabin " CABIN_CABIN_PKG_VERSION << commitInfo();
fmt::print("cabin {}{}\n", CABIN_CABIN_PKG_VERSION, commitInfo());
if (isVerbose()) {
std::cout << "release: " CABIN_CABIN_PKG_VERSION
"\n"
"commit-hash: " COMMIT_HASH
"\n"
"commit-date: " COMMIT_DATE
"\n"
"compiler: " COMPILER_VERSION "\n"
<< "compile-date: " << COMPILE_DATE << '\n'
<< "libgit2: " << git2::Version() << '\n'
<< "libcurl: " << curl::Version() << '\n';
fmt::print(
"release: {}\n"
"commit-hash: {}\n"
"commit-date: {}\n"
"compiler: {}\n"
"compile-date: {}\n"
"libgit2: {}\n"
"libcurl: {}\n",
CABIN_CABIN_PKG_VERSION, COMMIT_HASH, COMMIT_DATE, COMPILER_VERSION,
COMPILE_DATE, git2::Version(), curl::Version()
);
}

return Ok();
Expand Down
68 changes: 64 additions & 4 deletions src/Command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <cstdio>
#include <cstdlib>
#include <fcntl.h>
#include <fmt/format.h>
#include <string>
#include <sys/select.h>
#include <sys/wait.h>
Expand All @@ -17,6 +18,56 @@ namespace cabin {

constexpr std::size_t BUFFER_SIZE = 128;

bool
ExitStatus::exitedNormally() const noexcept {
return WIFEXITED(rawStatus);
}
bool
ExitStatus::killedBySignal() const noexcept {
return WIFSIGNALED(rawStatus);
}
bool
ExitStatus::stoppedBySignal() const noexcept {
return WIFSTOPPED(rawStatus);
}
int
ExitStatus::exitCode() const noexcept {
return WEXITSTATUS(rawStatus);
}
int
ExitStatus::termSignal() const noexcept {
return WTERMSIG(rawStatus);
}
int
ExitStatus::stopSignal() const noexcept {
return WSTOPSIG(rawStatus);
}
bool
ExitStatus::coreDumped() const noexcept {
return WCOREDUMP(rawStatus);
}

// Successful only if normally exited with code 0
bool
ExitStatus::success() const noexcept {
return exitedNormally() && exitCode() == 0;
}

std::string
ExitStatus::toString() const {
if (exitedNormally()) {
return fmt::format("exited with code {}", exitCode());
} else if (killedBySignal()) {
return fmt::format(
"killed by signal {}{}", termSignal(),
coreDumped() ? " (core dumped)" : ""
);
} else if (stoppedBySignal()) {
return fmt::format("stopped by signal {}", stopSignal());
}
return "unknown status";
}

Result<ExitStatus>
Child::wait() const noexcept {
int status{};
Expand Down Expand Up @@ -238,9 +289,18 @@ Command::toString() const {
return res;
}

std::ostream&
operator<<(std::ostream& os, const Command& cmd) {
return os << cmd.toString();
} // namespace cabin

auto
fmt::formatter<cabin::ExitStatus>::format(
const cabin::ExitStatus& v, format_context& ctx
) const -> format_context::iterator {
return formatter<std::string>::format(v.toString(), ctx);
}

} // namespace cabin
auto
fmt::formatter<cabin::Command>::format(
const cabin::Command& v, format_context& ctx
) const -> format_context::iterator {
return formatter<std::string>::format(v.toString(), ctx);
}
70 changes: 20 additions & 50 deletions src/Command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@
#include <cstdint>
#include <cstdlib>
#include <filesystem>
#include <fmt/core.h>
#include <fmt/ostream.h>
#include <ostream>
#include <fmt/format.h>
#include <span>
#include <string>
#include <string_view>
#include <sys/types.h>
#include <sys/wait.h>
#include <utility>
#include <vector>

Expand All @@ -25,51 +21,18 @@ class ExitStatus {
ExitStatus() noexcept : rawStatus(EXIT_SUCCESS) {}
explicit ExitStatus(int status) noexcept : rawStatus(status) {}

bool exitedNormally() const noexcept {
return WIFEXITED(rawStatus);
}
bool killedBySignal() const noexcept {
return WIFSIGNALED(rawStatus);
}
bool stoppedBySignal() const noexcept {
return WIFSTOPPED(rawStatus);
}
int exitCode() const noexcept {
return WEXITSTATUS(rawStatus);
}
int termSignal() const noexcept {
return WTERMSIG(rawStatus);
}
int stopSignal() const noexcept {
return WSTOPSIG(rawStatus);
}
bool coreDumped() const noexcept {
return WCOREDUMP(rawStatus);
}
bool exitedNormally() const noexcept;
bool killedBySignal() const noexcept;
bool stoppedBySignal() const noexcept;
int exitCode() const noexcept;
int termSignal() const noexcept;
int stopSignal() const noexcept;
bool coreDumped() const noexcept;

// Successful only if normally exited with code 0
bool success() const noexcept {
return exitedNormally() && exitCode() == 0;
}
bool success() const noexcept;

std::string toString() const {
if (exitedNormally()) {
return fmt::format("exited with code {}", exitCode());
} else if (killedBySignal()) {
return fmt::format(
"killed by signal {}{}", termSignal(),
coreDumped() ? " (core dumped)" : ""
);
} else if (stoppedBySignal()) {
return fmt::format("stopped by signal {}", stopSignal());
}
return "unknown status";
}

friend std::ostream& operator<<(std::ostream& os, const ExitStatus& status) {
os << status.toString();
return os;
}
std::string toString() const;
};

struct CommandOutput {
Expand Down Expand Up @@ -139,9 +102,16 @@ struct Command {
Result<CommandOutput> output() const noexcept;
};

std::ostream& operator<<(std::ostream& os, const Command& cmd);

} // namespace cabin

template <>
struct fmt::formatter<cabin::ExitStatus> : ostream_formatter {};
struct fmt::formatter<cabin::ExitStatus> : formatter<std::string> {
auto format(const cabin::ExitStatus& v, format_context& ctx) const
-> format_context::iterator;
};

template <>
struct fmt::formatter<cabin::Command> : formatter<std::string> {
auto format(const cabin::Command& v, format_context& ctx) const
-> format_context::iterator;
};
30 changes: 20 additions & 10 deletions src/CurlVersion.cc
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
#include "CurlVersion.hpp"

#include <ostream>
#include <fmt/format.h>
#include <string>

namespace curl {

std::ostream&
operator<<(std::ostream& os, const Version& version) {
if (version.data) {
os << version.data->version << " (ssl: ";
if (version.data->ssl_version) {
os << version.data->ssl_version;
std::string
Version::toString() const {
std::string str;
if (data) {
str += data->version;
str += " (ssl: ";
if (data->ssl_version) {
str += data->ssl_version;
} else {
os << "none";
str += "none";
}
os << ")";
str += ")";
}
return os;
return str;
}

}; // namespace curl

auto
fmt::formatter<curl::Version>::format(
const curl::Version& v, format_context& ctx
) const -> format_context::iterator {
return formatter<std::string>::format(v.toString(), ctx);
}
13 changes: 10 additions & 3 deletions src/CurlVersion.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
#pragma once

#include <curl/curl.h>
#include <ostream>
#include <fmt/format.h>
#include <string>

namespace curl {

struct Version {
curl_version_info_data* data;

Version() : data(curl_version_info(CURLVERSION_NOW)) {}
};

std::ostream& operator<<(std::ostream& os, const Version& version);
std::string toString() const;
};

}; // namespace curl

template <>
struct fmt::formatter<curl::Version> : formatter<std::string> {
auto format(const curl::Version& v, format_context& ctx) const
-> format_context::iterator;
};
Loading
Loading