aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorNerdyPepper <[email protected]>2019-05-20 13:48:12 +0100
committerNerdyPepper <[email protected]>2019-05-20 13:48:12 +0100
commitf982aa673701bd5084a228ce9caee6941e3d86f1 (patch)
treedaf943b3bbc686e0818dfb697c6ad145a95e2f3f /src/main.rs
parentaf5b620f472ceefc2408f59fdf30b85e92390f7f (diff)
grand refactor
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs96
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 @@
1use std::env; 1mod cwd;
2use tico::tico; 2mod prompt_char;
3use git2::{ Repository, Status }; 3mod vcs;
4mod venv;
5
4use colored::*; 6use colored::*;
5 7
6fn main() { 8fn 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
13fn 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
32fn 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
43fn 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}