mirror of
https://github.com/qwjyh/xdbm
synced 2024-12-05 04:51:04 +09:00
switch coloring crate from colored to console
- console can handle Style separately
This commit is contained in:
parent
a9bb3b4952
commit
592226f1b6
5 changed files with 51 additions and 45 deletions
19
Cargo.lock
generated
19
Cargo.lock
generated
|
@ -348,13 +348,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422"
|
||||
|
||||
[[package]]
|
||||
name = "colored"
|
||||
version = "2.1.0"
|
||||
name = "console"
|
||||
version = "0.15.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8"
|
||||
checksum = "0e1f83fc076bd6dd27517eacdf25fef6c4dfe5f1d7448bafaaf3a26f13b5e4eb"
|
||||
dependencies = [
|
||||
"encode_unicode",
|
||||
"lazy_static",
|
||||
"windows-sys 0.48.0",
|
||||
"libc",
|
||||
"unicode-width",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -464,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"
|
||||
|
@ -1851,7 +1860,7 @@ dependencies = [
|
|||
"clap",
|
||||
"clap-verbosity-flag",
|
||||
"clap_complete",
|
||||
"colored",
|
||||
"console",
|
||||
"dirs",
|
||||
"dunce",
|
||||
"env_logger",
|
||||
|
|
|
@ -30,7 +30,7 @@ byte-unit = "5.1.4"
|
|||
anyhow = "1.0"
|
||||
pathdiff = "0.2.1"
|
||||
unicode-width = "0.1.13"
|
||||
colored = "2"
|
||||
console = "0.15"
|
||||
|
||||
[dev-dependencies]
|
||||
assert_cmd = "2.0.14"
|
||||
|
|
|
@ -6,7 +6,7 @@ use std::{
|
|||
|
||||
use anyhow::{anyhow, Context, Ok, Result};
|
||||
use chrono::{Local, TimeDelta};
|
||||
use colored::Colorize;
|
||||
use console::Style;
|
||||
use dunce::canonicalize;
|
||||
use git2::Repository;
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
@ -154,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,
|
||||
|
@ -192,34 +203,27 @@ fn write_backups_list(
|
|||
"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();
|
||||
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().red(),
|
||||
None => {
|
||||
let style = Style::new().red();
|
||||
(style.apply_to("---".to_string()), style)
|
||||
},
|
||||
};
|
||||
if !longprint {
|
||||
writeln!(
|
||||
writer,
|
||||
"{name:<name_width$} [{dev:<dev_width$}] {src:<src_storage_width$} → {dest:<dest_storage_width$} {last_backup_elapsed}",
|
||||
dev = dev.blue(),
|
||||
name = style_on_time_elapsed.apply_to(backup.name()),
|
||||
dev = console::style(dev).blue(),
|
||||
src = backup.source().storage,
|
||||
dest = backup.destination().storage,
|
||||
)?;
|
||||
|
@ -227,8 +231,8 @@ fn write_backups_list(
|
|||
writeln!(
|
||||
writer,
|
||||
"[{dev:<dev_width$}] {name:<name_width$} {last_backup_elapsed}",
|
||||
dev = dev.blue(),
|
||||
name = name.bold(),
|
||||
dev = console::style(dev).blue(),
|
||||
name = style_on_time_elapsed.bold().apply_to(backup.name()),
|
||||
)?;
|
||||
let last_backup_date = match backup.last_backup() {
|
||||
Some(date) => date.datetime.format("%Y-%m-%d %T").to_string(),
|
||||
|
@ -238,27 +242,27 @@ fn write_backups_list(
|
|||
writeln!(
|
||||
writer,
|
||||
"{s_src} {src}",
|
||||
s_src = "src :".italic().bright_black(),
|
||||
s_src = console::style("src :").italic().bright().black(),
|
||||
src = src.display()
|
||||
)?;
|
||||
writeln!(
|
||||
writer,
|
||||
"{s_dest} {dest}",
|
||||
s_dest = "dest:".italic().bright_black(),
|
||||
s_dest = console::style("dest:").italic().bright().black(),
|
||||
dest = dest.display()
|
||||
)?;
|
||||
writeln!(
|
||||
writer,
|
||||
"{s_last} {last}",
|
||||
s_last = "last:".italic().bright_black(),
|
||||
s_last = console::style("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(),
|
||||
s_cmd = console::style("cmd :").italic().bright().black(),
|
||||
cmd_name = console::style(cmd_name).underlined(),
|
||||
note = console::style(cmd_note).italic(),
|
||||
)?;
|
||||
writeln!(writer)?;
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ use std::{
|
|||
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use byte_unit::{Byte, UnitType};
|
||||
use console::Style;
|
||||
use dunce::canonicalize;
|
||||
use git2::Repository;
|
||||
use inquire::{Confirm, CustomType, Text};
|
||||
|
|
18
src/util.rs
18
src/util.rs
|
@ -1,7 +1,7 @@
|
|||
use std::path::{self, PathBuf};
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use colored::{ColoredString, Colorize};
|
||||
use console::Style;
|
||||
|
||||
use crate::{
|
||||
devices::Device,
|
||||
|
@ -52,21 +52,13 @@ pub fn expand_tilde(path: PathBuf) -> Result<PathBuf> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn format_summarized_duration(dt: chrono::Duration) -> ColoredString {
|
||||
pub fn format_summarized_duration(dt: chrono::Duration) -> String {
|
||||
if dt.num_days() > 0 {
|
||||
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(),
|
||||
}
|
||||
format!("{}d", dt.num_days())
|
||||
} else if dt.num_hours() > 0 {
|
||||
format!("{}h", dt.num_hours()).green()
|
||||
format!("{}h", dt.num_hours())
|
||||
} else {
|
||||
format!("{}min", dt.num_minutes()).green()
|
||||
format!("{}min", dt.num_minutes())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue