pretty printing for backup list and backup list --long

This commit is contained in:
qwjyh 2024-06-27 06:55:43 +09:00
parent a7c81d5976
commit a9bb3b4952
4 changed files with 87 additions and 20 deletions

17
Cargo.lock generated
View file

@ -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",

View file

@ -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"

View file

@ -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:<name_width$} [{dev:<dev_width$}] {src:<src_storage_width$} → {dest:<dest_storage_width$} {last_backup_elapsed}",
name = backup.name(),
src = backup.source().storage,
dest = backup.destination().storage,
)?;
if longprint {
let cmd_note = backup.command().note();
writeln!(writer, " src : {src:<src_width$}", src = src.display())?;
if !longprint {
writeln!(
writer,
" dest: {dest:<dest_width$}",
"{name:<name_width$} [{dev:<dev_width$}] {src:<src_storage_width$} → {dest:<dest_storage_width$} {last_backup_elapsed}",
dev = dev.blue(),
src = backup.source().storage,
dest = backup.destination().storage,
)?;
} else {
writeln!(
writer,
"[{dev:<dev_width$}] {name:<name_width$} {last_backup_elapsed}",
dev = dev.blue(),
name = name.bold(),
)?;
let last_backup_date = match backup.last_backup() {
Some(date) => 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:<cmd_name_width$}({note})",
note = cmd_note,
"{s_last} {last}",
s_last = "last:".italic().bright_black(),
last = last_backup_date,
)?;
writeln!(
writer,
"{s_cmd} {cmd_name}({note})",
s_cmd = "cmd :".italic().bright_black(),
cmd_name = cmd_name.underline(),
note = cmd_note.italic(),
)?;
writeln!(writer)?;
}
}
Ok(())

View file

@ -1,6 +1,7 @@
use std::path::{self, PathBuf};
use anyhow::{Context, Result};
use colored::{ColoredString, Colorize};
use crate::{
devices::Device,
@ -51,13 +52,21 @@ pub fn expand_tilde(path: PathBuf) -> Result<PathBuf> {
}
}
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()
}
}