tui-lipan 0.1.0

Opinionated, component-based TUI framework for Rust - declarative components, reconciliation, layout engine, focus, overlays, and rich widgets on top of ratatui.
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::process::Command as ProcessCommand;
use std::sync::Arc;

use super::fs::FileKind;
use super::mod_private::FileTreeProps;
use super::{FileTreeChange, FileTreeChangeStatus};
use crate::style::Style;

/// Icon style for git status indicators.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
pub enum GitIconStyle {
    /// Text labels like "M", "A", "D"
    Text,
    /// Nerd font icons
    #[default]
    NerdFont,
}

/// Individual git change state (staged or unstaged).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum GitChangeState {
    /// File is modified.
    Modified,
    /// File is added.
    Added,
    /// File is deleted.
    Deleted,
    /// File is renamed.
    Renamed,
    /// File is untracked.
    Untracked,
    /// File has merge conflict.
    Conflicted,
}

impl GitChangeState {
    /// Get the marker/icon for this state based on the git icon style.
    /// Note: Icons do not include spacing; row rendering inserts separators.
    pub(crate) fn marker(self, style: GitIconStyle, is_staged: bool) -> &'static str {
        match style {
            GitIconStyle::Text => match self {
                Self::Modified => "M",
                Self::Added => "A",
                Self::Deleted => "D",
                Self::Renamed => "R",
                Self::Untracked => "?",
                Self::Conflicted => "!",
            },
            GitIconStyle::NerdFont => {
                // Simplified icons: different icons for staged vs unstaged
                match (self, is_staged) {
                    (Self::Modified, false) => "", // Unstaged modified
                    (Self::Modified, true) => "",  // Staged modified
                    (Self::Added, false) => "",    // Unstaged added (shows as untracked)
                    (Self::Added, true) => "",     // Staged added
                    (Self::Deleted, false) => "",  // Unstaged deleted
                    (Self::Deleted, true) => "",   // Staged deleted
                    (Self::Renamed, false) => "󰁕",  // Unstaged renamed
                    (Self::Renamed, true) => "󰛂",   // Staged renamed
                    (Self::Untracked, _) => "",
                    (Self::Conflicted, _) => "",
                }
            }
        }
    }

    pub(crate) fn style(self, props: &FileTreeProps) -> Style {
        match self {
            Self::Modified => props.git_style_modified,
            Self::Added => props.git_style_added,
            Self::Deleted => props.git_style_deleted,
            Self::Renamed => props.git_style_renamed,
            Self::Untracked => props.git_style_untracked,
            Self::Conflicted => props.git_style_conflicted,
        }
    }

    pub(crate) fn priority(self) -> u8 {
        match self {
            Self::Conflicted => 6,
            Self::Deleted => 5,
            Self::Renamed => 4,
            Self::Added => 3,
            Self::Modified => 2,
            Self::Untracked => 1,
        }
    }
}

/// Git file status entry holding both staged and unstaged states.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct GitFileStatus {
    /// Staged changes (index/worktree staging area).
    pub staged: Option<GitChangeState>,
    /// Unstaged changes (worktree modifications).
    pub unstaged: Option<GitChangeState>,
}

impl GitFileStatus {
    /// Create a new status entry from staged and unstaged states.
    pub fn new(staged: Option<GitChangeState>, unstaged: Option<GitChangeState>) -> Self {
        Self { staged, unstaged }
    }

    /// Get the primary status for styling (prioritizes staged over unstaged).
    pub fn primary_state(self) -> Option<GitChangeState> {
        self.staged.or(self.unstaged)
    }

    /// Get style for the primary state.
    /// Prioritizes staged changes (rendered as green).
    pub(crate) fn style(self, props: &FileTreeProps) -> Style {
        if self.staged.is_some() {
            return props.git_style_added;
        }
        match self.unstaged {
            Some(state) => state.style(props),
            None => Style::default(),
        }
    }
}

/// Numeric line-change summary from `git diff --numstat`.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct GitDiffStat {
    pub(crate) added: usize,
    pub(crate) removed: usize,
}

impl GitDiffStat {
    pub(crate) fn is_empty(self) -> bool {
        self.added == 0 && self.removed == 0
    }

    fn saturating_add(self, other: Self) -> Self {
        Self {
            added: self.added.saturating_add(other.added),
            removed: self.removed.saturating_add(other.removed),
        }
    }
}

/// Git decorations for a file-tree row.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct GitFileDecorations {
    pub(crate) status: GitFileStatus,
    pub(crate) diff_stat: Option<GitDiffStat>,
    pub(crate) direct: bool,
}

impl GitFileDecorations {
    pub(crate) fn from_status(status: GitFileStatus, direct: bool) -> Self {
        Self {
            status,
            diff_stat: None,
            direct,
        }
    }

    pub(crate) fn from_diff_stat(diff_stat: GitDiffStat, direct: bool) -> Self {
        Self {
            status: GitFileStatus::new(None, None),
            diff_stat: Some(diff_stat),
            direct,
        }
    }
}

/// Snapshot of git-derived decorations for a repository.
#[derive(Clone, Debug, Default)]
pub(crate) struct GitStatusSnapshot {
    pub(crate) entries: HashMap<Arc<str>, GitFileDecorations>,
    pub(crate) changed_paths: Vec<Arc<str>>,
    pub(crate) kinds: HashMap<Arc<str>, FileKind>,
    pub(crate) virtual_changes: bool,
}

impl GitDiffStat {
    pub(crate) fn new(added: usize, removed: usize) -> Self {
        Self { added, removed }
    }
}

pub(crate) fn provided_change_snapshot(
    root: &str,
    changes: &[FileTreeChange],
) -> GitStatusSnapshot {
    let root_path_buf = provided_root_path(root);
    let root_path = root_path_buf.as_path();
    let mut snapshot = GitStatusSnapshot {
        entries: HashMap::new(),
        changed_paths: Vec::new(),
        kinds: HashMap::new(),
        virtual_changes: true,
    };

    for change in changes {
        let Some(full_path) = lexical_change_path(root_path, change.path.as_ref()) else {
            continue;
        };
        let path = Arc::<str>::from(full_path.to_string_lossy().as_ref());
        snapshot.changed_paths.push(path.clone());
        if let Some(kind) = change.kind {
            snapshot.kinds.insert(path.clone(), kind);
        }

        let status = provided_status(change.status, change.staged);
        insert_provided_decoration_path_and_parents(
            &mut snapshot.entries,
            &full_path,
            GitFileDecorations::from_status(status, true),
            root_path,
        );

        if change.additions > 0 || change.deletions > 0 {
            insert_provided_decoration_path_and_parents(
                &mut snapshot.entries,
                &full_path,
                GitFileDecorations::from_diff_stat(
                    GitDiffStat::new(change.additions, change.deletions),
                    true,
                ),
                root_path,
            );
        }
    }

    snapshot.changed_paths.sort();
    snapshot.changed_paths.dedup();
    snapshot
}

fn provided_status(status: FileTreeChangeStatus, staged: bool) -> GitFileStatus {
    let state = Some(GitChangeState::from(status));
    if staged {
        GitFileStatus::new(state, None)
    } else {
        GitFileStatus::new(None, state)
    }
}

pub(crate) fn provided_root_path(root: &str) -> PathBuf {
    let root = Path::new(root);
    if let Ok(canonical) = std::fs::canonicalize(root) {
        return canonical;
    }

    let absolute = if root.is_absolute() {
        root.to_path_buf()
    } else {
        std::env::current_dir()
            .unwrap_or_else(|_| PathBuf::from("."))
            .join(root)
    };
    lexical_normalize_path(&absolute)
}

fn lexical_change_path(root: &Path, path: &str) -> Option<PathBuf> {
    let input = Path::new(path);
    let output = if input.is_absolute() {
        lexical_normalize_path(input)
    } else {
        lexical_normalize_path(&root.join(input))
    };

    if output.starts_with(root) {
        Some(output)
    } else {
        None
    }
}

fn lexical_normalize_path(path: &Path) -> PathBuf {
    let mut output = PathBuf::new();
    for component in path.components() {
        match component {
            std::path::Component::Prefix(prefix) => output.push(prefix.as_os_str()),
            std::path::Component::RootDir => output.push(component.as_os_str()),
            std::path::Component::CurDir => {}
            std::path::Component::ParentDir => {
                output.pop();
            }
            std::path::Component::Normal(part) => output.push(part),
        }
    }
    output
}

pub(crate) fn discover_git_root(path: &Path) -> Option<PathBuf> {
    let start = if path.is_dir() {
        path.to_path_buf()
    } else {
        path.parent()?.to_path_buf()
    };

    for ancestor in start.ancestors() {
        let marker = ancestor.join(".git");
        if marker.exists() {
            return Some(ancestor.to_path_buf());
        }
    }

    None
}

pub(crate) fn load_git_snapshot(
    repo_root: &Path,
    include_diff_stats: bool,
) -> Option<GitStatusSnapshot> {
    let output = ProcessCommand::new("git")
        .arg("-C")
        .arg(repo_root)
        .arg("status")
        .arg("--porcelain=v1")
        .arg("--untracked-files=normal")
        .arg("--ignored=no")
        .output()
        .ok()?;

    if !output.status.success() {
        return None;
    }

    let stdout = String::from_utf8_lossy(&output.stdout);
    let mut snapshot = GitStatusSnapshot {
        entries: HashMap::new(),
        changed_paths: Vec::new(),
        kinds: HashMap::new(),
        virtual_changes: false,
    };

    for line in stdout.lines() {
        let Some((relative_path, status)) = parse_git_porcelain_line(line) else {
            continue;
        };
        let full_path = super::fs::normalize_path(&repo_root.join(relative_path));
        snapshot.changed_paths.push(full_path.clone());
        insert_decoration_path_and_parents(
            &mut snapshot.entries,
            Path::new(full_path.as_ref()),
            GitFileDecorations::from_status(status, true),
            repo_root,
        );
    }

    if include_diff_stats {
        load_git_diff_stats(repo_root, &mut snapshot.entries);
    }

    Some(snapshot)
}

pub(crate) fn parse_git_porcelain_line(line: &str) -> Option<(&str, GitFileStatus)> {
    if line.len() < 3 {
        return None;
    }

    let status_code = &line[..2];
    let path_field = line.get(3..)?.trim();

    let status = if status_code == "??" {
        GitFileStatus::new(None, Some(GitChangeState::Untracked))
    } else {
        let mut chars = status_code.chars();
        let x = chars.next()?;
        let y = chars.next()?;
        git_status_from_xy(x, y)?
    };

    let path = if status_code.contains('R') {
        path_field
            .rsplit_once(" -> ")
            .map(|(_, to)| to)
            .unwrap_or(path_field)
    } else {
        path_field
    };

    Some((path, status))
}

pub(crate) fn parse_git_numstat_line(line: &str) -> Option<(String, GitDiffStat)> {
    let mut fields = line.splitn(3, '\t');
    let added = fields.next()?;
    let removed = fields.next()?;
    let path = fields.next()?.trim();

    if added == "-" || removed == "-" || path.is_empty() {
        return None;
    }

    Some((
        normalize_numstat_path(path),
        GitDiffStat {
            added: added.parse().ok()?,
            removed: removed.parse().ok()?,
        },
    ))
}

fn normalize_numstat_path(path: &str) -> String {
    let Some((before, after)) = path.split_once(" => ") else {
        return path.to_string();
    };

    if let Some(open_brace) = before.rfind('{') {
        let prefix = &before[..open_brace];
        let (renamed, suffix) = after.split_once('}').unwrap_or((after, ""));
        return format!("{prefix}{renamed}{suffix}");
    }

    after.to_string()
}

fn load_git_diff_stats(repo_root: &Path, entries: &mut HashMap<Arc<str>, GitFileDecorations>) {
    let Some(output) = ProcessCommand::new("git")
        .arg("-C")
        .arg(repo_root)
        .arg("diff")
        .arg("--numstat")
        .arg("HEAD")
        .arg("--")
        .output()
        .ok()
    else {
        return;
    };

    if !output.status.success() {
        return;
    }

    let stdout = String::from_utf8_lossy(&output.stdout);
    for line in stdout.lines() {
        let Some((relative_path, diff_stat)) = parse_git_numstat_line(line) else {
            continue;
        };
        let full_path = super::fs::normalize_path(&repo_root.join(relative_path));
        insert_decoration_path_and_parents(
            entries,
            Path::new(full_path.as_ref()),
            GitFileDecorations::from_diff_stat(diff_stat, true),
            repo_root,
        );
    }
}

fn char_to_change_state(c: char) -> Option<GitChangeState> {
    match c {
        'M' | 'C' => Some(GitChangeState::Modified),
        'A' => Some(GitChangeState::Added),
        'D' => Some(GitChangeState::Deleted),
        'R' => Some(GitChangeState::Renamed),
        'U' => Some(GitChangeState::Conflicted),
        _ => None,
    }
}

fn git_status_from_xy(x: char, y: char) -> Option<GitFileStatus> {
    let staged = char_to_change_state(x);
    let unstaged = char_to_change_state(y);

    // If both are None, there's no status
    if staged.is_none() && unstaged.is_none() {
        return None;
    }

    Some(GitFileStatus::new(staged, unstaged))
}

fn insert_decoration_path_and_parents(
    entries: &mut HashMap<Arc<str>, GitFileDecorations>,
    full_path: &Path,
    decoration: GitFileDecorations,
    repo_root: &Path,
) {
    insert_decoration(entries, super::fs::normalize_path(full_path), decoration);

    let mut current = full_path.parent();
    while let Some(parent) = current {
        if !parent.starts_with(repo_root) {
            break;
        }
        insert_decoration(
            entries,
            super::fs::normalize_path(parent),
            GitFileDecorations {
                direct: false,
                ..decoration
            },
        );
        if parent == repo_root {
            break;
        }
        current = parent.parent();
    }
}

fn insert_provided_decoration_path_and_parents(
    entries: &mut HashMap<Arc<str>, GitFileDecorations>,
    full_path: &Path,
    decoration: GitFileDecorations,
    repo_root: &Path,
) {
    insert_decoration(entries, lossy_path_arc(full_path), decoration);

    let mut current = full_path.parent();
    while let Some(parent) = current {
        insert_decoration(
            entries,
            lossy_path_arc(parent),
            GitFileDecorations {
                direct: false,
                ..decoration
            },
        );
        if parent == repo_root {
            break;
        }
        current = parent.parent();
    }
}

fn lossy_path_arc(path: &Path) -> Arc<str> {
    Arc::<str>::from(path.to_string_lossy().as_ref())
}

#[cfg(test)]
pub(crate) fn insert_status(
    statuses: &mut HashMap<Arc<str>, GitFileStatus>,
    path: Arc<str>,
    status: GitFileStatus,
) {
    match statuses.get(path.as_ref()).copied() {
        Some(existing) => {
            // Merge the statuses - keep the higher priority for each field
            let merged = GitFileStatus::new(
                // Keep staged if it has higher priority, otherwise use new
                match (existing.staged, status.staged) {
                    (Some(e), Some(s)) if e.priority() >= s.priority() => Some(e),
                    (e, None) => e,
                    (_, s) => s,
                },
                // Keep unstaged if it has higher priority, otherwise use new
                match (existing.unstaged, status.unstaged) {
                    (Some(e), Some(s)) if e.priority() >= s.priority() => Some(e),
                    (e, None) => e,
                    (_, s) => s,
                },
            );
            statuses.insert(path, merged);
        }
        None => {
            statuses.insert(path, status);
        }
    }
}

pub(crate) fn insert_decoration(
    entries: &mut HashMap<Arc<str>, GitFileDecorations>,
    path: Arc<str>,
    decoration: GitFileDecorations,
) {
    match entries.get(path.as_ref()).copied() {
        Some(existing) => {
            let merged = GitFileDecorations {
                status: merge_status(existing.status, decoration.status),
                diff_stat: merge_diff_stat(existing.diff_stat, decoration.diff_stat),
                direct: existing.direct || decoration.direct,
            };
            entries.insert(path, merged);
        }
        None => {
            entries.insert(path, decoration);
        }
    }
}

fn merge_status(existing: GitFileStatus, status: GitFileStatus) -> GitFileStatus {
    GitFileStatus::new(
        match (existing.staged, status.staged) {
            (Some(e), Some(s)) if e.priority() >= s.priority() => Some(e),
            (e, None) => e,
            (_, s) => s,
        },
        match (existing.unstaged, status.unstaged) {
            (Some(e), Some(s)) if e.priority() >= s.priority() => Some(e),
            (e, None) => e,
            (_, s) => s,
        },
    )
}

fn merge_diff_stat(
    existing: Option<GitDiffStat>,
    diff_stat: Option<GitDiffStat>,
) -> Option<GitDiffStat> {
    match (existing, diff_stat) {
        (Some(existing), Some(diff_stat)) => Some(existing.saturating_add(diff_stat)),
        (Some(existing), None) => Some(existing),
        (None, Some(diff_stat)) => Some(diff_stat),
        (None, None) => None,
    }
}