mirror of
https://github.com/gitui-org/gitui
synced 2026-05-24 09:28:21 +00:00
introduce queue to get rid of event() return type
This commit is contained in:
parent
176f2ee2fa
commit
ef4d3f7b56
4 changed files with 61 additions and 18 deletions
45
src/app.rs
45
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<CommandInfo>,
|
||||
queue: Queue,
|
||||
}
|
||||
|
||||
// public interface
|
||||
impl App {
|
||||
///
|
||||
pub fn new(sender: Sender<AsyncNotification>) -> 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<CommandInfo> {
|
||||
let mut res = Vec::new();
|
||||
|
||||
|
|
|
|||
|
|
@ -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 => {
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ mod app;
|
|||
mod components;
|
||||
mod keys;
|
||||
mod poll;
|
||||
mod queue;
|
||||
mod strings;
|
||||
mod ui;
|
||||
|
||||
|
|
|
|||
10
src/queue.rs
Normal file
10
src/queue.rs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
use std::{cell::RefCell, collections::VecDeque, rc::Rc};
|
||||
|
||||
///
|
||||
pub enum InternalEvent {
|
||||
///
|
||||
ResetFile(String),
|
||||
}
|
||||
|
||||
///
|
||||
pub type Queue = Rc<RefCell<VecDeque<InternalEvent>>>;
|
||||
Loading…
Reference in a new issue