summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.gitignore3
-rw-r--r--src/modules/directory.rs2
-rw-r--r--src/modules/mod.rs2
-rw-r--r--src/modules/username.rs49
-rw-r--r--src/print.rs1
-rw-r--r--tests/directory.rs18
6 files changed, 66 insertions, 9 deletions
diff --git a/.gitignore b/.gitignore
index a81d3520e..4315faee5 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 710f73593..991378bbb 100644
--- a/src/modules/directory.rs
+++ b/src/modules/directory.rs
@@ -40,6 +40,8 @@ pub fn segment(context: &Context) -> Option<Module> {
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 5b3e7167f..a16409e74 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<Module> {
"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 000000000..1e35ff679
--- /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<Module> {
+ 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<u32> {
+ match Command::new("id").arg("-u").output() {
+ Ok(output) => String::from_utf8(output.stdout)
+ .map(|uid| uid.trim().parse::<u32>().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 f4e5ae950..97717ad6f 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 d14b7fc25..42d6398ad 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")