use crate::runtime::BranchEntry;
use ratatui::layout::Rect;
use std::cell::RefCell;
use std::path::PathBuf;
#[derive(Debug, Clone)]
pub struct WorktreeRow {
pub path: PathBuf,
pub branch: Option<String>,
pub slug: String,
pub is_current: bool,
}
#[derive(Debug, Clone)]
pub struct NewWorktreeForm {
pub branch_input: String,
pub path_input: String,
pub path_dirty: bool,
pub parent: PathBuf,
pub branches: Vec<BranchEntry>,
pub filtered: Vec<usize>,
pub selected: usize,
pub focus: NewFormField,
pub error: Option<String>,
}
impl NewWorktreeForm {
pub fn show_create_sentinel(&self) -> bool {
if self.branch_input.is_empty() {
return false;
}
!self.branches.iter().any(|b| b.name == self.branch_input)
}
pub fn total_options(&self) -> usize {
self.filtered.len() + if self.show_create_sentinel() { 1 } else { 0 }
}
pub fn push_char(&mut self, c: char) {
match self.focus {
NewFormField::Path => {
self.path_input.push(c);
self.path_dirty = true;
}
NewFormField::Branch => {
self.branch_input.push(c);
self.refilter_branches();
self.sync_path_from_branch();
}
}
self.error = None;
}
pub fn pop_char(&mut self) {
match self.focus {
NewFormField::Path => {
self.path_input.pop();
self.path_dirty = true;
}
NewFormField::Branch => {
self.branch_input.pop();
self.refilter_branches();
self.sync_path_from_branch();
}
}
self.error = None;
}
pub fn toggle_focus(&mut self) {
self.focus = match self.focus {
NewFormField::Path => NewFormField::Branch,
NewFormField::Branch => NewFormField::Path,
};
}
pub fn select_next(&mut self) {
if self.focus != NewFormField::Branch {
return;
}
let total = self.total_options();
if total > 0 {
self.selected = (self.selected + 1).min(total - 1);
}
}
pub fn select_prev(&mut self) {
if self.focus != NewFormField::Branch {
return;
}
self.selected = self.selected.saturating_sub(1);
}
pub fn resolve(&self) -> Option<NewWorktreeAction> {
if self.path_input.trim().is_empty() {
return None;
}
let branch = match self.focus {
NewFormField::Path => {
if self.branch_input.trim().is_empty() {
return None;
}
if self.branches.iter().any(|b| b.name == self.branch_input) {
BranchSpec::Existing(self.branch_input.clone())
} else {
BranchSpec::CreateOff(self.branch_input.clone())
}
}
NewFormField::Branch => {
if self.selected < self.filtered.len() {
let idx = self.filtered[self.selected];
BranchSpec::Existing(self.branches[idx].name.clone())
} else if self.show_create_sentinel() {
BranchSpec::CreateOff(self.branch_input.clone())
} else {
return None;
}
}
};
Some(NewWorktreeAction {
path: self.path_input.clone(),
branch,
})
}
fn refilter_branches(&mut self) {
if self.branch_input.is_empty() {
self.filtered = (0..self.branches.len()).collect();
} else {
let q = self.branch_input.to_lowercase();
self.filtered = self
.branches
.iter()
.enumerate()
.filter(|(_, b)| b.name.to_lowercase().contains(&q))
.map(|(i, _)| i)
.collect();
}
let total = self.total_options();
self.selected = if total == 0 {
0
} else {
self.selected.min(total - 1)
};
}
fn sync_path_from_branch(&mut self) {
if self.path_dirty {
return;
}
let dir = if self.branch_input.is_empty() {
String::new()
} else {
crate::runtime::slugify(&self.branch_input)
};
if dir.is_empty() {
self.path_input.clear();
} else {
self.path_input = self.parent.join(dir).display().to_string();
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NewFormField {
Path,
Branch,
}
#[derive(Debug, Clone)]
pub struct NewWorktreeAction {
pub path: String,
pub branch: BranchSpec,
}
#[derive(Debug, Clone)]
pub enum BranchSpec {
Existing(String),
CreateOff(String),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SwitcherConfirm {
Switched,
OpenCreateForm,
NoOp,
}
#[derive(Debug, Clone)]
pub struct WorktreeSwitcher {
pub entries: Vec<WorktreeRow>,
pub selected: usize,
pub creating: Option<NewWorktreeForm>,
pub row_rects: RefCell<Vec<Rect>>,
}
impl WorktreeSwitcher {
pub fn new_row_index(&self) -> usize {
self.entries.len()
}
pub fn total_rows(&self) -> usize {
self.entries.len() + 1
}
pub fn select_next(&mut self) {
let total = self.total_rows();
if total > 0 {
self.selected = (self.selected + 1).min(total - 1);
}
}
pub fn select_prev(&mut self) {
self.selected = self.selected.saturating_sub(1);
}
pub fn select_at(&mut self, idx: usize) {
let total = self.total_rows();
if total > 0 {
self.selected = idx.min(total - 1);
}
}
}