oo-ide 0.0.4

∞ is a terminal IDE focused on low distraction, high usability.
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
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
543
544
545
546
547
548
549
//! Branch Management View — `vcs.branches`.
//!
//! Layout:
//! ```text
//! ┌─ Git Branches ─────────────────────────────────────────────────────┐
//! │ ─── Local Branches ─────────────────────────────────────────────── │
//! │ ● main                                                              │
//! │   feature/editor-refactor                                           │
//! │   bugfix/crash-on-start                                             │
//! │                                                                     │
//! │ ─── Remote Branches ────────────────────────────────────────────── │
//! │   origin/main                                                       │
//! │   origin/feature/editor-refactor                                    │
//! ├────────────────────────────────────────────────────────────────────┤
//! │ [Enter: checkout]  [n: new branch]  [Esc: back]                    │
//! └────────────────────────────────────────────────────────────────────┘
//! ```
//!
//! Keybindings:
//!   Up / Down / PgUp / PgDn  — navigate branch list
//!   Enter                    — checkout highlighted branch
//!   n                        — open new-branch name input
//!   Esc                      — close view / cancel input

use std::path::PathBuf;
use std::cell::Cell;

use ratatui::{
    Frame,
    layout::{Constraint, Direction, Layout, Rect},
    style::{Modifier, Style},
    text::{Line, Span},
    widgets::{List, ListItem, Paragraph},
};

use crate::prelude::*;
use input::{Key, KeyEvent};
use operation::{BranchViewOp, Event, Operation};
use settings::Settings;
use views::View;
use widgets::input_field::{self, InputField};

// ---------------------------------------------------------------------------
// Row — a unified list entry (section header or branch item)
// ---------------------------------------------------------------------------

#[derive(Debug, Clone)]
enum RowKind {
    Branch {
        name: String,
        is_current: bool,
        is_remote: bool,
    },
}

// ---------------------------------------------------------------------------
// BranchView
// ---------------------------------------------------------------------------

#[derive(Debug)]
pub struct BranchView {
    pub repo_path: PathBuf,
    local: Vec<String>,
    remote: Vec<String>,
    current: String,
    /// Flattened display rows (headers + branch items).
    rows: Vec<RowKind>,
    /// Cursor index into `rows`.  Always points to a `Branch` row.
    cursor: usize,
    /// Scroll offset (first visible row index) into `rows`.
    scroll: Cell<usize>,
    /// Last rendered visible height used for scrolling decisions.
    last_height: Cell<usize>,
    /// True while the initial branch list is being loaded.
    pub loading: bool,
    /// Optional new-branch name input (shown when user presses `n`).
    pub new_branch_input: Option<InputField>,
    /// Pending delete confirmation: branch name that needs a second Delete press.
    pub pending_delete: Option<String>,
    status_msg: Option<String>,
}

impl BranchView {
    pub fn new(repo_path: PathBuf) -> Self {
        Self {
            repo_path,
            local: Vec::new(),
            remote: Vec::new(),
            current: String::new(),
            rows: Vec::new(),
            cursor: 0,
            scroll: Cell::new(0),
            last_height: Cell::new(0),
            loading: true,
            new_branch_input: None,
            pending_delete: None,
            status_msg: None,
        }
    }

    /// Rebuild the flat row list from the current local/remote/current state.
    fn rebuild_rows(&mut self) {
        let mut rows = Vec::new();

        // Combine local then remote branches into a single flat list.
        for name in &self.local {
            rows.push(RowKind::Branch {
                name: name.clone(),
                is_current: name == &self.current,
                is_remote: false,
            });
        }

        for name in &self.remote {
            rows.push(RowKind::Branch {
                name: name.clone(),
                is_current: name == &self.current,
                is_remote: true,
            });
        }

        self.rows = rows;
        self.clamp_cursor_to_branch();
    }

    /// Ensure `cursor` points to a `Branch` row; advance to the first one if needed.
    fn clamp_cursor_to_branch(&mut self) {
        if matches!(self.rows.get(self.cursor), Some(RowKind::Branch { .. })) {
            return;
        }
        for (i, row) in self.rows.iter().enumerate() {
            if matches!(row, RowKind::Branch { .. }) {
                self.cursor = i;
                self.scroll.set(self.cursor); // ensure visible when rebuilding
                return;
            }
        }
    }

    fn move_up(&mut self) {
        if self.cursor == 0 {
            return;
        }
        let mut i = self.cursor - 1;
        loop {
            if matches!(self.rows.get(i), Some(RowKind::Branch { .. })) {
                self.cursor = i;
                // If cursor is above current scroll window, adjust scroll to show it.
                if self.cursor < self.scroll.get() {
                    self.scroll.set(self.cursor);
                }
                return;
            }
            if i == 0 {
                return;
            }
            i -= 1;
        }
    }

    fn move_down(&mut self) {
        let mut i = self.cursor + 1;
        while i < self.rows.len() {
            if matches!(self.rows.get(i), Some(RowKind::Branch { .. })) {
                self.cursor = i;
                // If cursor moved past bottom of visible area, adjust scroll.
                let vh = self.last_height.get();
                if vh > 0 && self.cursor >= self.scroll.get() + vh {
                    self.scroll.set(self.cursor + 1 - vh);
                }
                return;
            }
            i += 1;
        }
    }

    /// Return the name of the currently highlighted branch, if any.
    pub fn selected_branch_name(&self) -> Option<&str> {
        match self.rows.get(self.cursor) {
            Some(RowKind::Branch { name, .. }) => Some(name.as_str()),
            _ => None,
        }
    }

    /// True when the highlighted row is a remote-tracking branch.
    pub fn is_remote_selected(&self) -> bool {
        matches!(
            self.rows.get(self.cursor),
            Some(RowKind::Branch { is_remote: true, .. })
        )
    }

    /// Text currently entered in the new-branch input, if open.
    pub fn new_branch_text(&self) -> Option<&str> {
        self.new_branch_input.as_ref().map(|f| f.text())
    }
}

// ---------------------------------------------------------------------------
// View trait
// ---------------------------------------------------------------------------

impl View for BranchView {
    const KIND: crate::views::ViewKind = crate::views::ViewKind::Primary;

    fn save_state(&mut self, _app: &mut crate::app_state::AppState) {}

    fn handle_key(&self, key: KeyEvent) -> Vec<Operation> {
        // While the new-branch input is open, route all keys to it.
        if self.new_branch_input.is_some() {
            return match key.key {
                Key::Enter => vec![Operation::BranchViewLocal(BranchViewOp::NewBranchConfirm)],
                Key::Escape => vec![Operation::BranchViewLocal(BranchViewOp::NewBranchCancel)],
                _ => {
                    if let Some(fop) = input_field::key_to_op(key) {
                        vec![Operation::BranchViewLocal(BranchViewOp::NewBranchInput(fop))]
                    } else {
                        vec![]
                    }
                }
            };
        }

        match key.key {
            Key::ArrowUp => vec![Operation::NavigateUp],
            Key::ArrowDown => vec![Operation::NavigateDown],
            Key::PageUp => vec![Operation::NavigatePageUp],
            Key::PageDown => vec![Operation::NavigatePageDown],
            Key::Enter => vec![Operation::BranchViewLocal(BranchViewOp::CheckoutBranch)],
            Key::Char('n') => vec![Operation::BranchViewLocal(BranchViewOp::NewBranchOpen)],
            Key::Delete => {
                // If a pending delete exists for the currently selected branch, confirm.
                if let Some(sel) = self.selected_branch_name() {
                    if self.pending_delete.as_deref() == Some(sel) {
                        vec![Operation::BranchViewLocal(BranchViewOp::DeleteConfirmed)]
                    } else {
                        vec![Operation::BranchViewLocal(BranchViewOp::DeleteRequested)]
                    }
                } else {
                    vec![]
                }
            }
            Key::Escape => vec![Operation::Close],
            _ => vec![],
        }
    }

    fn handle_operation(&mut self, op: &Operation, _settings: &Settings) -> Option<Event> {
        match op {
            Operation::NavigateUp => {
                self.move_up();
                Some(Event::applied("git_branches", op.clone()))
            }
            Operation::NavigateDown => {
                self.move_down();
                Some(Event::applied("git_branches", op.clone()))
            }
            Operation::NavigatePageUp => {
                for _ in 0..10 {
                    self.move_up();
                }
                Some(Event::applied("git_branches", op.clone()))
            }
            Operation::NavigatePageDown => {
                for _ in 0..10 {
                    self.move_down();
                }
                Some(Event::applied("git_branches", op.clone()))
            }

            Operation::BranchViewLocal(bv_op) => {
                match bv_op {
                    BranchViewOp::BranchesLoaded { local, remote, current } => {
                        self.local = local.clone();
                        self.remote = remote.clone();
                        self.current = current.clone();
                        self.loading = false;
                        self.rebuild_rows();
                        // Try to position cursor on the current branch.
                        for (i, row) in self.rows.iter().enumerate() {
                            match row {
                                RowKind::Branch { name, .. } if name == current => {
                                    self.cursor = i;
                                    break;
                                }
                                _ => {}
                            }
                        }
                        self.scroll.set(self.cursor);
                    }
                    BranchViewOp::LoadError(msg) => {
                        self.status_msg = Some(format!("Error: {msg}"));
                        self.loading = false;
                    }
                    // CheckoutBranch and NewBranchConfirm are intercepted in app.rs;
                    // the view does not need to react here.
                    BranchViewOp::CheckoutBranch | BranchViewOp::NewBranchConfirm => {}
                    BranchViewOp::CheckoutCompleted(branch) => {
                        self.current = branch.clone();
                        self.status_msg = Some(format!("Switched to '{branch}'"));
                        self.loading = false;
                        self.rebuild_rows();
                    }
                    BranchViewOp::CheckoutError(msg) => {
                        self.status_msg = Some(format!("Checkout failed: {msg}"));
                        self.loading = false;
                    }
                    BranchViewOp::NewBranchOpen => {
                        self.new_branch_input = Some(InputField::new("Branch name"));
                        self.status_msg = None;
                    }
                    BranchViewOp::NewBranchInput(fop) => {
                        if let Some(f) = &mut self.new_branch_input {
                            f.apply(fop);
                        }
                    }
                    BranchViewOp::NewBranchCancel => {
                        self.new_branch_input = None;
                    }
                    BranchViewOp::CreateCompleted(branch) => {
                        self.new_branch_input = None;
                        self.current = branch.clone();
                        self.status_msg = Some(format!("Created and switched to '{branch}'"));
                        self.loading = false;
                        self.rebuild_rows();
                    }
                    BranchViewOp::CreateError(msg) => {
                        self.new_branch_input = None;
                        self.status_msg = Some(format!("Create failed: {msg}"));
                        self.loading = false;
                    }
                    BranchViewOp::DeleteConfirmNeeded { branch, merged } => {
                        // Ask the user to press Delete again to confirm.
                        self.pending_delete = Some(branch.clone());
                        if *merged {
                            self.status_msg = Some(format!("Press Delete again to delete '{branch}' (merged)"));
                        } else {
                            self.status_msg = Some(format!("Press Delete again to delete '{branch}' (not merged)"));
                        }
                    }
                    BranchViewOp::DeleteCompleted(branch) => {
                        self.pending_delete = None;
                        // Remove branch from lists if present.
                        self.local.retain(|b| b != branch);
                        self.remote.retain(|b| b != branch);
                        self.status_msg = Some(format!("Deleted '{branch}'"));
                        self.rebuild_rows();
                    }
                    BranchViewOp::DeleteError(msg) => {
                        self.pending_delete = None;
                        self.status_msg = Some(format!("Delete failed: {msg}"));
                    }
                    BranchViewOp::DeleteRequested | BranchViewOp::DeleteConfirmed => {
                        // Intercepted/handled by app.rs — no view action here.
                    }
                }
                Some(Event::applied("git_branches", op.clone()))
            }

            _ => None,
        }
    }

    fn render(&self, frame: &mut Frame, area: Rect, theme: &crate::theme::Theme) {
        render_view(self, frame, area, theme);
    }

    fn status_bar(
        &self,
        _state: &crate::app_state::AppState,
        bar: &mut crate::widgets::status_bar::StatusBarBuilder,
    ) {
        bar.label("Git Branches");
        if !self.current.is_empty() {
            bar.label(format!("  {}", self.current));
        }
    }
}

// ---------------------------------------------------------------------------
// Render
// ---------------------------------------------------------------------------

fn render_view(view: &BranchView, frame: &mut Frame, area: Rect, theme: &crate::theme::Theme) {
    let _bg = theme.bg();

    // Borderless layout: render directly into the provided area (no outer block).
    let inner = area;

    // Vertical split: branch list | optional input area (3 rows)
    let has_input = view.new_branch_input.is_some();
    let input_height = if has_input { 3 } else { 0 };

    let rows = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Min(1), Constraint::Length(input_height)])
        .split(inner);

    let list_area = rows[0];
    let input_area = rows[1];

    render_branch_list(view, frame, list_area, theme);

    if has_input {
        render_new_branch_input(view, frame, input_area, theme);
    }
}

fn render_branch_list(
    view: &BranchView,
    frame: &mut Frame,
    area: Rect,
    theme: &crate::theme::Theme,
) {
    let bg = theme.bg();
    let fg = theme.fg();
    let fg_dim = theme.fg_dim();
    let sel_bg = theme.selection_bg();
    let sel_fg = theme.selection_fg();
    let accent = theme.accent();

    if view.loading && view.rows.is_empty() {
        frame.render_widget(
            Paragraph::new(Span::styled("Loading…", Style::default().fg(fg_dim))),
            area,
        );
        return;
    }

    // Compute visible window (scroll) but preserve the view's scroll so that
    // upward movement only scrolls when the cursor reaches the top of the window.
    let height = area.height as usize;
    view.last_height.set(height);
    let total = view.rows.len();

    // Start from the persisted scroll offset.
    let mut scroll = if height == 0 { 0 } else { view.scroll.get() };

    // Clamp scroll into a valid range.
    let max_scroll = total.saturating_sub(height);
    if scroll > max_scroll { scroll = max_scroll; }

    if height > 0 {
        if view.cursor < scroll {
            // Cursor moved above the visible window — scroll up to show it at top.
            scroll = view.cursor;
        } else if view.cursor >= scroll + height {
            // Cursor moved below visible window — scroll down so cursor is visible at bottom.
            scroll = view.cursor + 1 - height;
            if scroll > max_scroll { scroll = max_scroll; }
        }
    } else {
        scroll = 0;
    }

    let items: Vec<ListItem> = view
        .rows
        .iter()
        .enumerate()
        .skip(scroll)
        .take(if height == 0 { 0 } else { height })
        .map(|(i, row)| {
            let RowKind::Branch { name, is_current, is_remote } = row;
            let selected = i == view.cursor;
            let item_bg = if selected { sel_bg } else { bg };
            let item_fg = if selected { sel_fg } else { fg };

            let indicator = if *is_current { "" } else { "  " };
            let indicator_style = Style::default()
                .fg(if *is_current { accent } else { fg_dim })
                .bg(item_bg);

            let name_style = Style::default()
                .fg(item_fg)
                .bg(item_bg)
                .add_modifier(if selected { Modifier::BOLD } else { Modifier::empty() });

            let remote_label_style = Style::default().fg(fg_dim).bg(item_bg);

            let mut spans = vec![
                Span::styled(indicator, indicator_style),
                Span::styled(name.clone(), name_style),
            ];

            if *is_remote {
                spans.push(Span::styled("  (remote)", remote_label_style));
            }

            if *is_current {
                spans.push(Span::styled(
                    "  (current)",
                    Style::default().fg(accent).bg(item_bg),
                ));
            }

            ListItem::new(Line::from(spans))
        })
        .collect();

    // If there are no items visible, show empty placeholder.
    if items.is_empty() {
        frame.render_widget(
            Paragraph::new(Span::styled("  (no branches)", Style::default().fg(fg_dim))),
            area,
        );
        // persist scroll
        view.scroll.set(0);
        return;
    }

    frame.render_widget(List::new(items).style(Style::default().bg(bg)), area);

    // persist computed scroll so subsequent moves use it.
    view.scroll.set(scroll);

    // Status message overlay at bottom-right of list.
    if let Some(msg) = &view.status_msg {
        let max_w = area.width.saturating_sub(2) as usize;
        let truncated: String = msg.chars().take(max_w).collect();
        let msg_len = truncated.chars().count() as u16;
        let msg_area = Rect {
            x: area.x + area.width.saturating_sub(msg_len + 1),
            y: area.y + area.height.saturating_sub(1),
            width: msg_len + 1,
            height: 1,
        };
        frame.render_widget(
            Paragraph::new(Span::styled(
                truncated,
                Style::default()
                    .fg(theme.fg_dim())
                    .add_modifier(Modifier::ITALIC),
            )),
            msg_area,
        );
    }
}


fn render_new_branch_input(
    view: &BranchView,
    frame: &mut Frame,
    area: Rect,
    theme: &crate::theme::Theme,
) {
    if let Some(input) = &view.new_branch_input {
        input.render(frame, area, true, theme);
    }
}