1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
use color_eyre::Result;
use crossterm::event::{KeyCode, KeyEvent, MouseButton, MouseEvent, MouseEventKind};
use ratatui::{
Frame,
layout::{Constraint, Direction, Layout, Rect},
style::{Modifier, Style},
text::{Line, Span},
widgets::{Block, Borders, List, ListItem, Paragraph},
};
use tokio::sync::mpsc::UnboundedSender;
use crate::action::Action;
use crate::components::Component;
use crate::repo_id::RepoId;
use super::*;
/// Half-open column ranges `[start, end)` of the subtree indicators rendered
/// on a repo row. `None` means the indicator is not drawn for this entry.
/// Used by the click handler to route a click to the right toggle.
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct IndicatorColumns {
pub(super) stash: Option<(u16, u16)>,
pub(super) worktree: Option<(u16, u16)>,
}
/// Compute the absolute column ranges of the stash and worktree toggle
/// segments for a repo row, given the leftmost content column. Both this
/// hit-test and [`RepoList::render_repo_item`] walk the same
/// [`attention_cells`] from [`RowLayout::attention_x`] — keep the two in
/// sync.
pub(super) fn indicator_columns(
entry: &RepoEntry,
base_x: u16,
layout: &RowLayout,
) -> IndicatorColumns {
let mut out = IndicatorColumns::default();
let Some(status) = entry.status.as_ref() else {
return out;
};
let mut x = base_x.saturating_add(layout.attention_x());
for (text, kind) in attention_cells(status, false, false) {
let width = text.chars().count() as u16;
match kind {
AttentionCell::Stash => out.stash = Some((x, x + width)),
AttentionCell::Worktree => out.worktree = Some((x, x + width)),
_ => {}
}
x += width + 1;
}
out
}
impl Component for RepoList {
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<()> {
self.action_tx = Some(tx);
Ok(())
}
fn init(&mut self) -> Result<()> {
Ok(())
}
fn handle_key_event(&mut self, key: KeyEvent) -> Result<Option<Action>> {
match key.code {
KeyCode::Char('j') | KeyCode::Down => {
self.select_next();
Ok(self.emit_selection_action())
}
KeyCode::Char('k') | KeyCode::Up => {
self.select_prev();
Ok(self.emit_selection_action())
}
KeyCode::Char('w') => {
self.toggle_expand();
Ok(self.emit_selection_action())
}
KeyCode::Char('S') => {
self.toggle_stash_expand();
Ok(self.emit_selection_action())
}
KeyCode::Char('f') => {
self.focus_mode = !self.focus_mode;
Ok(None)
}
_ => Ok(None),
}
}
fn handle_mouse_event(&mut self, mouse: MouseEvent) -> Result<Option<Action>> {
match mouse.kind {
MouseEventKind::Down(MouseButton::Left) => {
let content_y = self.render_area.y + 1;
if mouse.column >= self.render_area.x
&& mouse.column < self.render_area.x + self.render_area.width
&& mouse.row >= content_y
{
let visual_row = (mouse.row - content_y) as usize;
let idx = visual_row + self.state.offset();
if idx < self.display_rows.len() {
// Click on the already-selected repo row toggles a subtree.
// When both worktrees and stashes are present we hit-test
// the click column against each indicator's range so the
// user can target either. A click elsewhere on the row
// falls back to whichever subtree exists (worktrees first).
if self.state.selected() == Some(idx)
&& let Some(DisplayRow::Repo(i)) = self.display_rows.get(idx)
&& let Some(status) = self.repos[*i].status.as_ref()
{
let base_x = self.render_area.x + 1;
let inner_width = self.render_area.width.saturating_sub(2);
let layout = row_layout(&self.repos, &self.live_panes, inner_width);
let cols = indicator_columns(&self.repos[*i], base_x, &layout);
let clicked_stash = cols
.stash
.is_some_and(|(s, e)| mouse.column >= s && mouse.column < e);
let clicked_worktree = cols
.worktree
.is_some_and(|(s, e)| mouse.column >= s && mouse.column < e);
if clicked_stash {
self.toggle_stash_expand();
return Ok(self.emit_selection_action());
}
if clicked_worktree {
self.toggle_expand();
return Ok(self.emit_selection_action());
}
if !status.worktree_info.is_empty() {
self.toggle_expand();
return Ok(self.emit_selection_action());
}
if !status.stashes.is_empty() {
self.toggle_stash_expand();
return Ok(self.emit_selection_action());
}
}
self.state.select(Some(idx));
return Ok(self.emit_selection_action());
}
}
Ok(None)
}
MouseEventKind::Down(MouseButton::Right) => {
let content_y = self.render_area.y + 1;
if mouse.column >= self.render_area.x
&& mouse.column < self.render_area.x + self.render_area.width
&& mouse.row >= content_y
{
let visual_row = (mouse.row - content_y) as usize;
let idx = visual_row + self.state.offset();
if idx < self.display_rows.len() {
self.state.select(Some(idx));
// Repo and worktree rows get a context menu. A worktree
// menu targets the worktree's own path so pull/push act
// on it directly. Stash rows have no menu.
let menu = match self.display_rows.get(idx) {
Some(DisplayRow::Repo(i)) => {
Some((RepoId(self.repos[*i].path.clone()), false))
}
Some(DisplayRow::Worktree(ri, wi)) => self.repos[*ri]
.status
.as_ref()
.and_then(|s| s.worktree_info.get(*wi))
.map(|wt| (RepoId(wt.path.clone()), true)),
_ => None,
};
if let Some((id, is_worktree)) = menu {
return Ok(Some(Action::ShowContextMenu {
id,
row: mouse.row,
col: mouse.column,
is_worktree,
}));
}
}
}
Ok(None)
}
MouseEventKind::ScrollUp => {
self.select_prev();
Ok(self.emit_selection_action())
}
MouseEventKind::ScrollDown => {
self.select_next();
Ok(self.emit_selection_action())
}
_ => Ok(None),
}
}
fn update(&mut self, action: Action) -> Result<Option<Action>> {
match action {
Action::SelectNextRepo => {
self.select_next();
Ok(self.emit_selection_action())
}
Action::SelectPrevRepo => {
self.select_prev();
Ok(self.emit_selection_action())
}
Action::RepoStatusUpdated { ref id, ref status } => {
if let Some(idx) = self.resolve_index(id) {
self.update_status(idx, status.clone());
}
Ok(None)
}
_ => Ok(None),
}
}
fn draw(&mut self, frame: &mut Frame, area: Rect) -> Result<()> {
self.render_area = area;
// Ensure display_rows is fresh
self.rebuild_display_rows();
let inner_width = area.width.saturating_sub(2);
let layout = row_layout(&self.repos, &self.live_panes, inner_width);
let selected_repo = self
.state
.selected()
.and_then(|s| self.display_rows.get(s))
.map(|row| match row {
DisplayRow::Repo(i) => *i,
DisplayRow::Worktree(ri, _) | DisplayRow::Stash(ri, _) => *ri,
});
let items: Vec<ListItem> = self
.display_rows
.iter()
.map(|row| {
let (item, repo_idx) = match row {
DisplayRow::Repo(i) => (self.render_repo_item(&self.repos[*i], &layout), *i),
DisplayRow::Worktree(ri, wi) => {
(self.render_worktree_item(&self.repos[*ri], *wi), *ri)
}
DisplayRow::Stash(ri, si) => {
(self.render_stash_item(&self.repos[*ri], *si), *ri)
}
};
// Focus mode: dim every repo but the selected one; the
// selected repo's worktree/stash rows stay bright too.
if self.focus_mode && selected_repo.is_some_and(|s| s != repo_idx) {
item.style(Style::default().add_modifier(Modifier::DIM))
} else {
item
}
})
.collect();
let t = &self.theme.repo_list;
let border_color = if self.focused {
t.border_focused
} else {
t.border_unfocused
};
// Persistent warning for configured roots that don't exist on disk.
// Discovery silently skips them, so without this the workspace can
// shrink (or vanish) with no explanation. Rendered above the list for
// the whole session — not a toast that expires after a few seconds.
let missing_hint = if self.missing_roots.is_empty() {
None
} else {
let names = self
.missing_roots
.iter()
.map(|p| p.display().to_string())
.collect::<Vec<_>>()
.join(", ");
Some(format!("root does not exist: {names}"))
};
let list_area = if let Some(text) = missing_hint {
let rows = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Length(1), Constraint::Min(0)])
.split(area);
let s = &self.theme.status_bar;
let label = Span::styled(
" ! ",
Style::default()
.fg(s.error_label_fg)
.bg(s.error_label_bg)
.add_modifier(Modifier::BOLD),
);
// The rendered line is the 3-cell " ! " label plus the text;
// give the ellipsize budget the text's share so the ellipsis
// marker itself isn't clipped by the paragraph.
let text = middle_ellipsize(&text, area.width.saturating_sub(3) as usize);
frame.render_widget(
Paragraph::new(Line::from(vec![
label,
Span::styled(text, Style::default().fg(s.error_text)),
])),
rows[0],
);
rows[1]
} else {
area
};
let list = List::new(items)
.block(
Block::default()
.title(" Repositories ")
.borders(Borders::ALL)
.border_style(Style::default().fg(border_color)),
)
.highlight_style(
Style::default()
.bg(t.selection_bg)
.add_modifier(Modifier::BOLD),
);
frame.render_stateful_widget(list, list_area, &mut self.state);
Ok(())
}
}