Skip to content

Commit 21cbc3b

Browse files
author
Stephan Dilly
committed
generalise command gathering
1 parent 572be62 commit 21cbc3b

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

src/components/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ macro_rules! accessors {
4848
};
4949
}
5050

51+
/// returns `true` if event was consumed
5152
pub fn event_pump(
5253
ev: Event,
5354
components: &mut [&mut dyn Component],
@@ -61,6 +62,23 @@ pub fn event_pump(
6162
false
6263
}
6364

65+
/// helper fn to simplify delegating command
66+
/// gathering down into child components
67+
/// see `event_pump`,`accessors`
68+
pub fn command_pump(
69+
out: &mut Vec<CommandInfo>,
70+
force_all: bool,
71+
components: &[&dyn Component],
72+
) {
73+
for c in components {
74+
if c.commands(out, force_all) != CommandBlocking::PassingOn
75+
&& !force_all
76+
{
77+
break;
78+
}
79+
}
80+
}
81+
6482
#[derive(Copy, Clone)]
6583
pub enum ScrollType {
6684
Up,

src/tabs/stashing.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use crate::{
2+
accessors,
23
components::{
3-
CommandBlocking, CommandInfo, Component, DrawableComponent,
4-
FileTreeComponent,
4+
command_pump, event_pump, CommandBlocking, CommandInfo,
5+
Component, DrawableComponent, FileTreeComponent,
56
},
67
keys,
78
queue::{InternalEvent, NeedsUpdate, Queue},
@@ -36,6 +37,8 @@ pub struct Stashing {
3637
}
3738

3839
impl Stashing {
40+
accessors!(self, [index]);
41+
3942
///
4043
pub fn new(
4144
sender: &Sender<AsyncNotification>,
@@ -162,7 +165,7 @@ impl Component for Stashing {
162165
out: &mut Vec<CommandInfo>,
163166
force_all: bool,
164167
) -> CommandBlocking {
165-
self.index.commands(out, force_all);
168+
command_pump(out, force_all, self.components().as_slice());
166169

167170
out.push(CommandInfo::new(
168171
commands::STASHING_SAVE,
@@ -189,9 +192,7 @@ impl Component for Stashing {
189192

190193
fn event(&mut self, ev: crossterm::event::Event) -> bool {
191194
if self.visible {
192-
let conusmed = self.index.event(ev);
193-
194-
if conusmed {
195+
if event_pump(ev, self.components_mut().as_mut_slice()) {
195196
return true;
196197
}
197198

src/tabs/status.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::{
22
accessors,
33
components::{
4-
event_pump, ChangesComponent, CommandBlocking, CommandInfo,
5-
Component, DiffComponent, DrawableComponent,
4+
self, event_pump, ChangesComponent, CommandBlocking,
5+
CommandInfo, Component, DiffComponent, DrawableComponent,
66
FileTreeItemKind,
77
},
88
keys,
@@ -14,6 +14,7 @@ use asyncgit::{
1414
sync::status::StatusType, AsyncDiff, AsyncNotification,
1515
AsyncStatus, DiffParams, StatusParams,
1616
};
17+
use components::command_pump;
1718
use crossbeam_channel::Sender;
1819
use crossterm::event::Event;
1920
use strings::commands;
@@ -262,14 +263,11 @@ impl Component for Status {
262263
force_all: bool,
263264
) -> CommandBlocking {
264265
if self.visible {
265-
for c in self.components() {
266-
if c.commands(out, force_all)
267-
!= CommandBlocking::PassingOn
268-
&& !force_all
269-
{
270-
break;
271-
}
272-
}
266+
command_pump(
267+
out,
268+
force_all,
269+
self.components().as_slice(),
270+
);
273271

274272
{
275273
let focus_on_diff = self.focus == Focus::Diff;
@@ -325,10 +323,7 @@ impl Component for Status {
325323

326324
fn event(&mut self, ev: crossterm::event::Event) -> bool {
327325
if self.visible {
328-
let conusmed =
329-
event_pump(ev, self.components_mut().as_mut_slice());
330-
331-
if conusmed {
326+
if event_pump(ev, self.components_mut().as_mut_slice()) {
332327
return true;
333328
}
334329

0 commit comments

Comments
 (0)