Compare commits

..

No commits in common. "eb08634d9d320f3ec8880c367a0efd7ba1ffc9f4" and "a9bb3b495254c46d7e446b267adba8de7ea486b6" have entirely different histories.

6 changed files with 55 additions and 71 deletions

19
Cargo.lock generated
View file

@ -348,16 +348,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422"
[[package]] [[package]]
name = "console" name = "colored"
version = "0.15.8" version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e1f83fc076bd6dd27517eacdf25fef6c4dfe5f1d7448bafaaf3a26f13b5e4eb" checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8"
dependencies = [ dependencies = [
"encode_unicode",
"lazy_static", "lazy_static",
"libc", "windows-sys 0.48.0",
"unicode-width",
"windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -467,12 +464,6 @@ version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b" checksum = "3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b"
[[package]]
name = "encode_unicode"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
[[package]] [[package]]
name = "env_filter" name = "env_filter"
version = "0.1.0" version = "0.1.0"
@ -1860,7 +1851,7 @@ dependencies = [
"clap", "clap",
"clap-verbosity-flag", "clap-verbosity-flag",
"clap_complete", "clap_complete",
"console", "colored",
"dirs", "dirs",
"dunce", "dunce",
"env_logger", "env_logger",

View file

@ -30,7 +30,7 @@ byte-unit = "5.1.4"
anyhow = "1.0" anyhow = "1.0"
pathdiff = "0.2.1" pathdiff = "0.2.1"
unicode-width = "0.1.13" unicode-width = "0.1.13"
console = "0.15" colored = "2"
[dev-dependencies] [dev-dependencies]
assert_cmd = "2.0.14" assert_cmd = "2.0.14"

View file

@ -6,7 +6,7 @@ use std::{
use anyhow::{anyhow, Context, Ok, Result}; use anyhow::{anyhow, Context, Ok, Result};
use chrono::{Local, TimeDelta}; use chrono::{Local, TimeDelta};
use console::Style; use colored::Colorize;
use dunce::canonicalize; use dunce::canonicalize;
use git2::Repository; use git2::Repository;
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
@ -154,17 +154,6 @@ pub fn cmd_backup_list(
Ok(()) 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 /// TODO: status printing
fn write_backups_list( fn write_backups_list(
mut writer: impl io::Write, mut writer: impl io::Write,
@ -203,27 +192,34 @@ fn write_backups_list(
"Couldn't find the device specified in the backup config: {}", "Couldn't find the device specified in the backup config: {}",
backup.name() 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 src = backup.source().path(storages, device)?;
let dest = backup.destination().path(storages, device)?; let dest = backup.destination().path(storages, device)?;
let cmd_name = backup.command().name(); let cmd_name = backup.command().name();
let (last_backup_elapsed, style_on_time_elapsed) = match backup.last_backup() { let last_backup_elapsed = match backup.last_backup() {
Some(log) => { Some(log) => {
let time = Local::now() - log.datetime; let time = Local::now() - log.datetime;
let s = util::format_summarized_duration(time); util::format_summarized_duration(time)
let style = duration_style(time);
(style.apply_to(s), style)
} }
None => { None => "---".to_string().red(),
let style = Style::new().red();
(style.apply_to("---".to_string()), style)
},
}; };
if !longprint { if !longprint {
writeln!( writeln!(
writer, writer,
"{name:<name_width$} [{dev:<dev_width$}] {src:<src_storage_width$} → {dest:<dest_storage_width$} {last_backup_elapsed}", "{name:<name_width$} [{dev:<dev_width$}] {src:<src_storage_width$} → {dest:<dest_storage_width$} {last_backup_elapsed}",
name = style_on_time_elapsed.apply_to(backup.name()), dev = dev.blue(),
dev = console::style(dev).blue(),
src = backup.source().storage, src = backup.source().storage,
dest = backup.destination().storage, dest = backup.destination().storage,
)?; )?;
@ -231,8 +227,8 @@ fn write_backups_list(
writeln!( writeln!(
writer, writer,
"[{dev:<dev_width$}] {name:<name_width$} {last_backup_elapsed}", "[{dev:<dev_width$}] {name:<name_width$} {last_backup_elapsed}",
dev = console::style(dev).blue(), dev = dev.blue(),
name = style_on_time_elapsed.bold().apply_to(backup.name()), name = name.bold(),
)?; )?;
let last_backup_date = match backup.last_backup() { let last_backup_date = match backup.last_backup() {
Some(date) => date.datetime.format("%Y-%m-%d %T").to_string(), Some(date) => date.datetime.format("%Y-%m-%d %T").to_string(),
@ -242,27 +238,27 @@ fn write_backups_list(
writeln!( writeln!(
writer, writer,
"{s_src} {src}", "{s_src} {src}",
s_src = console::style("src :").italic().bright().black(), s_src = "src :".italic().bright_black(),
src = src.display() src = src.display()
)?; )?;
writeln!( writeln!(
writer, writer,
"{s_dest} {dest}", "{s_dest} {dest}",
s_dest = console::style("dest:").italic().bright().black(), s_dest = "dest:".italic().bright_black(),
dest = dest.display() dest = dest.display()
)?; )?;
writeln!( writeln!(
writer, writer,
"{s_last} {last}", "{s_last} {last}",
s_last = console::style("last:").italic().bright().black(), s_last = "last:".italic().bright_black(),
last = last_backup_date, last = last_backup_date,
)?; )?;
writeln!( writeln!(
writer, writer,
"{s_cmd} {cmd_name}({note})", "{s_cmd} {cmd_name}({note})",
s_cmd = console::style("cmd :").italic().bright().black(), s_cmd = "cmd :".italic().bright_black(),
cmd_name = console::style(cmd_name).underlined(), cmd_name = cmd_name.underline(),
note = console::style(cmd_note).italic(), note = cmd_note.italic(),
)?; )?;
writeln!(writer)?; writeln!(writer)?;
} }

View file

@ -7,7 +7,6 @@ use std::{
use anyhow::{anyhow, Context, Result}; use anyhow::{anyhow, Context, Result};
use byte_unit::{Byte, UnitType}; use byte_unit::{Byte, UnitType};
use console::style;
use dunce::canonicalize; use dunce::canonicalize;
use git2::Repository; use git2::Repository;
use inquire::{Confirm, CustomType, Text}; use inquire::{Confirm, CustomType, Text};
@ -212,7 +211,7 @@ fn write_storages_list(
"-" "-"
} }
} else { } else {
"" " "
}; };
let path = storage.mount_path(device).map_or_else( let path = storage.mount_path(device).map_or_else(
|e| { |e| {
@ -228,24 +227,23 @@ fn write_storages_list(
} else { } else {
"" ""
}; };
let typestyle = storage.typestyle();
writeln!( writeln!(
writer, writer,
"{stype}{isremovable:<1}: {name:<name_width$} {size:>10} {parent:<name_width$} {path}", "{stype}{isremovable}: {name:<name_width$} {size:>10} {parent:<name_width$} {path}",
stype = typestyle.apply_to(storage.shorttypename()), stype = storage.shorttypename(),
isremovable = isremovable, isremovable = isremovable,
name = typestyle.apply_to(storage.name()), name = storage.name(),
size = size_str, size = size_str,
parent = console::style(parent_name).bright().black(), parent = parent_name,
path = path, path = path,
)?; )?;
if long_display { if long_display {
let note = match storage { let note = match storage {
Storage::Physical(s) => format!("kind: {}", s.kind()), Storage::Physical(s) => s.kind(),
Storage::SubDirectory(s) => s.notes.clone(), Storage::SubDirectory(s) => &s.notes,
Storage::Online(s) => s.provider.clone(), Storage::Online(s) => &s.provider,
}; };
writeln!(writer, " {}", style(note).italic())?; writeln!(writer, " {}", note)?;
} }
} }
Ok(()) Ok(())

View file

@ -1,6 +1,5 @@
//! Manipulates storages. //! Manipulates storages.
use console::{style, Style, StyledObject};
use crate::devices; use crate::devices;
use crate::storages::{ use crate::storages::{
directory::Directory, online_storage::OnlineStorage, directory::Directory, online_storage::OnlineStorage,
@ -10,7 +9,7 @@ use anyhow::{anyhow, Context, Result};
use clap::ValueEnum; use clap::ValueEnum;
use core::panic; use core::panic;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{collections::BTreeMap, fmt, fs, io, path}; use std::{collections::BTreeMap, fmt, fs, io, path, u64};
/// YAML file to store known storages.. /// YAML file to store known storages..
pub const STORAGESFILE: &str = "storages.yml"; pub const STORAGESFILE: &str = "storages.yml";
@ -51,14 +50,6 @@ impl Storage {
Self::Online(_) => "O", 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 { impl StorageExt for Storage {

View file

@ -1,7 +1,7 @@
use std::path::{self, PathBuf}; use std::path::{self, PathBuf};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use console::Style; use colored::{ColoredString, Colorize};
use crate::{ use crate::{
devices::Device, devices::Device,
@ -52,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 { 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 { } else if dt.num_hours() > 0 {
format!("{}h", dt.num_hours()) format!("{}h", dt.num_hours()).green()
} else { } else {
format!("{}min", dt.num_minutes()) format!("{}min", dt.num_minutes()).green()
} }
} }