Skip to content

Commit 197f359

Browse files
Refactor tab (ChrisTitusTech#347)
Co-authored-by: Chris Titus <contact@christitus.com>
1 parent 9ed0a48 commit 197f359

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

tui/src/hint.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ pub fn draw_shortcuts(state: &AppState, frame: &mut Frame, area: Rect) {
120120
hints.push(Shortcut::new(vec!["q", "CTRL-c"], "Exit linutil"));
121121

122122
if state.at_root() {
123-
hints.push(Shortcut::new(vec!["h", "Left", "Tab"], "Focus tab list"));
123+
hints.push(Shortcut::new(vec!["h", "Left"], "Focus tab list"));
124124
hints.push(get_list_item_shortcut(state));
125125
} else {
126126
if state.selected_item_is_up_dir() {
@@ -136,14 +136,14 @@ pub fn draw_shortcuts(state: &AppState, frame: &mut Frame, area: Rect) {
136136
hints.push(Shortcut::new(vec!["d"], "Command Description"));
137137
}
138138
}
139-
hints.push(Shortcut::new(vec!["Tab"], "Focus tab list"));
140139
};
141140

142141
hints.push(Shortcut::new(vec!["k", "Up"], "Select item above"));
143142
hints.push(Shortcut::new(vec!["j", "Down"], "Select item below"));
144143
hints.push(Shortcut::new(vec!["t"], "Next theme"));
145144
hints.push(Shortcut::new(vec!["T"], "Previous theme"));
146-
145+
hints.push(Shortcut::new(vec!["Tab"], "Next tab"));
146+
hints.push(Shortcut::new(vec!["Shift-Tab"], "Previous tab"));
147147
ShortcutList {
148148
scope_name: "Item list",
149149
hints,
@@ -154,11 +154,13 @@ pub fn draw_shortcuts(state: &AppState, frame: &mut Frame, area: Rect) {
154154
scope_name: "Tab list",
155155
hints: vec![
156156
Shortcut::new(vec!["q", "CTRL-c"], "Exit linutil"),
157-
Shortcut::new(vec!["l", "Right", "Tab", "Enter"], "Focus action list"),
157+
Shortcut::new(vec!["l", "Right", "Enter"], "Focus action list"),
158158
Shortcut::new(vec!["k", "Up"], "Select item above"),
159159
Shortcut::new(vec!["j", "Down"], "Select item below"),
160160
Shortcut::new(vec!["t"], "Next theme"),
161161
Shortcut::new(vec!["T"], "Previous theme"),
162+
Shortcut::new(vec!["Tab"], "Next tab"),
163+
Shortcut::new(vec!["Shift-Tab"], "Previous tab"),
162164
],
163165
},
164166

tui/src/state.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,31 @@ impl AppState {
247247
return true;
248248
}
249249

250+
// Handle key only when Tablist or List is focused
251+
// Prevents exiting the application even when a command is running
252+
// Add keys here which should work on both TabList and List
253+
if matches!(self.focus, Focus::TabList | Focus::List) {
254+
match key.code {
255+
KeyCode::Tab => {
256+
if self.current_tab.selected().unwrap() == self.tabs.len() - 1 {
257+
self.current_tab.select_first(); // Select first tab when it is at last
258+
} else {
259+
self.current_tab.select_next();
260+
}
261+
self.refresh_tab();
262+
}
263+
KeyCode::BackTab => {
264+
if self.current_tab.selected().unwrap() == 0 {
265+
self.current_tab.select(Some(self.tabs.len() - 1)); // Select last tab when it is at first
266+
} else {
267+
self.current_tab.select_previous();
268+
}
269+
self.refresh_tab();
270+
}
271+
_ => {}
272+
}
273+
}
274+
250275
match &mut self.focus {
251276
Focus::FloatingWindow(command) => {
252277
if command.handle_key_event(key) {
@@ -268,9 +293,7 @@ impl AppState {
268293
}
269294

270295
Focus::TabList => match key.code {
271-
KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right | KeyCode::Tab => {
272-
self.focus = Focus::List
273-
}
296+
KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => self.focus = Focus::List,
274297

275298
KeyCode::Char('j') | KeyCode::Down
276299
if self.current_tab.selected().unwrap() + 1 < self.tabs.len() =>
@@ -304,7 +327,6 @@ impl AppState {
304327
}
305328
}
306329
KeyCode::Char('/') => self.enter_search(),
307-
KeyCode::Tab => self.focus = Focus::TabList,
308330
KeyCode::Char('t') => self.theme.next(),
309331
KeyCode::Char('T') => self.theme.prev(),
310332
_ => {}

0 commit comments

Comments
 (0)