use crate::tui::dialogs::switcher::{NewFormField, NewWorktreeForm, WorktreeSwitcher};
use crate::tui::ui::{SELECTION_BG, SELECTION_FG, centered_rect};
use ratatui::Frame;
use ratatui::layout::{Constraint, Direction, Layout, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{
Block, BorderType, Borders, Clear, HighlightSpacing, List, ListItem, ListState, Padding,
Paragraph,
};
pub fn render(switcher: &WorktreeSwitcher, accent: Color, frame: &mut Frame) {
if let Some(form) = switcher.creating.as_ref() {
render_form(form, accent, frame);
} else {
render_list(switcher, accent, frame);
}
}
fn render_list(switcher: &WorktreeSwitcher, accent: Color, frame: &mut Frame) {
let total_rows = switcher.total_rows();
let height = (total_rows as u16 + 6).min(20);
let area = centered_rect(frame.area(), 60, height);
let block = Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.padding(Padding::new(2, 2, 1, 1))
.title(Line::from(vec![
Span::raw(" "),
Span::styled(
"switch worktree",
Style::default().fg(accent).add_modifier(Modifier::BOLD),
),
Span::raw(" "),
]));
frame.render_widget(Clear, area);
let inner = block.inner(area);
frame.render_widget(block, area);
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(total_rows as u16),
Constraint::Length(1), Constraint::Length(1), ])
.split(inner);
let items: Vec<ListItem> = switcher
.entries
.iter()
.map(|row| {
let current_marker = if row.is_current {
Span::styled(" ●", Style::default().fg(Color::Green))
} else {
Span::raw(" ")
};
let branch_label = row
.branch
.clone()
.unwrap_or_else(|| "<detached>".to_string());
ListItem::new(Line::from(vec![
Span::raw(format!("{branch_label:<24}")),
current_marker,
Span::raw(" "),
Span::styled(
row.path.display().to_string(),
Style::default().fg(Color::DarkGray),
),
]))
})
.chain(std::iter::once(ListItem::new(Line::from(Span::styled(
"+ new worktree",
Style::default().fg(accent),
)))))
.collect();
let list = List::new(items)
.highlight_style(
Style::default()
.fg(SELECTION_FG)
.bg(SELECTION_BG)
.add_modifier(Modifier::BOLD),
)
.highlight_spacing(HighlightSpacing::Always);
let mut state = ListState::default();
state.select(Some(switcher.selected));
frame.render_stateful_widget(list, chunks[0], &mut state);
let offset = state.offset();
let mut rects = switcher.row_rects.borrow_mut();
rects.clear();
rects.resize(total_rows, Rect::default());
for i in 0..total_rows {
if i < offset {
continue;
}
let row = (i - offset) as u16;
if row >= chunks[0].height {
break;
}
rects[i] = Rect {
x: chunks[0].x,
y: chunks[0].y + row,
width: chunks[0].width,
height: 1,
};
}
frame.render_widget(
Paragraph::new(Line::from(Span::styled(
"↑↓ nav · enter switch · esc cancel",
Style::default().fg(Color::DarkGray),
))),
chunks[2],
);
}
fn render_form(form: &NewWorktreeForm, accent: Color, frame: &mut Frame) {
const MAX_BRANCH_ROWS: usize = 8;
let total_options = form.total_options();
let list_rows = total_options.clamp(1, MAX_BRANCH_ROWS);
let body_rows = 2 + 1 + list_rows + 1 + 1 + if form.error.is_some() { 2 } else { 0 };
let height = (body_rows as u16 + 4).min(24);
let area = centered_rect(frame.area(), 64, height);
let block = Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.padding(Padding::new(2, 2, 1, 1))
.title(Line::from(vec![
Span::raw(" "),
Span::styled(
"new worktree",
Style::default().fg(accent).add_modifier(Modifier::BOLD),
),
Span::raw(" "),
]));
let path_focus = matches!(form.focus, NewFormField::Path);
let branch_focus = matches!(form.focus, NewFormField::Branch);
frame.render_widget(Clear, area);
let inner = block.inner(area);
frame.render_widget(block, area);
let mut constraints: Vec<Constraint> = vec![
Constraint::Length(1), Constraint::Length(1), Constraint::Length(1), Constraint::Length(list_rows as u16),
Constraint::Length(1), Constraint::Length(1), ];
if form.error.is_some() {
constraints.push(Constraint::Length(1)); constraints.push(Constraint::Length(1)); }
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints(constraints)
.split(inner);
frame.render_widget(
Paragraph::new(field_row(
"branch",
&form.branch_input,
branch_focus,
accent,
)),
chunks[0],
);
frame.render_widget(
Paragraph::new(field_row("path", &form.path_input, path_focus, accent)),
chunks[1],
);
if total_options == 0 {
frame.render_widget(
Paragraph::new(Line::from(Span::styled(
"(no branches)",
Style::default().fg(Color::DarkGray),
))),
chunks[3],
);
} else {
let items: Vec<ListItem> = (0..total_options)
.map(|option_idx| ListItem::new(branch_row(form, option_idx, accent)))
.collect();
let highlight_style = if branch_focus {
Style::default()
.fg(SELECTION_FG)
.bg(SELECTION_BG)
.add_modifier(Modifier::BOLD)
} else {
Style::default()
};
let list = List::new(items)
.highlight_style(highlight_style)
.highlight_spacing(HighlightSpacing::Always);
let mut state = ListState::default();
state.select(if branch_focus {
Some(form.selected)
} else {
None
});
frame.render_stateful_widget(list, chunks[3], &mut state);
}
frame.render_widget(
Paragraph::new(Line::from(Span::styled(
"↑↓ pick · tab edit path · enter create · esc back",
Style::default().fg(Color::DarkGray),
))),
chunks[5],
);
if let Some(err) = form.error.as_ref() {
frame.render_widget(
Paragraph::new(Line::from(Span::styled(
err.clone(),
Style::default().fg(Color::Red),
))),
chunks[7],
);
}
}
fn branch_row(form: &NewWorktreeForm, option_idx: usize, accent: Color) -> Line<'static> {
let is_sentinel = option_idx == form.filtered.len() && form.show_create_sentinel();
if is_sentinel {
return Line::from(vec![
Span::raw(" "),
Span::styled(
format!("+ create branch '{}' off HEAD", form.branch_input),
Style::default().fg(accent),
),
]);
}
let entry = &form.branches[form.filtered[option_idx]];
let mut spans = vec![Span::raw(" "), Span::raw(entry.name.clone())];
if entry.remote_only {
spans.push(Span::raw(" "));
spans.push(Span::styled(
"[remote]".to_string(),
Style::default().fg(Color::DarkGray),
));
}
Line::from(spans)
}
fn field_row(label: &'static str, value: &str, focused: bool, accent: Color) -> Line<'static> {
let label_style = if focused {
Style::default().fg(accent).add_modifier(Modifier::BOLD)
} else {
Style::default().fg(Color::DarkGray)
};
let value_style = if focused {
Style::default().add_modifier(Modifier::BOLD)
} else {
Style::default()
};
let cursor_span = if focused {
Span::styled("█", Style::default().fg(accent))
} else {
Span::raw("")
};
Line::from(vec![
Span::styled(format!("{label:<8}"), label_style),
Span::styled(value.to_string(), value_style),
cursor_span,
])
}