gitpane 0.16.1

Multi-repo Git workspace dashboard TUI
Documentation
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
use crate::git::graph_render;
use ratatui::{
    Frame,
    layout::Rect,
    style::{Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, List, ListItem, Paragraph, Wrap},
};

use super::*;

impl GitGraph {
    /// The split used for this frame's detail layout: `detail_split` with the
    /// message|files border overridden by the message's line count while the
    /// user has not dragged that border (see `msg_dragged`).
    pub(super) fn detail_split_with_auto_msg(&self, area: Rect, detail: &CommitDetail) -> [f64; 3] {
        let axis = if self.horizontal_layout {
            area.height
        } else {
            area.width
        };
        // Feasible integer boundary positions for this axis (short axes leave
        // no room between the stored 0.40/0.65 borders, so clamp in cells).
        let [b0, b1, b2] = detail_cell_bounds(axis, self.detail_split);
        if self.horizontal_layout && !self.msg_dragged {
            let line_count = detail.message.lines().count().max(1) as u16;
            let want = msg_auto_cells(line_count, axis);
            let min_cells = detail_min_cells(axis);
            // Message|files border sits `want` cells below the graph border,
            // clamped so the message and files panes each keep a minimum.
            let b1 = b0
                .saturating_add(want)
                .clamp(b0 + min_cells, b2 - min_cells);
            return [
                b0 as f64 / axis as f64,
                b1 as f64 / axis as f64,
                b2 as f64 / axis as f64,
            ];
        }
        [
            b0 as f64 / axis as f64,
            b1 as f64 / axis as f64,
            b2 as f64 / axis as f64,
        ]
    }

    pub(super) fn filter_summary(&self) -> Option<String> {
        let mut parts = Vec::new();
        if let Some(branches) = &self.graph_options.filters.branches {
            parts.push(format!(
                "branches {}/{}",
                branches.len(),
                self.filter_branches.len()
            ));
        }
        if let Some(authors) = &self.graph_options.filters.authors {
            parts.push(format!(
                "authors {}/{}",
                authors.len(),
                self.filter_authors.len()
            ));
        }
        (!parts.is_empty()).then(|| format!(" [{}]", parts.join(", ")))
    }

    pub(super) fn draw_graph_list(&mut self, frame: &mut Frame, area: Rect) {
        let collapsed_count = self.collapsed_branches.len();
        let mut title = match (self.graph_options.first_parent, collapsed_count) {
            (true, 0) => format!(" Git Graph — {} [1st-parent] ", self.repo_name),
            (true, n) => format!(
                " Git Graph — {} [1st-parent] ({n} collapsed) ",
                self.repo_name
            ),
            (false, 0) => format!(" Git Graph — {} ", self.repo_name),
            (false, n) => format!(" Git Graph — {} ({n} collapsed) ", self.repo_name),
        };
        if let Some(summary) = self.filter_summary() {
            title.push_str(&summary);
        }
        let t = &self.theme.graph;
        let border_color = if self.focused && self.commit_detail.is_none() {
            t.border_focused
        } else {
            t.border_unfocused
        };

        let block = Block::default()
            .title(title)
            .borders(Borders::ALL)
            .border_style(Style::default().fg(border_color));

        if self.loading {
            let paragraph = Paragraph::new("Loading graph...")
                .style(Style::default().fg(t.loading))
                .block(block);
            frame.render_widget(paragraph, area);
            return;
        }

        if let Some(ref err) = self.error {
            let paragraph = Paragraph::new(err.as_str())
                .style(Style::default().fg(t.error_text))
                .block(block);
            frame.render_widget(paragraph, area);
            return;
        }

        if self.display_rows().is_empty() {
            let paragraph = Paragraph::new("No commits")
                .style(Style::default().fg(t.empty))
                .block(block);
            frame.render_widget(paragraph, area);
            return;
        }

        let label_max_len = self.graph_options.label_max_len;
        let max_width = area.width.saturating_sub(2) as usize; // 2 for borders
        let has_search = !self.search.input.is_empty() && !self.search.matches.is_empty();
        // One clock read for the whole frame's relative-time column.
        let now_secs = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs() as i64;

        // Rendered row bodies are cached per (commit, theme, width, highlight,
        // collapse) state; only the volatile tail (relative time, diff stats)
        // is rebuilt each frame. Take the cache out of `self` so the closure
        // can mutate it while `display_rows` is still borrowed.
        let mut render_cache = std::mem::take(&mut self.render_cache);
        let items: Vec<ListItem> = self
            .display_rows()
            .iter()
            .enumerate()
            .map(|(i, row)| {
                let dimmed = has_search && !self.search.matches.contains(&i);
                let is_collapsed = row.collapsed.is_some();

                let key = RowRenderKey {
                    oid: row.oid,
                    theme_generation: self.theme_generation,
                    label_max_len,
                    dimmed,
                    collapsed: is_collapsed,
                };
                let mut spans = match render_cache.get(&key) {
                    Some(cached) => cached.clone(),
                    None => {
                        let built = graph_render::render_row_body(
                            row,
                            t,
                            label_max_len,
                            dimmed,
                            is_collapsed,
                        );
                        if render_cache.len() >= RENDER_CACHE_CAPACITY {
                            render_cache.clear();
                        }
                        render_cache.insert(key, built.clone());
                        built
                    }
                };

                spans.extend(graph_render::render_row_tail(row, t, now_secs, dimmed));

                graph_render::h_scroll_line(&mut spans, self.h_scroll, max_width);
                ListItem::new(Line::from(spans))
            })
            .collect();
        self.render_cache = render_cache;

        let list = List::new(items).block(block).highlight_style(
            Style::default()
                .bg(t.selection_bg)
                .add_modifier(Modifier::BOLD),
        );

        frame.render_stateful_widget(list, area, &mut self.state);
    }

    pub(super) fn draw_commit_message(
        detail: &mut CommitDetail,
        frame: &mut Frame,
        area: Rect,
        theme: &crate::theme::GraphTheme,
    ) {
        let title = format!(" Message — {} ", &detail.oid[..7.min(detail.oid.len())]);
        detail.msg_area = area;

        let msg_block = Block::default()
            .title(title)
            .borders(Borders::ALL)
            .border_style(Style::default().fg(theme.commit_msg_border));

        let msg_paragraph = Paragraph::new(detail.message.as_str())
            .style(Style::default().fg(theme.commit_msg_text))
            .block(msg_block)
            .wrap(Wrap { trim: false })
            .scroll((detail.msg_scroll, 0));
        frame.render_widget(msg_paragraph, area);
    }

    pub(super) fn draw_commit_file_list(
        detail: &mut CommitDetail,
        frame: &mut Frame,
        area: Rect,
        theme: &crate::theme::GraphTheme,
    ) {
        let mut title = format!(" Files: {} ", &detail.oid[..7.min(detail.oid.len())]);
        if detail.file_searching || detail.file_filter.is_active() {
            title.push_str(&format!(" /{}", detail.file_filter.query()));
            if detail.file_filter.count() == 0 {
                title.push_str(" No matches ");
            }
        }
        detail.file_list_area = area;

        let files_block = Block::default()
            .title(title)
            .borders(Borders::ALL)
            .border_style(Style::default().fg(theme.commit_files_border));

        if detail.files.is_empty() {
            let paragraph = Paragraph::new("No files changed")
                .style(Style::default().fg(theme.commit_files_empty))
                .block(files_block);
            frame.render_widget(paragraph, area);
            return;
        }

        let items: Vec<ListItem> = detail
            .files
            .iter()
            .map(|(status, path)| {
                let color = match status.as_str() {
                    "M" => theme.commit_files_status_modified,
                    "A" => theme.commit_files_status_added,
                    "D" => theme.commit_files_status_deleted,
                    "R" => theme.commit_files_status_renamed,
                    _ => theme.commit_files_status_other,
                };
                let mut spans = vec![Span::styled(
                    format!(" {} ", status),
                    Style::default().fg(color).add_modifier(Modifier::BOLD),
                )];
                spans.extend(crate::components::highlight_matches(
                    path,
                    detail.file_filter.query(),
                    Style::default().fg(theme.commit_files_path),
                    Style::default()
                        .fg(theme.commit_files_border)
                        .add_modifier(Modifier::BOLD),
                ));
                ListItem::new(Line::from(spans))
            })
            .collect();

        let list = List::new(items).block(files_block).highlight_style(
            Style::default()
                .bg(theme.selection_bg)
                .add_modifier(Modifier::BOLD),
        );

        frame.render_stateful_widget(list, area, &mut detail.file_state);
    }

    pub(super) fn draw_commit_diff(
        detail: &CommitDetail,
        frame: &mut Frame,
        area: Rect,
        theme: &crate::theme::GraphTheme,
    ) {
        let Some(ref content) = detail.diff_content else {
            return;
        };

        let title = if detail.diff_focused {
            " Commit Diff (Esc to leave) "
        } else {
            " Commit Diff (Enter to scroll) "
        };
        let block = Block::default()
            .title(title)
            .borders(Borders::ALL)
            .border_style(Style::default().fg(theme.commit_diff_border));

        let lines: Vec<Line> = content
            .lines()
            .map(|line| {
                let style = if line.starts_with('+') && !line.starts_with("+++") {
                    Style::default().fg(theme.commit_diff_added)
                } else if line.starts_with('-') && !line.starts_with("---") {
                    Style::default().fg(theme.commit_diff_removed)
                } else if line.starts_with("@@") {
                    Style::default().fg(theme.commit_diff_hunk)
                } else if line.starts_with("diff ") || line.starts_with("index ") {
                    Style::default().fg(theme.commit_diff_meta)
                } else {
                    Style::default().fg(theme.commit_diff_context)
                };
                Line::from(Span::styled(line, style))
            })
            .collect();

        let paragraph = Paragraph::new(lines)
            .block(block)
            .wrap(Wrap { trim: false })
            .scroll((detail.diff_scroll, 0));

        frame.render_widget(paragraph, area);
    }
}

/// ±2 cells hit zone for grabbing a commit-detail border.
pub(super) const DETAIL_GRAB_ZONE: u16 = 2;

/// Split the commit-detail `area` into graph / message / files / diff rects
/// along the detail layout axis. `horizontal_layout` = the outer panels are
/// side by side, so the detail splits vertically (historical convention).
/// `split` holds the graph|message, message|files and files|diff fractions.
/// Each pane keeps at least `detail_min_cells(axis)` cells on the axis.
pub(super) fn detail_chunks(area: Rect, split: [f64; 3], horizontal_layout: bool) -> [Rect; 4] {
    let axis = if horizontal_layout {
        area.height
    } else {
        area.width
    };
    let [b0, b1, b2] = detail_cell_bounds(axis, split);
    let make = |start: u16, len: u16, area: Rect, vertical: bool| {
        if vertical {
            Rect {
                y: area.y + start,
                height: len,
                ..area
            }
        } else {
            Rect {
                x: area.x + start,
                width: len,
                ..area
            }
        }
    };
    [
        make(0, b0, area, horizontal_layout),
        make(b0, b1 - b0, area, horizontal_layout),
        make(b1, b2 - b1, area, horizontal_layout),
        make(b2, axis - b2, area, horizontal_layout),
    ]
}

/// The minimum number of cells each detail pane keeps on the axis. Capped at
/// three, but never more than a quarter of the axis: four panes each need a
/// quarter, so a short axis (e.g. 10 cells) drops the floor to `axis / 4`
/// cells per pane, otherwise the clamp bounds below would invert.
pub(super) fn detail_min_cells(axis: u16) -> u16 {
    3u16.min(axis / 4)
}

/// Feasible integer boundary positions (cells on the detail axis) for a
/// three-way split, derived from the desired fractional `split`. Each pane
/// keeps at least `detail_min_cells(axis)` cells. The clamping happens in
/// integer cell space, so float rounding can never produce an inverted
/// (min > max) range that panics the layout.
pub(super) fn detail_cell_bounds(axis: u16, split: [f64; 3]) -> [u16; 3] {
    if axis == 0 {
        return [0, 0, 0];
    }
    let min_cells = detail_min_cells(axis);
    let axis_f = axis as f64;
    // Desired boundary positions (graph|message, message|files, files|diff).
    let d0 = (split[0] * axis_f).round() as u16;
    let d1 = (split[1] * axis_f).round() as u16;
    let d2 = (split[2] * axis_f).round() as u16;
    // Clamp in order; each pane keeps at least `min_cells` cells.
    let b0 = d0.clamp(min_cells, axis - 3 * min_cells);
    let b1 = d1.clamp(b0 + min_cells, axis - 2 * min_cells);
    let b2 = d2.clamp(b1 + min_cells, axis - min_cells);
    [b0, b1, b2]
}

/// Auto height (in cells) for the commit message block: follows the message's
/// line count, capped at 40% of the available axis, at least 3 cells.
pub(super) fn msg_auto_cells(line_count: u16, axis: u16) -> u16 {
    (line_count + 2).min(axis * 40 / 100).max(3)
}

/// Maximum scroll offset (rows) for a wrapping paragraph inside a bordered
/// block: the content's wrapped row count (each source line wrapped at the
/// inner width using display-width word wrapping, exactly as ratatui renders
/// it) minus the rows visible between the borders. `0` when the content fits
/// or the viewport is too small to matter.
pub(super) fn max_scroll_lines(text: &str, viewport_height: u16, viewport_width: u16) -> u16 {
    if viewport_height <= 2 || viewport_width <= 2 {
        return 0;
    }
    let inner = viewport_width - 2;
    // Use the same wrapping ratatui applies when rendering the paragraph
    // (display width + word boundaries), so wide (CJK) glyphs and long words
    // don't leave the content end unreachable.
    let content_rows = Paragraph::new(text)
        .wrap(Wrap { trim: false })
        .line_count(inner) as u16;
    content_rows.saturating_sub(viewport_height - 2)
}

/// Axis coordinates (row in a vertical detail layout, column in a horizontal
/// one) of the commit-detail borders: 0 = graph|message, 1 = message|files,
/// 2 = files|diff. The files|diff border is absent until a diff is loaded.
pub(super) fn detail_border_positions(
    graph: Rect,
    message: Rect,
    files: Rect,
    diff: Rect,
    vertical: bool,
) -> [Option<u16>; 3] {
    let end = |r: Rect| {
        if vertical {
            r.y + r.height
        } else {
            r.x + r.width
        }
    };
    let live = |r: Rect| r.width > 0 && r.height > 0;
    let has_diff = live(diff);
    [
        live(graph).then(|| end(graph)),
        live(message).then(|| end(message)),
        if has_diff {
            live(files).then(|| end(files))
        } else {
            None
        },
    ]
}