From ea0acf177ce1f4caec7fb07bebedbe42f8299c4b Mon Sep 17 00:00:00 2001 From: qwjyh <62229267+qwjyh@users.noreply.github.com> Date: Thu, 27 Jun 2024 18:06:27 +0900 Subject: [PATCH] pretty printing (#15) * pretty printing for `backup list` and `backup list --long` * switch coloring crate from colored to console - console can handle Style separately * Style for storage list --- Cargo.lock | 26 +++++++++++++++ Cargo.toml | 1 + src/cmd_backup.rs | 80 +++++++++++++++++++++++++++++++++++----------- src/cmd_storage.rs | 20 ++++++------ src/storages.rs | 11 ++++++- src/util.rs | 1 + 6 files changed, 111 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index efc48ce..acb12dd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -347,6 +347,19 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" +[[package]] +name = "console" +version = "0.15.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e1f83fc076bd6dd27517eacdf25fef6c4dfe5f1d7448bafaaf3a26f13b5e4eb" +dependencies = [ + "encode_unicode", + "lazy_static", + "libc", + "unicode-width", + "windows-sys 0.52.0", +] + [[package]] name = "core-foundation-sys" version = "0.8.6" @@ -454,6 +467,12 @@ version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b" +[[package]] +name = "encode_unicode" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" + [[package]] name = "env_filter" version = "0.1.0" @@ -724,6 +743,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + [[package]] name = "libc" version = "0.2.155" @@ -1835,6 +1860,7 @@ dependencies = [ "clap", "clap-verbosity-flag", "clap_complete", + "console", "dirs", "dunce", "env_logger", diff --git a/Cargo.toml b/Cargo.toml index 8c7aa5a..c11d975 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,6 +30,7 @@ byte-unit = "5.1.4" anyhow = "1.0" pathdiff = "0.2.1" unicode-width = "0.1.13" +console = "0.15" [dev-dependencies] assert_cmd = "2.0.14" diff --git a/src/cmd_backup.rs b/src/cmd_backup.rs index 618ad6c..bcfd42b 100644 --- a/src/cmd_backup.rs +++ b/src/cmd_backup.rs @@ -5,7 +5,8 @@ use std::{ }; use anyhow::{anyhow, Context, Ok, Result}; -use chrono::Local; +use chrono::{Local, TimeDelta}; +use console::Style; use dunce::canonicalize; use git2::Repository; use unicode_width::UnicodeWidthStr; @@ -153,6 +154,17 @@ pub fn cmd_backup_list( Ok(()) } +fn duration_style(time: TimeDelta) -> Style { + match time { + x if x < TimeDelta::days(7) => Style::new().green(), + x if x < TimeDelta::days(14) => Style::new().yellow(), + x if x < TimeDelta::days(28) => Style::new().magenta(), + x if x < TimeDelta::days(28 * 3) => Style::new().red(), + x if x < TimeDelta::days(180) => Style::new().red().bold(), + _ => Style::new().on_red().black(), + } +} + /// TODO: status printing fn write_backups_list( mut writer: impl io::Write, @@ -188,39 +200,71 @@ fn write_backups_list( // main printing for ((dev, _name), backup) in &backups { let device = backup.device(devices).context(format!( - "Couldn't find device specified in backup config {}", + "Couldn't find the device specified in the backup config: {}", backup.name() ))?; let src = backup.source().path(storages, device)?; let dest = backup.destination().path(storages, device)?; let cmd_name = backup.command().name(); - let last_backup_elapsed = match backup.last_backup() { + let (last_backup_elapsed, style_on_time_elapsed) = match backup.last_backup() { Some(log) => { let time = Local::now() - log.datetime; - util::format_summarized_duration(time) + let s = util::format_summarized_duration(time); + let style = duration_style(time); + (style.apply_to(s), style) } - None => "---".to_string(), + None => { + let style = Style::new().red(); + (style.apply_to("---".to_string()), style) + }, }; - writeln!( - writer, - "{name: date.datetime.format("%Y-%m-%d %T").to_string(), + None => "never".to_string(), + }; + let cmd_note = backup.command().note(); + writeln!( + writer, + "{s_src} {src}", + s_src = console::style("src :").italic().bright().black(), + src = src.display() + )?; + writeln!( + writer, + "{s_dest} {dest}", + s_dest = console::style("dest:").italic().bright().black(), dest = dest.display() )?; writeln!( writer, - " {cmd_name:10} {parent:10} {parent: s.kind(), - Storage::SubDirectory(s) => &s.notes, - Storage::Online(s) => &s.provider, + Storage::Physical(s) => format!("kind: {}", s.kind()), + Storage::SubDirectory(s) => s.notes.clone(), + Storage::Online(s) => s.provider.clone(), }; - writeln!(writer, " {}", note)?; + writeln!(writer, " {}", style(note).italic())?; } } Ok(()) diff --git a/src/storages.rs b/src/storages.rs index 8137546..7562b0e 100644 --- a/src/storages.rs +++ b/src/storages.rs @@ -1,5 +1,6 @@ //! Manipulates storages. +use console::{style, Style, StyledObject}; use crate::devices; use crate::storages::{ directory::Directory, online_storage::OnlineStorage, @@ -9,7 +10,7 @@ use anyhow::{anyhow, Context, Result}; use clap::ValueEnum; use core::panic; use serde::{Deserialize, Serialize}; -use std::{collections::BTreeMap, fmt, fs, io, path, u64}; +use std::{collections::BTreeMap, fmt, fs, io, path}; /// YAML file to store known storages.. pub const STORAGESFILE: &str = "storages.yml"; @@ -50,6 +51,14 @@ impl Storage { Self::Online(_) => "O", } } + + pub fn typestyle(&self) -> Style { + match self { + Storage::Physical(_) => Style::new().cyan(), + Storage::SubDirectory(_) => Style::new().yellow(), + Storage::Online(_) => Style::new().green(), + } + } } impl StorageExt for Storage { diff --git a/src/util.rs b/src/util.rs index 36c6714..820de33 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,6 +1,7 @@ use std::path::{self, PathBuf}; use anyhow::{Context, Result}; +use console::Style; use crate::{ devices::Device,