dim some texts in t list
This commit is contained in:
parent
65751e9aad
commit
f86e936dd2
|
@ -4,6 +4,7 @@ use std::io::Write;
|
||||||
use clap::ArgMatches;
|
use clap::ArgMatches;
|
||||||
use chrono::{DateTime, Utc, Duration, Local};
|
use chrono::{DateTime, Utc, Duration, Local};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
use ansi_term::Style;
|
||||||
|
|
||||||
use crate::error::{Error, Result};
|
use crate::error::{Error, Result};
|
||||||
use crate::database::Database;
|
use crate::database::Database;
|
||||||
|
@ -62,12 +63,27 @@ impl<'a> Command<'a> for ListCommand {
|
||||||
|
|
||||||
entries.sort_unstable_by_key(|e| e.sheet.clone());
|
entries.sort_unstable_by_key(|e| e.sheet.clone());
|
||||||
|
|
||||||
|
let mut total_running = Duration::seconds(0);
|
||||||
|
let mut total_today = Duration::seconds(0);
|
||||||
|
let mut total = Duration::seconds(0);
|
||||||
|
|
||||||
let sheets: Vec<_> = entries
|
let sheets: Vec<_> = entries
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.group_by(|e| e.sheet.clone())
|
.group_by(|e| e.sheet.clone())
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(key, group)| {
|
.map(|(key, group)| {
|
||||||
let entries: Vec<_> = group.into_iter().collect();
|
let entries: Vec<_> = group.into_iter().collect();
|
||||||
|
let s_running = now - entries.iter().find(|e| e.end.is_none()).map(|e| e.start).unwrap_or(now);
|
||||||
|
let s_today = entries.iter().filter(|e| e.start > today).fold(Duration::seconds(0), |acc, e| {
|
||||||
|
acc + (e.end.unwrap_or(now) - e.start)
|
||||||
|
});
|
||||||
|
let s_total = entries.into_iter().fold(Duration::seconds(0), |acc, e| {
|
||||||
|
acc + (e.end.unwrap_or(now) - e.start)
|
||||||
|
});
|
||||||
|
|
||||||
|
total_running = total_running + s_running;
|
||||||
|
total_today = total_today + s_today;
|
||||||
|
total = total + s_total;
|
||||||
|
|
||||||
(
|
(
|
||||||
if current.as_ref() == Some(&key) {
|
if current.as_ref() == Some(&key) {
|
||||||
|
@ -77,22 +93,14 @@ impl<'a> Command<'a> for ListCommand {
|
||||||
} else {
|
} else {
|
||||||
"".into()
|
"".into()
|
||||||
},
|
},
|
||||||
|
|
||||||
key,
|
key,
|
||||||
|
|
||||||
format_duration(
|
format_duration(s_running),
|
||||||
now - entries.iter().find(|e| e.end.is_none()).map(|e| e.start).unwrap_or(now)
|
|
||||||
),
|
|
||||||
|
|
||||||
format_duration(
|
format_duration(s_today),
|
||||||
entries.iter().filter(|e| e.start > today).fold(Duration::seconds(0), |acc, e| {
|
|
||||||
acc + (e.end.unwrap_or(now) - e.start)
|
|
||||||
})
|
|
||||||
),
|
|
||||||
|
|
||||||
// total
|
format_duration(s_total),
|
||||||
format_duration(entries.into_iter().fold(Duration::seconds(0), |acc, e| {
|
|
||||||
acc + (e.end.unwrap_or(now) - e.start)
|
|
||||||
})),
|
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
@ -105,10 +113,10 @@ impl<'a> Command<'a> for ListCommand {
|
||||||
Col::min_width(9).and_alignment(Left),
|
Col::min_width(9).and_alignment(Left),
|
||||||
|
|
||||||
// running time
|
// running time
|
||||||
Col::min_width(9).and_alignment(Right),
|
Col::min_width(9).and_alignment(Right).color_if(Style::new().dimmed(), |s| s == "0:00:00"),
|
||||||
|
|
||||||
// today
|
// today
|
||||||
Col::min_width(9).and_alignment(Right),
|
Col::min_width(9).and_alignment(Right).color_if(Style::new().dimmed(), |s| s == "0:00:00"),
|
||||||
|
|
||||||
// accumulated
|
// accumulated
|
||||||
Col::min_width(12).and_alignment(Right),
|
Col::min_width(12).and_alignment(Right),
|
||||||
|
@ -121,6 +129,16 @@ impl<'a> Command<'a> for ListCommand {
|
||||||
tabs.feed(vec![sheet.0, sheet.1, sheet.2, sheet.3, sheet.4]);
|
tabs.feed(vec![sheet.0, sheet.1, sheet.2, sheet.3, sheet.4]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tabs.separator('-');
|
||||||
|
|
||||||
|
tabs.feed(vec![
|
||||||
|
"".into(),
|
||||||
|
"".into(),
|
||||||
|
format_duration(total_running),
|
||||||
|
format_duration(total_today),
|
||||||
|
format_duration(total),
|
||||||
|
]);
|
||||||
|
|
||||||
out.write_all(tabs.print().as_bytes())?;
|
out.write_all(tabs.print().as_bytes())?;
|
||||||
|
|
||||||
warn_if_needed(err, needs_warning)?;
|
warn_if_needed(err, needs_warning)?;
|
||||||
|
@ -133,7 +151,7 @@ impl<'a> Command<'a> for ListCommand {
|
||||||
mod tests {
|
mod tests {
|
||||||
use chrono::{Utc, TimeZone};
|
use chrono::{Utc, TimeZone};
|
||||||
use pretty_assertions::assert_eq;
|
use pretty_assertions::assert_eq;
|
||||||
use ansi_term::Color::Yellow;
|
use ansi_term::{Color::Yellow, Style};
|
||||||
|
|
||||||
use crate::database::{SqliteDatabase, Database};
|
use crate::database::{SqliteDatabase, Database};
|
||||||
use crate::test_utils::Ps;
|
use crate::test_utils::Ps;
|
||||||
|
@ -163,13 +181,15 @@ mod tests {
|
||||||
|
|
||||||
ListCommand::handle(args, &mut db, &mut out, &mut err, &config, now).unwrap();
|
ListCommand::handle(args, &mut db, &mut out, &mut err, &config, now).unwrap();
|
||||||
|
|
||||||
assert_eq!(Ps(&String::from_utf8_lossy(&out)), Ps(" Timesheet Running Today Total Time
|
assert_eq!(Ps(&String::from_utf8_lossy(&out)), Ps(&format!(" Timesheet Running Today Total Time
|
||||||
|
|
||||||
sheet1 0:00:00 0:00:00 10:13:55
|
sheet1 {0} {0} 10:13:55
|
||||||
* sheet2 0:00:00 0:00:00 0:00:00
|
* sheet2 {0} {0} 0:00:00
|
||||||
sheet3 0:00:00 1:52:45 9:32:03
|
sheet3 {0} 1:52:45 9:32:03
|
||||||
- sheet4 1:52:45 1:52:45 1:52:45
|
- sheet4 1:52:45 1:52:45 1:52:45
|
||||||
"));
|
--------------------------------------------
|
||||||
|
1:52:45 3:45:30 21:38:43
|
||||||
|
", Style::new().dimmed().paint(" 0:00:00"))));
|
||||||
|
|
||||||
// now show all the sheets
|
// now show all the sheets
|
||||||
let mut out = Vec::new();
|
let mut out = Vec::new();
|
||||||
|
@ -180,14 +200,16 @@ mod tests {
|
||||||
|
|
||||||
ListCommand::handle(args, &mut db, &mut out, &mut err, &config, now).unwrap();
|
ListCommand::handle(args, &mut db, &mut out, &mut err, &config, now).unwrap();
|
||||||
|
|
||||||
assert_eq!(Ps(&String::from_utf8_lossy(&out)), Ps(" Timesheet Running Today Total Time
|
assert_eq!(Ps(&String::from_utf8_lossy(&out)), Ps(&format!(" Timesheet Running Today Total Time
|
||||||
|
|
||||||
_archived 0:00:00 0:00:00 1:00:00
|
_archived {0} {0} 1:00:00
|
||||||
sheet1 0:00:00 0:00:00 10:13:55
|
sheet1 {0} {0} 10:13:55
|
||||||
* sheet2 0:00:00 0:00:00 0:00:00
|
* sheet2 {0} {0} 0:00:00
|
||||||
sheet3 0:00:00 1:52:45 9:32:03
|
sheet3 {0} 1:52:45 9:32:03
|
||||||
- sheet4 1:52:45 1:52:45 1:52:45
|
- sheet4 1:52:45 1:52:45 1:52:45
|
||||||
"));
|
--------------------------------------------
|
||||||
|
1:52:45 3:45:30 22:38:43
|
||||||
|
", Style::new().dimmed().paint(" 0:00:00"))));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -205,6 +227,8 @@ mod tests {
|
||||||
assert_eq!(Ps(&String::from_utf8_lossy(&out)), Ps(" Timesheet Running Today Total Time
|
assert_eq!(Ps(&String::from_utf8_lossy(&out)), Ps(" Timesheet Running Today Total Time
|
||||||
|
|
||||||
* default 0:10:24 0:10:26 0:10:26
|
* default 0:10:24 0:10:26 0:10:26
|
||||||
|
--------------------------------------------
|
||||||
|
0:10:24 0:10:26 0:10:26
|
||||||
"));
|
"));
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
|
#![allow(clippy::type_complexity)]
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
|
use ansi_term::Style;
|
||||||
|
|
||||||
fn lpad(s: &str, len: usize) -> String {
|
fn lpad(s: &str, len: usize) -> String {
|
||||||
let padding = " ".repeat(len.saturating_sub(s.chars().count()));
|
let padding = " ".repeat(len.saturating_sub(s.chars().count()));
|
||||||
|
|
||||||
|
@ -24,11 +27,12 @@ pub enum Align {
|
||||||
|
|
||||||
use Align::*;
|
use Align::*;
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Col {
|
pub struct Col {
|
||||||
min_width: usize,
|
min_width: usize,
|
||||||
max_width: Option<usize>,
|
max_width: Option<usize>,
|
||||||
align: Align,
|
align: Align,
|
||||||
|
conditonal_styles: Vec<(Style, fn(&str) -> bool)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Col {
|
impl Col {
|
||||||
|
@ -37,6 +41,7 @@ impl Col {
|
||||||
min_width: 0,
|
min_width: 0,
|
||||||
align: Align::Left,
|
align: Align::Left,
|
||||||
max_width: None,
|
max_width: None,
|
||||||
|
conditonal_styles: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,6 +50,7 @@ impl Col {
|
||||||
min_width: size,
|
min_width: size,
|
||||||
align: Align::Left,
|
align: Align::Left,
|
||||||
max_width: None,
|
max_width: None,
|
||||||
|
conditonal_styles: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,6 +67,17 @@ impl Col {
|
||||||
..self
|
..self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn color_if(self, style: Style, f: fn(&str) -> bool) -> Col {
|
||||||
|
let mut conditonal_styles = self.conditonal_styles;
|
||||||
|
|
||||||
|
conditonal_styles.push((style, f));
|
||||||
|
|
||||||
|
Col {
|
||||||
|
conditonal_styles,
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Col {
|
impl Default for Col {
|
||||||
|
@ -146,10 +163,20 @@ impl Tabulate {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
DataOrSep::Data(d) => {
|
DataOrSep::Data(d) => {
|
||||||
d.into_iter().zip(widths.iter()).zip(cols.iter()).map(|((d, &w), &c)| {
|
d.into_iter().zip(widths.iter()).zip(cols.iter()).map(|((d, &w), c)| {
|
||||||
match c.align {
|
let style = c.conditonal_styles.iter().find(|(_s, f)| {
|
||||||
|
f(&d)
|
||||||
|
}).map(|(s, _f)| s);
|
||||||
|
|
||||||
|
let s = match c.align {
|
||||||
Left => rpad(&d, w),
|
Left => rpad(&d, w),
|
||||||
Right => lpad(&d, w),
|
Right => lpad(&d, w),
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(style) = style {
|
||||||
|
style.paint(s).to_string()
|
||||||
|
} else {
|
||||||
|
s
|
||||||
}
|
}
|
||||||
}).collect::<Vec<_>>().join(" ").trim_end().to_string() + "\n"
|
}).collect::<Vec<_>>().join(" ").trim_end().to_string() + "\n"
|
||||||
},
|
},
|
||||||
|
@ -380,4 +407,22 @@ adiós
|
||||||
ta güeno
|
ta güeno
|
||||||
"));
|
"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn add_a_color_condition() {
|
||||||
|
let mut tabs = Tabulate::with_columns(vec![
|
||||||
|
Col::new().color_if(Style::new().dimmed(), |val| {
|
||||||
|
val == "key"
|
||||||
|
}),
|
||||||
|
Col::new(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
tabs.feed(vec!["foo".into(), "key".into()]);
|
||||||
|
tabs.feed(vec!["key".into(), "foo".into()]);
|
||||||
|
|
||||||
|
assert_eq!(tabs.print(), format!("\
|
||||||
|
foo key
|
||||||
|
{} foo
|
||||||
|
", Style::new().dimmed().paint("key")));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue