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

Update rang to 3.1.0 and add global option --color=auto|always|never #73

Merged
merged 2 commits into from
May 21, 2018
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
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ add_subdirectory(${PEGTL_ROOT} EXCLUDE_FROM_ALL)
# Rang
set(RANG_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/rang)
set(RANG_INCLUDE_DIR ${RANG_ROOT}/include)
add_subdirectory(${RANG_ROOT} EXCLUDE_FROM_ALL)

# CLI11
set(CLI11_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/CLI11)
Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ set(NeopgToolHeaders
cli/packet_command.h
cli/random_command.h
cli/version_command.h
global_options.h
io/streams.h
)
add_library(neopg-tool STATIC
Expand All @@ -34,6 +35,7 @@ target_include_directories(neopg-tool PUBLIC
"${CMAKE_BINARY_DIR}/include"
${BOTAN2_INCLUDE_DIRS}
${CLI11_INCLUDE_DIR}
${RANG_INCLUDE_DIR}
../include
)

Expand Down
45 changes: 45 additions & 0 deletions src/global_options.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// global_options.h
// Copyright 2017 The NeoPG developers
//
// NeoPG is released under the Simplified BSD License (see license.txt)

#pragma once

#include <iostream>

#include <CLI11.hpp>
#include <rang.hpp>

namespace rang {
std::istream& operator>>(std::istream& in, rang::control& when) {
std::string label;
in >> label;
if (label == "always")
when = rang::control::Force;
else if (label == "never")
when = rang::control::Off;
else if (label == "auto")
when = rang::control::Auto;
else {
// We are not pushing back what we couldn't parse. Seems to be OK.
in.setstate(std::ios_base::failbit);
}
return in;
}

std::ostream& operator<<(std::ostream& in, const rang::control& when) {
switch (when) {
case rang::control::Force:
return in << "always";
case rang::control::Off:
return in << "never";
default:
return in << "auto";
}
}
} // namespace rang

class GlobalOptions {
public:
rang::control color{rang::control::Auto};
};
10 changes: 10 additions & 0 deletions src/neopg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
NeoPG is released under the Simplified BSD License (see license.txt)
*/

#include "global_options.h"

#include <iostream>

#include <CLI11.hpp>
#include <rang.hpp>

#include <boost/algorithm/string/predicate.hpp>
#include <boost/format.hpp>
Expand Down Expand Up @@ -125,6 +128,7 @@ int main(int argc, char* argv[]) {
args.emplace(args.begin(), "dirmngr-client");

CLI::App app{_("NeoPG implements the OpenPGP standard.")};
GlobalOptions options;

/* Translators, please add a second line saying "Report translation bugs to
<...>" with the address for translation bugs (typically your translation
Expand Down Expand Up @@ -152,6 +156,12 @@ int main(int argc, char* argv[]) {
}
});

app.add_set("--color", options.color,
{rang::control::Auto, rang::control::Force, rang::control::Off},
"colorize the output (auto, always, or never)")
->set_type_name("WHEN");
app.set_callback([&options]() { rang::setControlMode(options.color); });

std::string legacy_group = "command to execute (GnuPG-compatible)";
LegacyCommand cmd_gpg2(app, gpg_main, "gpg2", "invoke gpg2", legacy_group);
LegacyCommand cmd_gpgsm(app, gpgsm_main, "gpgsm", "invoke gpgsm",
Expand Down