From 9ef7ba021629c9fa3e111798c61b35a02dfaae20 Mon Sep 17 00:00:00 2001 From: duckyriley Date: Mon, 23 Nov 2026 21:21:22 +0800 Subject: Fix inactive tab closing and add Lua shortcut actions and themes --- src/main.rs | 165 +++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 108 insertions(+), 57 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index f6a6dfb..f2e2165 100644 --- a/src/main.rs +++ b/src/main.rs @@ -80,13 +80,23 @@ impl Ducky { cx.notify(); } fn close_tab(&mut self, cx: &mut Context) { - self.tabs.remove(self.active); + self.close_tab_at(self.active, cx); + } + fn close_tab_at(&mut self, index: usize, cx: &mut Context) { + if index >= self.tabs.len() { + return; + } + let terminal = self.tabs.remove(index); + // Waiting for an old shell to exit must never stall GPUI's event loop. + std::thread::spawn(move || drop(terminal)); if self.tabs.is_empty() { cx.quit(); return; } - self.active = self.active.min(self.tabs.len() - 1); + self.active = active_after_close(self.active, index, self.tabs.len()); self.selection = None; + self.dragging = false; + self.composition.clear(); cx.notify(); } fn reload(&mut self, cx: &mut Context) { @@ -147,61 +157,59 @@ impl Ducky { } fn key(&mut self, event: &KeyDownEvent, window: &mut Window, cx: &mut Context) { let key = &event.keystroke; - let m = key.modifiers; - if key.unparse() == self.config.status_bar_toggle { - self.config.show_status_bar = !self.config.show_status_bar; - cx.notify(); + // User byte bindings can override application actions deliberately. + let stroke = key.unparse(); + if let Some(data) = self.config.bindings.get(&stroke).cloned() { + self.send(data.as_bytes(), cx); cx.stop_propagation(); return; } - if m.control && m.shift { - match key.key.to_lowercase().as_str() { - "t" => self.new_tab(cx), - "w" => self.close_tab(cx), - "r" => self.reload(cx), - "c" => self.copy(cx), - "v" => self.paste(cx), - "tab" => { + let action = self.config.action_for(key); + if let Some(action) = action { + match action.as_str() { + "new_tab" => self.new_tab(cx), + "close_tab" => self.close_tab(cx), + "copy" => self.copy(cx), + "paste" => self.paste(cx), + "reload" => self.reload(cx), + "toggle_status" => { + self.config.show_status_bar = !self.config.show_status_bar; + } + "interrupt" => self.send(&[3], cx), + "next_tab" => { + self.active = (self.active + 1) % self.tabs.len(); + self.selection = None; + } + "previous_tab" => { self.active = (self.active + self.tabs.len() - 1) % self.tabs.len(); self.selection = None; - cx.notify(); } - "=" | "+" => { + "font_larger" => { self.config.font_size = (self.config.font_size + 1.).min(48.); self.config.line_height = self.config.font_size * 1.4; - cx.notify(); } - "-" => { + "font_smaller" => { self.config.font_size = (self.config.font_size - 1.).max(8.); self.config.line_height = self.config.font_size * 1.4; - cx.notify(); } + "scroll_up" => self.tabs[self.active].scroll(20), + "scroll_down" => self.tabs[self.active].scroll(-20), _ => { - if let Some(data) = self.config.bindings.get(&key.unparse()).cloned() { - self.send(data.as_bytes(), cx); - } else { - return; + if let Some(index) = action + .strip_prefix("tab:") + .and_then(|s| s.parse::().ok()) + { + if index <= self.tabs.len() { + self.active = index - 1; + self.selection = None; + } } } } - cx.stop_propagation(); - return; - } - if m.control && key.key == "tab" { - self.active = (self.active + 1) % self.tabs.len(); - self.selection = None; - cx.notify(); - cx.stop_propagation(); - return; - } - if let Some(data) = self.config.bindings.get(&key.unparse()).cloned() { - self.send(data.as_bytes(), cx); - cx.stop_propagation(); - return; - } - if m.shift && (key.key == "pageup" || key.key == "pagedown") { - self.tabs[self.active].scroll(if key.key == "pageup" { 20 } else { -20 }); - cx.notify(); + if !self.tabs.is_empty() { + cx.notify(); + } + window.prevent_default(); cx.stop_propagation(); return; } @@ -365,6 +373,12 @@ impl Ducky { } } +fn active_after_close(active: usize, closed: usize, remaining: usize) -> usize { + active + .saturating_sub(usize::from(closed < active)) + .min(remaining.saturating_sub(1)) +} + fn key_bytes(key: &Keystroke, application: bool) -> Option> { let m = key.modifiers; let code = match key.key.as_str() { @@ -473,7 +487,38 @@ impl Render for Ducky { "" } )) - .on_click(cx.listener(move |this, _, _, cx| { + .child( + div() + .id(("close-tab", i)) + .size(px(18.)) + .rounded_sm() + .flex() + .items_center() + .justify_center() + .text_color(rgb(c.muted)) + .hover(|s| s.bg(rgb(c.border)).text_color(rgb(c.foreground))) + .child("×") + .on_mouse_down( + MouseButton::Left, + cx.listener(move |this, _, window, cx| { + cx.stop_propagation(); + window.prevent_default(); + this.close_tab_at(i, cx); + window.focus(&this.focus); + }), + ), + ) + .on_mouse_down( + MouseButton::Middle, + cx.listener(move |this, _, window, cx| { + cx.stop_propagation(); + window.prevent_default(); + this.close_tab_at(i, cx); + window.focus(&this.focus); + }), + ) + .on_click(cx.listener(move |this, _, window, cx| { + window.focus(&this.focus); this.active = i; this.selection = None; cx.notify(); @@ -486,11 +531,14 @@ impl Render for Ducky { .unwrap() .screen() .scrollback(); - let toggle = c - .status_bar_toggle - .replace('-', "+") - .replace("ctrl", "Ctrl") - .replace("shift", "Shift"); + let toggle = c.shortcut("toggle_status"); + let hints = format!( + "{} tab · {} copy · {} paste · {} reload", + c.shortcut("new_tab"), + c.shortcut("copy"), + c.shortcut("paste"), + c.shortcut("reload") + ); div() .size_full() .flex() @@ -690,15 +738,11 @@ impl Render for Ducky { }), )) .child( - div() - .flex() - .gap_4() - .child("Ctrl+Shift T tab · C copy · V paste · R reload") - .child( - div() - .text_color(rgb(c.accent)) - .child(format!("{toggle} hide")), - ), + div().flex().gap_4().child(hints).child( + div() + .text_color(rgb(c.accent)) + .child(format!("{toggle} hide")), + ), ), ) }) @@ -791,7 +835,7 @@ fn main() -> anyhow::Result<()> { } if args.iter().any(|s| s == "--help") { println!( - "Ducky Shell\n\nUsage: ducky [--check-config | --config-dir | --version]\n\nCtrl+Shift+T/W: new/close tab\nCtrl+Tab: next tab\nCtrl+Shift+C/V: copy/paste\nCtrl+Shift+R: reload Lua config\nCtrl+Shift+B: toggle status bar\nCtrl+Shift++/-: font size\nShift+PageUp/Down: scrollback" + "Ducky Shell\n\nUsage: ducky [--check-config | --config-dir | --version]\n\nCtrl+T/W: new/close tab\nAlt+1..9 / Alt+0: select tab\nCtrl+Tab: next tab\nCtrl+C/V: copy/paste\nCtrl+Alt+C: terminal interrupt\nCtrl+R: reload Lua config\nCtrl+B: toggle status bar\nCtrl+=/-: font size\nShift+PageUp/Down: scrollback" ); return Ok(()); } @@ -849,6 +893,13 @@ fn main() -> anyhow::Result<()> { mod tests { use super::*; #[test] + fn closing_earlier_or_inactive_tabs_preserves_selection() { + assert_eq!(active_after_close(3, 0, 3), 2); + assert_eq!(active_after_close(0, 2, 3), 0); + assert_eq!(active_after_close(2, 2, 2), 1); + assert_eq!(active_after_close(1, 1, 3), 1); + } + #[test] fn terminal_key_encoding() { assert_eq!( key_bytes(&Keystroke::parse("ctrl-c").unwrap(), false), -- cgit v1.2.3