implement t now
This commit is contained in:
parent
f025e69055
commit
7d53546646
|
@ -20,6 +20,7 @@ pub mod out;
|
|||
pub mod resume;
|
||||
pub mod backend;
|
||||
pub mod kill;
|
||||
pub mod now;
|
||||
|
||||
pub trait Command<'a> {
|
||||
type Args: TryFrom<&'a ArgMatches<'a>>;
|
||||
|
|
|
@ -41,7 +41,6 @@ impl<'a> Command<'a> for ListCommand {
|
|||
O: Write,
|
||||
E: Write,
|
||||
{
|
||||
// no sheet, list sheets
|
||||
let today = now.with_timezone(&Local).date().and_hms(0, 0, 0).with_timezone(&Utc);
|
||||
let entries = if args.all {
|
||||
db.entries_full(None, None)?
|
||||
|
@ -60,11 +59,6 @@ impl<'a> Command<'a> for ListCommand {
|
|||
id: 1, sheet: current.clone(), start: now, end: Some(now), note: None,
|
||||
});
|
||||
}
|
||||
if let Some(ref last) = last {
|
||||
entries.push(Entry {
|
||||
id: 2, sheet: last.clone(), start: now, end: Some(now), note: None,
|
||||
});
|
||||
}
|
||||
|
||||
entries.sort_unstable_by_key(|e| e.sheet.clone());
|
||||
|
||||
|
@ -102,7 +96,7 @@ impl<'a> Command<'a> for ListCommand {
|
|||
.collect();
|
||||
|
||||
let mut tabs = Tabulate::with_columns(vec![
|
||||
// indicator of current of prev sheet
|
||||
// indicator of current or prev sheet
|
||||
Col::min_width(1).and_alignment(Right),
|
||||
|
||||
// sheet name
|
||||
|
|
|
@ -0,0 +1,146 @@
|
|||
use std::convert::TryFrom;
|
||||
use std::io::Write;
|
||||
|
||||
use clap::ArgMatches;
|
||||
use chrono::{DateTime, Utc};
|
||||
|
||||
use crate::error::{Result, Error};
|
||||
use crate::database::Database;
|
||||
use crate::config::Config;
|
||||
use crate::old::{entries_or_warning, warn_if_needed};
|
||||
use crate::tabulate::{Tabulate, Col, Align::*};
|
||||
use crate::formatters::text::format_duration;
|
||||
|
||||
use super::Command;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Args {
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<&'a ArgMatches<'a>> for Args {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(_matches: &'a ArgMatches) -> Result<Args> {
|
||||
Ok(Args { })
|
||||
}
|
||||
}
|
||||
|
||||
pub struct NowCommand { }
|
||||
|
||||
impl<'a> Command<'a> for NowCommand {
|
||||
type Args = Args;
|
||||
|
||||
fn handle<D, O, E>(_args: Self::Args, db: &mut D, out: &mut O, err: &mut E, _config: &Config, now: DateTime<Utc>) -> Result<()>
|
||||
where
|
||||
D: Database,
|
||||
O: Write,
|
||||
E: Write,
|
||||
{
|
||||
let entries = db.running_entries()?;
|
||||
|
||||
let (entries, needs_warning) = entries_or_warning(entries, db)?;
|
||||
|
||||
let current = db.current_sheet()?;
|
||||
let last = db.last_sheet()?;
|
||||
|
||||
let mut tabs = Tabulate::with_columns(vec![
|
||||
// indicator of current or prev sheet
|
||||
Col::min_width(1).and_alignment(Right),
|
||||
|
||||
// sheet name
|
||||
Col::min_width(9).and_alignment(Left),
|
||||
|
||||
// running time
|
||||
Col::min_width(9).and_alignment(Right),
|
||||
|
||||
// activity
|
||||
Col::min_width(0).and_alignment(Left),
|
||||
]);
|
||||
|
||||
tabs.feed(vec!["".into(), "Timesheet".into(), "Running".into(), "Activity".into()]);
|
||||
tabs.separator(' ');
|
||||
|
||||
for entry in entries {
|
||||
tabs.feed(vec![
|
||||
if current.as_ref() == Some(&entry.sheet) {
|
||||
"*".into()
|
||||
} else if last.as_ref() == Some(&entry.sheet) {
|
||||
"-".into()
|
||||
} else {
|
||||
"".into()
|
||||
},
|
||||
entry.sheet,
|
||||
format_duration(now - entry.start),
|
||||
entry.note.unwrap_or("".into())
|
||||
]);
|
||||
}
|
||||
|
||||
out.write_all(tabs.print().as_bytes())?;
|
||||
|
||||
warn_if_needed(err, needs_warning)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use chrono::{Utc, TimeZone, Local};
|
||||
use pretty_assertions::assert_eq;
|
||||
use ansi_term::Color::Yellow;
|
||||
|
||||
use crate::database::{SqliteDatabase, Database};
|
||||
use crate::test_utils::PrettyString;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn list_sheets() {
|
||||
std::env::set_var("TZ", "UTC");
|
||||
|
||||
let mut db = SqliteDatabase::from_memory().unwrap();
|
||||
let mut out = Vec::new();
|
||||
let mut err = Vec::new();
|
||||
let config = Default::default();
|
||||
db.init().unwrap();
|
||||
db.set_current_sheet("sheet2").unwrap();
|
||||
db.set_last_sheet("sheet4").unwrap();
|
||||
|
||||
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".into()).unwrap();
|
||||
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".into()).unwrap();
|
||||
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".into()).unwrap();
|
||||
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".into()).unwrap();
|
||||
db.entry_insert(Utc.ymd(2021, 1, 1).and_hms(12, 0, 0), None, Some("some".into()), "sheet4".into()).unwrap();
|
||||
|
||||
let now = Utc.ymd(2021, 1, 1).and_hms(13, 52, 45);
|
||||
|
||||
NowCommand::handle(Default::default(), &mut db, &mut out, &mut err, &config, now).unwrap();
|
||||
|
||||
assert_eq!(PrettyString(&String::from_utf8_lossy(&out)), PrettyString(" Timesheet Running Activity
|
||||
|
||||
- sheet4 1:52:45 some
|
||||
"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn old_database() {
|
||||
let mut db = SqliteDatabase::from_path("assets/test_list_old_database.db").unwrap();
|
||||
let mut out = Vec::new();
|
||||
let mut err = Vec::new();
|
||||
let config = Default::default();
|
||||
|
||||
let now = Local.ymd(2021, 7, 16).and_hms(11, 30, 45);
|
||||
|
||||
NowCommand::handle(Default::default(), &mut db, &mut out, &mut err, &config, now.with_timezone(&Utc)).unwrap();
|
||||
|
||||
assert_eq!(PrettyString(&String::from_utf8_lossy(&out)), PrettyString(" Timesheet Running Activity
|
||||
|
||||
* default 0:10:24 que
|
||||
"));
|
||||
|
||||
assert_eq!(
|
||||
String::from_utf8_lossy(&err),
|
||||
format!("{} You are using the old timetrap format, it is advised that you update your database using t migrate\n", Yellow.bold().paint("[WARNING]")),
|
||||
);
|
||||
}
|
||||
}
|
|
@ -165,6 +165,10 @@ pub trait Database {
|
|||
Ok(self.entry_query("select * from entries where end is null and sheet=?1", &[&sheet])?.into_iter().next())
|
||||
}
|
||||
|
||||
fn running_entries(&self) -> Result<Vec<Entry>> {
|
||||
Ok(self.entry_query("select * from entries where end is null order by sheet asc", &[])?)
|
||||
}
|
||||
|
||||
fn last_checkout_of_sheet(&self, sheet: &str) -> Result<Option<Entry>> {
|
||||
Ok(self.entry_query("select * from entries where end is not null and sheet=?1 order by end desc limit 1", &[&sheet])?.into_iter().next())
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ use tiempo::commands::{
|
|||
today::TodayCommand, yesterday::YesterdayCommand, week::WeekCommand,
|
||||
month::MonthCommand, list::ListCommand, out::OutCommand,
|
||||
resume::ResumeCommand, backend::BackendCommand, kill::KillCommand,
|
||||
now::NowCommand,
|
||||
};
|
||||
|
||||
fn error_trap(matches: ArgMatches) -> error::Result<()> {
|
||||
|
@ -42,6 +43,7 @@ fn error_trap(matches: ArgMatches) -> error::Result<()> {
|
|||
("sheet", Some(matches)) => SheetCommand::handle(matches.try_into()?, &mut conn, &mut out, &mut err, &config, now),
|
||||
("list", Some(matches)) => ListCommand::handle(matches.try_into()?, &mut conn, &mut out, &mut err, &config, now),
|
||||
("kill", Some(matches)) => KillCommand::handle(matches.try_into()?, &mut conn, &mut out, &mut err, &config, now),
|
||||
("now", Some(matches)) => NowCommand::handle(matches.try_into()?, &mut conn, &mut out, &mut err, &config, now),
|
||||
|
||||
(cmd, _) => Err(error::Error::UnimplementedCommand(cmd.into())),
|
||||
}
|
||||
|
@ -285,6 +287,11 @@ fn main() {
|
|||
))
|
||||
)
|
||||
|
||||
.subcommand(SubCommand::with_name("now")
|
||||
.visible_alias("n")
|
||||
.about("Show all running entries")
|
||||
)
|
||||
|
||||
.get_matches();
|
||||
|
||||
if let Err(e) = error_trap(matches) {
|
||||
|
|
Loading…
Reference in New Issue