gitkraft-tui 0.9.1

GitKraft — Git IDE terminal application (Ratatui 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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
use ratatui::layout::{Constraint, Direction, Layout, Rect};
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, List, ListItem, Padding, Paragraph};
use ratatui::Frame;

use gitkraft_core::FileStatus;

use crate::app::{ActivePane, App, InputMode, InputPurpose, StagingFocus};
use crate::utils::pad_right;

/// Render the staging area — split into three columns:
///  1. Unstaged changes list
///  2. Staged changes list
///  3. Commit message input OR key hints
pub fn render(app: &mut App, frame: &mut Frame, area: Rect) {
    let is_active = app.active_pane == ActivePane::Staging;

    // Split the staging area into three columns
    let cols = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Ratio(1, 3), // unstaged
            Constraint::Ratio(1, 3), // staged
            Constraint::Ratio(1, 3), // commit input / hints
        ])
        .split(area);

    render_unstaged(app, frame, cols[0], is_active);
    render_staged(app, frame, cols[1], is_active);
    render_commit_or_hints(app, frame, cols[2], is_active);
}

/// Render the unstaged changes list.
fn render_unstaged(app: &mut App, frame: &mut Frame, area: Rect, pane_active: bool) {
    let theme = app.theme();
    let is_focused = pane_active && app.tab().staging_focus == StagingFocus::Unstaged;

    let border_color = if is_focused {
        theme.border_active
    } else if pane_active {
        theme.accent
    } else {
        theme.border_inactive
    };

    let title = format!(" Unstaged ({}) ", app.tab().unstaged_changes.len());
    let block = Block::default()
        .title(title)
        .borders(Borders::ALL)
        .border_style(Style::default().fg(border_color))
        .style(Style::default().bg(theme.bg));

    if app.tab().unstaged_changes.is_empty() {
        let items: Vec<ListItem> = vec![ListItem::new(Line::from(Span::styled(
            "  No unstaged changes",
            Style::default().fg(theme.text_muted),
        )))];
        let list = List::new(items).block(block);
        frame.render_widget(list, area);
        return;
    }

    let selected = app.tab().selected_unstaged.clone();
    // Pre-sort selected indices so we can show a stable 1-based rank badge.
    let mut sorted_selected: Vec<usize> = selected.iter().copied().collect();
    sorted_selected.sort_unstable();
    let multi = sorted_selected.len() >= 2;
    let items: Vec<ListItem> = app
        .tab()
        .unstaged_changes
        .iter()
        .enumerate()
        .map(|(idx, diff)| {
            let file_name = diff.display_path().to_owned();
            let (status_char, status_color) = status_display(&diff.status, &theme);
            let is_selected = selected.contains(&idx);

            // Single selection: show ● bullet. Range selection (2+): show rank number.
            let badge = if let Some(pos) = sorted_selected.iter().position(|&i| i == idx) {
                if multi {
                    format!("{:<2}", pos + 1)
                } else {
                    "".to_string()
                }
            } else {
                "  ".to_string()
            };
            let name_style = if is_selected {
                Style::default()
                    .fg(theme.accent)
                    .add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(theme.text_primary)
            };

            let line = Line::from(vec![
                Span::styled(badge, Style::default().fg(theme.accent)),
                Span::styled(
                    format!("{} ", status_char),
                    Style::default()
                        .fg(status_color)
                        .add_modifier(Modifier::BOLD),
                ),
                Span::styled(file_name, name_style),
            ]);

            ListItem::new(line)
        })
        .collect();

    let list = List::new(items)
        .block(block)
        .highlight_style(
            Style::default()
                .bg(theme.sel_bg)
                .add_modifier(Modifier::REVERSED),
        )
        .highlight_symbol("");

    let tab = app.tab_mut();
    frame.render_stateful_widget(list, area, &mut tab.unstaged_list_state);
}

/// Render the staged changes list.
fn render_staged(app: &mut App, frame: &mut Frame, area: Rect, pane_active: bool) {
    let theme = app.theme();
    let is_focused = pane_active && app.tab().staging_focus == StagingFocus::Staged;

    let border_color = if is_focused {
        theme.border_active
    } else if pane_active {
        theme.accent
    } else {
        theme.border_inactive
    };

    let title = format!(" Staged ({}) ", app.tab().staged_changes.len());
    let block = Block::default()
        .title(title)
        .borders(Borders::ALL)
        .border_style(Style::default().fg(border_color))
        .style(Style::default().bg(theme.bg));

    if app.tab().staged_changes.is_empty() {
        let items: Vec<ListItem> = vec![ListItem::new(Line::from(Span::styled(
            "  No staged changes",
            Style::default().fg(theme.text_muted),
        )))];
        let list = List::new(items).block(block);
        frame.render_widget(list, area);
        return;
    }

    let selected = app.tab().selected_staged.clone();
    let mut sorted_selected: Vec<usize> = selected.iter().copied().collect();
    sorted_selected.sort_unstable();
    let multi = sorted_selected.len() >= 2;
    let items: Vec<ListItem> = app
        .tab()
        .staged_changes
        .iter()
        .enumerate()
        .map(|(idx, diff)| {
            let file_name = diff.display_path().to_owned();
            let (status_char, status_color) = status_display(&diff.status, &theme);
            let is_selected = selected.contains(&idx);

            let badge = if let Some(pos) = sorted_selected.iter().position(|&i| i == idx) {
                if multi {
                    format!("{:<2}", pos + 1)
                } else {
                    "".to_string()
                }
            } else {
                "  ".to_string()
            };
            let name_style = if is_selected {
                Style::default()
                    .fg(theme.accent)
                    .add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(theme.text_primary)
            };

            let line = Line::from(vec![
                Span::styled(badge, Style::default().fg(theme.accent)),
                Span::styled(
                    format!("{} ", status_char),
                    Style::default()
                        .fg(status_color)
                        .add_modifier(Modifier::BOLD),
                ),
                Span::styled(file_name, name_style),
            ]);

            ListItem::new(line)
        })
        .collect();

    let list = List::new(items)
        .block(block)
        .highlight_style(
            Style::default()
                .bg(theme.sel_bg)
                .add_modifier(Modifier::REVERSED),
        )
        .highlight_symbol("");

    let tab = app.tab_mut();
    frame.render_stateful_widget(list, area, &mut tab.staged_list_state);
}

/// Render either the commit message input (if in input mode) or key hints.
fn render_commit_or_hints(app: &mut App, frame: &mut Frame, area: Rect, pane_active: bool) {
    let theme = app.theme();
    let border_color = if pane_active {
        theme.border_active
    } else {
        theme.border_inactive
    };

    let is_commit_input =
        app.input_mode == InputMode::Input && app.input_purpose == InputPurpose::CommitMessage;

    if is_commit_input {
        // Show commit message editor
        let block = Block::default()
            .title(" Commit Message ")
            .borders(Borders::ALL)
            .border_style(Style::default().fg(theme.warning))
            .style(Style::default().bg(theme.bg));

        let cursor_char = if app.tick_count % 10 < 5 { "" } else { " " };

        let lines = vec![
            Line::from(""),
            Line::from(vec![
                Span::styled(" ", Style::default()),
                Span::styled(&app.input_buffer, Style::default().fg(theme.text_primary)),
                Span::styled(
                    cursor_char,
                    Style::default()
                        .fg(theme.warning)
                        .add_modifier(Modifier::BOLD),
                ),
            ]),
            Line::from(""),
            Line::from(Span::styled(
                " Enter: commit │ Esc: cancel",
                Style::default().fg(theme.text_muted),
            )),
        ];

        let paragraph = Paragraph::new(lines).block(block);
        frame.render_widget(paragraph, area);
    } else {
        // Show key hints in bordered inner sections (tui-file-explorer style)
        let outer_block = Block::default()
            .title(Line::from(vec![
                Span::styled("", Style::default().fg(theme.accent)),
                Span::styled(
                    "Actions",
                    Style::default()
                        .fg(theme.accent)
                        .add_modifier(Modifier::BOLD),
                ),
            ]))
            .borders(Borders::ALL)
            .border_style(Style::default().fg(border_color))
            .style(Style::default().bg(theme.bg))
            .padding(Padding::new(1, 1, 0, 0));

        let inner_area = outer_block.inner(area);
        frame.render_widget(outer_block, area);

        let key_style = Style::default()
            .fg(theme.warning)
            .add_modifier(Modifier::BOLD);
        let desc_style = Style::default().fg(theme.text_primary);
        let value_style = Style::default().fg(theme.accent);
        let section_title = Style::default().fg(theme.text_muted);

        // Split inner area into sections
        let sections = Layout::default()
            .direction(Direction::Vertical)
            .constraints([
                Constraint::Length(6), // Staging section
                Constraint::Length(7), // Git section
                Constraint::Length(5), // Branch Actions section
                Constraint::Length(5), // Commit Actions section
                Constraint::Min(2),    // remaining / warnings
            ])
            .split(inner_area);

        // -- Staging section --
        {
            let block = Block::default()
                .title(Span::styled(" Staging ", section_title))
                .borders(Borders::ALL)
                .border_style(Style::default().fg(theme.border_inactive))
                .style(Style::default().bg(theme.bg));

            let lines = vec![
                Line::from(vec![
                    Span::styled(pad_right("s", 8), key_style),
                    Span::styled(pad_right("stage", 12), desc_style),
                    Span::styled(pad_right("u", 8), key_style),
                    Span::styled("unstage", desc_style),
                ]),
                Line::from(vec![
                    Span::styled(pad_right("S", 8), key_style),
                    Span::styled(pad_right("stage all", 12), desc_style),
                    Span::styled(pad_right("U", 8), key_style),
                    Span::styled("unstage all", desc_style),
                ]),
                Line::from(vec![
                    Span::styled(pad_right("Space", 8), key_style),
                    Span::styled(pad_right("toggle", 12), desc_style),
                    Span::styled(pad_right("J/K", 8), key_style),
                    Span::styled("range select", desc_style),
                ]),
                Line::from(vec![
                    Span::styled(pad_right("E", 8), key_style),
                    Span::styled(pad_right("editor", 12), desc_style),
                    Span::styled(pad_right("", 8), key_style),
                    Span::styled("", desc_style),
                ]),
            ];

            let paragraph = Paragraph::new(lines).block(block);
            frame.render_widget(paragraph, sections[0]);
        }

        // -- Git section --
        {
            let block = Block::default()
                .title(Span::styled(" Git ", section_title))
                .borders(Borders::ALL)
                .border_style(Style::default().fg(theme.border_inactive))
                .style(Style::default().bg(theme.bg));

            let lines = vec![
                Line::from(vec![
                    Span::styled(pad_right("c", 8), key_style),
                    Span::styled(pad_right("commit", 12), desc_style),
                    Span::styled(pad_right("z", 8), key_style),
                    Span::styled("stash", desc_style),
                ]),
                Line::from(vec![
                    Span::styled(pad_right("d", 8), key_style),
                    Span::styled(pad_right("discard", 12), desc_style),
                    Span::styled(pad_right("Z", 8), key_style),
                    Span::styled("stash pop", desc_style),
                ]),
                Line::from(vec![
                    Span::styled(pad_right("p", 8), key_style),
                    Span::styled(pad_right("pull", 12), desc_style),
                    Span::styled(pad_right("P", 8), key_style),
                    Span::styled("push", desc_style),
                ]),
                Line::from(vec![
                    Span::styled(pad_right("F", 8), key_style),
                    Span::styled(pad_right("force push", 12), desc_style),
                    Span::styled(pad_right("f", 8), key_style),
                    Span::styled("fetch", desc_style),
                ]),
            ];

            let paragraph = Paragraph::new(lines).block(block);
            frame.render_widget(paragraph, sections[1]);
        }

        // -- Branch Actions section --
        {
            let block = Block::default()
                .title(Span::styled(" Branch Actions ", section_title))
                .borders(Borders::ALL)
                .border_style(Style::default().fg(theme.border_inactive))
                .style(Style::default().bg(theme.bg));

            let lines = vec![
                Line::from(vec![
                    Span::styled(pad_right("m", 8), key_style),
                    Span::styled(pad_right("merge", 12), desc_style),
                    Span::styled(pad_right("R", 8), key_style),
                    Span::styled("rebase onto", desc_style),
                ]),
                Line::from(vec![
                    Span::styled(pad_right("D", 8), key_style),
                    Span::styled(pad_right("delete", 12), desc_style),
                    Span::styled(pad_right("b", 8), key_style),
                    Span::styled("new branch", desc_style),
                ]),
            ];

            let paragraph = Paragraph::new(lines).block(block);
            frame.render_widget(paragraph, sections[2]);
        }

        // -- Commit Actions section --
        {
            let block = Block::default()
                .title(Span::styled(" Commit Actions ", section_title))
                .borders(Borders::ALL)
                .border_style(Style::default().fg(theme.border_inactive))
                .style(Style::default().bg(theme.bg));

            let lines = vec![
                Line::from(vec![
                    Span::styled(pad_right("e", 8), key_style),
                    Span::styled(pad_right("revert", 12), desc_style),
                    Span::styled(pad_right("x", 8), key_style),
                    Span::styled("reset soft", desc_style),
                ]),
                Line::from(vec![
                    Span::styled(pad_right("X", 8), key_style),
                    Span::styled(pad_right("reset hard", 12), desc_style),
                ]),
            ];

            let paragraph = Paragraph::new(lines).block(block);
            frame.render_widget(paragraph, sections[3]);
        }

        // -- Remaining area: navigation hint + discard warning --
        {
            let mut lines = vec![Line::from(vec![
                Span::styled(" Tab", key_style),
                Span::styled(" focus  ", desc_style),
                Span::styled("Enter", key_style),
                Span::styled(" diff  ", desc_style),
                Span::styled("E", key_style),
                Span::styled(" editor  ", value_style),
                Span::styled("T", key_style),
                Span::styled(" theme  ", value_style),
                Span::styled("O", key_style),
                Span::styled(" options", value_style),
            ])];

            if app.tab().confirm_discard {
                lines.push(Line::from(Span::styled(
                    " ⚠ Press d again to confirm discard",
                    Style::default()
                        .fg(theme.error)
                        .add_modifier(Modifier::BOLD),
                )));
            }

            let paragraph = Paragraph::new(lines);
            frame.render_widget(paragraph, sections[4]);
        }
    }
}

/// Map a `FileStatus` to a display character and color.
fn status_display(
    status: &FileStatus,
    theme: &crate::features::theme::palette::UiTheme,
) -> (&'static str, ratatui::style::Color) {
    match status {
        FileStatus::Modified => ("M", theme.warning),
        FileStatus::New => ("A", theme.success),
        FileStatus::Deleted => ("D", theme.error),
        FileStatus::Renamed => ("R", theme.accent),
        FileStatus::Copied => ("C", theme.accent),
        FileStatus::Typechange => ("T", theme.text_secondary),
        FileStatus::Untracked => ("?", theme.text_secondary),
    }
}