Compare commits
21 Commits
631b4cab32
...
v0.0.2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
12bdf43721 | ||
|
|
a693776a59 | ||
|
|
d97be3123b | ||
|
|
f6da50ff6f | ||
|
|
09e1c57f32 | ||
|
|
fcb144b437 | ||
|
|
e6ceef2ead | ||
|
|
7463ca2069 | ||
|
|
1235b38636 | ||
|
|
cf47c63bd7 | ||
|
|
0f9711aaa1 | ||
|
|
326b79a3b1 | ||
|
|
caa09089b0 | ||
|
|
d580122f76 | ||
|
|
a96c5dc1f8 | ||
|
|
b2c7df2361 | ||
|
|
faa884a645 | ||
|
|
d7a7a404ee | ||
|
|
2fc3fc605b | ||
|
|
b2df61a235 | ||
|
|
38e6e19d7b |
185
Cargo.lock
generated
185
Cargo.lock
generated
@@ -2,6 +2,21 @@
|
||||
# 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"
|
||||
@@ -50,6 +65,47 @@ dependencies = [
|
||||
"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"
|
||||
@@ -96,6 +152,12 @@ 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"
|
||||
@@ -114,6 +176,29 @@ 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"
|
||||
@@ -130,6 +215,42 @@ 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"
|
||||
@@ -208,6 +329,7 @@ dependencies = [
|
||||
name = "tsheet-cli"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"clap",
|
||||
"serde",
|
||||
"serde_yaml",
|
||||
@@ -231,6 +353,69 @@ 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"
|
||||
|
||||
@@ -6,6 +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"
|
||||
|
||||
149
src/main.rs
149
src/main.rs
@@ -4,6 +4,7 @@ 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 {
|
||||
@@ -21,15 +22,31 @@ struct Flags {
|
||||
|
||||
#[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 flags = Flags::parse();
|
||||
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.since).unwrap();
|
||||
log(&flags.f, &flags.log, &flags.since, &flags.verbose, &flags.precision.unwrap_or(0)).unwrap();
|
||||
}
|
||||
|
||||
println!("{:?}", flags);
|
||||
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> {
|
||||
@@ -47,20 +64,111 @@ fn add(f: &String, x: &Option<String>, tag: &Option<String>) -> Result<(), Strin
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn log(f: &String, since: &Option<String>) -> Result<(), String> {
|
||||
#[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);
|
||||
println!("{:?}", tsheet);
|
||||
Err("not impl".to_string())
|
||||
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) => {
|
||||
Err("not impl".to_string())
|
||||
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)
|
||||
))
|
||||
},
|
||||
None => Ok(SystemTime::now().sub(Duration::from_secs(60*60*24*7))),
|
||||
Err(msg) => Err(format!("failed to parse {}: {}", since, msg)),
|
||||
}
|
||||
},
|
||||
None => Ok(SystemTime::now().sub(Duration::from_secs(Local::now().hour() as u64*60*60))),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,7 +177,7 @@ struct TSheet {
|
||||
xs: Vec<X>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone, Ord, Eq, PartialOrd)]
|
||||
struct X {
|
||||
t: i64,
|
||||
x: String,
|
||||
@@ -126,7 +234,7 @@ mod test_save_load {
|
||||
X{t: 2, x: "ghi".to_string(), tag: "".to_string()},
|
||||
]};
|
||||
assert_eq!(
|
||||
load("./src/testdata/standalone.yaml".to_string()).expect("cant load standalone.yaml"),
|
||||
load(&"./src/testdata/standalone.yaml".to_string()).expect("cant load standalone.yaml"),
|
||||
want,
|
||||
);
|
||||
|
||||
@@ -148,17 +256,36 @@ impl TSheet {
|
||||
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: t.duration_since(UNIX_EPOCH).unwrap().as_secs() as i64,
|
||||
t: system_time_to_unix_seconds(&t),
|
||||
x: x,
|
||||
tag: tag,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user