Skip to content

Commit 414d06f

Browse files
committed
improve interactive mode controls
1 parent bdcc0b0 commit 414d06f

File tree

4 files changed

+54
-29
lines changed

4 files changed

+54
-29
lines changed

src/commands/interactive/command.rs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ where
167167
KeyCode::Esc | KeyCode::Char('q') => Ok(LoopControl::Exit(None)),
168168
KeyCode::Tab | KeyCode::BackTab => {
169169
if key.code == KeyCode::Tab {
170-
self.focus = self.focus.next();
170+
self.focus_forward();
171171
} else {
172-
self.focus = self.focus.prev();
172+
self.focus_backward();
173173
}
174174
Ok(LoopControl::Continue)
175175
}
@@ -202,6 +202,52 @@ where
202202
}
203203
}
204204

205+
fn focus_forward(&mut self) {
206+
self.focus = match self.focus {
207+
Focus::Worktrees => {
208+
if self.selected.is_some() {
209+
Focus::Actions
210+
} else if super::GLOBAL_ACTIONS.is_empty() {
211+
Focus::Worktrees
212+
} else {
213+
Focus::GlobalActions
214+
}
215+
}
216+
Focus::Actions => Focus::Worktrees,
217+
Focus::GlobalActions => {
218+
if self.selected.is_some() {
219+
Focus::Actions
220+
} else {
221+
Focus::Worktrees
222+
}
223+
}
224+
};
225+
}
226+
227+
fn focus_backward(&mut self) {
228+
self.focus = match self.focus {
229+
Focus::Worktrees => {
230+
if self.selected.is_some() {
231+
Focus::Actions
232+
} else if super::GLOBAL_ACTIONS.is_empty() {
233+
Focus::Worktrees
234+
} else {
235+
Focus::GlobalActions
236+
}
237+
}
238+
Focus::Actions => {
239+
if self.selected.is_some() {
240+
Focus::Worktrees
241+
} else if super::GLOBAL_ACTIONS.is_empty() {
242+
Focus::Actions
243+
} else {
244+
Focus::GlobalActions
245+
}
246+
}
247+
Focus::GlobalActions => Focus::Worktrees,
248+
};
249+
}
250+
205251
fn handle_enter(&mut self) -> Result<LoopControl> {
206252
match self.focus {
207253
Focus::Worktrees => {

src/commands/interactive/mod.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,6 @@ pub(crate) enum Focus {
3636
GlobalActions,
3737
}
3838

39-
impl Focus {
40-
pub(crate) fn next(self) -> Self {
41-
match self {
42-
Focus::Worktrees => Focus::Actions,
43-
Focus::Actions => Focus::GlobalActions,
44-
Focus::GlobalActions => Focus::Worktrees,
45-
}
46-
}
47-
48-
pub(crate) fn prev(self) -> Self {
49-
match self {
50-
Focus::Worktrees => Focus::GlobalActions,
51-
Focus::Actions => Focus::Worktrees,
52-
Focus::GlobalActions => Focus::Actions,
53-
}
54-
}
55-
}
56-
5739
pub(crate) const GLOBAL_ACTIONS: [&str; 2] = ["Create worktree", "Cd to root dir"];
5840

5941
#[derive(Clone, Debug, PartialEq, Eq)]

src/commands/interactive/tests.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ fn create_action_adds_new_worktree() -> Result<()> {
203203
let backend = TestBackend::new(60, 18);
204204
let terminal = Terminal::new(backend)?;
205205
let events = StubEvents::new(vec![
206-
key(KeyCode::Tab),
207-
key(KeyCode::Tab),
206+
key(KeyCode::Up),
207+
key(KeyCode::Up),
208208
key(KeyCode::Enter),
209209
char_key('n'),
210210
char_key('e'),
@@ -248,8 +248,8 @@ fn cancelling_create_leaves_state_unchanged() -> Result<()> {
248248
let backend = TestBackend::new(60, 18);
249249
let terminal = Terminal::new(backend)?;
250250
let events = StubEvents::new(vec![
251-
key(KeyCode::Tab),
252-
key(KeyCode::Tab),
251+
key(KeyCode::Up),
252+
key(KeyCode::Up),
253253
key(KeyCode::Enter),
254254
key(KeyCode::Esc),
255255
key(KeyCode::Esc),
@@ -277,9 +277,7 @@ fn cd_to_root_global_action_exits() -> Result<()> {
277277
let backend = TestBackend::new(40, 12);
278278
let terminal = Terminal::new(backend)?;
279279
let events = StubEvents::new(vec![
280-
key(KeyCode::Tab),
281-
key(KeyCode::Tab),
282-
key(KeyCode::Right),
280+
key(KeyCode::Up),
283281
key(KeyCode::Enter),
284282
]);
285283

@@ -328,7 +326,6 @@ fn up_from_top_after_tabbing_picks_last_global_action() -> Result<()> {
328326
let backend = TestBackend::new(40, 12);
329327
let terminal = Terminal::new(backend)?;
330328
let events = StubEvents::new(vec![
331-
key(KeyCode::Tab),
332329
key(KeyCode::Tab),
333330
key(KeyCode::Tab),
334331
key(KeyCode::Up),

src/commands/interactive/view.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl Snapshot {
147147

148148
let actions = Paragraph::new(Line::from(spans)).block(
149149
Block::default()
150-
.title("Worktree Actions")
150+
.title("Worktree Actions (Tab key)")
151151
.borders(Borders::ALL),
152152
);
153153
frame.render_widget(actions, detail_chunks[1]);

0 commit comments

Comments
 (0)