From 0b04fe4c6dca18412f19685760fb8bca06ee5c76 Mon Sep 17 00:00:00 2001 From: orciument Date: Sun, 14 Apr 2024 04:22:21 +0200 Subject: [PATCH] add colored error messages --- Cargo.toml | 1 + src/grafzahl/count_modes/count_mode.rs | 3 ++- .../count_modes/custom_modes/line_type.rs | 15 ++++++++++++--- src/grafzahl/generic_counter.rs | 7 ++++--- src/grafzahl/io_reader.rs | 5 +++-- src/grafzahl/language/languages.rs | 6 ++++-- src/main.rs | 5 +++-- 7 files changed, 29 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 09a6085..17ee489 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,4 @@ edition = "2021" ignore = "0.4.20" quicli = "0.4.0" structopt = "0.3.26" +colored = "2.1.0" diff --git a/src/grafzahl/count_modes/count_mode.rs b/src/grafzahl/count_modes/count_mode.rs index b10b438..95ca1c6 100644 --- a/src/grafzahl/count_modes/count_mode.rs +++ b/src/grafzahl/count_modes/count_mode.rs @@ -1,5 +1,6 @@ use std::path::PathBuf; use std::str::FromStr; +use colored::Colorize; use structopt::StructOpt; use crate::{AppState, Cli}; use crate::grafzahl::count_modes::count_mode::CountMode::*; @@ -59,7 +60,7 @@ impl FromStr for CountMode { "language" => Ok(Language), "languageloc" => Ok(LanguageLOC), "langloc" => Ok(LanguageLOC), - _ => Err(String::from("NotAnOption: LINE, WORD, CHAR, LOC, LANGUAGE, LANGUAGELOC")) + _ => Err(format!("{}", "NotAnOption: LINE, WORD, CHAR, LOC, LANGUAGE, LANGUAGELOC".red())) }; } } diff --git a/src/grafzahl/count_modes/custom_modes/line_type.rs b/src/grafzahl/count_modes/custom_modes/line_type.rs index 54fcfa3..1629c37 100644 --- a/src/grafzahl/count_modes/custom_modes/line_type.rs +++ b/src/grafzahl/count_modes/custom_modes/line_type.rs @@ -1,6 +1,7 @@ use std::iter::Sum; use std::fmt::{Display, Formatter}; use std::ops::Add; +use colored::Colorize; use crate::AppState; use crate::grafzahl::count_modes::countable::Countable; use crate::grafzahl::language::annotator::annotate; @@ -36,11 +37,15 @@ impl Countable for LineTypeCount { println!(" Lines of Code: {}", self.code_count); println!(" Lines of Comments: {}", self.comment_count); println!(" Lines of New Lines: {}", self.empty_count); - println!("Total: {}", self.code_count + self.comment_count + self.empty_count); + println!("Total: {}", (self.code_count + self.comment_count + self.empty_count).to_string().underline()); } fn display_legend() { - println!("Legend: => (Lines of Code, Lines with Comments, New Lines)"); + println!("Legend: => ({}, {}, {})", + "Lines of Code".blue(), + "Lines with Comments".green(), + "New Lines".white() + ); } fn display_description() { @@ -80,7 +85,11 @@ impl Sum for LineTypeCount { impl Display for LineTypeCount { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "({}, {}, {})", self.code_count, self.comment_count, self.empty_count) + write!(f, "({}, {}, {})", + self.code_count.to_string().blue(), + self.comment_count.to_string().green(), + self.empty_count.to_string().white() + ) } } diff --git a/src/grafzahl/generic_counter.rs b/src/grafzahl/generic_counter.rs index 2721457..09c6923 100644 --- a/src/grafzahl/generic_counter.rs +++ b/src/grafzahl/generic_counter.rs @@ -1,5 +1,6 @@ use std::ffi::OsStr; use std::path::PathBuf; +use colored::Colorize; use crate::{AppState, Cli}; use crate::grafzahl::count_modes::countable::Countable; use crate::grafzahl::io_reader::{read_dir, read_file}; @@ -79,7 +80,7 @@ fn count_file(path: &PathBuf, name: &String, state: &AppSt Ok(v) => v, Err(e) => { return (TreeNode { - string: format!("{name} => [ERR] {e}"), + string: format!("{name} => [ERR] {e}").bright_red().to_string(), errored: true, ..Default::default() }, CountMode::default()); @@ -94,7 +95,7 @@ fn count_file(path: &PathBuf, name: &String, state: &AppSt }, v), Err(e) => (TreeNode { - string: format!("{name} => [ERR] {e}"), + string: format!("{name} => [ERR] {e}").bright_red().to_string(), errored: true, ..Default::default() }, CountMode::default()) @@ -106,7 +107,7 @@ fn count_folder(path: &PathBuf, state: &AppState, name: &S Ok(v) => v.iter().map(|p| count_dir(p, state)).collect(), Err(e) => { return (TreeNode { - string: format!("{name}/ => [ERR] {e}"), + string: format!("{name}/ => [ERR] {e}").bright_red().to_string(), errored: true, ..Default::default() }, CountMode::default()); diff --git a/src/grafzahl/io_reader.rs b/src/grafzahl/io_reader.rs index 1b66934..3de6cdb 100644 --- a/src/grafzahl/io_reader.rs +++ b/src/grafzahl/io_reader.rs @@ -3,6 +3,7 @@ use std::fs::File; use std::io; use std::io::{BufRead, BufReader}; use std::path::PathBuf; +use colored::Colorize; use crate::grafzahl::io_reader::ReadFileError::{EncodingNotSupported, IoError}; pub(crate) enum ReadFileError { @@ -13,8 +14,8 @@ pub(crate) enum ReadFileError { impl Display for ReadFileError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { - IoError(e) => write!(f, "IoError: {e}"), - EncodingNotSupported => write!(f, "EncodingNotSupported: Non Unicode"), + IoError(e) => write!(f, "{} {}", "IoError: ".red(), e.to_string().red()), + EncodingNotSupported => write!(f, "{}", "EncodingNotSupported: Non Unicode".red()), } } } diff --git a/src/grafzahl/language/languages.rs b/src/grafzahl/language/languages.rs index 7d4a317..c06c354 100644 --- a/src/grafzahl/language/languages.rs +++ b/src/grafzahl/language/languages.rs @@ -3,6 +3,7 @@ use std::fs::File; use std::io::{BufRead, BufReader}; use std::path::PathBuf; use std::str::FromStr; +use colored::Colorize; use crate::{AppState, get_config_location}; #[derive(Debug, Clone)] @@ -75,8 +76,9 @@ pub fn import_languages() -> Vec { let lang = match Language::from_str(&l) { Ok(x) => x, Err(_) => { - eprintln!("Error parsing line: "); - eprintln!("{l}"); + eprintln!("{}", "Error parsing line:".red().underline()); + eprintln!("{}", l.red()); + eprintln!(" "); continue; } }; diff --git a/src/main.rs b/src/main.rs index a2dc2b4..444a882 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ use std::env; use std::path::PathBuf; use std::process::exit; use std::str::FromStr; +use colored::Colorize; use ignore::gitignore::Gitignore; use quicli::prelude::CliResult; @@ -47,7 +48,7 @@ impl FromStr for Override { "disable" => Ok(Override::Disable), "none" => Ok(Override::None), "default" => Ok(Override::None), - _ => Err(String::from("NotAnOption: ON, OFF")) + _ => Err(format!("{}", "NotAnOption: ON, OFF".red())) }; } } @@ -75,7 +76,7 @@ struct Cli { show_config: bool, #[structopt(short = "e", long = "explain")] - /// Show location of current config files + /// Explain selected count mode explain_mode: bool, #[structopt(short = "u", default_value = "none")]