use crate colored in order to support Windows

This commit is contained in:
Fabrice Reix 2021-02-13 20:51:22 +01:00
parent a7bf5bb144
commit 6827671944
12 changed files with 70 additions and 152 deletions

13
Cargo.lock generated
View File

@ -171,6 +171,17 @@ dependencies = [
"bitflags",
]
[[package]]
name = "colored"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd"
dependencies = [
"atty",
"lazy_static",
"winapi",
]
[[package]]
name = "crc32fast"
version = "1.2.1"
@ -324,6 +335,7 @@ dependencies = [
"brotli",
"chrono",
"clap",
"colored",
"curl",
"encoding",
"float-cmp",
@ -351,6 +363,7 @@ version = "1.2.0"
dependencies = [
"atty",
"clap",
"colored",
"hurl_core",
"proptest",
"regex",

View File

@ -22,6 +22,7 @@ base64 = "0.11.0"
brotli="3.3.0"
chrono = "0.4.11"
clap = "2.33.0"
colored = "2"
curl = "0.4.33"
encoding = "0.2"
float-cmp = "0.6.0"

View File

@ -1,60 +0,0 @@
/*
* hurl (https://hurl.dev)
* Copyright (C) 2020 Orange
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#[allow(unused)]
pub enum TerminalColor {
Black,
Red,
Green,
Yellow,
Blue,
Magenta,
Cyan,
LightGray,
LightBlack,
LightRed,
LightGreen,
LightYellow,
LightBlue,
LightMagenta,
LightCyan,
LightWhite,
}
impl TerminalColor {
pub fn format(self, v: String) -> String {
match self {
TerminalColor::Black => format!("\x1b[0;30m{}\x1b[0m", v),
TerminalColor::Red => format!("\x1b[1;31m{}\x1b[0m", v),
TerminalColor::Green => format!("\x1b[0;32m{}\x1b[0m", v),
TerminalColor::Yellow => format!("\x1b[0;33m{}\x1b[0m", v),
TerminalColor::Blue => format!("\x1b[0;34m{}\x1b[0m", v),
TerminalColor::Magenta => format!("\x1b[0;35m{}\x1b[0m", v),
TerminalColor::Cyan => format!("\x1b[0;36m{}\x1b[0m", v),
TerminalColor::LightGray => format!("\x1b[0;37m{}\x1b[0m", v),
TerminalColor::LightBlack => format!("\x1b[0;90m{}\x1b[0m", v),
TerminalColor::LightRed => format!("\x1b[0;91m{}\x1b[0m", v),
TerminalColor::LightGreen => format!("\x1b[0;92m{}\x1b[0m", v),
TerminalColor::LightYellow => format!("\x1b[0;93m{}\x1b[0m", v),
TerminalColor::LightBlue => format!("\x1b[0;94m{}\x1b[0m", v),
TerminalColor::LightMagenta => format!("\x1b[0;95m{}\x1b[0m", v),
TerminalColor::LightCyan => format!("\x1b[0;96m{}\x1b[0m", v),
TerminalColor::LightWhite => format!("\x1b[0;97m{}\x1b[0m", v),
}
}
}

View File

@ -16,13 +16,12 @@
*
*/
use crate::runner;
use super::color::TerminalColor;
use colored::*;
use hurl_core::error::Error;
use hurl_core::parser;
use crate::runner;
pub fn make_logger_verbose(verbose: bool) -> impl Fn(&str) {
move |message| log_verbose(verbose, message)
}
@ -57,10 +56,10 @@ pub fn log_info(message: &str) {
fn log_error_message(color: bool, warning: bool, message: &str) {
let log_type = match (color, warning) {
(false, false) => "warning".to_string(),
(false, true) => "error".to_string(),
(true, false) => TerminalColor::Red.format("error".to_string()),
(true, true) => TerminalColor::Yellow.format("warning".to_string()),
(false, true) => "warning".to_string(),
(false, false) => "error".to_string(),
(true, true) => "warning".yellow().bold().to_string(),
(true, false) => "error".red().bold().to_string(),
};
eprintln!("{}: {}", log_type, message);
}
@ -94,9 +93,9 @@ fn log_error(
let error_type = if !color {
error_type
} else if warning {
TerminalColor::Yellow.format(error_type)
error_type.yellow().to_string()
} else {
TerminalColor::Red.format(error_type)
error_type.red().to_string()
};
eprintln!("{}: {}", error_type, error.description());

View File

@ -22,7 +22,6 @@ pub use self::logger::{
make_logger_verbose,
};
mod color;
mod fs;
pub mod interactive;
mod logger;

View File

@ -59,6 +59,17 @@ pub struct CLIOptions {
pub user: Option<String>,
}
#[cfg(target_family = "unix")]
pub fn init_colored() {
colored::control::set_override(true);
}
#[cfg(target_family = "windows")]
pub fn init_colored() {
colored::control::set_override(true);
colored::control::set_virtual_terminal(true);
}
fn execute(
filename: &str,
contents: String,
@ -611,7 +622,7 @@ fn parse_options(matches: ArgMatches) -> Result<CLIOptions, CLIError> {
fn main() {
let app = app();
let matches = app.clone().get_matches();
init_colored();
let mut filenames = match matches.values_of("INPUT") {
None => vec![],
Some(v) => v.collect(),

View File

@ -16,6 +16,7 @@ strict = []
[dependencies]
atty = "0.2.13"
clap = "2.33.0"
colored = "2"
hurl_core = { version = "1.1.0", path = "../hurl_core" }
regex = "1.1.0"

View File

@ -1,58 +0,0 @@
/*
* hurl (https://hurl.dev)
* Copyright (C) 2020 Orange
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
pub enum TerminalColor {
Black,
Red,
Green,
Yellow,
Blue,
Magenta,
Cyan,
LightGray,
LightBlack,
LightRed,
LightGreen,
LightYellow,
LightBlue,
LightMagenta,
LightCyan,
LightWhite,
}
impl TerminalColor {
pub fn format(self, v: String) -> String {
match self {
TerminalColor::Black => format!("\x1b[0;30m{}\x1b[0m", v),
TerminalColor::Red => format!("\x1b[1;31m{}\x1b[0m", v),
TerminalColor::Green => format!("\x1b[0;32m{}\x1b[0m", v),
TerminalColor::Yellow => format!("\x1b[0;33m{}\x1b[0m", v),
TerminalColor::Blue => format!("\x1b[0;34m{}\x1b[0m", v),
TerminalColor::Magenta => format!("\x1b[0;35m{}\x1b[0m", v),
TerminalColor::Cyan => format!("\x1b[0;36m{}\x1b[0m", v),
TerminalColor::LightGray => format!("\x1b[0;37m{}\x1b[0m", v),
TerminalColor::LightBlack => format!("\x1b[0;90m{}\x1b[0m", v),
TerminalColor::LightRed => format!("\x1b[0;91m{}\x1b[0m", v),
TerminalColor::LightGreen => format!("\x1b[0;92m{}\x1b[0m", v),
TerminalColor::LightYellow => format!("\x1b[0;93m{}\x1b[0m", v),
TerminalColor::LightBlue => format!("\x1b[0;94m{}\x1b[0m", v),
TerminalColor::LightMagenta => format!("\x1b[0;95m{}\x1b[0m", v),
TerminalColor::LightCyan => format!("\x1b[0;96m{}\x1b[0m", v),
TerminalColor::LightWhite => format!("\x1b[0;97m{}\x1b[0m", v),
}
}
}

View File

@ -15,12 +15,13 @@
* limitations under the License.
*
*/
use colored::*;
use super::color::TerminalColor;
use crate::linter;
use hurl_core::error::Error;
use hurl_core::parser;
use crate::linter;
pub fn make_logger_verbose(verbose: bool) -> impl Fn(&str) {
move |message| log_verbose(verbose, message)
}
@ -55,10 +56,10 @@ pub fn log_info(message: &str) {
fn log_error_message(color: bool, warning: bool, message: &str) {
let log_type = match (color, warning) {
(false, false) => "warning".to_string(),
(false, true) => "error".to_string(),
(true, false) => TerminalColor::Red.format("error".to_string()),
(true, true) => TerminalColor::Yellow.format("warning".to_string()),
(false, true) => "warning".to_string(),
(false, false) => "error".to_string(),
(true, true) => "warning".yellow().bold().to_string(),
(true, false) => "error".red().bold().to_string(),
};
eprintln!("{}: {}", log_type, message);
}
@ -92,9 +93,9 @@ fn log_error(
let error_type = if !color {
error_type
} else if warning {
TerminalColor::Yellow.format(error_type)
error_type.yellow().bold().to_string()
} else {
TerminalColor::Red.format(error_type)
error_type.red().bold().to_string()
};
eprintln!("{}: {}", error_type, error.description());

View File

@ -16,14 +16,12 @@
*
*/
pub use self::color::TerminalColor;
pub use self::fs::read_to_string;
pub use self::logger::{
log_info, make_logger_error_message, make_logger_linter_error, make_logger_parser_error,
make_logger_verbose,
};
mod color;
mod fs;
mod logger;

View File

@ -15,10 +15,11 @@
* limitations under the License.
*
*/
use colored::*;
use hurl_core::ast::*;
use super::token::*;
use crate::cli::TerminalColor;
pub fn format(hurl_file: HurlFile, color: bool) -> String {
let mut buffer = String::from("");
@ -33,7 +34,7 @@ pub fn format_token(token: Token, color: bool) -> String {
Token::Whitespace(value) => value,
Token::Method(value) => {
if color {
TerminalColor::LightYellow.format(value)
value.yellow().to_string()
} else {
value
}
@ -42,14 +43,14 @@ pub fn format_token(token: Token, color: bool) -> String {
Token::Status(value) => value,
Token::SectionHeader(value) => {
if color {
TerminalColor::Magenta.format(value)
value.magenta().to_string()
} else {
value
}
}
Token::Comment(value) => {
if color {
TerminalColor::LightBlack.format(value)
value.bright_black().to_string()
} else {
value
}
@ -58,56 +59,56 @@ pub fn format_token(token: Token, color: bool) -> String {
Token::Colon(value) => value,
Token::QueryType(value) => {
if color {
TerminalColor::LightCyan.format(value)
value.cyan().to_string()
} else {
value
}
}
Token::PredicateType(value) => {
if color {
TerminalColor::LightYellow.format(value)
value.yellow().to_string()
} else {
value
}
}
Token::Not(value) => {
if color {
TerminalColor::LightYellow.format(value)
value.yellow().to_string()
} else {
value
}
}
Token::Boolean(value) | Token::Number(value) => {
if color {
TerminalColor::Cyan.format(value)
value.cyan().to_string()
} else {
value
}
}
Token::String(value) => {
if color {
TerminalColor::Green.format(value)
value.green().to_string()
} else {
value
}
}
Token::Quote(value) => {
if color {
TerminalColor::Green.format(value)
value.green().to_string()
} else {
value
}
}
Token::CodeDelimiter(value) => {
if color {
TerminalColor::Green.format(value)
value.green().to_string()
} else {
value
}
}
Token::CodeVariable(value) => {
if color {
TerminalColor::Green.format(value)
value.green().to_string()
} else {
value
}

View File

@ -29,6 +29,17 @@ use hurlfmt::cli;
use hurlfmt::format;
use hurlfmt::linter::Lintable;
#[cfg(target_family = "unix")]
pub fn init_colored() {
colored::control::set_override(true);
}
#[cfg(target_family = "windows")]
pub fn init_colored() {
colored::control::set_override(true);
colored::control::set_virtual_terminal(true);
}
fn main() {
let app = clap::App::new("hurlfmt")
// .author(clap::crate_authors!())
@ -93,6 +104,7 @@ fn main() {
);
let matches = app.clone().get_matches();
init_colored();
// Additional checks
if matches.is_present("standalone") && matches.value_of("format") != Some("html") {