Skip to content

Commit

Permalink
refactor: replace some ostream use with fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-matsui committed Jan 30, 2025
1 parent efd13ab commit 279db01
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 116 deletions.
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
25 changes: 13 additions & 12 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/core.h>
#include <string_view>

#ifndef CABIN_CABIN_PKG_VERSION
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
57 changes: 57 additions & 0 deletions src/Command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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 @@ -244,3 +294,10 @@ operator<<(std::ostream& os, const Command& cmd) {
}

} // 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);
}
61 changes: 13 additions & 48 deletions src/Command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@
#include <cstdint>
#include <cstdlib>
#include <filesystem>
#include <fmt/core.h>
#include <fmt/ostream.h>
#include <ostream>
#include <span>
#include <string>
#include <string_view>
#include <sys/types.h>
#include <sys/wait.h>
#include <utility>
#include <vector>

Expand All @@ -25,51 +20,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;
}

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";
}
bool success() const noexcept;

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 @@ -144,4 +106,7 @@ 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;
};
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/core.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/core.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;
};
29 changes: 19 additions & 10 deletions src/Git2/Version.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,26 @@

#include "Exception.hpp"

#include <fmt/core.h>
#include <git2/common.h>
#include <string>

namespace git2 {

Version::Version() : features(git2Throw(git_libgit2_features())) {
git2Throw(git_libgit2_version(&this->major, &this->minor, &this->rev));
}

std::string
Version::toString() const {
const auto flagStr = [](const bool flag) { return flag ? "on" : "off"; };
return fmt::format(
"{}.{}.{} (threads: {}, https: {}, ssh: {}, nsec: {})", major, minor, rev,
flagStr(hasThread()), flagStr(hasHttps()), flagStr(hasSsh()),
flagStr(hasNsec())
);
}

bool
Version::hasThread() const noexcept {
return this->features & GIT_FEATURE_THREADS;
Expand All @@ -30,14 +42,11 @@ Version::hasNsec() const noexcept {
return this->features & GIT_FEATURE_NSEC;
}

std::ostream&
operator<<(std::ostream& os, const Version& version) {
const auto flagStr = [](const bool flag) { return flag ? "on" : "off"; };
return os << version.major << '.' << version.minor << '.' << version.rev
<< " (threads: " << flagStr(version.hasThread()) << ", "
<< "https: " << flagStr(version.hasHttps()) << ", "
<< "ssh: " << flagStr(version.hasSsh()) << ", "
<< "nsec: " << flagStr(version.hasNsec()) << ')';
}

} // namespace git2

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

0 comments on commit 279db01

Please sign in to comment.