From f982aa673701bd5084a228ce9caee6941e3d86f1 Mon Sep 17 00:00:00 2001 From: NerdyPepper Date: Mon, 20 May 2019 18:18:12 +0530 Subject: grand refactor --- src/cwd.rs | 22 +++++++++++++ src/main.rs | 96 +++++------------------------------------------------- src/prompt_char.rs | 14 ++++++++ src/vcs.rs | 55 +++++++++++++++++++++++++++++++ src/venv.rs | 19 +++++++++++ 5 files changed, 118 insertions(+), 88 deletions(-) create mode 100644 src/cwd.rs create mode 100644 src/prompt_char.rs create mode 100644 src/vcs.rs create mode 100644 src/venv.rs (limited to 'src') 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 @@ +use std::env; +use tico::tico; +use colored::*; + +pub fn cwd() -> colored::ColoredString { + let mut path = env::var("PWD").unwrap(); + let home = env::var("HOME").unwrap(); + let tilde_expand = env::var("EXPAND_TILDE").unwrap_or("0".into()); + + match tilde_expand.as_ref() { + "0" => {}, + _ => path = path.replace(&home[..], "~") + }; + + let cwd_shorten = env::var("SHORTEN_CWD").unwrap_or("1".into()); + let cwd_color = env::var("CWD_COLOR").unwrap_or("white".into()); + match cwd_shorten.as_ref() { + "0" => return path.color(cwd_color), + _ => return tico(&path[..]).color(cwd_color) + } + +} 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 @@ -use std::env; -use tico::tico; -use git2::{ Repository, Status }; +mod cwd; +mod prompt_char; +mod vcs; +mod venv; + use colored::*; fn main() { - print!("{}", cwd()); - let (branch, status) = vcs_status().unwrap_or(("".into(), "".into())); + print!("{}", cwd::cwd()); + let (branch, status) = vcs::vcs_status().unwrap_or(("".into(), "".into())); println!(" {} {}", branch, status.dimmed()); - print!("{} ", prompt_char()); -} - -fn cwd() -> colored::ColoredString { - let mut path = env::var("PWD").unwrap(); - let home = env::var("HOME").unwrap(); - let tilde_expand = env::var("EXPAND_TILDE").unwrap_or("0".into()); - - match tilde_expand.as_ref() { - "0" => {}, - _ => path = path.replace(&home[..], "~") - }; - - let cwd_shorten = env::var("SHORTEN_CWD").unwrap_or("1".into()); - let cwd_color = env::var("CWD_COLOR").unwrap_or("white".into()); - match cwd_shorten.as_ref() { - "0" => return path.color(cwd_color), - _ => return tico(&path[..]).color(cwd_color) - } - -} - -fn prompt_char() -> colored::ColoredString { - 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.red(), - _ => return user_char.green() - } -} - -fn vcs_status() -> Option<(colored::ColoredString, colored::ColoredString)> { - 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(); - let mut branch; - - if reference.is_branch() { - branch = format!("{}", reference.shorthand().unwrap()).bright_black(); - } else { - let commit = reference.peel_to_commit().unwrap(); - let id = commit.id(); - branch = format!("{:.6}", id).bright_black(); - } - - let mut repo_stat = "".white(); - let git_clean_color = env::var("GIT_CLEAN_COLOR").unwrap_or("green".into()); - let git_wt_modified_color = env::var("GIT_WT_MODIFIED_COLOR").unwrap_or("red".into()); - let git_index_modified_color = env::var("GIT_INDEX_MODIFIED_COLOR").unwrap_or("yellow".into()); - - let file_stats = repo.statuses(None).unwrap(); - for file in file_stats.iter() { - match file.status() { - // STATE: unstaged (working tree modified) - Status::WT_NEW | Status::WT_MODIFIED | - Status::WT_DELETED | Status::WT_TYPECHANGE | - Status::WT_RENAMED => { - let stat_char = env::var("GIT_WT_MODIFIED").unwrap_or("×".into()); - repo_stat = stat_char.color(&git_wt_modified_color[..]); - break; - }, - // STATE: staged (changes added to index) - Status::INDEX_NEW | Status::INDEX_MODIFIED | - Status::INDEX_DELETED | Status::INDEX_TYPECHANGE | - Status::INDEX_RENAMED => { - let stat_char = env::var("GIT_INDEX_MODIFIED").unwrap_or("±".into()); - repo_stat = stat_char.color(&git_index_modified_color[..]); - }, - // STATE: comitted (changes have been saved in the repo) - _ => { - let stat_char = env::var("GIT_CLEAN").unwrap_or("·".into()); - repo_stat = stat_char.color(&git_clean_color[..]); - } - } - } - return Some((branch, repo_stat)) + print!("{}{} ", venv::get_name(), prompt_char::prompt_char()); } 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 @@ +use std::env; +use colored::*; + +pub fn prompt_char() -> colored::ColoredString { + 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.red(), + _ => return user_char.green() + } +} + 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 @@ +use std::env; +use git2::{ Repository, Status }; +use colored::*; + +pub fn vcs_status() -> Option<(colored::ColoredString, colored::ColoredString)> { + 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(); + let mut branch; + + if reference.is_branch() { + branch = format!("{}", reference.shorthand().unwrap()).bright_black(); + } else { + let commit = reference.peel_to_commit().unwrap(); + let id = commit.id(); + branch = format!("{:.6}", id).bright_black(); + } + + let mut repo_stat = "".white(); + let git_clean_color = env::var("GIT_CLEAN_COLOR").unwrap_or("green".into()); + let git_wt_modified_color = env::var("GIT_WT_MODIFIED_COLOR").unwrap_or("red".into()); + let git_index_modified_color = env::var("GIT_INDEX_MODIFIED_COLOR").unwrap_or("yellow".into()); + + let file_stats = repo.statuses(None).unwrap(); + for file in file_stats.iter() { + match file.status() { + // STATE: unstaged (working tree modified) + Status::WT_NEW | Status::WT_MODIFIED | + Status::WT_DELETED | Status::WT_TYPECHANGE | + Status::WT_RENAMED => { + let stat_char = env::var("GIT_WT_MODIFIED").unwrap_or("×".into()); + repo_stat = stat_char.color(&git_wt_modified_color[..]); + break; + }, + // STATE: staged (changes added to index) + Status::INDEX_NEW | Status::INDEX_MODIFIED | + Status::INDEX_DELETED | Status::INDEX_TYPECHANGE | + Status::INDEX_RENAMED => { + let stat_char = env::var("GIT_INDEX_MODIFIED").unwrap_or("±".into()); + repo_stat = stat_char.color(&git_index_modified_color[..]); + }, + // STATE: comitted (changes have been saved in the repo) + _ => { + let stat_char = env::var("GIT_CLEAN").unwrap_or("·".into()); + repo_stat = stat_char.color(&git_clean_color[..]); + } + } + } + return Some((branch, repo_stat)) +} 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 @@ +use std::env; +use colored::*; +use std::path::Path; + +pub fn get_name() -> colored::ColoredString { + match env::var("VIRTUAL_ENV") { + Ok(venv_path) => { + let venv_name = Path::new(&venv_path[..]).file_name(); + if let Some(name) = venv_name { + if let Some(valid_name) = name.to_str() { + return format!("({})", valid_name).bright_black(); + } + } + } + Err(_) => {} + } + return "".white() +} + -- cgit v1.2.3