add a --flat option to list and fish completion
This commit is contained in:
parent
a689a68267
commit
762520797a
|
@ -0,0 +1,16 @@
|
||||||
|
# tiempo does not accept files as arguments
|
||||||
|
complete --command t --no-files
|
||||||
|
|
||||||
|
# define all subcommands
|
||||||
|
set -l sheetcommandslong display kill sheet week month
|
||||||
|
set -l sheetcommandsshort d k s w m
|
||||||
|
set -l sheetcommandsall $sheetcommandslong $sheetcommandsshort
|
||||||
|
set -l commandslong archive backend configure edit in list now out resume
|
||||||
|
set -l commandsshort a b c e i l n o r
|
||||||
|
set -l subcommands $sheetcommandslong $commandslong $sheetcommandsshort $commandsshort
|
||||||
|
|
||||||
|
# add subcommands
|
||||||
|
complete --command t --condition "not __fish_seen_subcommand_from $subcommands" -a "$subcommands"
|
||||||
|
|
||||||
|
# complete sheet name
|
||||||
|
complete --command t --condition "__fish_seen_subcommand_from $sheetcommandsall" -a "(t l --all --flat)"
|
|
@ -19,6 +19,7 @@ use super::{Command, Facts};
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Args {
|
pub struct Args {
|
||||||
all: bool,
|
all: bool,
|
||||||
|
flat: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> TryFrom<&'a ArgMatches<'a>> for Args {
|
impl<'a> TryFrom<&'a ArgMatches<'a>> for Args {
|
||||||
|
@ -27,6 +28,7 @@ impl<'a> TryFrom<&'a ArgMatches<'a>> for Args {
|
||||||
fn try_from(matches: &ArgMatches) -> Result<Args> {
|
fn try_from(matches: &ArgMatches) -> Result<Args> {
|
||||||
Ok(Args {
|
Ok(Args {
|
||||||
all: matches.is_present("all"),
|
all: matches.is_present("all"),
|
||||||
|
flat: matches.is_present("flat"),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -49,9 +51,7 @@ impl<'a> Command<'a> for ListCommand {
|
||||||
} else {
|
} else {
|
||||||
streams.db.entries_all_visible(None, None)?
|
streams.db.entries_all_visible(None, None)?
|
||||||
};
|
};
|
||||||
|
|
||||||
let (mut entries, needs_warning) = entries_or_warning(entries, &streams.db)?;
|
let (mut entries, needs_warning) = entries_or_warning(entries, &streams.db)?;
|
||||||
|
|
||||||
let current = streams.db.current_sheet()?;
|
let current = streams.db.current_sheet()?;
|
||||||
let last = streams.db.last_sheet()?;
|
let last = streams.db.last_sheet()?;
|
||||||
|
|
||||||
|
@ -62,88 +62,99 @@ 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);
|
if args.flat {
|
||||||
let mut total_today = Duration::seconds(0);
|
let sheets: Vec<_> = entries
|
||||||
let mut total = Duration::seconds(0);
|
.into_iter()
|
||||||
|
.map(|e| e.sheet)
|
||||||
|
.unique()
|
||||||
|
.collect();
|
||||||
|
|
||||||
let sheets: Vec<_> = entries
|
streams.out.write_all(sheets.join("\n").as_bytes())?;
|
||||||
.into_iter()
|
streams.out.write(b"\n")?;
|
||||||
.group_by(|e| e.sheet.clone())
|
} else {
|
||||||
.into_iter()
|
let mut total_running = Duration::seconds(0);
|
||||||
.map(|(key, group)| {
|
let mut total_today = Duration::seconds(0);
|
||||||
let entries: Vec<_> = group.into_iter().collect();
|
let mut total = Duration::seconds(0);
|
||||||
let s_running = facts.now - entries.iter().find(|e| e.end.is_none()).map(|e| e.start).unwrap_or(facts.now);
|
|
||||||
let s_today = entries.iter().filter(|e| e.start > today).fold(Duration::seconds(0), |acc, e| {
|
|
||||||
acc + (e.end.unwrap_or(facts.now) - e.start)
|
|
||||||
});
|
|
||||||
let s_total = entries.into_iter().fold(Duration::seconds(0), |acc, e| {
|
|
||||||
acc + (e.end.unwrap_or(facts.now) - e.start)
|
|
||||||
});
|
|
||||||
|
|
||||||
total_running = total_running + s_running;
|
let sheets: Vec<_> = entries
|
||||||
total_today = total_today + s_today;
|
.into_iter()
|
||||||
total = total + s_total;
|
.group_by(|e| e.sheet.clone())
|
||||||
|
.into_iter()
|
||||||
|
.map(|(key, group)| {
|
||||||
|
let entries: Vec<_> = group.into_iter().collect();
|
||||||
|
let s_running = facts.now - entries.iter().find(|e| e.end.is_none()).map(|e| e.start).unwrap_or(facts.now);
|
||||||
|
let s_today = entries.iter().filter(|e| e.start > today).fold(Duration::seconds(0), |acc, e| {
|
||||||
|
acc + (e.end.unwrap_or(facts.now) - e.start)
|
||||||
|
});
|
||||||
|
let s_total = entries.into_iter().fold(Duration::seconds(0), |acc, e| {
|
||||||
|
acc + (e.end.unwrap_or(facts.now) - e.start)
|
||||||
|
});
|
||||||
|
|
||||||
(
|
total_running = total_running + s_running;
|
||||||
if current == key {
|
total_today = total_today + s_today;
|
||||||
"*"
|
total = total + s_total;
|
||||||
} else if last.as_ref() == Some(&key) {
|
|
||||||
"-"
|
|
||||||
} else {
|
|
||||||
""
|
|
||||||
},
|
|
||||||
|
|
||||||
key,
|
(
|
||||||
|
if current == key {
|
||||||
|
"*"
|
||||||
|
} else if last.as_ref() == Some(&key) {
|
||||||
|
"-"
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
},
|
||||||
|
|
||||||
format_duration(s_running),
|
key,
|
||||||
|
|
||||||
format_duration(s_today),
|
format_duration(s_running),
|
||||||
|
|
||||||
format_duration(s_total),
|
format_duration(s_today),
|
||||||
)
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let mut tabs = Tabulate::with_columns(vec![
|
format_duration(s_total),
|
||||||
// indicator of current or prev sheet
|
)
|
||||||
Col::new().min_width(1).and_alignment(Right),
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
// sheet name
|
let mut tabs = Tabulate::with_columns(vec![
|
||||||
Col::new().min_width(9).and_alignment(Left),
|
// indicator of current or prev sheet
|
||||||
|
Col::new().min_width(1).and_alignment(Right),
|
||||||
|
|
||||||
// running time
|
// sheet name
|
||||||
Col::new().min_width(9).and_alignment(Right)
|
Col::new().min_width(9).and_alignment(Left),
|
||||||
.color_if(Style::new().dimmed(), |s| s == "0:00:00")
|
|
||||||
.color_if(Style::new().bold(), |s| s != "0:00:00"),
|
|
||||||
|
|
||||||
// today
|
// running time
|
||||||
Col::new().min_width(9).and_alignment(Right)
|
Col::new().min_width(9).and_alignment(Right)
|
||||||
.color_if(Style::new().dimmed(), |s| s == "0:00:00")
|
.color_if(Style::new().dimmed(), |s| s == "0:00:00")
|
||||||
.color_if(Style::new().bold(), |s| s != "0:00:00"),
|
.color_if(Style::new().bold(), |s| s != "0:00:00"),
|
||||||
|
|
||||||
// accumulated
|
// today
|
||||||
Col::new().min_width(12).and_alignment(Right),
|
Col::new().min_width(9).and_alignment(Right)
|
||||||
]);
|
.color_if(Style::new().dimmed(), |s| s == "0:00:00")
|
||||||
|
.color_if(Style::new().bold(), |s| s != "0:00:00"),
|
||||||
|
|
||||||
tabs.feed(vec!["", "Timesheet", "Running", "Today", "Total Time"]);
|
// accumulated
|
||||||
tabs.separator(' ');
|
Col::new().min_width(12).and_alignment(Right),
|
||||||
|
]);
|
||||||
|
|
||||||
for sheet in sheets {
|
tabs.feed(vec!["", "Timesheet", "Running", "Today", "Total Time"]);
|
||||||
tabs.feed(vec![sheet.0.to_string(), sheet.1, sheet.2, sheet.3, sheet.4]);
|
tabs.separator(' ');
|
||||||
|
|
||||||
|
for sheet in sheets {
|
||||||
|
tabs.feed(vec![sheet.0.to_string(), sheet.1, sheet.2, sheet.3, sheet.4]);
|
||||||
|
}
|
||||||
|
|
||||||
|
tabs.separator('-');
|
||||||
|
|
||||||
|
tabs.feed(vec![
|
||||||
|
"".to_string(),
|
||||||
|
"".to_string(),
|
||||||
|
format_duration(total_running),
|
||||||
|
format_duration(total_today),
|
||||||
|
format_duration(total),
|
||||||
|
]);
|
||||||
|
|
||||||
|
streams.out.write_all(tabs.print(facts.env.stdout_is_tty).as_bytes())?;
|
||||||
}
|
}
|
||||||
|
|
||||||
tabs.separator('-');
|
|
||||||
|
|
||||||
tabs.feed(vec![
|
|
||||||
"".to_string(),
|
|
||||||
"".to_string(),
|
|
||||||
format_duration(total_running),
|
|
||||||
format_duration(total_today),
|
|
||||||
format_duration(total),
|
|
||||||
]);
|
|
||||||
|
|
||||||
streams.out.write_all(tabs.print(facts.env.stdout_is_tty).as_bytes())?;
|
|
||||||
|
|
||||||
warn_if_needed(&mut streams.err, needs_warning, &facts.env)?;
|
warn_if_needed(&mut streams.err, needs_warning, &facts.env)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -153,7 +164,7 @@ impl<'a> Command<'a> for ListCommand {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use chrono::{Utc, TimeZone};
|
use chrono::{Utc, TimeZone};
|
||||||
use pretty_assertions::assert_eq;
|
use pretty_assertions::assert_str_eq;
|
||||||
|
|
||||||
use crate::database::{SqliteDatabase, Database};
|
use crate::database::{SqliteDatabase, Database};
|
||||||
|
|
||||||
|
@ -180,7 +191,7 @@ mod tests {
|
||||||
|
|
||||||
ListCommand::handle(args, &mut streams, &facts).unwrap();
|
ListCommand::handle(args, &mut streams, &facts).unwrap();
|
||||||
|
|
||||||
assert_eq!(&String::from_utf8_lossy(&streams.out), " Timesheet Running Today Total Time
|
assert_str_eq!(&String::from_utf8_lossy(&streams.out), " Timesheet Running Today Total Time
|
||||||
|
|
||||||
sheet1 0:00:00 0:00:00 10:13:55
|
sheet1 0:00:00 0:00:00 10:13:55
|
||||||
* sheet2 0:00:00 0:00:00 0:00:00
|
* sheet2 0:00:00 0:00:00 0:00:00
|
||||||
|
@ -195,11 +206,12 @@ mod tests {
|
||||||
|
|
||||||
let args = Args {
|
let args = Args {
|
||||||
all: true,
|
all: true,
|
||||||
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
ListCommand::handle(args, &mut streams, &facts).unwrap();
|
ListCommand::handle(args, &mut streams, &facts).unwrap();
|
||||||
|
|
||||||
assert_eq!(&String::from_utf8_lossy(&streams.out), " Timesheet Running Today Total Time
|
assert_str_eq!(&String::from_utf8_lossy(&streams.out), " Timesheet Running Today Total Time
|
||||||
|
|
||||||
_archived 0:00:00 0:00:00 1:00:00
|
_archived 0:00:00 0:00:00 1:00:00
|
||||||
sheet1 0:00:00 0:00:00 10:13:55
|
sheet1 0:00:00 0:00:00 10:13:55
|
||||||
|
@ -223,16 +235,54 @@ mod tests {
|
||||||
|
|
||||||
ListCommand::handle(args, &mut streams, &facts).unwrap();
|
ListCommand::handle(args, &mut streams, &facts).unwrap();
|
||||||
|
|
||||||
assert_eq!(&String::from_utf8_lossy(&streams.out), " Timesheet Running Today Total Time
|
assert_str_eq!(&String::from_utf8_lossy(&streams.out), " 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
|
0:10:24 0:10:26 0:10:26
|
||||||
");
|
");
|
||||||
|
|
||||||
assert_eq!(
|
assert_str_eq!(
|
||||||
String::from_utf8_lossy(&streams.err),
|
String::from_utf8_lossy(&streams.err),
|
||||||
"[WARNING] You are using the old timetrap format, it is advised that you update your database using t migrate. To supress this warning set TIEMPO_SUPRESS_TIMETRAP_WARNING=1\n"
|
"[WARNING] You are using the old timetrap format, it is advised that you update your database using t migrate. To supress this warning set TIEMPO_SUPRESS_TIMETRAP_WARNING=1\n"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn flat_display() {
|
||||||
|
std::env::set_var("TZ", "CST+6");
|
||||||
|
|
||||||
|
let mut streams = Streams::fake(b"");
|
||||||
|
|
||||||
|
streams.db.set_current_sheet("sheet2").unwrap();
|
||||||
|
streams.db.set_last_sheet("sheet4").unwrap();
|
||||||
|
|
||||||
|
streams.db.entry_insert(Utc.ymd(2021, 1, 1).and_hms(0, 0, 0), Some(Utc.ymd(2021, 1, 1).and_hms(1, 0, 0)), None, "_archived").unwrap();
|
||||||
|
streams.db.entry_insert(Utc.ymd(2021, 1, 1).and_hms(0, 0, 0), Some(Utc.ymd(2021, 1, 1).and_hms(10,13, 55)), None, "sheet1").unwrap();
|
||||||
|
streams.db.entry_insert(Utc.ymd(2021, 1, 1).and_hms(0, 0, 0), Some(Utc.ymd(2021, 1, 1).and_hms(7, 39, 18)), None, "sheet3").unwrap();
|
||||||
|
streams.db.entry_insert(Utc.ymd(2021, 1, 1).and_hms(12, 0, 0), Some(Utc.ymd(2021, 1, 1).and_hms(13, 52, 45)), None, "sheet3").unwrap();
|
||||||
|
streams.db.entry_insert(Utc.ymd(2021, 1, 1).and_hms(12, 0, 0), None, None, "sheet4").unwrap();
|
||||||
|
|
||||||
|
let now = Utc.ymd(2021, 1, 1).and_hms(13, 52, 45);
|
||||||
|
let facts = Facts::new().with_now(now);
|
||||||
|
let args = Args {
|
||||||
|
flat: true,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
ListCommand::handle(args, &mut streams, &facts).unwrap();
|
||||||
|
|
||||||
|
assert_str_eq!(&String::from_utf8_lossy(&streams.out), "sheet1\nsheet2\nsheet3\nsheet4\n");
|
||||||
|
|
||||||
|
let facts = Facts::new().with_now(now);
|
||||||
|
let args = Args {
|
||||||
|
flat: true,
|
||||||
|
all: true,
|
||||||
|
};
|
||||||
|
streams.out.clear();
|
||||||
|
|
||||||
|
ListCommand::handle(args, &mut streams, &facts).unwrap();
|
||||||
|
|
||||||
|
assert_str_eq!(&String::from_utf8_lossy(&streams.out), "_archived\nsheet1\nsheet2\nsheet3\nsheet4\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -330,6 +330,9 @@ fn main() {
|
||||||
.arg(Arg::with_name("all")
|
.arg(Arg::with_name("all")
|
||||||
.short("a").long("all")
|
.short("a").long("all")
|
||||||
.help("List archive sheets also"))
|
.help("List archive sheets also"))
|
||||||
|
.arg(Arg::with_name("flat")
|
||||||
|
.short("f").long("flat")
|
||||||
|
.help("show only the sheet names"))
|
||||||
)
|
)
|
||||||
|
|
||||||
.subcommand(SubCommand::with_name("kill")
|
.subcommand(SubCommand::with_name("kill")
|
||||||
|
|
Loading…
Reference in New Issue