cargo fmt
parent
d7af07b275
commit
a6193fcb16
|
@ -1,63 +1,55 @@
|
||||||
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>;
|
||||||
|
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub interval_secs: u64,
|
pub interval_secs: u64,
|
||||||
pub prompt: String,
|
pub prompt: String,
|
||||||
|
|
||||||
// Open dev tools in a web browser and run `Math.random () * 2225`
|
// Open dev tools in a web browser and run `Math.random () * 2225`
|
||||||
pub offset_secs: u64,
|
pub offset_secs: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Config {
|
impl Default for Config {
|
||||||
fn default () -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
interval_secs: 2225,
|
interval_secs: 2225,
|
||||||
prompt: "Write a journal entry, then hit Tab, Enter to submit it.".into (),
|
prompt: "Write a journal entry, then hit Tab, Enter to submit it.".into(),
|
||||||
offset_secs: 0,
|
offset_secs: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
||||||
};
|
|
||||||
|
// Finally found the difference: https://stackoverflow.com/questions/1788923/parameter-vs-argument
|
||||||
let mut that = Self::default ();
|
|
||||||
|
while let Some(arg) = args.next() {
|
||||||
// Finally found the difference: https://stackoverflow.com/questions/1788923/parameter-vs-argument
|
if arg == "--interval-secs" {
|
||||||
|
let val = args.next().ok_or(ParamNeedsArg("--interval-secs"))?;
|
||||||
while let Some (arg) = args.next () {
|
let val =
|
||||||
if arg == "--interval-secs" {
|
u64::from_str(&val).map_err(|_| CannotParseArg("--interval-secs <u64>"))?;
|
||||||
let val = args.next ().ok_or (ParamNeedsArg ("--interval-secs"))?;
|
that.interval_secs = val;
|
||||||
let val = u64::from_str (&val).map_err (|_| CannotParseArg ("--interval-secs <u64>"))?;
|
} else if arg == "--offset-secs" {
|
||||||
that.interval_secs = val;
|
let val = args.next().ok_or(ParamNeedsArg("--offset-secs"))?;
|
||||||
}
|
let val = u64::from_str(&val).map_err(|_| CannotParseArg("--offset-secs <u64>"))?;
|
||||||
else if arg == "--offset-secs" {
|
that.offset_secs = val;
|
||||||
let val = args.next ().ok_or (ParamNeedsArg ("--offset-secs"))?;
|
} else if arg == "--prompt" {
|
||||||
let val = u64::from_str (&val).map_err (|_| CannotParseArg ("--offset-secs <u64>"))?;
|
let val = args.next().ok_or(ParamNeedsArg("--prompt"))?;
|
||||||
that.offset_secs = val;
|
that.prompt = val;
|
||||||
}
|
}
|
||||||
else if arg == "--prompt" {
|
}
|
||||||
let val = args.next ().ok_or (ParamNeedsArg ("--prompt"))?;
|
|
||||||
that.prompt = val;
|
Ok(that)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Ok (that)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg (test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
#[test]
|
#[test]
|
||||||
fn parse () {
|
fn parse() {}
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
26
src/error.rs
26
src/error.rs
|
@ -1,17 +1,17 @@
|
||||||
use fltk::prelude::*;
|
use fltk::prelude::*;
|
||||||
|
|
||||||
#[derive (thiserror::Error, Debug)]
|
#[derive(thiserror::Error, Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
#[error ("46MVLSEL Cannot parse argument: {0}")]
|
#[error("46MVLSEL Cannot parse argument: {0}")]
|
||||||
CannotParseArg (&'static str),
|
CannotParseArg(&'static str),
|
||||||
#[error ("4JZ5B2FN Editor has no buffer, this should be impossible")]
|
#[error("4JZ5B2FN Editor has no buffer, this should be impossible")]
|
||||||
EditorHasNoBuffer,
|
EditorHasNoBuffer,
|
||||||
#[error ("OKE7Z5O6 FLTK: {0}")]
|
#[error("OKE7Z5O6 FLTK: {0}")]
|
||||||
Fltk (#[from] FltkError),
|
Fltk(#[from] FltkError),
|
||||||
#[error ("4BQPBIAJ IO")]
|
#[error("4BQPBIAJ IO")]
|
||||||
Io (#[from] std::io::Error),
|
Io(#[from] std::io::Error),
|
||||||
#[error ("KDP4DNOP JSON serialization failed")]
|
#[error("KDP4DNOP JSON serialization failed")]
|
||||||
JsonSerialization (#[from] serde_json::Error),
|
JsonSerialization(#[from] serde_json::Error),
|
||||||
#[error ("3MYHBQWV Parameter {0} needs an argument")]
|
#[error("3MYHBQWV Parameter {0} needs an argument")]
|
||||||
ParamNeedsArg (&'static str),
|
ParamNeedsArg(&'static str),
|
||||||
}
|
}
|
||||||
|
|
326
src/main.rs
326
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;
|
||||||
|
@ -34,164 +18,166 @@ use config::Config;
|
||||||
use error::Error;
|
use error::Error;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main () -> Result <(), Error> {
|
async fn main() -> Result<(), Error> {
|
||||||
let config = Config::from_args (env::args ())?;
|
let config = Config::from_args(env::args())?;
|
||||||
|
|
||||||
let (fltk_tx, fltk_rx) = app::channel::<Message> ();
|
let (fltk_tx, fltk_rx) = app::channel::<Message>();
|
||||||
|
|
||||||
let app = app::App::default ();
|
let app = app::App::default();
|
||||||
|
|
||||||
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,
|
Duration::from_secs(config.interval_secs),
|
||||||
config.offset_secs,
|
);
|
||||||
).into (),
|
i.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
||||||
Duration::from_secs (config.interval_secs),
|
|
||||||
);
|
loop {
|
||||||
i.set_missed_tick_behavior (tokio::time::MissedTickBehavior::Skip);
|
i.tick().await;
|
||||||
|
eprintln!("3SAHNQ43 Popping up");
|
||||||
loop {
|
fltk_tx.send(Message::PopUp);
|
||||||
i.tick ().await;
|
}
|
||||||
eprintln! ("3SAHNQ43 Popping up");
|
});
|
||||||
fltk_tx.send (Message::PopUp);
|
|
||||||
}
|
let mut gui = Gui::new(config, fltk_tx);
|
||||||
});
|
|
||||||
|
while app.wait() {
|
||||||
let mut gui = Gui::new (config, fltk_tx);
|
let msg = match fltk_rx.recv() {
|
||||||
|
Some(x) => x,
|
||||||
while app.wait () {
|
None => continue,
|
||||||
let msg = match fltk_rx.recv () {
|
};
|
||||||
Some (x) => x,
|
|
||||||
None => continue,
|
match msg {
|
||||||
};
|
Message::Submit => {
|
||||||
|
if let Err(e) = gui.submit().await {
|
||||||
match msg {
|
eprintln!("DVW4SBNB Error while submitting: {:?}", e);
|
||||||
Message::Submit => {
|
}
|
||||||
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);
|
||||||
Message::PopUp => {
|
}
|
||||||
if let Err (e) = gui.pop_up () {
|
}
|
||||||
eprintln! ("5BWNNQT6 Error while popping up: {:?}", e);
|
}
|
||||||
}
|
}
|
||||||
},
|
|
||||||
}
|
Ok(())
|
||||||
}
|
|
||||||
|
|
||||||
Ok (())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive (Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
enum Message {
|
enum Message {
|
||||||
PopUp,
|
PopUp,
|
||||||
Submit,
|
Submit,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive (serde::Serialize)]
|
#[derive(serde::Serialize)]
|
||||||
struct JournalLine {
|
struct JournalLine {
|
||||||
text: String,
|
text: String,
|
||||||
time_popped_up: Option <String>,
|
time_popped_up: Option<String>,
|
||||||
time_submitted: String,
|
time_submitted: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Gui {
|
struct Gui {
|
||||||
time_popped_up: Option <DateTime <Local>>,
|
time_popped_up: Option<DateTime<Local>>,
|
||||||
editor: text::TextEditor,
|
editor: text::TextEditor,
|
||||||
status: frame::Frame,
|
status: frame::Frame,
|
||||||
wind: Window,
|
wind: Window,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Gui {
|
impl Gui {
|
||||||
fn new (config: Config, fltk_tx: app::Sender <Message>) -> Self {
|
fn new(config: Config, fltk_tx: app::Sender<Message>) -> Self {
|
||||||
let mut wind = Window::new (100, 100, 640, 480, "Annoying Journal");
|
let mut wind = Window::new(100, 100, 640, 480, "Annoying Journal");
|
||||||
wind.make_resizable (true);
|
wind.make_resizable(true);
|
||||||
|
|
||||||
let mut buffer = text::TextBuffer::default ();
|
let mut buffer = text::TextBuffer::default();
|
||||||
buffer.set_text (&config.prompt);
|
buffer.set_text(&config.prompt);
|
||||||
let mut display = text::TextDisplay::new (0, 0, 640, 50, "");
|
let mut display = text::TextDisplay::new(0, 0, 640, 50, "");
|
||||||
display.set_buffer (Some (buffer));
|
display.set_buffer(Some(buffer));
|
||||||
|
|
||||||
let mut editor = text::TextEditor::new (0, 50, 640, 480 - 50 - 50, "");
|
let mut editor = text::TextEditor::new(0, 50, 640, 480 - 50 - 50, "");
|
||||||
editor.set_buffer (Some (text::TextBuffer::default ()));
|
editor.set_buffer(Some(text::TextBuffer::default()));
|
||||||
|
|
||||||
editor.set_tab_nav (true);
|
editor.set_tab_nav(true);
|
||||||
|
|
||||||
let mut but = button::ReturnButton::new (640 - 100, 480 - 50, 100, 50, "Submit");
|
let mut but = button::ReturnButton::new(640 - 100, 480 - 50, 100, 50, "Submit");
|
||||||
but.emit (fltk_tx, Message::Submit);
|
but.emit(fltk_tx, Message::Submit);
|
||||||
|
|
||||||
let status = frame::Frame::new (0, 480 - 50, 640 - 100, 50, "");
|
let status = frame::Frame::new(0, 480 - 50, 640 - 100, 50, "");
|
||||||
|
|
||||||
wind.set_label ("ANNOYING JOURNAL");
|
wind.set_label("ANNOYING JOURNAL");
|
||||||
wind.end ();
|
wind.end();
|
||||||
wind.show ();
|
wind.show();
|
||||||
|
|
||||||
let mut that = Self {
|
let mut that = Self {
|
||||||
time_popped_up: Some (Local::now ()),
|
time_popped_up: Some(Local::now()),
|
||||||
editor,
|
editor,
|
||||||
status,
|
status,
|
||||||
wind,
|
wind,
|
||||||
};
|
};
|
||||||
|
|
||||||
that.refresh_status ();
|
that.refresh_status();
|
||||||
|
|
||||||
that
|
that
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
.map (|x| x.to_rfc3339_opts (SecondsFormat::Secs, true))
|
.time_popped_up
|
||||||
.unwrap_or_else (|| "(???)".to_string ());
|
.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> {
|
}
|
||||||
if self.time_popped_up.is_some () {
|
|
||||||
eprintln! ("O4U6E36V Ignoring pop-up, already popped up");
|
fn pop_up(&mut self) -> Result<(), Error> {
|
||||||
return Ok (());
|
if self.time_popped_up.is_some() {
|
||||||
}
|
eprintln!("O4U6E36V Ignoring pop-up, already popped up");
|
||||||
|
return Ok(());
|
||||||
self.time_popped_up = Some (Local::now ());
|
}
|
||||||
self.refresh_status ();
|
|
||||||
self.wind.set_label ("ANNOYING JOURNAL");
|
self.time_popped_up = Some(Local::now());
|
||||||
self.wind.show ();
|
self.refresh_status();
|
||||||
self.editor.take_focus ()?;
|
self.wind.set_label("ANNOYING JOURNAL");
|
||||||
|
self.wind.show();
|
||||||
Ok (())
|
self.editor.take_focus()?;
|
||||||
}
|
|
||||||
|
Ok(())
|
||||||
async fn submit (&mut self) -> Result <(), Error> {
|
}
|
||||||
let buffer = match self.editor.buffer () {
|
|
||||||
None => return Err (Error::EditorHasNoBuffer),
|
async fn submit(&mut self) -> Result<(), Error> {
|
||||||
Some (x) => x,
|
let buffer = match self.editor.buffer() {
|
||||||
};
|
None => return Err(Error::EditorHasNoBuffer),
|
||||||
|
Some(x) => x,
|
||||||
let jl = JournalLine {
|
};
|
||||||
text: buffer.text (),
|
|
||||||
time_popped_up: self.time_popped_up.map (|x| x.to_rfc3339_opts (SecondsFormat::Secs, true)),
|
let jl = JournalLine {
|
||||||
time_submitted: Local::now ().to_rfc3339_opts (SecondsFormat::Secs, true),
|
text: buffer.text(),
|
||||||
};
|
time_popped_up: self
|
||||||
|
.time_popped_up
|
||||||
let s = serde_json::to_string (&jl)?;
|
.map(|x| x.to_rfc3339_opts(SecondsFormat::Secs, true)),
|
||||||
|
time_submitted: Local::now().to_rfc3339_opts(SecondsFormat::Secs, true),
|
||||||
fs::create_dir_all ("annoying_journal").await?;
|
};
|
||||||
|
|
||||||
let mut f = fs::OpenOptions::new ()
|
let s = serde_json::to_string(&jl)?;
|
||||||
.append (true)
|
|
||||||
.create (true)
|
fs::create_dir_all("annoying_journal").await?;
|
||||||
.open ("annoying_journal/journal.jsonl").await?;
|
|
||||||
f.write_all (s.as_bytes ()).await?;
|
let mut f = fs::OpenOptions::new()
|
||||||
f.write_all (b"\n").await?;
|
.append(true)
|
||||||
|
.create(true)
|
||||||
println! ("{}", s);
|
.open("annoying_journal/journal.jsonl")
|
||||||
self.editor.set_buffer (text::TextBuffer::default ());
|
.await?;
|
||||||
self.wind.iconize ();
|
f.write_all(s.as_bytes()).await?;
|
||||||
self.time_popped_up = None;
|
f.write_all(b"\n").await?;
|
||||||
self.wind.set_label ("annoying journal");
|
|
||||||
|
println!("{}", s);
|
||||||
Ok (())
|
self.editor.set_buffer(text::TextBuffer::default());
|
||||||
}
|
self.wind.iconize();
|
||||||
|
self.time_popped_up = None;
|
||||||
|
self.wind.set_label("annoying journal");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,33 +1,39 @@
|
||||||
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)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[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))
|
||||||
assert_eq! (get_phase (now, 2225, 0), 394000);
|
.unwrap();
|
||||||
assert_eq! (get_phase (now, 2225, 30), 424000);
|
|
||||||
}
|
assert_eq!(get_phase(now, 2225, 0), 394000);
|
||||||
|
assert_eq!(get_phase(now, 2225, 30), 424000);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue