quinjet 0.0.58

A fast, live, keyboard-first Git source-control interface for the terminal
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
#[cfg_attr(not(test), expect(clippy::wildcard_imports, reason = "shared"))]
use super::*;
use crate::git::github::PullRequestStackMember;

const WIDE_STACK_WORKSPACE: u16 = 104;

#[derive(Clone, Copy)]
struct StackRailMemberState {
    selected: bool,
    in_range: bool,
    tip: bool,
}

#[derive(Default)]
pub(super) struct StackWorkspaceGeometry {
    pub sidebar: Rect,
    pub sidebar_divider: Rect,
    pub content: Rect,
    pub diff_divider: Option<Rect>,
    pub sidebar_hits: Vec<SidebarHitArea>,
    pub stack_inspector_hits: Vec<StackInspectorHitArea>,
    pub content_file_hits: Vec<ContentFileHit>,
    pub content_step_hits: Vec<ContentStepHit>,
    pub content_review_hits: Vec<ContentReviewHit>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum StackGateState {
    Pass,
    Fail,
    Running,
    Stale,
    Skip,
    Blocked,
}

impl StackGateState {
    pub(super) const fn label(self) -> &'static str {
        match self {
            Self::Pass => "PASS",
            Self::Fail => "FAIL",
            Self::Running => "RUNNING",
            Self::Stale => "STALE",
            Self::Skip => "SKIP",
            Self::Blocked => "BLOCKED",
        }
    }

    pub(super) const fn color(self, theme: &Theme) -> Color {
        match self {
            Self::Pass => theme.success,
            Self::Fail | Self::Blocked => theme.error,
            Self::Running => theme.modified,
            Self::Stale => theme.conflict,
            Self::Skip => theme.muted,
        }
    }
}

pub(super) fn draw_pull_request_stack_workspace(
    frame: &mut Frame<'_>,
    area: Rect,
    app: &mut App,
    theme: &Theme,
    link_hits: &mut Vec<LinkHit>,
) -> StackWorkspaceGeometry {
    let Some(stack) = app.pull_request_stack.clone() else {
        return StackWorkspaceGeometry::default();
    };
    let heading_height = if area.height >= 14 { 2 } else { 1 };
    let gate_height = if area.height >= 16 {
        4
    } else if area.height >= 12 {
        3
    } else {
        2
    };
    let [heading, gate, workspace] = Layout::vertical([
        Constraint::Length(heading_height),
        Constraint::Length(gate_height),
        Constraint::Min(4),
    ])
    .areas(area);
    let mut stack_inspector_hits = draw_stack_heading(frame, heading, app, &stack, theme);
    stack_inspector_hits.push(pull_request_stack_gate::draw_tip_gate(
        frame, gate, app, &stack, theme,
    ));

    let wide = workspace.width >= WIDE_STACK_WORKSPACE;
    let (sidebar, sidebar_divider, content, sidebar_hits) = if wide {
        let maximum = workspace.width.saturating_sub(44).max(30);
        let rail_width = app.sidebar_width.clamp(30, maximum);
        let [rail, divider, detail] = Layout::horizontal([
            Constraint::Length(rail_width),
            Constraint::Length(1),
            Constraint::Min(43),
        ])
        .areas(workspace);
        let hits = draw_stack_rail(frame, rail, app, &stack, theme);
        draw_main_divider(frame, divider, app.resize_target.is_some(), theme);
        (rail, divider, detail, hits)
    } else {
        let [strip, detail] =
            Layout::vertical([Constraint::Length(2), Constraint::Min(3)]).areas(workspace);
        let hits =
            pull_request_stack_strip::draw_compact_stack_strip(frame, strip, app, &stack, theme);
        (strip, Rect::default(), detail, hits)
    };

    let (diff_divider, content_file_hits, content_step_hits, content_review_hits) =
        if app.stack_inspector.diff_open {
            draw_content(frame, content, app, theme, link_hits)
        } else {
            pull_request_stack_detail::draw_stack_member_detail(
                frame,
                content,
                app,
                theme,
                &mut stack_inspector_hits,
                link_hits,
            );
            (None, Vec::new(), Vec::new(), Vec::new())
        };
    StackWorkspaceGeometry {
        sidebar,
        sidebar_divider,
        content,
        diff_divider,
        sidebar_hits,
        stack_inspector_hits,
        content_file_hits,
        content_step_hits,
        content_review_hits,
    }
}

fn draw_stack_heading(
    frame: &mut Frame<'_>,
    area: Rect,
    app: &App,
    stack: &crate::git::github::PullRequestStack,
    theme: &Theme,
) -> Vec<StackInspectorHitArea> {
    let tip = stack.tip();
    let partial = if stack.truncated { " · PARTIAL" } else { "" };
    let tip = tip.map_or_else(
        || "TIP ?".to_owned(),
        |member| format!("TIP #{}", member.number),
    );
    let status = if app.pull_request_stack_error.is_some() {
        " · STALE METADATA".to_owned()
    } else if app.pull_request_stack_loading {
        " · REFRESHING".to_owned()
    } else if app.pull_request_warnings.is_empty() {
        String::new()
    } else {
        format!(" · WARN {}", app.pull_request_warnings.len())
    };
    let first = format!(
        " STACK #{}{status} · {} PR{} · {} -> {tip}{partial}",
        stack.number,
        stack.size,
        if stack.size == 1 { "" } else { "S" },
        stack.base_ref,
    );
    frame.render_widget(
        Paragraph::new(truncate_end(&first, usize::from(area.width))).style(
            Style::default()
                .fg(theme.accent)
                .bg(theme.panel)
                .add_modifier(Modifier::BOLD),
        ),
        Rect::new(area.x, area.y, area.width, area.height.min(1)),
    );
    let Some((from, to)) = app.pull_request_stack_range() else {
        return Vec::new();
    };
    let action = if app.stack_inspector.diff_open {
        " [d Diff open] "
    } else {
        " [d Open range diff] "
    };
    let action_width = cells(action.width()).min(area.width);
    let action_area = Rect::new(
        area.right().saturating_sub(action_width),
        area.y.saturating_add(1),
        action_width,
        area.height.saturating_sub(1).min(1),
    );
    let range_area = Rect::new(
        area.x,
        area.y.saturating_add(1),
        area.width.saturating_sub(action_width),
        area.height.saturating_sub(1).min(1),
    );
    frame.render_widget(
        Paragraph::new(format!(" RANGE {from}..{to} · base({from}) -> head({to})"))
            .style(Style::default().fg(theme.muted).bg(theme.panel_alt)),
        range_area,
    );
    frame.render_widget(
        Paragraph::new(action).style(
            Style::default()
                .fg(theme.accent)
                .bg(theme.panel_alt)
                .add_modifier(Modifier::BOLD),
        ),
        action_area,
    );
    vec![StackInspectorHitArea {
        area: action_area,
        target: StackInspectorHit::Diff,
    }]
}

pub(super) fn stack_gate_state(app: &App) -> StackGateState {
    let checks = &app.stack_inspector.tip_checks.checks;
    let tip = app
        .pull_request_stack
        .as_ref()
        .and_then(crate::git::github::PullRequestStack::tip);
    if app.pull_request_stack_error.is_some() || tip.is_none() {
        return StackGateState::Blocked;
    }
    if checks
        .iter()
        .any(|check| check.status == PullRequestCheckStatus::Failed)
    {
        return StackGateState::Fail;
    }
    if checks
        .iter()
        .any(|check| check.status == PullRequestCheckStatus::Pending)
        || (app.stack_inspector.tip_checks_loading && !app.stack_inspector.tip_checks_loaded)
    {
        return StackGateState::Running;
    }
    if checks.iter().any(|check| {
        check.status == PullRequestCheckStatus::Cancelled
            || check.state.eq_ignore_ascii_case("stale")
    }) {
        return StackGateState::Stale;
    }
    if tip.is_some_and(stack_member_blocked) || app.stack_inspector.tip_checks_error.is_some() {
        return StackGateState::Blocked;
    }
    if app.stack_inspector.tip_checks_loaded {
        if checks
            .iter()
            .any(|check| check.status == PullRequestCheckStatus::Unknown)
        {
            return StackGateState::Blocked;
        }
        if checks.is_empty()
            || checks
                .iter()
                .all(|check| check.status == PullRequestCheckStatus::Skipped)
        {
            return StackGateState::Skip;
        }
        return StackGateState::Pass;
    }
    match tip.map_or("", |member| member.checks_state.as_str()) {
        "FAILURE" | "ERROR" => StackGateState::Fail,
        "PENDING" | "EXPECTED" => StackGateState::Running,
        "STALE" => StackGateState::Stale,
        "SUCCESS" => StackGateState::Pass,
        _ => StackGateState::Skip,
    }
}

fn stack_member_blocked(member: &PullRequestStackMember) -> bool {
    member.is_draft
        || member.mergeable.eq_ignore_ascii_case("conflicting")
        || member
            .review_decision
            .eq_ignore_ascii_case("changes_requested")
        || matches!(member.merge_state.as_str(), "BEHIND" | "BLOCKED" | "DIRTY")
}

fn draw_stack_rail(
    frame: &mut Frame<'_>,
    area: Rect,
    app: &mut App,
    stack: &crate::git::github::PullRequestStack,
    theme: &Theme,
) -> Vec<SidebarHitArea> {
    frame.render_widget(
        section_separator_block("BASE -> TIP · MEMBER HEALTH", theme),
        Rect::new(area.x, area.y, area.width, area.height.min(1)),
    );
    let list = Rect::new(
        area.x,
        area.y.saturating_add(1),
        area.width,
        area.height.saturating_sub(1),
    );
    let capacity = usize::from(list.height).div_euclid(2);
    let cursor = stack
        .members
        .iter()
        .position(|member| Some(member.position) == app.pull_request_stack_cursor)
        .unwrap_or_default();
    app.sidebar_viewport(cursor, capacity, stack.members.len());
    let start = app.sidebar_offset;
    let range = app.pull_request_stack_range();
    stack
        .members
        .iter()
        .skip(start)
        .take(capacity)
        .enumerate()
        .map(|(visible, member)| {
            let row = Rect::new(
                list.x,
                list.y.saturating_add(cells(visible.saturating_mul(2))),
                list.width,
                2,
            );
            draw_stack_rail_member(
                frame,
                row,
                member,
                StackRailMemberState {
                    selected: Some(member.position) == app.pull_request_stack_cursor,
                    in_range: range
                        .is_some_and(|(from, to)| (from..=to).contains(&member.position)),
                    tip: stack.tip() == Some(member),
                },
                theme,
            );
            SidebarHitArea {
                area: row,
                target: SidebarHit::PullRequestStackMember(member.position),
            }
        })
        .collect()
}

fn draw_stack_rail_member(
    frame: &mut Frame<'_>,
    area: Rect,
    member: &PullRequestStackMember,
    member_state: StackRailMemberState,
    theme: &Theme,
) {
    let background = if member_state.selected {
        theme.selected
    } else if member_state.in_range {
        theme.panel_alt
    } else {
        theme.panel
    };
    let marker = if member_state.selected {
        ">"
    } else if member_state.in_range {
        "●"
    } else {
        "│"
    };
    let tip = if member_state.tip { " TIP" } else { "" };
    let state = if member.is_draft {
        "DRAFT"
    } else {
        member.state.as_str()
    };
    let prefix = format!(" {marker} {} #{} ", member.position, member.number);
    let suffix = format!(" {tip} {state}");
    let title = truncate_end(
        &member.title,
        usize::from(area.width)
            .saturating_sub(prefix.width())
            .saturating_sub(suffix.width()),
    );
    frame.render_widget(
        Paragraph::new(Line::from(vec![
            Span::styled(prefix, Style::default().fg(theme.accent)),
            Span::styled(title, Style::default().fg(theme.text)),
            Span::styled(
                suffix,
                Style::default().fg(member_state_color(state, theme)),
            ),
        ]))
        .style(Style::default().bg(background)),
        Rect::new(area.x, area.y, area.width, 1),
    );
    let health = member_health_label(member);
    let signal = format!(
        "   {} · CI {health} · +{} -{}",
        member.review_decision.replace('_', " "),
        member.additions,
        member.deletions,
    );
    frame.render_widget(
        Paragraph::new(truncate_end(&signal, usize::from(area.width))).style(
            Style::default()
                .fg(member_health_color(health, theme))
                .bg(background),
        ),
        Rect::new(area.x, area.y.saturating_add(1), area.width, 1),
    );
}

fn member_health_label(member: &PullRequestStackMember) -> &'static str {
    if stack_member_blocked(member) {
        return "BLOCKED";
    }
    match member.checks_state.as_str() {
        "SUCCESS" => "PASS",
        "FAILURE" | "ERROR" => "FAIL",
        "PENDING" | "EXPECTED" => "RUNNING",
        "STALE" => "STALE",
        _ => "SKIP",
    }
}

const fn member_health_color(health: &str, theme: &Theme) -> Color {
    match health.as_bytes() {
        b"PASS" => theme.success,
        b"FAIL" | b"BLOCKED" => theme.error,
        b"RUNNING" => theme.modified,
        b"STALE" => theme.conflict,
        _ => theme.muted,
    }
}

const fn member_state_color(state: &str, theme: &Theme) -> Color {
    match state.as_bytes() {
        b"OPEN" => theme.success,
        b"MERGED" => theme.accent,
        b"CLOSED" => theme.removed,
        _ => theme.modified,
    }
}