From 12cef5fccd42847cc01770cb5d9561064dd791cb Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Wed, 29 Apr 2020 23:33:44 +0200 Subject: [PATCH] also use status fetching for spinner state --- asyncgit/src/status.rs | 17 ++++++++++++++++- src/app.rs | 2 +- src/spinner.rs | 3 ++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/asyncgit/src/status.rs b/asyncgit/src/status.rs index 8f7c4b3a..f29792ac 100644 --- a/asyncgit/src/status.rs +++ b/asyncgit/src/status.rs @@ -3,7 +3,10 @@ use crossbeam_channel::Sender; use log::trace; use std::{ hash::Hash, - sync::{Arc, Mutex}, + sync::{ + atomic::{AtomicUsize, Ordering}, + Arc, Mutex, + }, }; use sync::status::StatusType; @@ -20,6 +23,7 @@ pub struct AsyncStatus { current: Arc>>, last: Arc>, sender: Sender, + pending: Arc, } impl AsyncStatus { @@ -29,6 +33,7 @@ impl AsyncStatus { current: Arc::new(Mutex::new(Request(0, None))), last: Arc::new(Mutex::new(Status::default())), sender, + pending: Arc::new(AtomicUsize::new(0)), } } @@ -38,6 +43,11 @@ impl AsyncStatus { last.clone() } + /// + pub fn is_pending(&self) -> bool { + self.pending.load(Ordering::Relaxed) > 0 + } + /// pub fn fetch(&mut self, request: u64) -> Option { let hash_request = hash(&request); @@ -58,7 +68,10 @@ impl AsyncStatus { let arc_current = Arc::clone(&self.current); let arc_last = Arc::clone(&self.last); let sender = self.sender.clone(); + let arc_pending = Arc::clone(&self.pending); rayon_core::spawn(move || { + arc_pending.fetch_add(1, Ordering::Relaxed); + let res = Self::get_status(); trace!("status fetched: {}", hash(&res)); @@ -74,6 +87,8 @@ impl AsyncStatus { *last = res; } + arc_pending.fetch_sub(1, Ordering::Relaxed); + sender .send(AsyncNotification::Status) .expect("error sending status"); diff --git a/src/app.rs b/src/app.rs index f40af11f..ce07f7fa 100644 --- a/src/app.rs +++ b/src/app.rs @@ -231,7 +231,7 @@ impl App { /// pub fn any_work_pending(&self) -> bool { - self.git_diff.is_pending() + self.git_diff.is_pending() || self.git_status.is_pending() } } diff --git a/src/spinner.rs b/src/spinner.rs index 924aa977..204b7a74 100644 --- a/src/spinner.rs +++ b/src/spinner.rs @@ -10,12 +10,13 @@ pub struct Spinner { } impl Spinner { - /// + /// increment spinner graphic by one pub fn update(&mut self) { self.idx += 1; self.idx %= SPINNER_CHARS.len(); } + /// draws or removes spinner char depending on `pending` state pub fn draw( &self, terminal: &mut Terminal,