aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 6ccca7be752e47575960c7d183ff12e8359de3f7 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
mod cwd;
mod prompt_char;
mod vcs;
mod venv;

use std::env;

use clap::{App, Arg};
use colored::*;

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 = match cwd::cwd() {
        Some(c) => c,
        None => "[directory does not exist]".color("red"),
    };
    let (branch, status) = match env::var("DISABLE_VCS").unwrap_or("0".into()).as_ref() {
        "0" => vcs::vcs_status().unwrap_or(("".into(), "".into())),
        _ => ("".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}{nix}{pchar} ",
            cwd = cwd,
            branch = branch,
            status = status,
            venv = venv,
            pchar = prompt_char,
            nix = venv::in_nix_shell()
        )
    }
}

fn pista_minimal(zsh: bool) -> String {
    let cwd = match cwd::cwd() {
        Some(c) => c,
        None => "[directory does not exist]".color("red"),
    };
    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
        )
    }
}