aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 65a293dc9f597f507bb046aa6c17725fb4301bdb (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
use std::env;
use tico::tico;
use git2::Repository;

fn main() {
    print!("{}", cwd());
    match vcs() {
        Some(br) => println!(" {}", br),
        None => println!()
    }
    println!("{}", prompt_char());
}

fn cwd() -> String {
    let path = env::var("PWD").unwrap();
    let short_or_not = env::var("SHORTEN_CWD").unwrap_or("1".into());

    match short_or_not.as_ref() {
        "0" => path,
        _ => tico(&path[..])
    }
}

fn prompt_char() -> String {
    let user_char = env::var("PROMPT_CHAR").unwrap_or("$ ".into());
    let root_char = env::var("PROMPT_CHAR_ROOT").unwrap_or("# ".into());

    let euid = unsafe { libc::geteuid() };
    match euid {
        0 => return root_char,
        _ => return user_char
    }
}

fn vcs() -> Option<String> {
    let current_dir = env::var("PWD").unwrap();

    let repo = match Repository::open(current_dir) {
        Ok(r) => r,
        Err(_) => return None
    };

    let reference = repo.head().unwrap();

    if reference.is_branch() {
        Some(format!("{}", reference.shorthand().unwrap()))
    } else {
        let commit = reference.peel_to_commit().unwrap();
        let id = commit.id();
        Some(format!("{}", id))
    }
}