paline 0.5.0

一个用 Rust 编写的代码行数统计命令行工具。递归扫描目录,按编程语言统计行数,并以彩色横向条形图在终端中可视化展示。
use owo_colors::OwoColorize;

pub enum Level {
    Info,
    Warning,
    Error,
}

#[derive(PartialEq, PartialOrd)]
pub enum Verbosity {
    Normal = 0,
    Verbose = 1,
    Debug = 2,
}

impl Verbosity {
    pub fn from_u8(verbosity: &u8) -> Self {
        match verbosity {
            0 => Verbosity::Normal,
            1 => Verbosity::Verbose,
            2 => Verbosity::Debug,
            _ => Verbosity::Debug,
        }
    }
}

pub struct Log {
    pub level: Level,
    pub text: String,
    pub verbose: Option<Verbosity>,
}

impl Log {
    pub fn new(level: Level, text: String, verbose: Option<Verbosity>) -> Self {
        Log {
            level,
            text,
            verbose,
        }
    }
}

pub fn logger(log: Log, verbose: Option<Verbosity>) {
    if verbose.unwrap_or(Verbosity::Debug) >= log.verbose.unwrap_or(Verbosity::Normal) {
        match log.level {
            Level::Info => {
                println!("{}: {}", "Info".green(), log.text);
            }
            Level::Warning => {
                println!("{}: {}", "Warning".yellow(), log.text);
            }
            Level::Error => {
                eprintln!("{}: {}", "Error".red(), log.text);
            }
        }
    }
}