Skip to content

Commit

Permalink
add colored error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Orciument committed Apr 14, 2024
1 parent 057d768 commit 681209b
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 13 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ edition = "2021"
ignore = "0.4.20"
quicli = "0.4.0"
structopt = "0.3.26"
colored = "2.1.0"
3 changes: 2 additions & 1 deletion src/grafzahl/count_modes/count_mode.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand Down Expand Up @@ -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()))
};
}
}
15 changes: 12 additions & 3 deletions src/grafzahl/count_modes/custom_modes/line_type.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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()
)
}
}

7 changes: 4 additions & 3 deletions src/grafzahl/generic_counter.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -79,7 +80,7 @@ fn count_file<CountMode: Countable>(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());
Expand All @@ -94,7 +95,7 @@ fn count_file<CountMode: Countable>(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())
Expand All @@ -106,7 +107,7 @@ fn count_folder<CountMode: Countable>(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());
Expand Down
5 changes: 3 additions & 2 deletions src/grafzahl/io_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()),
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/grafzahl/language/languages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -75,8 +76,9 @@ pub fn import_languages() -> Vec<Language> {
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;
}
};
Expand Down
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()))
};
}
}
Expand Down Expand Up @@ -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")]
Expand Down

0 comments on commit 681209b

Please sign in to comment.