Use console crate for ansi colors in list command

This commit is contained in:
Filip Tibell 2023-02-09 23:07:30 +01:00
parent f18fe48008
commit eebe009873
No known key found for this signature in database
5 changed files with 22 additions and 14 deletions

2
Cargo.lock generated
View file

@ -624,7 +624,9 @@ version = "0.3.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clap", "clap",
"console",
"full_moon", "full_moon",
"lazy_static",
"lune", "lune",
"regex", "regex",
"reqwest", "reqwest",

View file

@ -21,6 +21,8 @@ panic = "abort" # Remove extra panic info
[workspace.dependencies] [workspace.dependencies]
console = "0.15.5"
lazy_static = "1.4.0"
serde_json = "1.0.91" serde_json = "1.0.91"
serde = { version = "1.0.152", features = ["derive"] } serde = { version = "1.0.152", features = ["derive"] }

View file

@ -18,6 +18,8 @@ path = "src/main.rs"
lune = { path = "../lib" } lune = { path = "../lib" }
console.workspace = true
lazy_static.workspace = true
serde_json.workspace = true serde_json.workspace = true
serde.workspace = true serde.workspace = true
tokio.workspace = true tokio.workspace = true

View file

@ -1,16 +1,16 @@
use std::{cmp::Ordering, fmt::Write as _}; use std::{cmp::Ordering, fmt::Write as _};
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use console::Style;
use lazy_static::lazy_static;
use tokio::{fs, io}; use tokio::{fs, io};
use super::files::parse_lune_description_from_file; use super::files::parse_lune_description_from_file;
// TODO: Use some crate for this instead lazy_static! {
pub const COLOR_RESET: &str = if cfg!(test) { "" } else { "\x1B[0m" }; pub static ref COLOR_BLUE: Style = Style::new().blue();
pub const COLOR_BLUE: &str = if cfg!(test) { "" } else { "\x1B[34m" }; pub static ref STYLE_DIM: Style = Style::new().dim();
}
pub const STYLE_RESET: &str = if cfg!(test) { "" } else { "\x1B[22m" };
pub const STYLE_DIM: &str = if cfg!(test) { "" } else { "\x1B[2m" };
pub async fn find_lune_scripts() -> Result<Vec<(String, String)>> { pub async fn find_lune_scripts() -> Result<Vec<(String, String)>> {
let mut lune_dir = fs::read_dir("lune").await; 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())); .fold(0, |acc, (file_name, _)| acc.max(file_name.len()));
let script_with_description_exists = scripts.iter().any(|(_, desc)| !desc.is_empty()); let script_with_description_exists = scripts.iter().any(|(_, desc)| !desc.is_empty());
// Pre-calculate some strings that will be used often // Pre-calculate some strings that will be used often
let prefix = format!("{STYLE_DIM}>{STYLE_RESET} "); let prefix = format!("{} ", COLOR_BLUE.apply_to('>'));
let separator = format!("{STYLE_DIM}-{STYLE_RESET}"); let separator = format!("{}", STYLE_DIM.apply_to('-'));
// Write the entire output to a buffer, doing this instead of using individual // 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 // println! calls will ensure that no output get mixed up in between these lines
let mut buffer = String::new(); 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 file_spacing = " ".repeat(file_name.len());
let line_spacing = " ".repeat(longest_file_name_len - file_name.len()); let line_spacing = " ".repeat(longest_file_name_len - file_name.len());
write!( write!(
&mut buffer, &mut buffer,
"\n{prefix}{file_name}{line_spacing} {separator} {COLOR_BLUE}{first_line}{COLOR_RESET}" "\n{prefix}{file_name}{line_spacing} {separator} {}",
)?; COLOR_BLUE.apply_to(first_line)
)?;
for line in lines { for line in lines {
write!( write!(
&mut buffer, &mut buffer,
"\n{prefix}{file_spacing}{line_spacing} {COLOR_BLUE}{line}{COLOR_RESET}" "\n{prefix}{file_spacing}{line_spacing} {}",
COLOR_BLUE.apply_to(line)
)?; )?;
} }
} }

View file

@ -15,15 +15,15 @@ path = "src/lib.rs"
[dependencies] [dependencies]
console.workspace = true
lazy_static.workspace = true
serde_json.workspace = true serde_json.workspace = true
serde.workspace = true serde.workspace = true
tokio.workspace = true tokio.workspace = true
reqwest.workspace = true reqwest.workspace = true
console = "0.15.5"
dialoguer = "0.10.3" dialoguer = "0.10.3"
directories = "4.0.1" directories = "4.0.1"
lazy_static = "1.4.0"
pin-project = "1.0.12" pin-project = "1.0.12"
os_str_bytes = "6.4.1" os_str_bytes = "6.4.1"