also use status fetching for spinner state

This commit is contained in:
Stephan Dilly 2020-04-29 23:33:44 +02:00
parent f502c81187
commit 12cef5fccd
3 changed files with 19 additions and 3 deletions

View File

@ -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<Mutex<Request<u64, Status>>>,
last: Arc<Mutex<Status>>,
sender: Sender<AsyncNotification>,
pending: Arc<AtomicUsize>,
}
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<Status> {
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");

View File

@ -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()
}
}

View File

@ -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<B: Backend>(
&self,
terminal: &mut Terminal<B>,