diff options
author | extrawurst <776816+extrawurst@users.noreply.github.com> | 2022-09-02 09:59:13 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-02 09:59:13 +0200 |
commit | cf9ce9d4ff16a00afae97d719b234a55a67463f9 (patch) | |
tree | 6017357087be4b7f6a58dbd0364f0f3c73e733f1 /src/main.rs | |
parent | 5f7213730beecb04c53df5a240d548aa2af7b33e (diff) |
File watching using notify (#1310)
closes #1
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/src/main.rs b/src/main.rs index b9395be4..fd70f233 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,11 +36,15 @@ mod strings; mod tabs; mod ui; mod version; +mod watcher; use crate::{app::App, args::process_cmdline}; use anyhow::{bail, Result}; use app::QuitState; -use asyncgit::{sync::RepoPath, AsyncGitNotification}; +use asyncgit::{ + sync::{utils::repo_work_dir, RepoPath}, + AsyncGitNotification, +}; use backtrace::Backtrace; use crossbeam_channel::{tick, unbounded, Receiver, Select}; use crossterm::{ @@ -67,14 +71,14 @@ use tui::{ Terminal, }; use ui::style::Theme; +use watcher::RepoWatcher; -static TICK_INTERVAL: Duration = Duration::from_secs(5); static SPINNER_INTERVAL: Duration = Duration::from_millis(80); /// #[derive(Clone)] pub enum QueueEvent { - Tick, + Notify, SpinnerUpdate, AsyncEvent(AsyncNotification), InputEvent(InputEvent), @@ -161,7 +165,8 @@ fn run_app( let (tx_app, rx_app) = unbounded(); let rx_input = input.receiver(); - let ticker = tick(TICK_INTERVAL); + let watcher = RepoWatcher::new(repo_work_dir(&repo)?.as_str())?; + let rx_watcher = watcher.receiver(); let spinner_ticker = tick(SPINNER_INTERVAL); let mut app = App::new( @@ -179,13 +184,13 @@ fn run_app( loop { let event = if first_update { first_update = false; - QueueEvent::Tick + QueueEvent::Notify } else { select_event( &rx_input, &rx_git, &rx_app, - &ticker, + &rx_watcher, &spinner_ticker, )? }; @@ -208,7 +213,7 @@ fn run_app( } app.event(ev)?; } - QueueEvent::Tick => app.update()?, + QueueEvent::Notify => app.update()?, QueueEvent::AsyncEvent(ev) => { if !matches!( ev, @@ -282,7 +287,7 @@ fn select_event( rx_input: &Receiver<InputEvent>, rx_git: &Receiver<AsyncGitNotification>, rx_app: &Receiver<AsyncAppNotification>, - rx_ticker: &Receiver<Instant>, + rx_notify: &Receiver<()>, rx_spinner: &Receiver<Instant>, ) -> Result<QueueEvent> { let mut sel = Select::new(); @@ -290,7 +295,7 @@ fn select_event( sel.recv(rx_input); sel.recv(rx_git); sel.recv(rx_app); - sel.recv(rx_ticker); + sel.recv(rx_notify); sel.recv(rx_spinner); let oper = sel.select(); @@ -304,7 +309,7 @@ fn select_event( 2 => oper.recv(rx_app).map(|e| { QueueEvent::AsyncEvent(AsyncNotification::App(e)) }), - 3 => oper.recv(rx_ticker).map(|_| QueueEvent::Tick), + 3 => oper.recv(rx_notify).map(|_| QueueEvent::Notify), 4 => oper.recv(rx_spinner).map(|_| QueueEvent::SpinnerUpdate), _ => bail!("unknown select source"), }?; |