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

feat:Make use of coloured output #197

Closed
wants to merge 4 commits into from
Closed
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
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
Copy link
Member

Choose a reason for hiding this comment

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

Where are we actually checking for TTY in the code?

Copy link
Author

Choose a reason for hiding this comment

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

Yes

Copy link
Author

@Pavankumar07s Pavankumar07s Nov 9, 2024

Choose a reason for hiding this comment

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

We haven't introduced yet . In this pr i have just inlmported the module for tty and color. In next pr we will introduce condition for tty as cli-program detected then we will continue using colors else we will turn off.

Copy link
Member

Choose a reason for hiding this comment

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

I think you still need the TTY check on this one, otherwise i.e. piping the help text will result in escaped characters

Copy link
Author

Choose a reason for hiding this comment

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

Alright @jviotti 👍

#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
21 changes: 9 additions & 12 deletions src/utils.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#ifndef SOURCEMETA_JSONSCHEMA_CLI_UTILS_H_
#define SOURCEMETA_JSONSCHEMA_CLI_UTILS_H_

#include <sourcemeta/blaze/compiler.h>
#include <sourcemeta/jsontoolkit/json.h>
#include <sourcemeta/jsontoolkit/jsonpointer.h>
#include <sourcemeta/jsontoolkit/jsonschema.h>

#include <sourcemeta/blaze/compiler.h>

#include <filesystem> // std::filesystem
#include <map> // std::map
#include <ostream> // std::ostream
Expand All @@ -29,11 +28,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 +41,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

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

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

Loading
Loading