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/vcs.rs | |
parent | af5b620f472ceefc2408f59fdf30b85e92390f7f (diff) |
grand refactor
Diffstat (limited to 'src/vcs.rs')
-rw-r--r-- | src/vcs.rs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/vcs.rs b/src/vcs.rs new file mode 100644 index 0000000..db69897 --- /dev/null +++ b/src/vcs.rs | |||
@@ -0,0 +1,55 @@ | |||
1 | use std::env; | ||
2 | use git2::{ Repository, Status }; | ||
3 | use colored::*; | ||
4 | |||
5 | pub fn vcs_status() -> Option<(colored::ColoredString, colored::ColoredString)> { | ||
6 | let current_dir = env::var("PWD").unwrap(); | ||
7 | |||
8 | let repo = match Repository::open(current_dir) { | ||
9 | Ok(r) => r, | ||
10 | Err(_) => return None | ||
11 | }; | ||
12 | |||
13 | let reference = repo.head().unwrap(); | ||
14 | let mut branch; | ||
15 | |||
16 | if reference.is_branch() { | ||
17 | branch = format!("{}", reference.shorthand().unwrap()).bright_black(); | ||
18 | } else { | ||
19 | let commit = reference.peel_to_commit().unwrap(); | ||
20 | let id = commit.id(); | ||
21 | branch = format!("{:.6}", id).bright_black(); | ||
22 | } | ||
23 | |||
24 | let mut repo_stat = "".white(); | ||
25 | let git_clean_color = env::var("GIT_CLEAN_COLOR").unwrap_or("green".into()); | ||
26 | let git_wt_modified_color = env::var("GIT_WT_MODIFIED_COLOR").unwrap_or("red".into()); | ||
27 | let git_index_modified_color = env::var("GIT_INDEX_MODIFIED_COLOR").unwrap_or("yellow".into()); | ||
28 | |||
29 | let file_stats = repo.statuses(None).unwrap(); | ||
30 | for file in file_stats.iter() { | ||
31 | match file.status() { | ||
32 | // STATE: unstaged (working tree modified) | ||
33 | Status::WT_NEW | Status::WT_MODIFIED | | ||
34 | Status::WT_DELETED | Status::WT_TYPECHANGE | | ||
35 | Status::WT_RENAMED => { | ||
36 | let stat_char = env::var("GIT_WT_MODIFIED").unwrap_or("×".into()); | ||
37 | repo_stat = stat_char.color(&git_wt_modified_color[..]); | ||
38 | break; | ||
39 | }, | ||
40 | // STATE: staged (changes added to index) | ||
41 | Status::INDEX_NEW | Status::INDEX_MODIFIED | | ||
42 | Status::INDEX_DELETED | Status::INDEX_TYPECHANGE | | ||
43 | Status::INDEX_RENAMED => { | ||
44 | let stat_char = env::var("GIT_INDEX_MODIFIED").unwrap_or("±".into()); | ||
45 | repo_stat = stat_char.color(&git_index_modified_color[..]); | ||
46 | }, | ||
47 | // STATE: comitted (changes have been saved in the repo) | ||
48 | _ => { | ||
49 | let stat_char = env::var("GIT_CLEAN").unwrap_or("·".into()); | ||
50 | repo_stat = stat_char.color(&git_clean_color[..]); | ||
51 | } | ||
52 | } | ||
53 | } | ||
54 | return Some((branch, repo_stat)) | ||
55 | } | ||