use crate::tui::app::{App, ClickTarget};
use crate::tui::dialogs::switcher::{BranchSpec, NewWorktreeAction, SwitcherConfirm};
use crate::tui::terminal::{ClickKind, hit_test, resolve_click};
use crossterm::event::{KeyCode, KeyModifiers, MouseButton, MouseEvent, MouseEventKind};
pub async fn handle_key(app: &mut App, code: KeyCode, modifiers: KeyModifiers) {
if modifiers.contains(KeyModifiers::CONTROL) && matches!(code, KeyCode::Char('c')) {
app.quit();
return;
}
if app.switcher().and_then(|s| s.creating.as_ref()).is_some() {
handle_form_key(app, code).await;
return;
}
match code {
KeyCode::Esc => app.close_switcher(),
KeyCode::Up | KeyCode::Char('k') => {
if let Some(s) = app.switcher_mut() {
s.select_prev();
}
}
KeyCode::Down | KeyCode::Char('j') => {
if let Some(s) = app.switcher_mut() {
s.select_next();
}
}
KeyCode::Enter => activate_selection(app).await,
_ => {}
}
}
async fn handle_form_key(app: &mut App, code: KeyCode) {
match code {
KeyCode::Esc => app.switcher_form_cancel(),
KeyCode::Tab => {
if let Some(f) = app.switcher_form_mut() {
f.toggle_focus();
}
}
KeyCode::Up => {
if let Some(f) = app.switcher_form_mut() {
f.select_prev();
}
}
KeyCode::Down => {
if let Some(f) = app.switcher_form_mut() {
f.select_next();
}
}
KeyCode::Backspace => {
if let Some(f) = app.switcher_form_mut() {
f.pop_char();
}
}
KeyCode::Enter => {
let Some(action) = app.switcher_form().and_then(|f| f.resolve()) else {
return;
};
let project_root = app.project_root().to_path_buf();
let result = create_worktree(&project_root, &action).await;
app.switcher_form_finish(result);
}
KeyCode::Char(c) => {
if let Some(f) = app.switcher_form_mut() {
f.push_char(c);
}
}
_ => {}
}
}
pub async fn handle_mouse(app: &mut App, me: MouseEvent) {
if app.switcher().is_some_and(|s| s.creating.is_some()) {
return;
}
match me.kind {
MouseEventKind::Down(MouseButton::Left) => {
let Some(idx) = app
.switcher()
.and_then(|s| hit_test(&s.row_rects.borrow(), me.column, me.row))
else {
return;
};
let target = ClickTarget::SwitcherRow(idx);
match resolve_click(app, target) {
ClickKind::Select => {
if let Some(s) = app.switcher_mut() {
s.select_at(idx);
}
}
ClickKind::Activate => {
if let Some(s) = app.switcher_mut() {
s.select_at(idx);
}
activate_selection(app).await;
}
}
}
MouseEventKind::ScrollDown => {
if let Some(s) = app.switcher_mut() {
s.select_next();
}
}
MouseEventKind::ScrollUp => {
if let Some(s) = app.switcher_mut() {
s.select_prev();
}
}
_ => {}
}
}
async fn activate_selection(app: &mut App) {
match app.switcher_confirm() {
SwitcherConfirm::OpenCreateForm => {
let project_root = app.project_root().to_path_buf();
let branches = crate::runtime::list_branches(&project_root).await;
let parent = crate::runtime::git_toplevel(&project_root)
.await
.and_then(|tl| tl.parent().map(|p| p.to_path_buf()));
app.open_create_form(branches, parent);
}
SwitcherConfirm::Switched | SwitcherConfirm::NoOp => {}
}
}
async fn create_worktree(
project_root: &std::path::Path,
action: &NewWorktreeAction,
) -> Result<std::path::PathBuf, String> {
let path = action.path.trim();
if path.is_empty() {
return Err("path is required".into());
}
let mut argv: Vec<String> = vec!["worktree".into(), "add".into()];
match &action.branch {
BranchSpec::Existing(name) => {
argv.push(path.to_string());
argv.push(name.clone());
}
BranchSpec::CreateOff(name) => {
argv.push("-b".into());
argv.push(name.clone());
argv.push(path.to_string());
}
}
let output = tokio::process::Command::new("git")
.args(&argv)
.current_dir(project_root)
.output()
.await
.map_err(|e| format!("failed to run git: {e}"))?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
return Err(if stderr.is_empty() {
format!(
"git worktree add failed (exit {})",
output.status.code().unwrap_or(-1)
)
} else {
stderr
});
}
let pb = std::path::PathBuf::from(path);
Ok(std::fs::canonicalize(&pb).unwrap_or(pb))
}