From a9bb3b495254c46d7e446b267adba8de7ea486b6 Mon Sep 17 00:00:00 2001 From: qwjyh Date: Thu, 27 Jun 2024 06:55:43 +0900 Subject: [PATCH] pretty printing for `backup list` and `backup list --long` --- Cargo.lock | 17 +++++++++++ Cargo.toml | 1 + src/cmd_backup.rs | 72 ++++++++++++++++++++++++++++++++++++----------- src/util.rs | 17 ++++++++--- 4 files changed, 87 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index efc48ce..ce76cfb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -347,6 +347,16 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" +[[package]] +name = "colored" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8" +dependencies = [ + "lazy_static", + "windows-sys 0.48.0", +] + [[package]] name = "core-foundation-sys" version = "0.8.6" @@ -724,6 +734,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 +1851,7 @@ dependencies = [ "clap", "clap-verbosity-flag", "clap_complete", + "colored", "dirs", "dunce", "env_logger", diff --git a/Cargo.toml b/Cargo.toml index 8c7aa5a..3e1ef7a 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" +colored = "2" [dev-dependencies] assert_cmd = "2.0.14" diff --git a/src/cmd_backup.rs b/src/cmd_backup.rs index 618ad6c..10b13d4 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 colored::Colorize; use dunce::canonicalize; use git2::Repository; use unicode_width::UnicodeWidthStr; @@ -188,9 +189,22 @@ 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 name = match backup.last_backup() { + Some(log) => { + let time = Local::now() - log.datetime; + match time { + x if x < TimeDelta::days(14) => backup.name().normal(), + x if x < TimeDelta::days(28) => backup.name().magenta(), + x if x < TimeDelta::days(28 * 3) => backup.name().red(), + x if x < TimeDelta::days(180) => backup.name().red().bold(), + _ => backup.name().black().on_red(), + } + } + None => backup.name().normal(), + }; let src = backup.source().path(storages, device)?; let dest = backup.destination().path(storages, device)?; let cmd_name = backup.command().name(); @@ -199,28 +213,54 @@ fn write_backups_list( let time = Local::now() - log.datetime; util::format_summarized_duration(time) } - None => "---".to_string(), + None => "---".to_string().red(), }; - 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 = "src :".italic().bright_black(), + src = src.display() + )?; + writeln!( + writer, + "{s_dest} {dest}", + s_dest = "dest:".italic().bright_black(), dest = dest.display() )?; writeln!( writer, - " {cmd_name: Result { } } -pub fn format_summarized_duration(dt: chrono::Duration) -> String { +pub fn format_summarized_duration(dt: chrono::Duration) -> ColoredString { if dt.num_days() > 0 { - format!("{}d", dt.num_days()) + let days = format!("{}d", dt.num_days()); + match dt.num_days() { + x if x < 7 => days.green(), + x if x < 14 => days.yellow(), + x if x < 28 => days.magenta(), + x if x < 28 * 3 => days.red(), + x if x < 180 => days.red().bold(), + _ => days.black().on_red(), + } } else if dt.num_hours() > 0 { - format!("{}h", dt.num_hours()) + format!("{}h", dt.num_hours()).green() } else { - format!("{}min", dt.num_minutes()) + format!("{}min", dt.num_minutes()).green() } }