Skip to content

Commit

Permalink
feat:color using termcolor
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavankumar07s committed Nov 13, 2024
1 parent 13d7bf1 commit 0c3797e
Show file tree
Hide file tree
Showing 30 changed files with 1,643 additions and 28 deletions.
17 changes: 17 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c17",
"cppStandard": "gnu++17",
"intelliSenseMode": "linux-gcc-x64",
"configurationProvider": "ms-vscode.makefile-tools"
}
],
"version": 4
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"C_Cpp.errorSquiggles": "enabled"
}
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.16)
project(jsonschema VERSION 4.3.0 LANGUAGES CXX)
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
include(vendor/noa/cmake/noa.cmake)
include_directories(vendor/termcolor/include)

# Options
option(JSONSCHEMA_TESTS "Build the JSON Schema CLI tests" OFF)
Expand Down
1 change: 1 addition & 0 deletions DEPENDENCIES
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ hydra https://github.com/sourcemeta/hydra a4a74f3cabd32f2f829f449d67339dac33f991
alterschema https://github.com/sourcemeta/alterschema 92e370ce9c1f0582014b54d43e388ee012dfe13d
jsonbinpack https://github.com/sourcemeta/jsonbinpack d777179441d3c703e1fda1187742541aa26836b5
blaze https://github.com/sourcemeta/blaze 4db8309470369332d3d0658ade9402a37abe418e
termcolor https://github.com/ikalnytskyi/termcolor.git v2.1.0
5 changes: 2 additions & 3 deletions src/command_frame.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
#include "command.h"
#include "utils.h"

static auto
enum_to_string(const sourcemeta::jsontoolkit::ReferenceEntryType type)
-> std::string {
static auto enum_to_string(
const sourcemeta::jsontoolkit::ReferenceEntryType type) -> std::string {
switch (type) {
case sourcemeta::jsontoolkit::ReferenceEntryType::Resource:
return "resource";
Expand Down
4 changes: 2 additions & 2 deletions src/command_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ get_schema_object(const sourcemeta::jsontoolkit::URI &identifier,
}

static auto get_data(const sourcemeta::jsontoolkit::JSON &test_case,
const std::filesystem::path &base, const bool verbose)
-> sourcemeta::jsontoolkit::JSON {
const std::filesystem::path &base,
const bool verbose) -> sourcemeta::jsontoolkit::JSON {
assert(base.is_absolute());
assert(test_case.is_object());
assert(test_case.defines("data") || test_case.defines("dataPath"));
Expand Down
3 changes: 1 addition & 2 deletions src/command_validate.cc
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
22 changes: 21 additions & 1 deletion src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@
#include <string> // std::string
#include <string_view> // std::string_view
#include <vector> // std::vector
#ifdef _WIN32
#include <io.h>
#define isatty _isatty
#else
#include <unistd.h>
#endif

#include "command.h"
#include "configure.h"
#include <termcolor/termcolor.hpp>

constexpr std::string_view USAGE_DETAILS{R"EOF(
Global Options:
Expand Down Expand Up @@ -88,6 +95,14 @@ 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 {
bool use_colors = true;

if (std::find(arguments.begin(), arguments.end(), "--no-color") !=
arguments.end()) {
use_colors = false;
} else if (!isatty(fileno(stdout))) {
use_colors = false;
}
if (command == "fmt") {
return sourcemeta::jsonschema::cli::fmt(arguments);
} else if (command == "frame") {
Expand Down Expand Up @@ -117,7 +132,12 @@ auto jsonschema_main(const std::string &program, const std::string &command,
<< sourcemeta::jsonschema::cli::PROJECT_VERSION << "\n";
std::cout << "Usage: " << std::filesystem::path{program}.filename().string()
<< " <command> [arguments...]\n";
std::cout << USAGE_DETAILS;
if (use_colors) {
std::cout << termcolor::yellow << USAGE_DETAILS << termcolor::reset
<< "\n";
} else {
std::cout << USAGE_DETAILS << "\n";
}
return EXIT_SUCCESS;
}
}
Expand Down
18 changes: 8 additions & 10 deletions src/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ auto parse_options(const std::span<const std::string> &arguments,
return options;
}

auto print(const sourcemeta::blaze::ErrorOutput &output, std::ostream &stream)
-> void {
auto print(const sourcemeta::blaze::ErrorOutput &output,
std::ostream &stream) -> void {
stream << "error: Schema validation failure\n";
for (const auto &entry : output) {
stream << " " << entry.message << "\n";
Expand All @@ -189,8 +189,8 @@ auto print(const sourcemeta::blaze::ErrorOutput &output, std::ostream &stream)
}
}

auto print(const sourcemeta::blaze::TraceOutput &output, std::ostream &stream)
-> void {
auto print(const sourcemeta::blaze::TraceOutput &output,
std::ostream &stream) -> void {
for (auto iterator = output.cbegin(); iterator != output.cend(); iterator++) {
const auto &entry{*iterator};

Expand Down Expand Up @@ -302,9 +302,8 @@ auto log_verbose(const std::map<std::string, std::vector<std::string>> &options)
return null_stream;
}

auto parse_extensions(
const std::map<std::string, std::vector<std::string>> &options)
-> std::set<std::string> {
auto parse_extensions(const std::map<std::string, std::vector<std::string>>
&options) -> std::set<std::string> {
std::set<std::string> result;

if (options.contains("extension")) {
Expand All @@ -328,9 +327,8 @@ auto parse_extensions(
return result;
}

auto parse_ignore(
const std::map<std::string, std::vector<std::string>> &options)
-> std::set<std::filesystem::path> {
auto parse_ignore(const std::map<std::string, std::vector<std::string>>
&options) -> std::set<std::filesystem::path> {
std::set<std::filesystem::path> result;

if (options.contains("ignore")) {
Expand Down
18 changes: 8 additions & 10 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ auto for_each_json(const std::vector<std::string> &arguments,
-> std::vector<
std::pair<std::filesystem::path, sourcemeta::jsontoolkit::JSON>>;

auto print(const sourcemeta::blaze::ErrorOutput &output, std::ostream &stream)
-> void;
auto print(const sourcemeta::blaze::ErrorOutput &output,
std::ostream &stream) -> void;

auto print(const sourcemeta::blaze::TraceOutput &output, std::ostream &stream)
-> void;
auto print(const sourcemeta::blaze::TraceOutput &output,
std::ostream &stream) -> void;

auto resolver(const std::map<std::string, std::vector<std::string>> &options,
const bool remote = false)
Expand All @@ -42,13 +42,11 @@ auto resolver(const std::map<std::string, std::vector<std::string>> &options,
auto log_verbose(const std::map<std::string, std::vector<std::string>> &options)
-> std::ostream &;

auto parse_extensions(
const std::map<std::string, std::vector<std::string>> &options)
-> std::set<std::string>;
auto parse_extensions(const std::map<std::string, std::vector<std::string>>
&options) -> std::set<std::string>;

auto parse_ignore(
const std::map<std::string, std::vector<std::string>> &options)
-> std::set<std::filesystem::path>;
auto parse_ignore(const std::map<std::string, std::vector<std::string>>
&options) -> std::set<std::filesystem::path>;

} // namespace sourcemeta::jsonschema::cli

Expand Down
1 change: 1 addition & 0 deletions vendor/termcolor/.mailmap

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

0 comments on commit 0c3797e

Please sign in to comment.