Skip to main content

git_worktree_manager/
console.rs

1/// Console output formatting utilities.
2///
3/// Provides styled terminal output using the `console` crate.
4use console::{style, Style, Term};
5
6/// Print a styled header line.
7pub fn print_header(text: &str) {
8    let term = Term::stdout();
9    let _ = term.write_line(&format!("{}", style(text).cyan().bold()));
10}
11
12/// Print a styled success message.
13pub fn print_success(text: &str) {
14    let _ = Term::stdout().write_line(&format!("{}", style(text).green()));
15}
16
17/// Print a styled warning message.
18pub fn print_warning(text: &str) {
19    let _ = Term::stderr().write_line(&format!("{}", style(text).yellow()));
20}
21
22/// Print a styled error message.
23pub fn print_error(text: &str) {
24    let _ = Term::stderr().write_line(&format!("{}", style(text).red()));
25}
26
27/// Print dimmed text.
28pub fn print_dim(text: &str) {
29    let _ = Term::stdout().write_line(&format!("{}", style(text).dim()));
30}
31
32/// Status → color mapping used by list, tree, global_ops.
33pub fn status_style(status: &str) -> Style {
34    match status {
35        "active" => Style::new().green().bold(),
36        "clean" => Style::new().green(),
37        "modified" => Style::new().yellow(),
38        "stale" => Style::new().red(),
39        _ => Style::new(),
40    }
41}
42
43/// Map worktree status string to a display icon.
44pub fn status_icon(status: &str) -> &'static str {
45    match status {
46        "active" => "●",
47        "clean" => "○",
48        "modified" => "◉",
49        "stale" => "x",
50        _ => "○",
51    }
52}
53
54/// Get current terminal width (fallback 80).
55pub fn terminal_width() -> usize {
56    Term::stdout().size().1 as usize
57}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn test_status_icon_all_variants() {
65        assert_eq!(status_icon("active"), "●");
66        assert_eq!(status_icon("clean"), "○");
67        assert_eq!(status_icon("modified"), "◉");
68        assert_eq!(status_icon("stale"), "x");
69    }
70
71    #[test]
72    fn test_status_icon_unknown_fallback() {
73        assert_eq!(status_icon("unknown"), "○");
74        assert_eq!(status_icon(""), "○");
75        assert_eq!(status_icon("something_else"), "○");
76    }
77
78    #[test]
79    fn test_status_style_known() {
80        // Just ensure no panic for all known statuses
81        let _ = status_style("active");
82        let _ = status_style("clean");
83        let _ = status_style("modified");
84        let _ = status_style("stale");
85        let _ = status_style("unknown");
86    }
87}