From eebe0098730de47b92fa0eb1729e582ab1830f50 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Thu, 9 Feb 2023 23:07:30 +0100 Subject: [PATCH] Use console crate for ansi colors in list command --- Cargo.lock | 2 ++ Cargo.toml | 2 ++ packages/cli/Cargo.toml | 2 ++ packages/cli/src/utils/listing.rs | 26 ++++++++++++++------------ packages/lib/Cargo.toml | 4 ++-- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 35cfacd..82742ed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -624,7 +624,9 @@ version = "0.3.0" dependencies = [ "anyhow", "clap", + "console", "full_moon", + "lazy_static", "lune", "regex", "reqwest", diff --git a/Cargo.toml b/Cargo.toml index c7bf22b..bd464f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,8 @@ panic = "abort" # Remove extra panic info [workspace.dependencies] +console = "0.15.5" +lazy_static = "1.4.0" serde_json = "1.0.91" serde = { version = "1.0.152", features = ["derive"] } diff --git a/packages/cli/Cargo.toml b/packages/cli/Cargo.toml index 228b22f..ade1bfa 100644 --- a/packages/cli/Cargo.toml +++ b/packages/cli/Cargo.toml @@ -18,6 +18,8 @@ path = "src/main.rs" lune = { path = "../lib" } +console.workspace = true +lazy_static.workspace = true serde_json.workspace = true serde.workspace = true tokio.workspace = true diff --git a/packages/cli/src/utils/listing.rs b/packages/cli/src/utils/listing.rs index efa96b4..307c980 100644 --- a/packages/cli/src/utils/listing.rs +++ b/packages/cli/src/utils/listing.rs @@ -1,16 +1,16 @@ use std::{cmp::Ordering, fmt::Write as _}; use anyhow::{bail, Result}; +use console::Style; +use lazy_static::lazy_static; use tokio::{fs, io}; use super::files::parse_lune_description_from_file; -// TODO: Use some crate for this instead -pub const COLOR_RESET: &str = if cfg!(test) { "" } else { "\x1B[0m" }; -pub const COLOR_BLUE: &str = if cfg!(test) { "" } else { "\x1B[34m" }; - -pub const STYLE_RESET: &str = if cfg!(test) { "" } else { "\x1B[22m" }; -pub const STYLE_DIM: &str = if cfg!(test) { "" } else { "\x1B[2m" }; +lazy_static! { + pub static ref COLOR_BLUE: Style = Style::new().blue(); + pub static ref STYLE_DIM: Style = Style::new().dim(); +} pub async fn find_lune_scripts() -> Result> { let mut lune_dir = fs::read_dir("lune").await; @@ -72,8 +72,8 @@ pub fn print_lune_scripts(scripts: Vec<(String, String)>) -> Result<()> { .fold(0, |acc, (file_name, _)| acc.max(file_name.len())); let script_with_description_exists = scripts.iter().any(|(_, desc)| !desc.is_empty()); // Pre-calculate some strings that will be used often - let prefix = format!("{STYLE_DIM}>{STYLE_RESET} "); - let separator = format!("{STYLE_DIM}-{STYLE_RESET}"); + let prefix = format!("{} ", COLOR_BLUE.apply_to('>')); + let separator = format!("{}", STYLE_DIM.apply_to('-')); // Write the entire output to a buffer, doing this instead of using individual // println! calls will ensure that no output get mixed up in between these lines let mut buffer = String::new(); @@ -87,13 +87,15 @@ pub fn print_lune_scripts(scripts: Vec<(String, String)>) -> Result<()> { let file_spacing = " ".repeat(file_name.len()); let line_spacing = " ".repeat(longest_file_name_len - file_name.len()); write!( - &mut buffer, - "\n{prefix}{file_name}{line_spacing} {separator} {COLOR_BLUE}{first_line}{COLOR_RESET}" - )?; + &mut buffer, + "\n{prefix}{file_name}{line_spacing} {separator} {}", + COLOR_BLUE.apply_to(first_line) + )?; for line in lines { write!( &mut buffer, - "\n{prefix}{file_spacing}{line_spacing} {COLOR_BLUE}{line}{COLOR_RESET}" + "\n{prefix}{file_spacing}{line_spacing} {}", + COLOR_BLUE.apply_to(line) )?; } } diff --git a/packages/lib/Cargo.toml b/packages/lib/Cargo.toml index 80e93f2..bf6b16f 100644 --- a/packages/lib/Cargo.toml +++ b/packages/lib/Cargo.toml @@ -15,15 +15,15 @@ path = "src/lib.rs" [dependencies] +console.workspace = true +lazy_static.workspace = true serde_json.workspace = true serde.workspace = true tokio.workspace = true reqwest.workspace = true -console = "0.15.5" dialoguer = "0.10.3" directories = "4.0.1" -lazy_static = "1.4.0" pin-project = "1.0.12" os_str_bytes = "6.4.1"