From 66b2f0793f236dccd3269ae1c3cbfe2293f7fb3d Mon Sep 17 00:00:00 2001 From: nc Date: Mon, 20 Jul 2020 18:30:32 -0400 Subject: Add RwLock around messages. Catch SIGINT and print that :q is the way to quit. Note that this doesn't actually capture Ctrl-C. I'm not sure how it works but termion somehow swollows Ctrl-C so and circumvents the signal handler... --- Cargo.toml | 1 + src/app/impl_self.rs | 12 +++++++----- src/app/impl_view.rs | 8 ++++---- src/app/mod.rs | 4 +++- src/main.rs | 9 ++++++++- 5 files changed, 23 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9cf3639..c97e2a4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ typetag = "0.1.4" directories = "3.0.1" clap = "2.33" notify = "4.0" +signal-hook = "0.1.16" [dependencies.cursive] version = "0.15" diff --git a/src/app/impl_self.rs b/src/app/impl_self.rs index 744f906..325c53f 100644 --- a/src/app/impl_self.rs +++ b/src/app/impl_self.rs @@ -5,6 +5,7 @@ use std::io::prelude::*; use std::path::PathBuf; use std::sync::mpsc::channel; use std::time::Duration; +use std::sync::{RwLock, Arc}; use chrono::Local; use cursive::direction::Absolute; @@ -23,13 +24,14 @@ impl App { let (tx, rx) = channel(); let mut watcher = watcher(tx, Duration::from_secs(1)).unwrap(); watcher.watch(utils::auto_habit_file(), RecursiveMode::Recursive); + return App { habits: vec![], focus: 0, _file_watcher: watcher, file_event_recv: rx, view_month_offset: 0, - message: "Type :add to get started, Ctrl-L to dismiss".into(), + message: Arc::new(RwLock::new("Type :add to get started, Ctrl-L to dismiss".into())), }; } @@ -41,7 +43,7 @@ impl App { let old_len = self.habits.len(); self.habits.retain(|h| h.name() != name); if old_len == self.habits.len() { - self.message + self.message.write().unwrap() .set_message(format!("Could not delete habit `{}`", name)) } } @@ -114,7 +116,7 @@ impl App { } pub fn clear_message(&mut self) { - self.message.clear(); + self.message.write().unwrap().clear(); } pub fn status(&self) -> StatusLine { @@ -236,8 +238,8 @@ impl App { Command::Blank => {} }, Err(e) => { - self.message.set_message(e.to_string()); - self.message.set_kind(MessageKind::Error); + self.message.write().unwrap().set_message(e.to_string()); + self.message.write().unwrap().set_kind(MessageKind::Error); } } } diff --git a/src/app/impl_view.rs b/src/app/impl_view.rs index 892b00c..acc387e 100644 --- a/src/app/impl_view.rs +++ b/src/app/impl_view.rs @@ -39,8 +39,8 @@ impl View for App { printer.print(offset, &status.1); // right status offset = offset.map_x(|_| 0).map_y(|_| self.max_size().y - 1); - printer.with_style(Color::from(self.message.kind()), |p| { - p.print(offset, self.message.contents()) + printer.with_style(Color::from(self.message.read().unwrap().kind()), |p| { + p.print(offset, self.message.read().unwrap().contents()) }); } @@ -161,8 +161,8 @@ impl View for App { return EventResult::Consumed(None); } Event::CtrlChar('l') => { - self.message.clear(); - self.message.set_kind(MessageKind::Info); + self.message.write().unwrap().clear(); + self.message.write().unwrap().set_kind(MessageKind::Info); return EventResult::Consumed(None); } diff --git a/src/app/mod.rs b/src/app/mod.rs index 2aecb33..bce6e79 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -5,6 +5,8 @@ use notify::{DebouncedEvent, RecommendedWatcher}; use crate::habit::HabitWrapper; +use std::sync::{RwLock, Arc}; + mod impl_self; mod impl_view; mod message; @@ -20,7 +22,7 @@ pub struct App { file_event_recv: Receiver, focus: usize, view_month_offset: u32, - message: Message, + pub message: Arc>, } impl Default for App { diff --git a/src/main.rs b/src/main.rs index d96119e..ca1b653 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,8 +50,15 @@ fn main() { ), } } else { - let mut s = termion().unwrap(); let app = App::load_state(); + let m = app.message.clone(); + unsafe { signal_hook::register(signal_hook::SIGINT, move || { + std::fs::File::create("killed").unwrap(); + m.write().unwrap().set_message("Use the :q command to quit"); + }) }.unwrap(); + + let mut s = termion().unwrap(); + let layout = NamedView::new( "Frame", LinearLayout::vertical().child(NamedView::new("Main", app)), -- cgit v1.2.3