cargo fmt
parent
d7af07b275
commit
a6193fcb16
|
@ -1,6 +1,4 @@
|
||||||
use std::{
|
use std::str::FromStr;
|
||||||
str::FromStr,
|
|
||||||
};
|
|
||||||
|
|
||||||
type Result<T> = std::result::Result<T, crate::error::Error>;
|
type Result<T> = std::result::Result<T, crate::error::Error>;
|
||||||
|
|
||||||
|
@ -24,10 +22,7 @@ impl Default for Config {
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
pub fn from_args<I: Iterator<Item = String>>(mut args: I) -> Result<Self> {
|
pub fn from_args<I: Iterator<Item = String>>(mut args: I) -> Result<Self> {
|
||||||
use crate::error::Error::{
|
use crate::error::Error::{CannotParseArg, ParamNeedsArg};
|
||||||
CannotParseArg,
|
|
||||||
ParamNeedsArg,
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut that = Self::default();
|
let mut that = Self::default();
|
||||||
|
|
||||||
|
@ -36,15 +31,14 @@ impl Config {
|
||||||
while let Some(arg) = args.next() {
|
while let Some(arg) = args.next() {
|
||||||
if arg == "--interval-secs" {
|
if arg == "--interval-secs" {
|
||||||
let val = args.next().ok_or(ParamNeedsArg("--interval-secs"))?;
|
let val = args.next().ok_or(ParamNeedsArg("--interval-secs"))?;
|
||||||
let val = u64::from_str (&val).map_err (|_| CannotParseArg ("--interval-secs <u64>"))?;
|
let val =
|
||||||
|
u64::from_str(&val).map_err(|_| CannotParseArg("--interval-secs <u64>"))?;
|
||||||
that.interval_secs = val;
|
that.interval_secs = val;
|
||||||
}
|
} else if arg == "--offset-secs" {
|
||||||
else if arg == "--offset-secs" {
|
|
||||||
let val = args.next().ok_or(ParamNeedsArg("--offset-secs"))?;
|
let val = args.next().ok_or(ParamNeedsArg("--offset-secs"))?;
|
||||||
let val = u64::from_str(&val).map_err(|_| CannotParseArg("--offset-secs <u64>"))?;
|
let val = u64::from_str(&val).map_err(|_| CannotParseArg("--offset-secs <u64>"))?;
|
||||||
that.offset_secs = val;
|
that.offset_secs = val;
|
||||||
}
|
} else if arg == "--prompt" {
|
||||||
else if arg == "--prompt" {
|
|
||||||
let val = args.next().ok_or(ParamNeedsArg("--prompt"))?;
|
let val = args.next().ok_or(ParamNeedsArg("--prompt"))?;
|
||||||
that.prompt = val;
|
that.prompt = val;
|
||||||
}
|
}
|
||||||
|
@ -57,7 +51,5 @@ impl Config {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
#[test]
|
#[test]
|
||||||
fn parse () {
|
fn parse() {}
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
46
src/main.rs
46
src/main.rs
|
@ -1,29 +1,13 @@
|
||||||
use std::{
|
use std::env;
|
||||||
env,
|
|
||||||
};
|
|
||||||
|
|
||||||
use chrono::{
|
use chrono::{DateTime, Local, SecondsFormat};
|
||||||
DateTime,
|
|
||||||
Local,
|
|
||||||
SecondsFormat,
|
|
||||||
};
|
|
||||||
|
|
||||||
use fltk::{
|
use fltk::{app, button, frame, prelude::*, text, window::Window};
|
||||||
app,
|
|
||||||
button,
|
|
||||||
frame,
|
|
||||||
prelude::*,
|
|
||||||
text,
|
|
||||||
window::Window,
|
|
||||||
};
|
|
||||||
|
|
||||||
use tokio::{
|
use tokio::{
|
||||||
fs,
|
fs,
|
||||||
io::AsyncWriteExt,
|
io::AsyncWriteExt,
|
||||||
time::{
|
time::{interval_at, Duration},
|
||||||
Duration,
|
|
||||||
interval_at,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
mod config;
|
mod config;
|
||||||
|
@ -43,10 +27,7 @@ async fn main () -> Result <(), Error> {
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
let mut i = interval_at(
|
let mut i = interval_at(
|
||||||
offset::get_next_tick (
|
offset::get_next_tick(config.interval_secs, config.offset_secs).into(),
|
||||||
config.interval_secs,
|
|
||||||
config.offset_secs,
|
|
||||||
).into (),
|
|
||||||
Duration::from_secs(config.interval_secs),
|
Duration::from_secs(config.interval_secs),
|
||||||
);
|
);
|
||||||
i.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
i.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
||||||
|
@ -71,12 +52,12 @@ async fn main () -> Result <(), Error> {
|
||||||
if let Err(e) = gui.submit().await {
|
if let Err(e) = gui.submit().await {
|
||||||
eprintln!("DVW4SBNB Error while submitting: {:?}", e);
|
eprintln!("DVW4SBNB Error while submitting: {:?}", e);
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Message::PopUp => {
|
Message::PopUp => {
|
||||||
if let Err(e) = gui.pop_up() {
|
if let Err(e) = gui.pop_up() {
|
||||||
eprintln!("5BWNNQT6 Error while popping up: {:?}", e);
|
eprintln!("5BWNNQT6 Error while popping up: {:?}", e);
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -141,11 +122,13 @@ impl Gui {
|
||||||
|
|
||||||
fn refresh_status(&mut self) {
|
fn refresh_status(&mut self) {
|
||||||
let version = option_env!("CARGO_PKG_VERSION").unwrap_or("(???)");
|
let version = option_env!("CARGO_PKG_VERSION").unwrap_or("(???)");
|
||||||
let time_popped_up = self.time_popped_up
|
let time_popped_up = self
|
||||||
|
.time_popped_up
|
||||||
.map(|x| x.to_rfc3339_opts(SecondsFormat::Secs, true))
|
.map(|x| x.to_rfc3339_opts(SecondsFormat::Secs, true))
|
||||||
.unwrap_or_else(|| "(???)".to_string());
|
.unwrap_or_else(|| "(???)".to_string());
|
||||||
|
|
||||||
self.status.set_label (&format! ("v{}, popped up at {}", version, time_popped_up));
|
self.status
|
||||||
|
.set_label(&format!("v{}, popped up at {}", version, time_popped_up));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pop_up(&mut self) -> Result<(), Error> {
|
fn pop_up(&mut self) -> Result<(), Error> {
|
||||||
|
@ -171,7 +154,9 @@ impl Gui {
|
||||||
|
|
||||||
let jl = JournalLine {
|
let jl = JournalLine {
|
||||||
text: buffer.text(),
|
text: buffer.text(),
|
||||||
time_popped_up: self.time_popped_up.map (|x| x.to_rfc3339_opts (SecondsFormat::Secs, true)),
|
time_popped_up: self
|
||||||
|
.time_popped_up
|
||||||
|
.map(|x| x.to_rfc3339_opts(SecondsFormat::Secs, true)),
|
||||||
time_submitted: Local::now().to_rfc3339_opts(SecondsFormat::Secs, true),
|
time_submitted: Local::now().to_rfc3339_opts(SecondsFormat::Secs, true),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -182,7 +167,8 @@ impl Gui {
|
||||||
let mut f = fs::OpenOptions::new()
|
let mut f = fs::OpenOptions::new()
|
||||||
.append(true)
|
.append(true)
|
||||||
.create(true)
|
.create(true)
|
||||||
.open ("annoying_journal/journal.jsonl").await?;
|
.open("annoying_journal/journal.jsonl")
|
||||||
|
.await?;
|
||||||
f.write_all(s.as_bytes()).await?;
|
f.write_all(s.as_bytes()).await?;
|
||||||
f.write_all(b"\n").await?;
|
f.write_all(b"\n").await?;
|
||||||
|
|
||||||
|
|
|
@ -1,22 +1,26 @@
|
||||||
use std::{
|
use std::time::{Duration, Instant, SystemTime};
|
||||||
time::{
|
|
||||||
Duration,
|
|
||||||
Instant,
|
|
||||||
SystemTime,
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
pub fn get_next_tick(interval_secs: u64, offset_secs: u64) -> Instant {
|
pub fn get_next_tick(interval_secs: u64, offset_secs: u64) -> Instant {
|
||||||
let now_sys = SystemTime::now();
|
let now_sys = SystemTime::now();
|
||||||
let now_mono = Instant::now();
|
let now_mono = Instant::now();
|
||||||
|
|
||||||
let phase = get_phase(now_sys, interval_secs, offset_secs);
|
let phase = get_phase(now_sys, interval_secs, offset_secs);
|
||||||
now_mono.checked_add (Duration::from_millis (interval_secs * 1000 - u64::try_from (phase).unwrap ())).unwrap ()
|
now_mono
|
||||||
|
.checked_add(Duration::from_millis(
|
||||||
|
interval_secs * 1000 - u64::try_from(phase).unwrap(),
|
||||||
|
))
|
||||||
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_phase(now: SystemTime, interval_secs: u64, offset_secs: u64) -> u64 {
|
fn get_phase(now: SystemTime, interval_secs: u64, offset_secs: u64) -> u64 {
|
||||||
let ms_since_epoch = now.duration_since (SystemTime::UNIX_EPOCH).unwrap ().as_millis ();
|
let ms_since_epoch = now
|
||||||
u64::try_from ((ms_since_epoch + u128::from (offset_secs) * 1000) % (u128::from (interval_secs) * 1000)).unwrap ()
|
.duration_since(SystemTime::UNIX_EPOCH)
|
||||||
|
.unwrap()
|
||||||
|
.as_millis();
|
||||||
|
u64::try_from(
|
||||||
|
(ms_since_epoch + u128::from(offset_secs) * 1000) % (u128::from(interval_secs) * 1000),
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -25,7 +29,9 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test() {
|
fn test() {
|
||||||
let now = SystemTime::UNIX_EPOCH.checked_add (Duration::from_secs (1649201544)).unwrap ();
|
let now = SystemTime::UNIX_EPOCH
|
||||||
|
.checked_add(Duration::from_secs(1649201544))
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(get_phase(now, 2225, 0), 394000);
|
assert_eq!(get_phase(now, 2225, 0), 394000);
|
||||||
assert_eq!(get_phase(now, 2225, 30), 424000);
|
assert_eq!(get_phase(now, 2225, 30), 424000);
|
||||||
|
|
Loading…
Reference in New Issue