gitwig 2.4.10

a rust based tui, an alternative to sourcetree and gitui
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! Commit popup editor with support for conventional prefixes, GPG/SSH signing, and message clearing.

use crate::components::{Component, DrawableComponent, EventState};
use crate::queue::{InternalEvent, Queue};
use crossterm::event::{Event, KeyCode, KeyModifiers};
use std::cell::Cell;

pub struct CommitPopup {
    pub queue: Queue,
    pub input_buffer: String,
    pub editing: bool,
    pub amend: bool,
    pub scroll: Cell<usize>,
    pub maximized: bool,
    pub width_pct: u16,
    pub height_pct: u16,
    pub cursor_idx: usize,
    pub last_height: Cell<usize>,
}

impl CommitPopup {
    pub fn new(queue: Queue) -> Self {
        Self {
            queue,
            input_buffer: String::new(),
            editing: true,
            amend: false,
            scroll: Cell::new(0),
            maximized: false,
            width_pct: 60,
            height_pct: 60,
            cursor_idx: 0,
            last_height: Cell::new(8),
        }
    }

    pub fn char_idx_to_line_col(&self, char_idx: usize) -> (usize, usize) {
        char_idx_to_line_col(&self.input_buffer, char_idx)
    }

    pub fn line_col_to_char_idx(&self, line: usize, col: usize) -> usize {
        line_col_to_char_idx(&self.input_buffer, line, col)
    }

    pub fn adjust_scroll(&mut self) {
        let (line, _) = self.char_idx_to_line_col(self.cursor_idx);
        let height = self.last_height.get().max(2);
        let scroll_val = self.scroll.get();
        if line < scroll_val {
            self.scroll.set(line);
        } else if line >= scroll_val + height {
            self.scroll.set(line - height + 1);
        }
    }

    fn insert_char(&mut self, c: char) {
        let mut idx = 0;
        let mut new_str = String::with_capacity(self.input_buffer.len() + 4);
        for ch in self.input_buffer.chars() {
            if idx == self.cursor_idx {
                new_str.push(c);
            }
            new_str.push(ch);
            idx += 1;
        }
        if idx == self.cursor_idx {
            new_str.push(c);
        }
        self.input_buffer = new_str;
        self.cursor_idx += 1;
        self.adjust_scroll();
    }

    fn backspace(&mut self) {
        if self.cursor_idx > 0 {
            let mut new_str = String::with_capacity(self.input_buffer.len());
            for (idx, ch) in self.input_buffer.chars().enumerate() {
                if idx != self.cursor_idx - 1 {
                    new_str.push(ch);
                }
            }
            self.input_buffer = new_str;
            self.cursor_idx -= 1;
            self.adjust_scroll();
        }
    }

    fn delete_char(&mut self) {
        if self.cursor_idx < self.input_buffer.chars().count() {
            let mut new_str = String::with_capacity(self.input_buffer.len());
            for (idx, ch) in self.input_buffer.chars().enumerate() {
                if idx != self.cursor_idx {
                    new_str.push(ch);
                }
            }
            self.input_buffer = new_str;
            self.adjust_scroll();
        }
    }

    fn delete_word_before_cursor(&mut self) {
        if self.cursor_idx > 0 {
            let chars: Vec<char> = self.input_buffer.chars().collect();
            let mut idx = self.cursor_idx;
            while idx > 0 && chars[idx - 1].is_whitespace() {
                idx -= 1;
            }
            while idx > 0 && !chars[idx - 1].is_whitespace() {
                idx -= 1;
            }
            let delete_count = self.cursor_idx - idx;
            if delete_count > 0 {
                let mut new_str = String::new();
                for (i, &ch) in chars.iter().enumerate() {
                    if i < idx || i >= self.cursor_idx {
                        new_str.push(ch);
                    }
                }
                self.input_buffer = new_str;
                self.cursor_idx = idx;
                self.adjust_scroll();
            }
        }
    }

    fn kill_line_after_cursor(&mut self) {
        let chars: Vec<char> = self.input_buffer.chars().collect();
        let mut idx = self.cursor_idx;
        while idx < chars.len() && chars[idx] != '\n' {
            idx += 1;
        }
        if idx == self.cursor_idx && idx < chars.len() && chars[idx] == '\n' {
            idx += 1;
        }
        let delete_count = idx - self.cursor_idx;
        if delete_count > 0 {
            let mut new_str = String::new();
            for (i, &ch) in chars.iter().enumerate() {
                if i < self.cursor_idx || i >= idx {
                    new_str.push(ch);
                }
            }
            self.input_buffer = new_str;
            self.adjust_scroll();
        }
    }

    fn move_up(&mut self) {
        let (line, col) = self.char_idx_to_line_col(self.cursor_idx);
        if line > 0 {
            self.cursor_idx = self.line_col_to_char_idx(line - 1, col);
            self.adjust_scroll();
        }
    }

    fn move_down(&mut self) {
        let (line, col) = self.char_idx_to_line_col(self.cursor_idx);
        self.cursor_idx = self.line_col_to_char_idx(line + 1, col);
        self.adjust_scroll();
    }

    fn move_home(&mut self) {
        let (line, _) = self.char_idx_to_line_col(self.cursor_idx);
        self.cursor_idx = self.line_col_to_char_idx(line, 0);
        self.adjust_scroll();
    }

    fn move_end(&mut self) {
        let (line, _) = self.char_idx_to_line_col(self.cursor_idx);
        self.cursor_idx = self.line_col_to_char_idx(line, usize::MAX);
        self.adjust_scroll();
    }
}

pub fn char_idx_to_line_col(text: &str, char_idx: usize) -> (usize, usize) {
    let mut line = 0;
    let mut col = 0;
    for (i, c) in text.chars().enumerate() {
        if i == char_idx {
            return (line, col);
        }
        if c == '\n' {
            line += 1;
            col = 0;
        } else {
            col += 1;
        }
    }
    (line, col)
}

pub fn line_col_to_char_idx(text: &str, line: usize, col: usize) -> usize {
    let mut current_line = 0;
    let mut current_col = 0;
    let mut line_start_char_idx = 0;
    for (i, c) in text.chars().enumerate() {
        if current_line == line {
            if current_col == col {
                return i;
            }
            if c == '\n' {
                return i;
            }
        }
        if c == '\n' {
            current_line += 1;
            current_col = 0;
            line_start_char_idx = i + 1;
        } else {
            current_col += 1;
        }
    }
    if current_line == line {
        return line_start_char_idx + col.min(current_col);
    }
    text.chars().count()
}

impl DrawableComponent for CommitPopup {
    fn draw(&self, _f: &mut ratatui::Frame, _rect: ratatui::layout::Rect) -> std::io::Result<()> {
        Ok(())
    }
}

impl Component for CommitPopup {
    fn event(&mut self, ev: &Event) -> std::io::Result<EventState> {
        if let Event::Key(key) = ev {
            if self.editing {
                match key.code {
                    KeyCode::Esc => {
                        self.queue.push(InternalEvent::ClosePopup);
                        return Ok(EventState::Consumed);
                    }
                    KeyCode::Char('s') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                        self.queue.push(InternalEvent::Commit);
                        return Ok(EventState::Consumed);
                    }
                    KeyCode::Char('a') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                        self.amend = !self.amend;
                        return Ok(EventState::Consumed);
                    }
                    KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                        self.editing = false;
                        return Ok(EventState::Consumed);
                    }
                    KeyCode::Char('h') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                        self.queue.push(InternalEvent::OpenCommitHistoryPicker);
                        return Ok(EventState::Consumed);
                    }
                    KeyCode::Char('d') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                        self.maximized = !self.maximized;
                        return Ok(EventState::Consumed);
                    }
                    KeyCode::Char('u') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                        self.input_buffer.clear();
                        self.cursor_idx = 0;
                        self.scroll.set(0);
                        return Ok(EventState::Consumed);
                    }
                    KeyCode::Char('k') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                        self.kill_line_after_cursor();
                        return Ok(EventState::Consumed);
                    }
                    KeyCode::Char('w') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                        self.delete_word_before_cursor();
                        return Ok(EventState::Consumed);
                    }
                    KeyCode::Char('b') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                        if self.cursor_idx > 0 {
                            self.cursor_idx -= 1;
                            self.adjust_scroll();
                        }
                        return Ok(EventState::Consumed);
                    }
                    KeyCode::Char('f') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                        if self.cursor_idx < self.input_buffer.chars().count() {
                            self.cursor_idx += 1;
                            self.adjust_scroll();
                        }
                        return Ok(EventState::Consumed);
                    }
                    KeyCode::Char('p') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                        self.move_up();
                        return Ok(EventState::Consumed);
                    }
                    KeyCode::Char('n') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                        self.move_down();
                        return Ok(EventState::Consumed);
                    }
                    KeyCode::Char(c) => {
                        self.insert_char(c);
                        return Ok(EventState::Consumed);
                    }
                    KeyCode::Backspace => {
                        self.backspace();
                        return Ok(EventState::Consumed);
                    }
                    KeyCode::Delete => {
                        self.delete_char();
                        return Ok(EventState::Consumed);
                    }
                    KeyCode::Enter => {
                        self.insert_char('\n');
                        return Ok(EventState::Consumed);
                    }
                    KeyCode::Left => {
                        if self.cursor_idx > 0 {
                            self.cursor_idx -= 1;
                            self.adjust_scroll();
                        }
                        return Ok(EventState::Consumed);
                    }
                    KeyCode::Right => {
                        if self.cursor_idx < self.input_buffer.chars().count() {
                            self.cursor_idx += 1;
                            self.adjust_scroll();
                        }
                        return Ok(EventState::Consumed);
                    }
                    KeyCode::Up => {
                        self.move_up();
                        return Ok(EventState::Consumed);
                    }
                    KeyCode::Down => {
                        self.move_down();
                        return Ok(EventState::Consumed);
                    }
                    KeyCode::Home => {
                        self.move_home();
                        return Ok(EventState::Consumed);
                    }
                    KeyCode::End => {
                        self.move_end();
                        return Ok(EventState::Consumed);
                    }
                    _ => {}
                }
            } else {
                match key.code {
                    KeyCode::Esc | KeyCode::Char('q') | KeyCode::Char('Q') => {
                        self.queue.push(InternalEvent::ClosePopup);
                        return Ok(EventState::Consumed);
                    }
                    KeyCode::Char('e') | KeyCode::Char('E') => {
                        self.editing = true;
                        return Ok(EventState::Consumed);
                    }
                    KeyCode::Char('a') | KeyCode::Char(' ') => {
                        self.amend = !self.amend;
                        return Ok(EventState::Consumed);
                    }
                    KeyCode::Char('d')
                    | KeyCode::Char('D')
                    | KeyCode::Char('m')
                    | KeyCode::Char('M') => {
                        self.maximized = !self.maximized;
                        return Ok(EventState::Consumed);
                    }
                    KeyCode::Enter => {
                        self.queue.push(InternalEvent::Commit);
                        return Ok(EventState::Consumed);
                    }
                    KeyCode::Up | KeyCode::Char('k') | KeyCode::Char('K') => {
                        self.scroll.set(self.scroll.get().saturating_sub(1));
                        return Ok(EventState::Consumed);
                    }
                    KeyCode::Down | KeyCode::Char('j') | KeyCode::Char('J') => {
                        self.scroll.set(self.scroll.get().saturating_add(1));
                        return Ok(EventState::Consumed);
                    }
                    KeyCode::Char('x')
                    | KeyCode::Char('X')
                    | KeyCode::Char('u')
                    | KeyCode::Char('U') => {
                        self.input_buffer.clear();
                        self.cursor_idx = 0;
                        self.scroll.set(0);
                        return Ok(EventState::Consumed);
                    }
                    _ => {}
                }
            }
        }
        Ok(EventState::NotConsumed)
    }
}

use crate::app::{App, Mode};
use crate::repo::RemoteInfo;
use crate::ui::layout::{centered_rect, centered_rect_fixed};
use crate::ui::style::{
    ACCENT, CARD_BORDER, DANGER, SUCCESS, WARNING, accent_style, muted_style, parse_color,
    primary_style,
};
use crate::ui_detail::DetailAreas;
use ratatui::Frame;
use ratatui::layout::{Alignment, Constraint, Direction, Layout, Margin, Position, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span, Text};
use ratatui::widgets::{Block, BorderType, Borders, Clear, Padding, Paragraph, Wrap};

pub fn draw_commit_popup(
    f: &mut Frame,
    input_buffer: &str,
    editing: bool,
    commit_amend: bool,
    _scroll: usize,
    area: Rect,
    app: &crate::app::App,
    areas: &mut DetailAreas,
) {
    let popup_area = if app.commit_popup.maximized {
        let width = area.width.saturating_sub(20).max(area.width.min(40));
        let height = area.height.saturating_sub(20).max(area.height.min(15));
        let x = area.x + (area.width.saturating_sub(width)) / 2;
        let y = area.y + (area.height.saturating_sub(height)) / 2;
        Rect::new(x, y, width, height)
    } else {
        centered_rect(app.commit_popup_width_pct, app.commit_popup_height_pct, area)
    };
    areas.commit_popup = Some(popup_area);
    areas.commit_popup_parent = Some(area);
    f.render_widget(Clear, popup_area);

    let border_color = if editing { ACCENT() } else { WARNING() };
    let border_style = Style::default().fg(border_color);

    let title_text = if editing {
        if commit_amend { " Amend Commit Message " } else { " Commit Message " }
    } else {
        if commit_amend { " Confirm Amend Commit " } else { " Confirm Commit " }
    };

    let title =
        Line::from(vec![Span::raw(" "), Span::styled(title_text, primary_style()), Span::raw(" ")]);

    let block = Block::default()
        .borders(Borders::ALL)
        .border_type(CARD_BORDER())
        .border_style(border_style)
        .title(title)
        .padding(Padding::horizontal(1));

    let inner_area = block.inner(popup_area);
    f.render_widget(block, popup_area);

    // Split inner area vertically: top is the commit message text area, bottom is the amend option.
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Min(0), Constraint::Length(1)])
        .split(inner_area);

    // Sync app.commit_popup.last_height so adjust_scroll works correctly on next event
    let text_area_height = chunks[0].height as usize;
    app.commit_popup.last_height.set(text_area_height);

    let scroll_val = app.commit_popup.scroll.get();
    let text = if input_buffer.is_empty() {
        Paragraph::new(Span::styled("(type commit message here...)", muted_style()))
            .wrap(Wrap { trim: true })
            .scroll((scroll_val as u16, 0))
    } else {
        Paragraph::new(input_buffer).wrap(Wrap { trim: true }).scroll((scroll_val as u16, 0))
    };

    f.render_widget(text, chunks[0]);

    // Render the amend checkbox.
    let checkbox = if commit_amend { "[X]" } else { "[ ]" };
    let checkbox_style = if commit_amend {
        Style::default().fg(SUCCESS()).add_modifier(Modifier::BOLD)
    } else {
        muted_style()
    };
    let checkbox_line = Line::from(vec![
        Span::styled(format!("{} ", checkbox), checkbox_style),
        Span::styled("Amend last commit", primary_style()),
        if !editing {
            Span::styled(" (toggle: [a/space] | clear: [x])", muted_style())
        } else {
            Span::styled(" (toggle: [⌃A] | clear: [⌃U] | history: [⌃H])", muted_style())
        },
    ]);
    f.render_widget(Paragraph::new(checkbox_line), chunks[1]);

    if editing {
        let (cursor_line, cursor_col) =
            char_idx_to_line_col(input_buffer, app.commit_popup.cursor_idx);
        let cursor_y = chunks[0].y.saturating_add(cursor_line.saturating_sub(scroll_val) as u16);
        let max_y = chunks[0].y.saturating_add(chunks[0].height.saturating_sub(1));
        let cursor_y = cursor_y.min(max_y);

        let cursor_x = chunks[0].x.saturating_add(cursor_col as u16);
        let max_x = chunks[0].x.saturating_add(chunks[0].width.saturating_sub(1));
        let cursor_x = cursor_x.min(max_x);

        f.set_cursor_position(ratatui::layout::Position::new(cursor_x, cursor_y));
    }
}

pub struct GenericInputPopup {
    pub queue: Queue,
}

impl GenericInputPopup {
    pub fn new(queue: Queue) -> Self {
        Self { queue }
    }
}

impl DrawableComponent for GenericInputPopup {
    fn draw(&self, _f: &mut ratatui::Frame, _rect: ratatui::layout::Rect) -> std::io::Result<()> {
        Ok(())
    }
}

impl Component for GenericInputPopup {
    fn event(&mut self, ev: &Event) -> std::io::Result<EventState> {
        if let Event::Key(key) = ev {
            match key.code {
                KeyCode::Esc => {
                    self.queue.push(InternalEvent::InputEsc);
                    return Ok(EventState::Consumed);
                }
                KeyCode::Enter => {
                    self.queue.push(InternalEvent::InputEnter);
                    return Ok(EventState::Consumed);
                }
                KeyCode::Backspace => {
                    self.queue.push(InternalEvent::InputBackspace);
                    return Ok(EventState::Consumed);
                }
                KeyCode::Char(c) => {
                    self.queue.push(InternalEvent::InputChar(c));
                    return Ok(EventState::Consumed);
                }
                _ => {}
            }
        }
        Ok(EventState::NotConsumed)
    }
}