From ef4d3f7b564f4cfa2c50b202c6293d2bd7170d25 Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Fri, 3 Apr 2020 17:52:41 +0200 Subject: [PATCH] introduce queue to get rid of event() return type --- src/app.rs | 45 +++++++++++++++++++++++++++++++-------- src/components/changes.rs | 23 ++++++++++++-------- src/main.rs | 1 + src/queue.rs | 10 +++++++++ 4 files changed, 61 insertions(+), 18 deletions(-) create mode 100644 src/queue.rs diff --git a/src/app.rs b/src/app.rs index dc02d697..a514bebc 100644 --- a/src/app.rs +++ b/src/app.rs @@ -4,17 +4,19 @@ use crate::{ CommitComponent, Component, DiffComponent, DrawableComponent, EventUpdate, HelpComponent, }, - keys, strings, + keys, + queue::{InternalEvent, Queue}, + strings, }; use asyncgit::{ - current_tick, AsyncDiff, AsyncNotification, AsyncStatus, - DiffParams, + current_tick, sync, AsyncDiff, AsyncNotification, AsyncStatus, + DiffParams, CWD, }; use crossbeam_channel::Sender; use crossterm::event::Event; use itertools::Itertools; use log::trace; -use std::borrow::Cow; +use std::{borrow::Cow, path::Path}; use strings::commands; use tui::{ backend::Backend, @@ -52,12 +54,14 @@ pub struct App { git_diff: AsyncDiff, git_status: AsyncStatus, current_commands: Vec, + queue: Queue, } // public interface impl App { /// pub fn new(sender: Sender) -> Self { + let queue = Queue::default(); Self { focus: Focus::WorkDir, diff_target: DiffTarget::WorkingDir, @@ -68,16 +72,19 @@ impl App { strings::TITLE_STATUS, true, true, + queue.clone(), ), index: ChangesComponent::new( strings::TITLE_INDEX, false, false, + queue.clone(), ), diff: DiffComponent::default(), git_diff: AsyncDiff::new(sender.clone()), git_status: AsyncStatus::new(sender), current_commands: Vec::new(), + queue, } } @@ -159,11 +166,7 @@ impl App { EventUpdate::Diff => self.update_diff(), _ => (), } - - return; - } - - if let Event::Key(k) = ev { + } else if let Event::Key(k) = ev { match k { keys::EXIT_1 | keys::EXIT_2 => self.do_quit = true, keys::FOCUS_WORKDIR => { @@ -180,6 +183,8 @@ impl App { _ => (), }; } + + self.process_queue(); } /// @@ -253,6 +258,28 @@ impl App { self.update_commands(); } + fn process_queue(&mut self) { + loop { + let front = self.queue.borrow_mut().pop_front(); + if let Some(e) = front { + self.process_internal_event(&e); + } else { + break; + } + } + self.queue.borrow_mut().clear(); + } + + fn process_internal_event(&mut self, ev: &InternalEvent) { + match ev { + InternalEvent::ResetFile(p) => { + if sync::reset_workdir(CWD, Path::new(p.as_str())) { + self.update(); + } + } + }; + } + fn commands(&self, force_all: bool) -> Vec { let mut res = Vec::new(); diff --git a/src/components/changes.rs b/src/components/changes.rs index e2d03307..8ca2dabc 100644 --- a/src/components/changes.rs +++ b/src/components/changes.rs @@ -1,7 +1,9 @@ use super::{CommandBlocking, DrawableComponent, EventUpdate}; use crate::{ components::{CommandInfo, Component}, - keys, strings, ui, + keys, + queue::{InternalEvent, Queue}, + strings, ui, }; use asyncgit::{hash, sync, StatusItem, StatusItemType, CWD}; use crossterm::event::Event; @@ -28,6 +30,7 @@ pub struct ChangesComponent { focused: bool, show_selection: bool, is_working_dir: bool, + queue: Queue, } impl ChangesComponent { @@ -36,6 +39,7 @@ impl ChangesComponent { title: &str, focus: bool, is_working_dir: bool, + queue: Queue, ) -> Self { Self { title: title.to_string(), @@ -45,6 +49,7 @@ impl ChangesComponent { focused: focus, show_selection: focus, is_working_dir, + queue, } } @@ -115,13 +120,13 @@ impl ChangesComponent { false } - fn reset_workdir(&mut self) -> bool { + fn dispatch_reset_workdir(&mut self) -> bool { if let Some(i) = self.selection() { - let path = Path::new(i.path.as_str()); + self.queue + .borrow_mut() + .push_back(InternalEvent::ResetFile(i.path.clone())); - if sync::reset_workdir(CWD, path) { - return true; - } + return true; } false } @@ -217,10 +222,10 @@ impl Component for ChangesComponent { } } keys::STATUS_RESET_FILE => { - if self.reset_workdir() { - Some(EventUpdate::All) - } else { + if self.dispatch_reset_workdir() { Some(EventUpdate::None) + } else { + None } } keys::MOVE_DOWN => { diff --git a/src/main.rs b/src/main.rs index da8cf467..d49642d2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ mod app; mod components; mod keys; mod poll; +mod queue; mod strings; mod ui; diff --git a/src/queue.rs b/src/queue.rs new file mode 100644 index 00000000..462cdd1c --- /dev/null +++ b/src/queue.rs @@ -0,0 +1,10 @@ +use std::{cell::RefCell, collections::VecDeque, rc::Rc}; + +/// +pub enum InternalEvent { + /// + ResetFile(String), +} + +/// +pub type Queue = Rc>>;