🐣 annoying journal
parent
a809fff4cb
commit
3e39be06e3
|
@ -1 +1,2 @@
|
|||
/annoying_journal
|
||||
/target
|
||||
|
|
|
@ -6,11 +6,20 @@ version = 3
|
|||
name = "annoying_journal"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"fltk",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.3.2"
|
||||
|
@ -35,6 +44,19 @@ version = "1.0.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "chrono"
|
||||
version = "0.4.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
"time",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cmake"
|
||||
version = "0.1.48"
|
||||
|
@ -95,6 +117,12 @@ dependencies = [
|
|||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35"
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
|
@ -141,7 +169,7 @@ dependencies = [
|
|||
"log",
|
||||
"miow",
|
||||
"ntapi",
|
||||
"wasi",
|
||||
"wasi 0.11.0+wasi-snapshot-preview1",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
|
@ -163,6 +191,25 @@ dependencies = [
|
|||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.44"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num_cpus"
|
||||
version = "1.13.1"
|
||||
|
@ -241,12 +288,49 @@ dependencies = [
|
|||
"bitflags",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f"
|
||||
|
||||
[[package]]
|
||||
name = "scopeguard"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.136"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.136"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.79"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e8d9fa5c3b304765ce1fd9c4c8a3de2c8db365a5b91be52f186efc675681d95"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"ryu",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "signal-hook-registry"
|
||||
version = "1.4.0"
|
||||
|
@ -303,6 +387,17 @@ dependencies = [
|
|||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.1.44"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"wasi 0.10.0+wasi-snapshot-preview1",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio"
|
||||
version = "1.17.0"
|
||||
|
@ -346,6 +441,12 @@ version = "0.2.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.10.0+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.11.0+wasi-snapshot-preview1"
|
||||
|
|
|
@ -7,6 +7,9 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
|
||||
chrono = "0.4.19"
|
||||
fltk = "1.3.1"
|
||||
serde = { version = "1.0.136", features = ["derive"] }
|
||||
serde_json = "1.0.79"
|
||||
thiserror = "1.0.30"
|
||||
tokio = { version = "1.17.0", features = ["full"] }
|
||||
|
|
170
src/main.rs
170
src/main.rs
|
@ -1,40 +1,55 @@
|
|||
use std::{
|
||||
str::FromStr,
|
||||
};
|
||||
|
||||
use chrono::{
|
||||
Local,
|
||||
SecondsFormat,
|
||||
};
|
||||
|
||||
use fltk::{
|
||||
app,
|
||||
button::Button,
|
||||
enums::CallbackTrigger,
|
||||
frame::Frame,
|
||||
input::*,
|
||||
button,
|
||||
prelude::*,
|
||||
window::Window
|
||||
text,
|
||||
window::Window,
|
||||
};
|
||||
|
||||
use tokio::{
|
||||
fs,
|
||||
io::AsyncWriteExt,
|
||||
time::{
|
||||
Duration,
|
||||
interval,
|
||||
},
|
||||
};
|
||||
|
||||
#[derive (Clone, Copy)]
|
||||
enum Message {
|
||||
PopUp,
|
||||
Submit,
|
||||
}
|
||||
|
||||
#[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 (fltk_tx, fltk_rx) = app::channel::<Message> ();
|
||||
|
||||
let app = app::App::default ();
|
||||
let mut wind = Window::new (100, 100, 640, 480, "Annoying Journal");
|
||||
|
||||
let mut gui = Gui::new (fltk_tx);
|
||||
|
||||
wind.end ();
|
||||
wind.show ();
|
||||
|
||||
tokio::spawn (async move {
|
||||
let mut i = interval (Duration::from_secs (3));
|
||||
let mut i = interval (Duration::from_secs (config.interval_secs));
|
||||
i.set_missed_tick_behavior (tokio::time::MissedTickBehavior::Skip);
|
||||
|
||||
loop {
|
||||
|
@ -44,6 +59,8 @@ async fn main () -> Result <(), Error> {
|
|||
}
|
||||
});
|
||||
|
||||
let mut gui = Gui::new (config, fltk_tx);
|
||||
|
||||
while app.wait () {
|
||||
let msg = match fltk_rx.recv () {
|
||||
Some (x) => x,
|
||||
|
@ -52,10 +69,14 @@ async fn main () -> Result <(), Error> {
|
|||
|
||||
match msg {
|
||||
Message::Submit => {
|
||||
wind.iconize ();
|
||||
if let Err (e) = gui.submit ().await {
|
||||
eprintln! ("DVW4SBNB Error while submitting: {:?}", e);
|
||||
}
|
||||
},
|
||||
Message::PopUp => {
|
||||
wind.show ();
|
||||
if let Err (e) = gui.pop_up () {
|
||||
eprintln! ("5BWNNQT6 Error while popping up: {:?}", e);
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -63,23 +84,122 @@ 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,
|
||||
time_submitted: String,
|
||||
}
|
||||
|
||||
struct Gui {
|
||||
but: Button,
|
||||
armed: bool,
|
||||
editor: text::TextEditor,
|
||||
wind: Window,
|
||||
}
|
||||
|
||||
impl Gui {
|
||||
fn new (fltk_tx: app::Sender <Message>) -> Self {
|
||||
let mut but = Button::new (640 - 100, 480 - 50, 100, 50, "Submit");
|
||||
but.set_trigger (CallbackTrigger::Release);
|
||||
fn new (config: Config, fltk_tx: app::Sender <Message>) -> Self {
|
||||
let mut wind = Window::new (100, 100, 640, 480, "Annoying Journal");
|
||||
wind.make_resizable (true);
|
||||
|
||||
let mut buffer = text::TextBuffer::default ();
|
||||
buffer.set_text (&config.prompt);
|
||||
let mut display = text::TextDisplay::new (0, 0, 640, 50, "");
|
||||
display.set_buffer (Some (buffer));
|
||||
|
||||
let mut editor = text::TextEditor::new (0, 50, 640, 480 - 50 - 50, "");
|
||||
editor.set_buffer (Some (text::TextBuffer::default ()));
|
||||
|
||||
editor.set_tab_nav (true);
|
||||
|
||||
let mut but = button::ReturnButton::new (640 - 100, 480 - 50, 100, 50, "Submit");
|
||||
but.emit (fltk_tx, Message::Submit);
|
||||
|
||||
wind.end ();
|
||||
wind.show ();
|
||||
|
||||
Self {
|
||||
but,
|
||||
armed: true,
|
||||
editor,
|
||||
wind,
|
||||
}
|
||||
}
|
||||
|
||||
fn pop_up (&mut self) -> Result <(), Error> {
|
||||
if ! self.armed {
|
||||
eprintln! ("O4U6E36V Ignoring pop-up, not armed");
|
||||
return Ok (());
|
||||
}
|
||||
|
||||
self.armed = false;
|
||||
self.wind.show ();
|
||||
self.editor.take_focus ()?;
|
||||
|
||||
Ok (())
|
||||
}
|
||||
|
||||
async fn submit (&mut self) -> Result <(), Error> {
|
||||
let buffer = match self.editor.buffer () {
|
||||
None => return Err (Error::EditorHasNoBuffer),
|
||||
Some (x) => x,
|
||||
};
|
||||
|
||||
let jl = JournalLine {
|
||||
text: buffer.text (),
|
||||
time_submitted: Local::now ().to_rfc3339_opts (SecondsFormat::Secs, true),
|
||||
};
|
||||
|
||||
let s = serde_json::to_string (&jl)?;
|
||||
|
||||
fs::create_dir_all ("annoying_journal").await?;
|
||||
|
||||
let mut f = fs::OpenOptions::new ()
|
||||
.append (true)
|
||||
.create (true)
|
||||
.open ("annoying_journal/journal.jsonl").await?;
|
||||
f.write_all (s.as_bytes ()).await?;
|
||||
f.write_all (b"\n").await?;
|
||||
|
||||
println! ("{}", s);
|
||||
self.editor.set_buffer (text::TextBuffer::default ());
|
||||
self.wind.iconize ();
|
||||
self.armed = true;
|
||||
|
||||
Ok (())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue