Skip to content

Feat:Termcolor #221

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ find_package(Core REQUIRED)
find_package(Blaze REQUIRED)
find_package(JSONBinPack REQUIRED)
find_package(Hydra REQUIRED)
find_package(TermColor REQUIRED)
add_subdirectory(src)

if(JSONSCHEMA_DEVELOPMENT)
Expand Down
1 change: 1 addition & 0 deletions DEPENDENCIES
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ core https://github.com/sourcemeta/core 32f08ad1e4d3e39097eda3fdd44e58ad0b5d59ec
hydra https://github.com/sourcemeta/hydra a67b879df800e834ed8a2d056f98398f7211d870
jsonbinpack https://github.com/sourcemeta/jsonbinpack d0f111b04fc4d252e278bafd66c382a9db3d3057
blaze https://github.com/sourcemeta/blaze ee117240f8399831001cb1c764d8b08503fc9112
termcolor https://github.com/ikalnytskyi/termcolor v2.1.0
13 changes: 13 additions & 0 deletions cmake/FindTermColor.cmake
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is overly complicated and not following the same conventions we have in other Find* files. Plus it's using the _FOUND suffixes that CMake actually expects, relying on targets instead. This works for me and it is pretty much exactly the same as with other dependencies:

if(NOT TermColor_FOUND)
  add_subdirectory("${PROJECT_SOURCE_DIR}/vendor/termcolor")
  set(TermColor_FOUND ON)
endif()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes changing to this way.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
if(TARGET termcolor::termcolor)
return()
endif()

add_library(termcolor INTERFACE)
add_library(termcolor::termcolor ALIAS termcolor)

target_include_directories(termcolor INTERFACE
${CMAKE_CURRENT_LIST_DIR}/../vendor/termcolor/include)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(TermColor
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this for?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mr. @jviotti, as you suggested, termcolor has its own CMake file, and I need to create this file to use it. I researched and came up with this approach.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

REQUIRED_VARS CMAKE_CURRENT_LIST_DIR)
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ target_link_libraries(jsonschema_cli PRIVATE sourcemeta::jsonbinpack::compiler)
target_link_libraries(jsonschema_cli PRIVATE sourcemeta::jsonbinpack::runtime)
target_link_libraries(jsonschema_cli PRIVATE sourcemeta::blaze::compiler)
target_link_libraries(jsonschema_cli PRIVATE sourcemeta::blaze::evaluator)
target_link_libraries(jsonschema_cli PRIVATE termcolor::termcolor)

configure_file(configure.h.in configure.h @ONLY)
target_include_directories(jsonschema_cli PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
Expand Down
3 changes: 1 addition & 2 deletions src/command_validate.cc
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like an unrelated change?

Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ auto sourcemeta::jsonschema::cli::validate(
log_verbose(options)
<< "ok: "
<< std::filesystem::weakly_canonical(instance_path).string()
<< " (entry #" << index << ")"
<< "\n matches "
<< " (entry #" << index << ")" << "\n matches "
<< std::filesystem::weakly_canonical(schema_path).string()
<< "\n";
} else {
Expand Down
38 changes: 29 additions & 9 deletions src/main.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include <sourcemeta/core/jsonschema.h>

#include "command.h"
#include "configure.h"
#include "utils.h"
#include <algorithm> // std::min
#include <cstdlib> // EXIT_FAILURE, EXIT_SUCCESS
#include <filesystem> // std::filesystem
Expand All @@ -8,14 +11,19 @@
#include <string> // std::string
#include <string_view> // std::string_view
#include <vector> // std::vector

#include "command.h"
#include "configure.h"

#ifdef _WIN32
#include <io.h>
#define isatty _isatty
#define fileno _fileno
#else
#include <unistd.h>
#endif
#include <termcolor/termcolor.hpp>
constexpr std::string_view USAGE_DETAILS{R"EOF(
Global Options:

--verbose, -v Enable verbose output
--no-color, -n Disable colored output
--resolve, -r Import the given JSON Schema (or directory of schemas)
into the resolution context

Expand Down Expand Up @@ -75,6 +83,11 @@ For more documentation, visit https://github.com/sourcemeta/jsonschema

auto jsonschema_main(const std::string &program, const std::string &command,
const std::span<const std::string> &arguments) -> int {
const std::set<std::string> flags{"no-color", "n", "verbose", "v"};
const auto options =
sourcemeta::jsonschema::cli::parse_options(arguments, flags);
const bool use_colors = !options.contains("no-color") &&
!options.contains("n") && isatty(fileno(stdout));
if (command == "fmt") {
return sourcemeta::jsonschema::cli::fmt(arguments);
} else if (command == "frame") {
Expand All @@ -94,11 +107,18 @@ auto jsonschema_main(const std::string &program, const std::string &command,
} else if (command == "decode") {
return sourcemeta::jsonschema::cli::decode(arguments);
} else {
std::cout << "JSON Schema CLI - v"
<< sourcemeta::jsonschema::cli::PROJECT_VERSION << "\n";
std::cout << "Usage: " << std::filesystem::path{program}.filename().string()
<< " <command> [arguments...]\n";
std::cout << USAGE_DETAILS;
std::cout << "JSON Schema CLI - ";
if (use_colors) {
std::cout << termcolor::yellow << "v"
<< sourcemeta::jsonschema::cli::PROJECT_VERSION
<< termcolor::reset;
} else {
std::cout << "v" << sourcemeta::jsonschema::cli::PROJECT_VERSION;
}
std::cout << "\nUsage: "
<< std::filesystem::path{program}.filename().string()
<< " <command> [arguments...]\n"
<< USAGE_DETAILS;
return EXIT_SUCCESS;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ auto parse_options(const std::span<const std::string> &arguments,
std::set<std::string> effective_flags{flags};
effective_flags.insert("v");
effective_flags.insert("verbose");
effective_flags.insert("n");
effective_flags.insert("no-color");

options.insert({"", {}});
std::optional<std::string> current_option;
Expand Down
1 change: 1 addition & 0 deletions vendor/termcolor/.mailmap
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's mask all of these files!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhk sir , reading the docs.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions vendor/termcolor/CMakeLists.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions vendor/termcolor/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading