Compare commits

..

33 Commits

Author SHA1 Message Date
Bel LaPointe
12bdf43721 accept --precision to change logging 2024-02-21 14:28:50 -07:00
Bel LaPointe
a693776a59 oops rust evidently will do some rounding for prints 2023-12-19 11:25:06 -05:00
Bel LaPointe
d97be3123b add -v/--verbose 2023-12-18 08:24:28 -07:00
Bel LaPointe
f6da50ff6f fix computing duration for each task 2023-12-18 08:24:20 -07:00
Bel LaPointe
09e1c57f32 when building day's log, skip stubs 2023-12-13 13:22:12 -07:00
Bel LaPointe
fcb144b437 --log=--log || --since=... 2023-12-11 16:09:18 -07:00
Bel LaPointe
e6ceef2ead add -c/--clock 2023-12-11 08:25:01 -07:00
Bel LaPointe
7463ca2069 default since is today 2023-12-07 09:14:44 -08:00
Bel LaPointe
1235b38636 timestamps from local timezone instead of utc 2023-12-07 09:09:26 -08:00
Bel LaPointe
cf47c63bd7 --since as local timezone 2023-12-07 09:09:17 -08:00
Bel LaPointe
0f9711aaa1 per task gets 1.1 hours, per day gets rounded 1 hours 2023-11-29 06:38:24 -07:00
Bel LaPointe
326b79a3b1 dont do things by default 2023-11-29 06:31:50 -07:00
Bel LaPointe
caa09089b0 flags complete 2023-11-29 06:27:54 -07:00
Bel LaPointe
d580122f76 ok we even got start mark 2023-11-29 06:18:30 -07:00
Bel LaPointe
a96c5dc1f8 tbh yaml too many lines this is k 2023-11-29 06:11:08 -07:00
Bel LaPointe
b2c7df2361 tbh yaml too many lines this is k 2023-11-29 06:08:35 -07:00
Bel LaPointe
faa884a645 mvp i think 2023-11-29 06:07:02 -07:00
Bel LaPointe
d7a7a404ee mvp i think 2023-11-29 06:06:38 -07:00
Bel LaPointe
2fc3fc605b at least it runs 2023-11-29 05:51:49 -07:00
Bel LaPointe
b2df61a235 break 2023-11-27 16:00:09 -07:00
Bel LaPointe
38e6e19d7b tests pass 2023-11-27 10:14:55 -07:00
Bel LaPointe
631b4cab32 its manual testing but it is working 2023-11-27 09:30:05 -07:00
Bel LaPointe
482eba9fd4 ok we can main thats nice 2023-11-27 09:12:57 -07:00
Bel LaPointe
f82418db47 test tsheet.add(x, tag) 2023-11-27 08:59:01 -07:00
Bel LaPointe
673f511e3d rename .from to .since 2023-11-27 08:44:39 -07:00
Bel LaPointe
3f83364f8f rename Storage to TSheet 2023-11-27 08:41:24 -07:00
bel
0722a6fc3b marginal progression woooo 2023-11-26 23:32:52 -07:00
bel
6955b7857c closerrrrrr to actual code woo 2023-11-26 23:25:53 -07:00
bel
0f6f4edf88 unittest load save storage 2023-11-26 22:57:47 -07:00
bel
644f0b4c8a nonzero unittests pass 2023-11-26 22:43:56 -07:00
bel
3531323de0 the lone unittest runs woo 2023-11-26 22:25:08 -07:00
bel
d565e9d382 almost a unittest 2023-11-26 22:19:13 -07:00
bel
f81465534e testdata 2023-11-26 21:58:39 -07:00
7 changed files with 830 additions and 2 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
**/*.sw*
/target/

483
Cargo.lock generated Normal file
View File

@@ -0,0 +1,483 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "android-tzdata"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
[[package]]
name = "android_system_properties"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
dependencies = [
"libc",
]
[[package]]
name = "anstream"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2ab91ebe16eb252986481c5b62f6098f3b698a45e34b5b98200cf20dd2484a44"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87"
[[package]]
name = "anstyle-parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "317b9a89c1868f5ea6ff1d9539a69f45dffc21ce321ac1fd1160dfa48c8e2140"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0699d10d2f4d628a98ee7b57b289abbc98ff3bad977cb3152709d4bf2330628"
dependencies = [
"anstyle",
"windows-sys",
]
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "bumpalo"
version = "3.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec"
[[package]]
name = "cc"
version = "1.0.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0"
dependencies = [
"libc",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f2c685bad3eb3d45a01354cedb7d5faa66194d1d58ba6e267a8de788f79db38"
dependencies = [
"android-tzdata",
"iana-time-zone",
"js-sys",
"num-traits",
"wasm-bindgen",
"windows-targets",
]
[[package]]
name = "clap"
version = "4.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2275f18819641850fa26c89acc84d465c1bf91ce57bc2748b28c420473352f64"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07cdf1b148b25c1e1f7a42225e30a0d99a615cd4637eae7365548dd4529b95bc"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1"
[[package]]
name = "colorchoice"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
[[package]]
name = "core-foundation-sys"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa"
[[package]]
name = "equivalent"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "hashbrown"
version = "0.14.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604"
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "iana-time-zone"
version = "0.1.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8326b86b6cff230b97d0d312a6c40a60726df3332e721f72a1b035f451663b20"
dependencies = [
"android_system_properties",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"wasm-bindgen",
"windows-core",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
dependencies = [
"cc",
]
[[package]]
name = "indexmap"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f"
dependencies = [
"equivalent",
"hashbrown",
]
[[package]]
name = "itoa"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38"
[[package]]
name = "js-sys"
version = "0.3.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cee9c64da59eae3b50095c18d3e74f8b73c0b86d2792824ff01bbce68ba229ca"
dependencies = [
"wasm-bindgen",
]
[[package]]
name = "libc"
version = "0.2.150"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c"
[[package]]
name = "log"
version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]]
name = "num-traits"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c"
dependencies = [
"autocfg",
]
[[package]]
name = "once_cell"
version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
[[package]]
name = "proc-macro2"
version = "1.0.70"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
dependencies = [
"proc-macro2",
]
[[package]]
name = "ryu"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741"
[[package]]
name = "serde"
version = "1.0.193"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.193"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_yaml"
version = "0.9.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3cc7a1570e38322cfe4154732e5110f887ea57e22b76f4bfd32b5bdd3368666c"
dependencies = [
"indexmap",
"itoa",
"ryu",
"serde",
"unsafe-libyaml",
]
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "2.0.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "tsheet-cli"
version = "0.1.0"
dependencies = [
"chrono",
"clap",
"serde",
"serde_yaml",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "unsafe-libyaml"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f28467d3e1d3c6586d8f25fa243f544f5800fec42d97032474e17222c2b75cfa"
[[package]]
name = "utf8parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "wasm-bindgen"
version = "0.2.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ed0d4f68a3015cc185aff4db9506a015f4b96f95303897bfa23f846db54064e"
dependencies = [
"cfg-if",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b56f625e64f3a1084ded111c4d5f477df9f8c92df113852fa5a374dbda78826"
dependencies = [
"bumpalo",
"log",
"once_cell",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0162dbf37223cd2afce98f3d0785506dcb8d266223983e4b5b525859e6e182b2"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ab9b36309365056cd639da3134bf87fa8f3d86008abf99e612384a6eecd459f"
[[package]]
name = "windows-core"
version = "0.51.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1f8cf84f35d2db49a46868f947758c7a1138116f7fac3bc844f43ade1292e64"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"

View File

@@ -6,3 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
chrono = "0.4.31"
clap = { version = "4.4.8", features = ["derive"] }
serde = { version = "1.0.193", features = ["derive"] }
serde_yaml = "0.9.27"

View File

@@ -1,3 +1,324 @@
fn main() {
println!("Hello, world!");
use serde::{Serialize, Deserialize};
use std::io::{Read, Write};
use std::fs::File;
use std::time::{SystemTime, UNIX_EPOCH, Duration};
use std::ops::{Add, Sub};
use clap::Parser;
use chrono::{TimeZone, Local, Timelike};
#[derive(Debug, Parser)]
struct Flags {
#[arg(short = 'f', long = "file")]
f: String,
#[arg(short = 'l', long = "log")]
log: bool,
#[arg(short = 's', long = "since")]
since: Option<String>,
#[arg(short = 'a', long = "add")]
add: Option<String>,
#[arg(short = 't', long = "tag")]
tag: Option<String>,
#[arg(short = 'c', long = "clock")]
clock: bool,
#[arg(short = 'v', long = "verbose")]
verbose: bool,
#[arg(short = 'p', long = "precision")]
precision: Option<u32>,
}
fn main() {
let mut flags = Flags::parse();
flags.log = flags.log || flags.since.is_some();
clock(&flags.f, &flags.clock).unwrap();
add(&flags.f, &flags.add, &flags.tag).unwrap();
log(&flags.f, &flags.log, &flags.since, &flags.verbose, &flags.precision.unwrap_or(0)).unwrap();
}
fn clock(f: &String, clock: &bool) -> Result<(), String> {
match clock {
true => add(&f, &Some("".to_string()), &None),
false => Ok(()),
}
}
fn add(f: &String, x: &Option<String>, tag: &Option<String>) -> Result<(), String> {
match x {
Some(x) => {
let mut tsheet = load(&f)?;
tsheet.add(
x.to_string(),
tag.clone().unwrap_or("".to_string()),
);
save(&f, tsheet)?;
},
None => {},
};
Ok(())
}
#[derive(Debug, Serialize, Clone)]
struct Log {
t: String,
d: f32,
xs: Vec<LogX>,
}
#[derive(Debug, Serialize, Clone)]
struct LogX {
d: f32,
x: String,
}
fn log(f: &String, enabled: &bool, since: &Option<String>, verbose: &bool, precision: &u32) -> Result<(), String> {
if !enabled {
return Ok(());
}
let since = parse_time(since)?;
if *verbose {
eprintln!("since = {} ({})", system_time_to_unix_seconds(&since), timestamp(&system_time_to_unix_seconds(&since)));
}
let tsheet = load(&f)?;
let tsheet = tsheet.since(since);
let tsheet = tsheet.sorted();
if *verbose {
eprintln!("tsheet = {:?}", &tsheet);
}
let mut result = vec![];
let mut curr = Log{t: "".to_string(), d: 0.0, xs: vec![]};
for i in 0..tsheet.xs.len() {
let x = &tsheet.xs[i];
if *verbose {
eprintln!("{} != {}?", &curr.t, x.timestamp());
}
if curr.t != x.timestamp() {
if curr.xs.len() > 0 {
if *verbose {
eprintln!("push {:?}", &curr.xs);
}
result.push(curr.clone());
}
curr.xs.truncate(0);
curr.t = x.timestamp();
curr.d = 0.0;
}
let mut d = 1.0;
if x.x.len() == 0 {
d = 0.0;
} else if i > 0 {
d = ((tsheet.xs[i].t - tsheet.xs[i-1].t) as f32 / (60.0*60.0)) as f32;
}
if *verbose {
eprintln!("d={} x='{}'", &x.x, &d);
}
match x.x.len() {
0 => {},
_ => {
curr.t = x.timestamp();
curr.xs.push(LogX{d: d, x: x.x.clone()});
},
};
}
if curr.xs.len() > 0 {
if *verbose {
eprintln!("push {:?} (final)", &curr.xs);
}
result.push(curr.clone());
}
for i in result.iter_mut() {
i.d = i.xs.iter().map(|x| x.d).sum();
if *verbose {
eprintln!("{} = {:?}", &i.d, &i.xs.iter().map(|x| x.d).collect::<Vec<_>>());
}
}
for log in result {
for x in log.xs {
if x.x.len() > 0 {
match precision {
0 => println!("{} ({:.0}) {} ({:.1})", log.t, log.d, x.x, x.d),
1 => println!("{} ({:.1}) {} ({:.2})", log.t, log.d, x.x, x.d),
_ => println!("{} ({:.2}) {} ({:.3})", log.t, log.d, x.x, x.d),
}
}
}
}
Ok(())
}
fn parse_time(since: &Option<String>) -> Result<SystemTime, String> {
match since {
Some(since) => {
match chrono::NaiveDate::parse_from_str(since, "%Y-%m-%d") {
Ok(nd) => {
let ndt = nd.and_hms_opt(1, 1, 1).unwrap();
let dt = Local.from_local_datetime(&ndt).unwrap();
Ok(UNIX_EPOCH.add(
Duration::from_secs(dt.timestamp() as u64)
))
},
Err(msg) => Err(format!("failed to parse {}: {}", since, msg)),
}
},
None => Ok(SystemTime::now().sub(Duration::from_secs(Local::now().hour() as u64*60*60))),
}
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct TSheet {
xs: Vec<X>,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone, Ord, Eq, PartialOrd)]
struct X {
t: i64,
x: String,
tag: String,
}
fn save(path: &String, tsheet: TSheet) -> Result<(), String> {
match File::create(path.clone()) {
Ok(mut writer) => _save(&mut writer, tsheet),
Err(reason) => Err(format!("failed to open {} to save tsheet: {}", path, reason)),
}
}
fn _save(writer: &mut dyn Write, tsheet: TSheet) -> Result<(), String> {
let mut w = serde_yaml::Serializer::new(writer);
match tsheet.serialize(&mut w) {
Ok(_) => Ok(()),
Err(reason) => Err(format!("failed to serialize tsheet: {}", reason)),
}
}
fn load(path: &String) -> Result<TSheet, String> {
match File::open(path.clone()) {
Ok(mut reader) => _load(&mut reader),
Err(reason) => Err(format!("failed to read tsheet {}: {}", path, reason)),
}
}
fn _load(reader: &mut dyn Read) -> Result<TSheet, String> {
match serde_yaml::from_reader::<&mut dyn Read, TSheet>(reader) {
Ok(tsheet) => Ok(tsheet),
Err(err) => Err(format!("failed to parse tsheet: {}", err)),
}
}
#[cfg(test)]
mod test_save_load {
use super::*;
#[test]
fn test_empty() {
let got = _load(&mut "xs: []".as_bytes()).expect("failed to parse 'xs: []' tsheet");
assert_eq!(got, TSheet{xs: vec![]});
let mut w = vec![];
_save(&mut w, got).expect("failed saving tsheet to writer");
assert_eq!(String::from_utf8(w).unwrap(), "xs: []\n".to_string());
}
#[test]
fn test_testdata_standalone_yaml() {
let want = TSheet{xs: vec![
X{t: 1, x: "def".to_string(), tag: "abc".to_string()},
X{t: 2, x: "ghi".to_string(), tag: "".to_string()},
]};
assert_eq!(
load(&"./src/testdata/standalone.yaml".to_string()).expect("cant load standalone.yaml"),
want,
);
let mut w = vec![];
_save(&mut w, want).expect("failed saving tsheet to writer");
assert_eq!(String::from_utf8(w).unwrap(), "xs:\n- t: 1\n x: def\n tag: abc\n- t: 2\n x: ghi\n tag: ''\n".to_string());
}
}
impl TSheet {
fn since(&self, t: SystemTime) -> TSheet {
let mut result = TSheet{xs: vec![]};
self.xs.iter()
.filter(|x| x.ts() >= t)
.for_each(|x| result.xs.push(x.clone()));
result
}
fn add(&mut self, x: String, tag: String) {
self.xs.push(new_x(SystemTime::now(), x, tag));
}
fn sorted(&self) -> TSheet {
let mut result = TSheet{xs: self.xs.clone()};
result.xs.sort();
return result;
}
}
impl X {
fn ts(&self) -> SystemTime {
UNIX_EPOCH.add(Duration::from_secs(self.t.try_into().unwrap()))
}
fn timestamp(&self) -> String {
timestamp(&self.t)
}
}
fn system_time_to_unix_seconds(st: &SystemTime) -> i64 {
st.duration_since(UNIX_EPOCH).unwrap().as_secs() as i64
}
fn timestamp(t: &i64) -> String {
let dt = Local.timestamp_opt(*t, 0).unwrap();
dt.format("%Y-%m-%d").to_string()
}
fn new_x(t: SystemTime, x: String, tag: String) -> X {
X{
t: system_time_to_unix_seconds(&t),
x: x,
tag: tag,
}
}
#[cfg(test)]
mod test_tsheet {
use super::*;
#[test]
fn test_add() {
let mut given = TSheet{xs: vec![
X{t: 1, x: "def".to_string(), tag: "abc".to_string()},
]};
given.add("ghi".to_string(), "".to_string());
assert_eq!(given.xs.len(), 2);
assert!(given.xs[1].t != 1);
assert_eq!(given.xs[1].x, "ghi".to_string());
assert_eq!(given.xs[1].tag, "".to_string());
}
#[test]
fn test_since_date() {
let given = TSheet{xs: vec![
X{t: 1, x: "def".to_string(), tag: "abc".to_string()},
X{t: 2, x: "ghi".to_string(), tag: "".to_string()},
X{t: 3, x: "jkl".to_string(), tag: "".to_string()},
]};
let want = TSheet{xs: vec![
X{t: 2, x: "ghi".to_string(), tag: "".to_string()},
X{t: 3, x: "jkl".to_string(), tag: "".to_string()},
]};
let got = given.since(UNIX_EPOCH.add(Duration::from_secs(2)));
assert_eq!(got, want);
}
}

3
src/testdata/cmdline.txt vendored Normal file
View File

@@ -0,0 +1,3 @@
$ tsheet-cli -clock-in # adds "" for the timestamp
$ tsheet-cli -add def -tag abc # adds def with tag abc using same-day for duration OR 1h
$ tsheet-cli -log -since YYYY-MM-DD # prints standalone.txt inclusive since given time

9
src/testdata/standalone.txt vendored Normal file
View File

@@ -0,0 +1,9 @@
- t: 1969-12-31
d:
xs:
- d: 1
x: abc
xs:
- def
- d: 0
x: ghi

7
src/testdata/standalone.yaml vendored Normal file
View File

@@ -0,0 +1,7 @@
xs:
- t: 1
x: def
tag: abc
- t: 2
x: ghi
tag: ""