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 | |
parent | af5b620f472ceefc2408f59fdf30b85e92390f7f (diff) |
grand refactor
-rw-r--r-- | src/cwd.rs | 22 | ||||
-rw-r--r-- | src/main.rs | 96 | ||||
-rw-r--r-- | src/prompt_char.rs | 14 | ||||
-rw-r--r-- | src/vcs.rs | 55 | ||||
-rw-r--r-- | src/venv.rs | 19 |
5 files changed, 118 insertions, 88 deletions
diff --git a/src/cwd.rs b/src/cwd.rs new file mode 100644 index 0000000..8f1f86a --- /dev/null +++ b/src/cwd.rs | |||
@@ -0,0 +1,22 @@ | |||
1 | use std::env; | ||
2 | use tico::tico; | ||
3 | use colored::*; | ||
4 | |||
5 | pub fn cwd() -> colored::ColoredString { | ||
6 | let mut path = env::var("PWD").unwrap(); | ||
7 | let home = env::var("HOME").unwrap(); | ||
8 | let tilde_expand = env::var("EXPAND_TILDE").unwrap_or("0".into()); | ||
9 | |||
10 | match tilde_expand.as_ref() { | ||
11 | "0" => {}, | ||
12 | _ => path = path.replace(&home[..], "~") | ||
13 | }; | ||
14 | |||
15 | let cwd_shorten = env::var("SHORTEN_CWD").unwrap_or("1".into()); | ||
16 | let cwd_color = env::var("CWD_COLOR").unwrap_or("white".into()); | ||
17 | match cwd_shorten.as_ref() { | ||
18 | "0" => return path.color(cwd_color), | ||
19 | _ => return tico(&path[..]).color(cwd_color) | ||
20 | } | ||
21 | |||
22 | } | ||
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 | } |
diff --git a/src/prompt_char.rs b/src/prompt_char.rs new file mode 100644 index 0000000..1df4c5d --- /dev/null +++ b/src/prompt_char.rs | |||
@@ -0,0 +1,14 @@ | |||
1 | use std::env; | ||
2 | use colored::*; | ||
3 | |||
4 | pub fn prompt_char() -> colored::ColoredString { | ||
5 | let user_char = env::var("PROMPT_CHAR").unwrap_or("$ ".into()); | ||
6 | let root_char = env::var("PROMPT_CHAR_ROOT").unwrap_or("# ".into()); | ||
7 | |||
8 | let euid = unsafe { libc::geteuid() }; | ||
9 | match euid { | ||
10 | 0 => return root_char.red(), | ||
11 | _ => return user_char.green() | ||
12 | } | ||
13 | } | ||
14 | |||
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 | } | ||
diff --git a/src/venv.rs b/src/venv.rs new file mode 100644 index 0000000..7906abc --- /dev/null +++ b/src/venv.rs | |||
@@ -0,0 +1,19 @@ | |||
1 | use std::env; | ||
2 | use colored::*; | ||
3 | use std::path::Path; | ||
4 | |||
5 | pub fn get_name() -> colored::ColoredString { | ||
6 | match env::var("VIRTUAL_ENV") { | ||
7 | Ok(venv_path) => { | ||
8 | let venv_name = Path::new(&venv_path[..]).file_name(); | ||
9 | if let Some(name) = venv_name { | ||
10 | if let Some(valid_name) = name.to_str() { | ||
11 | return format!("({})", valid_name).bright_black(); | ||
12 | } | ||
13 | } | ||
14 | } | ||
15 | Err(_) => {} | ||
16 | } | ||
17 | return "".white() | ||
18 | } | ||
19 | |||