Compare commits
7 Commits
Author | SHA1 | Date |
---|---|---|
_ | d7af07b275 | |
_ | 233567cf72 | |
_ | 6fbbe918fe | |
_ | bb619b2452 | |
_ | 0a73740b16 | |
_ | fc61de612a | |
_ | 06a99fd6ae |
|
@ -4,7 +4,7 @@ version = 3
|
|||
|
||||
[[package]]
|
||||
name = "annoying_journal"
|
||||
version = "0.1.0"
|
||||
version = "0.1.3"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"fltk",
|
||||
|
|
|
@ -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.3"
|
||||
|
||||
exclude = [
|
||||
"COPYING",
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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 () {
|
||||
|
||||
}
|
||||
}
|
|
@ -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),
|
||||
}
|
90
src/main.rs
90
src/main.rs
|
@ -1,5 +1,5 @@
|
|||
use std::{
|
||||
str::FromStr,
|
||||
env,
|
||||
};
|
||||
|
||||
use chrono::{
|
||||
|
@ -11,6 +11,7 @@ use chrono::{
|
|||
use fltk::{
|
||||
app,
|
||||
button,
|
||||
frame,
|
||||
prelude::*,
|
||||
text,
|
||||
window::Window,
|
||||
|
@ -21,36 +22,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 +83,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,
|
||||
|
@ -131,6 +99,7 @@ struct JournalLine {
|
|||
struct Gui {
|
||||
time_popped_up: Option <DateTime <Local>>,
|
||||
editor: text::TextEditor,
|
||||
status: frame::Frame,
|
||||
wind: Window,
|
||||
}
|
||||
|
||||
|
@ -152,15 +121,31 @@ impl Gui {
|
|||
let mut but = button::ReturnButton::new (640 - 100, 480 - 50, 100, 50, "Submit");
|
||||
but.emit (fltk_tx, Message::Submit);
|
||||
|
||||
let status = frame::Frame::new (0, 480 - 50, 640 - 100, 50, "");
|
||||
|
||||
wind.set_label ("ANNOYING JOURNAL");
|
||||
wind.end ();
|
||||
wind.show ();
|
||||
|
||||
Self {
|
||||
let mut that = Self {
|
||||
time_popped_up: Some (Local::now ()),
|
||||
editor,
|
||||
status,
|
||||
wind,
|
||||
}
|
||||
};
|
||||
|
||||
that.refresh_status ();
|
||||
|
||||
that
|
||||
}
|
||||
|
||||
fn refresh_status (&mut self) {
|
||||
let version = option_env! ("CARGO_PKG_VERSION").unwrap_or ("(???)");
|
||||
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));
|
||||
}
|
||||
|
||||
fn pop_up (&mut self) -> Result <(), Error> {
|
||||
|
@ -170,6 +155,7 @@ impl Gui {
|
|||
}
|
||||
|
||||
self.time_popped_up = Some (Local::now ());
|
||||
self.refresh_status ();
|
||||
self.wind.set_label ("ANNOYING JOURNAL");
|
||||
self.wind.show ();
|
||||
self.editor.take_focus ()?;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue