diff options
author | NerdyPepper <[email protected]> | 2019-05-20 13:48:12 +0100 |
---|---|---|
committer | NerdyPepper <[email protected]> | 2019-05-20 13:48:12 +0100 |
commit | f982aa673701bd5084a228ce9caee6941e3d86f1 (patch) | |
tree | daf943b3bbc686e0818dfb697c6ad145a95e2f3f /src/main.rs | |
parent | af5b620f472ceefc2408f59fdf30b85e92390f7f (diff) |
grand refactor
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 96 |
1 files changed, 8 insertions, 88 deletions
diff --git a/src/main.rs b/src/main.rs index 33d706d..bfa47a0 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -1,93 +1,13 @@ | |||
1 | use std::env; | 1 | mod cwd; |
2 | use tico::tico; | 2 | mod prompt_char; |
3 | use git2::{ Repository, Status }; | 3 | mod vcs; |
4 | mod venv; | ||
5 | |||
4 | use colored::*; | 6 | use colored::*; |
5 | 7 | ||
6 | fn main() { | 8 | fn main() { |
7 | print!("{}", cwd()); | 9 | print!("{}", cwd::cwd()); |
8 | let (branch, status) = vcs_status().unwrap_or(("".into(), "".into())); | 10 | let (branch, status) = vcs::vcs_status().unwrap_or(("".into(), "".into())); |
9 | println!(" {} {}", branch, status.dimmed()); | 11 | println!(" {} {}", branch, status.dimmed()); |
10 | print!("{} ", prompt_char()); | 12 | print!("{}{} ", venv::get_name(), prompt_char::prompt_char()); |
11 | } | ||
12 | |||
13 | fn cwd() -> colored::ColoredString { | ||
14 | let mut path = env::var("PWD").unwrap(); | ||
15 | let home = env::var("HOME").unwrap(); | ||
16 | let tilde_expand = env::var("EXPAND_TILDE").unwrap_or("0".into()); | ||
17 | |||
18 | match tilde_expand.as_ref() { | ||
19 | "0" => {}, | ||
20 | _ => path = path.replace(&home[..], "~") | ||
21 | }; | ||
22 | |||
23 | let cwd_shorten = env::var("SHORTEN_CWD").unwrap_or("1".into()); | ||
24 | let cwd_color = env::var("CWD_COLOR").unwrap_or("white".into()); | ||
25 | match cwd_shorten.as_ref() { | ||
26 | "0" => return path.color(cwd_color), | ||
27 | _ => return tico(&path[..]).color(cwd_color) | ||
28 | } | ||
29 | |||
30 | } | ||
31 | |||
32 | fn prompt_char() -> colored::ColoredString { | ||
33 | let user_char = env::var("PROMPT_CHAR").unwrap_or("$ ".into()); | ||
34 | let root_char = env::var("PROMPT_CHAR_ROOT").unwrap_or("# ".into()); | ||
35 | |||
36 | let euid = unsafe { libc::geteuid() }; | ||
37 | match euid { | ||
38 | 0 => return root_char.red(), | ||
39 | _ => return user_char.green() | ||
40 | } | ||
41 | } | ||
42 | |||
43 | fn vcs_status() -> Option<(colored::ColoredString, colored::ColoredString)> { | ||
44 | let current_dir = env::var("PWD").unwrap(); | ||
45 | |||
46 | let repo = match Repository::open(current_dir) { | ||
47 | Ok(r) => r, | ||
48 | Err(_) => return None | ||
49 | }; | ||
50 | |||
51 | let reference = repo.head().unwrap(); | ||
52 | let mut branch; | ||
53 | |||
54 | if reference.is_branch() { | ||
55 | branch = format!("{}", reference.shorthand().unwrap()).bright_black(); | ||
56 | } else { | ||
57 | let commit = reference.peel_to_commit().unwrap(); | ||
58 | let id = commit.id(); | ||
59 | branch = format!("{:.6}", id).bright_black(); | ||
60 | } | ||
61 | |||
62 | let mut repo_stat = "".white(); | ||
63 | let git_clean_color = env::var("GIT_CLEAN_COLOR").unwrap_or("green".into()); | ||
64 | let git_wt_modified_color = env::var("GIT_WT_MODIFIED_COLOR").unwrap_or("red".into()); | ||
65 | let git_index_modified_color = env::var("GIT_INDEX_MODIFIED_COLOR").unwrap_or("yellow".into()); | ||
66 | |||
67 | let file_stats = repo.statuses(None).unwrap(); | ||
68 | for file in file_stats.iter() { | ||
69 | match file.status() { | ||
70 | // STATE: unstaged (working tree modified) | ||
71 | Status::WT_NEW | Status::WT_MODIFIED | | ||
72 | Status::WT_DELETED | Status::WT_TYPECHANGE | | ||
73 | Status::WT_RENAMED => { | ||
74 | let stat_char = env::var("GIT_WT_MODIFIED").unwrap_or("×".into()); | ||
75 | repo_stat = stat_char.color(&git_wt_modified_color[..]); | ||
76 | break; | ||
77 | }, | ||
78 | // STATE: staged (changes added to index) | ||
79 | Status::INDEX_NEW | Status::INDEX_MODIFIED | | ||
80 | Status::INDEX_DELETED | Status::INDEX_TYPECHANGE | | ||
81 | Status::INDEX_RENAMED => { | ||
82 | let stat_char = env::var("GIT_INDEX_MODIFIED").unwrap_or("±".into()); | ||
83 | repo_stat = stat_char.color(&git_index_modified_color[..]); | ||
84 | }, | ||
85 | // STATE: comitted (changes have been saved in the repo) | ||
86 | _ => { | ||
87 | let stat_char = env::var("GIT_CLEAN").unwrap_or("·".into()); | ||
88 | repo_stat = stat_char.color(&git_clean_color[..]); | ||
89 | } | ||
90 | } | ||
91 | } | ||
92 | return Some((branch, repo_stat)) | ||
93 | } | 13 | } |