aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 0603cba79d16390a9e34e12f5d538b57fccbdb8e (plain)
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
mod cwd;
mod prompt_char;
mod vcs;
mod venv;

use clap::{Arg, App};

fn main() {
    let matches = App::new("Pista")
        .version(env!("CARGO_PKG_VERSION"))
        .author(env!("CARGO_PKG_AUTHORS"))
        .about(env!("CARGO_PKG_DESCRIPTION"))
        .arg(Arg::with_name("minimal")
             .short("m")
             .long("minimal")
             .help("use minimal variant")
        )
        .arg(Arg::with_name("zsh")
             .short("z")
             .long("zsh")
             .help("Use ZSH formatting")
        )
        .get_matches();
    if matches.is_present("minimal") {
        println!("{}", pista_minimal(matches.is_present("zsh")));
    } else {
        println!("{}", pista(matches.is_present("zsh")));
    }
}

fn pista(zsh: bool) -> String {
    let cwd = cwd::cwd();
    let (branch, status) = vcs::vcs_status().unwrap_or(("".into(), "".into()));
    let venv = venv::get_name();
    let prompt_char = prompt_char::get_char();
    if zsh {
        format!("%{{{cwd} {branch} {status}%}} %{{\n{venv}{pchar}%}} ",
            cwd=cwd,
            branch=branch,
            status=status,
            venv=venv,
            pchar=prompt_char
            )
    } else {
        format!("{cwd} {branch} {status}\n{venv}{pchar} ",
            cwd=cwd,
            branch=branch,
            status=status,
            venv=venv,
            pchar=prompt_char
            )
    }
}

fn pista_minimal(zsh: bool) -> String {
    let cwd = cwd::cwd();
    let vcs_tuple = vcs::vcs_status();
    let mut vcs_component = String::new();
    if let Some((branch, status)) = vcs_tuple {
        vcs_component = format!(" [{} {}] ", branch, status);
    } else {
        vcs_component.push(' ');
    }
    let venv = venv::get_name();
    let prompt_char = prompt_char::get_char();
    if zsh {
        let fmt = format!("{cwd}{vcs}{venv}{pchar} ",
            cwd=cwd,
            vcs=vcs_component,
            venv=venv,
            pchar=prompt_char
        );
        let mut ret = String::new();
        let mut color = false;
        for ch in fmt.chars() {
            if color {
                if ch == 'm' { // colors always end with m
                    ret.push_str("m%}");
                    color = false;
                } else {
                    ret.push(ch)
                }
            } else {
                if ch == 0x1b_u8.into() { // ESC char, always starts colors
                    ret.push_str(&format!("%{{{esc}", esc=ch));
                    color = true;
                } else {
                    ret.push(ch);
                }
            }
        }
        ret
    } else {
        format!("{cwd}{vcs}{venv}{pchar} ",
            cwd=cwd,
            vcs=vcs_component,
            venv=venv,
            pchar=prompt_char
            )
    }
}