cargo fmt

main
_ 2025-05-19 23:07:01 +00:00
parent d7af07b275
commit a6193fcb16
4 changed files with 243 additions and 259 deletions

View File

@ -1,6 +1,4 @@
use std::{
str::FromStr,
};
use std::str::FromStr;
type Result<T> = std::result::Result<T, crate::error::Error>;
@ -24,10 +22,7 @@ impl Default for Config {
impl Config {
pub fn from_args<I: Iterator<Item = String>>(mut args: I) -> Result<Self> {
use crate::error::Error::{
CannotParseArg,
ParamNeedsArg,
};
use crate::error::Error::{CannotParseArg, ParamNeedsArg};
let mut that = Self::default();
@ -36,15 +31,14 @@ impl Config {
while let Some(arg) = args.next() {
if arg == "--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;
}
else if arg == "--offset-secs" {
} else if arg == "--offset-secs" {
let val = args.next().ok_or(ParamNeedsArg("--offset-secs"))?;
let val = u64::from_str(&val).map_err(|_| CannotParseArg("--offset-secs <u64>"))?;
that.offset_secs = val;
}
else if arg == "--prompt" {
} else if arg == "--prompt" {
let val = args.next().ok_or(ParamNeedsArg("--prompt"))?;
that.prompt = val;
}
@ -57,7 +51,5 @@ impl Config {
#[cfg(test)]
mod test {
#[test]
fn parse () {
}
fn parse() {}
}

View File

@ -1,29 +1,13 @@
use std::{
env,
};
use std::env;
use chrono::{
DateTime,
Local,
SecondsFormat,
};
use chrono::{DateTime, Local, SecondsFormat};
use fltk::{
app,
button,
frame,
prelude::*,
text,
window::Window,
};
use fltk::{app, button, frame, prelude::*, text, window::Window};
use tokio::{
fs,
io::AsyncWriteExt,
time::{
Duration,
interval_at,
},
time::{interval_at, Duration},
};
mod config;
@ -43,10 +27,7 @@ async fn main () -> Result <(), Error> {
tokio::spawn(async move {
let mut i = interval_at(
offset::get_next_tick (
config.interval_secs,
config.offset_secs,
).into (),
offset::get_next_tick(config.interval_secs, config.offset_secs).into(),
Duration::from_secs(config.interval_secs),
);
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 {
eprintln!("DVW4SBNB Error while submitting: {:?}", e);
}
},
}
Message::PopUp => {
if let Err(e) = gui.pop_up() {
eprintln!("5BWNNQT6 Error while popping up: {:?}", e);
}
},
}
}
}
@ -141,11 +122,13 @@ impl Gui {
fn refresh_status(&mut self) {
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))
.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> {
@ -171,7 +154,9 @@ impl Gui {
let jl = JournalLine {
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),
};
@ -182,7 +167,8 @@ impl Gui {
let mut f = fs::OpenOptions::new()
.append(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(b"\n").await?;

View File

@ -1,22 +1,26 @@
use std::{
time::{
Duration,
Instant,
SystemTime,
}
};
use std::time::{Duration, Instant, SystemTime};
pub fn get_next_tick(interval_secs: u64, offset_secs: u64) -> Instant {
let now_sys = SystemTime::now();
let now_mono = Instant::now();
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 {
let ms_since_epoch = now.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 ()
let ms_since_epoch = now
.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)]
@ -25,7 +29,9 @@ mod test {
#[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, 30), 424000);