Compare commits

...

5 Commits

Author SHA1 Message Date
_ 6fbbe918fe bump to v0.1.2 2022-04-05 23:58:56 +00:00
_ bb619b2452 offset feature 2022-04-05 23:58:28 +00:00
_ 0a73740b16 ♻️ refactor: config to its own module 2022-04-05 23:11:47 +00:00
_ fc61de612a 📝 2022-04-05 23:11:34 +00:00
_ 06a99fd6ae ⬆️ forgot to let the version propagate to Cargo.lock 2022-04-05 23:11:03 +00:00
7 changed files with 134 additions and 54 deletions

2
Cargo.lock generated
View File

@ -4,7 +4,7 @@ version = 3
[[package]]
name = "annoying_journal"
version = "0.1.0"
version = "0.1.2"
dependencies = [
"chrono",
"fltk",

View File

@ -8,7 +8,7 @@ license = "AGPL-3.0"
name = "annoying_journal"
readme = "README.md"
repository = "https://six-five-six-four.com/git/reactor/annoying_journal"
version = "0.1.1"
version = "0.1.2"
exclude = [
"COPYING",

View File

@ -1,7 +1,7 @@
# annoying_journal
A journal that pops up and steals keyboard focus every 37 minutes,
to force you to write an entry.
A low-dependency GUI journal that pops up and steals keyboard focus
every 37 minutes, to force you to write an entry.
[Video](videos/002-demo.webm)

63
src/config.rs Normal file
View File

@ -0,0 +1,63 @@
use std::{
str::FromStr,
};
type Result <T> = std::result::Result <T, crate::error::Error>;
pub struct Config {
pub interval_secs: u64,
pub prompt: String,
// Open dev tools in a web browser and run `Math.random () * 2225`
pub offset_secs: u64,
}
impl Default for Config {
fn default () -> Self {
Self {
interval_secs: 2225,
prompt: "Write a journal entry, then hit Tab, Enter to submit it.".into (),
offset_secs: 0,
}
}
}
impl Config {
pub fn from_args <I: Iterator <Item=String>> (mut args: I) -> Result <Self> {
use crate::error::Error::{
CannotParseArg,
ParamNeedsArg,
};
let mut that = Self::default ();
// Finally found the difference: https://stackoverflow.com/questions/1788923/parameter-vs-argument
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>"))?;
that.interval_secs = val;
}
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" {
let val = args.next ().ok_or (ParamNeedsArg ("--prompt"))?;
that.prompt = val;
}
}
Ok (that)
}
}
#[cfg (test)]
mod test {
#[test]
fn parse () {
}
}

17
src/error.rs Normal file
View File

@ -0,0 +1,17 @@
use fltk::prelude::*;
#[derive (thiserror::Error, Debug)]
pub enum Error {
#[error ("46MVLSEL Cannot parse argument: {0}")]
CannotParseArg (&'static str),
#[error ("4JZ5B2FN Editor has no buffer, this should be impossible")]
EditorHasNoBuffer,
#[error ("OKE7Z5O6 FLTK: {0}")]
Fltk (#[from] FltkError),
#[error ("4BQPBIAJ IO")]
Io (#[from] std::io::Error),
#[error ("KDP4DNOP JSON serialization failed")]
JsonSerialization (#[from] serde_json::Error),
#[error ("3MYHBQWV Parameter {0} needs an argument")]
ParamNeedsArg (&'static str),
}

View File

@ -1,5 +1,5 @@
use std::{
str::FromStr,
env,
};
use chrono::{
@ -21,36 +21,33 @@ use tokio::{
io::AsyncWriteExt,
time::{
Duration,
interval,
interval_at,
},
};
mod config;
mod error;
mod offset;
use config::Config;
use error::Error;
#[tokio::main]
async fn main () -> Result <(), Error> {
let mut config = Config::default ();
let mut args = std::env::args ();
// Finally found the difference: https://stackoverflow.com/questions/1788923/parameter-vs-argument
while let Some (arg) = args.next () {
if arg == "--interval-secs" {
let val = args.next ().ok_or (Error::ParamNeedsArg ("--interval-secs"))?;
let val = u64::from_str (&val).map_err (|_| Error::CannotParseArg ("--interval-secs <u64>"))?;
config.interval_secs = val;
}
else if arg == "--prompt" {
let val = args.next ().ok_or (Error::ParamNeedsArg ("--prompt"))?;
config.prompt = val;
}
}
let config = Config::from_args (env::args ())?;
let (fltk_tx, fltk_rx) = app::channel::<Message> ();
let app = app::App::default ();
tokio::spawn (async move {
let mut i = interval (Duration::from_secs (config.interval_secs));
let mut i = interval_at (
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);
loop {
@ -85,42 +82,12 @@ async fn main () -> Result <(), Error> {
Ok (())
}
struct Config {
interval_secs: u64,
prompt: String,
}
impl Default for Config {
fn default () -> Self {
Self {
interval_secs: 2225,
prompt: "Write a journal entry, then hit Tab, Enter to submit it.".into (),
}
}
}
#[derive (Clone, Copy)]
enum Message {
PopUp,
Submit,
}
#[derive (thiserror::Error, Debug)]
enum Error {
#[error ("46MVLSEL Cannot parse argument: {0}")]
CannotParseArg (&'static str),
#[error ("4JZ5B2FN Editor has no buffer, this should be impossible")]
EditorHasNoBuffer,
#[error ("OKE7Z5O6 FLTK: {0}")]
Fltk (#[from] FltkError),
#[error ("4BQPBIAJ IO")]
Io (#[from] std::io::Error),
#[error ("KDP4DNOP JSON serialization failed")]
JsonSerialization (#[from] serde_json::Error),
#[error ("3MYHBQWV Parameter {0} needs an argument")]
ParamNeedsArg (&'static str),
}
#[derive (serde::Serialize)]
struct JournalLine {
text: String,

33
src/offset.rs Normal file
View File

@ -0,0 +1,33 @@
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 ()
}
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 ()
}
#[cfg (test)]
mod test {
use super::*;
#[test]
fn test () {
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);
}
}