From 08b238114e69aee9154b627eafdb1f3814dee670 Mon Sep 17 00:00:00 2001 From: Youssef Habri Date: Mon, 20 May 2019 02:26:12 +0000 Subject: [PATCH] feat: Implement the prompt module for username (#56) --- .gitignore | 3 +++ src/modules/directory.rs | 2 ++ src/modules/mod.rs | 2 ++ src/modules/username.rs | 49 ++++++++++++++++++++++++++++++++++++++++ src/print.rs | 1 + tests/directory.rs | 18 +++++++-------- 6 files changed, 66 insertions(+), 9 deletions(-) create mode 100644 src/modules/username.rs diff --git a/.gitignore b/.gitignore index a81d3520..4315faee 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,6 @@ Cargo.lock # VSCode configuration .vscode/ + +# Intellij IDE configuration +.idea/ \ No newline at end of file diff --git a/src/modules/directory.rs b/src/modules/directory.rs index 710f7359..991378bb 100644 --- a/src/modules/directory.rs +++ b/src/modules/directory.rs @@ -40,6 +40,8 @@ pub fn segment(context: &Context) -> Option { let truncated_dir_string = truncate(dir_string, DIR_TRUNCATION_LENGTH); module.new_segment("path", truncated_dir_string); + module.get_prefix().set_value("in "); + Some(module) } diff --git a/src/modules/mod.rs b/src/modules/mod.rs index 5b3e7167..a16409e7 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -8,6 +8,7 @@ mod nodejs; mod package; mod python; mod rust; +mod username; use crate::context::Context; use crate::module::Module; @@ -24,6 +25,7 @@ pub fn handle(module: &str, context: &Context) -> Option { "package" => package::segment(context), "git_branch" => git_branch::segment(context), "git_status" => git_status::segment(context), + "username" => username::segment(context), _ => panic!("Unknown module: {}", module), } diff --git a/src/modules/username.rs b/src/modules/username.rs new file mode 100644 index 00000000..1e35ff67 --- /dev/null +++ b/src/modules/username.rs @@ -0,0 +1,49 @@ +use ansi_term::{Color, Style}; +use std::env; +use std::process::Command; + +use super::{Context, Module}; + +/// Creates a segment with the current user's username +/// +/// Will display the usename if any of the following criteria are met: +/// - The current user isn't the same as the one that is logged in ($LOGNAME != $USER) +/// - The current user is root (UID = 0) +/// - The user is currently connected as an SSH session ($SSH_CONNECTION) +pub fn segment(_context: &Context) -> Option { + let user = env::var("USER").ok(); + let logname = env::var("LOGNAME").ok(); + let ssh_connection = env::var("SSH_CONNECTION").ok(); + + let mut module_color = Color::Yellow.bold(); + + if user != logname || ssh_connection.is_some() || is_root(&mut module_color) { + let mut module = Module::new("username"); + module.set_style(module_color); + module.new_segment("username", user?); + + return Some(module); + } + + None +} + +fn get_uid() -> Option { + match Command::new("id").arg("-u").output() { + Ok(output) => String::from_utf8(output.stdout) + .map(|uid| uid.trim().parse::().ok()) + .ok()?, + Err(_) => None, + } +} + +fn is_root(style: &mut Style) -> bool { + match get_uid() { + Some(uid) if uid == 0 => { + style.clone_from(&Color::Red.bold()); + + true + } + _ => false, + } +} diff --git a/src/print.rs b/src/print.rs index f4e5ae95..97717ad6 100644 --- a/src/print.rs +++ b/src/print.rs @@ -8,6 +8,7 @@ use crate::modules; pub fn prompt(args: ArgMatches) { let prompt_order = vec![ + "username", "directory", "git_branch", "git_status", diff --git a/tests/directory.rs b/tests/directory.rs index d14b7fc2..42d6398a 100644 --- a/tests/directory.rs +++ b/tests/directory.rs @@ -12,7 +12,7 @@ mod common; fn home_directory() -> io::Result<()> { let dir = Path::new("~"); - let expected = format!("via {} ", Color::Cyan.bold().paint("~").to_string()); + let expected = format!("in {} ", Color::Cyan.bold().paint("~").to_string()); let actual = common::render_module("dir", &dir); assert_eq!(expected, actual); @@ -26,7 +26,7 @@ fn directory_in_home() -> io::Result<()> { fs::create_dir_all(&dir)?; let expected = format!( - "via {} ", + "in {} ", Color::Cyan.bold().paint("~/starship/engine").to_string() ); let actual = common::render_module("dir", &dir); @@ -42,7 +42,7 @@ fn truncated_directory_in_home() -> io::Result<()> { fs::create_dir_all(&dir)?; let expected = format!( - "via {} ", + "in {} ", Color::Cyan .bold() .paint("starship/engine/schematics") @@ -58,7 +58,7 @@ fn truncated_directory_in_home() -> io::Result<()> { fn root_directory() -> io::Result<()> { let dir = Path::new("/"); - let expected = format!("via {} ", Color::Cyan.bold().paint("/").to_string()); + let expected = format!("in {} ", Color::Cyan.bold().paint("/").to_string()); let actual = common::render_module("dir", &dir); assert_eq!(expected, actual); @@ -69,7 +69,7 @@ fn root_directory() -> io::Result<()> { fn directory_in_root() -> io::Result<()> { let dir = Path::new("/tmp"); - let expected = format!("via {} ", Color::Cyan.bold().paint("/tmp").to_string()); + let expected = format!("in {} ", Color::Cyan.bold().paint("/tmp").to_string()); let actual = common::render_module("dir", &dir); assert_eq!(expected, actual); @@ -83,7 +83,7 @@ fn truncated_directory_in_root() -> io::Result<()> { fs::create_dir_all(&dir)?; let expected = format!( - "via {} ", + "in {} ", Color::Cyan .bold() .paint("starship/thrusters/rocket") @@ -105,7 +105,7 @@ fn git_repo_root() -> io::Result<()> { Repository::init(&repo_dir).unwrap(); let expected = format!( - "via {} ", + "in {} ", Color::Cyan.bold().paint("rocket-controls").to_string() ); let actual = common::render_module("dir", &repo_dir); @@ -125,7 +125,7 @@ fn directory_in_git_repo() -> io::Result<()> { Repository::init(&repo_dir).unwrap(); let expected = format!( - "via {} ", + "in {} ", Color::Cyan.bold().paint("rocket-controls/src").to_string() ); let actual = common::render_module("dir", &dir); @@ -145,7 +145,7 @@ fn truncated_directory_in_git_repo() -> io::Result<()> { Repository::init(&repo_dir).unwrap(); let expected = format!( - "via {} ", + "in {} ", Color::Cyan .bold() .paint("src/meters/fuel-gauge")