From 18168cf0e11cfdd213c4ad949af0420ff551ae03 Mon Sep 17 00:00:00 2001 From: candifloss Date: Tue, 26 Nov 2024 13:43:39 +0530 Subject: [PATCH] shorten pwd --- .idea/workspace.xml | 102 ----------------------------------------- src/indicators/path.rs | 13 ------ src/indicators/pwd.rs | 33 +++++++++++++ src/indicators/user.rs | 9 ++-- src/main.rs | 7 +-- 5 files changed, 40 insertions(+), 124 deletions(-) delete mode 100644 .idea/workspace.xml delete mode 100644 src/indicators/path.rs create mode 100644 src/indicators/pwd.rs diff --git a/.idea/workspace.xml b/.idea/workspace.xml deleted file mode 100644 index aaf8a4a..0000000 --- a/.idea/workspace.xml +++ /dev/null @@ -1,102 +0,0 @@ - - - - - - - - - - - - - - - - { - "associatedIndex": 2 -} - - - - - - - - - - - - - - - - - 1723662281778 - - - - - - \ No newline at end of file diff --git a/src/indicators/path.rs b/src/indicators/path.rs deleted file mode 100644 index 504b503..0000000 --- a/src/indicators/path.rs +++ /dev/null @@ -1,13 +0,0 @@ -use ansi_term::ANSIGenericString; -use ansi_term::Colour::RGB; -use std::env::current_dir; - -pub const PATH_COL: ansi_term::Colour = RGB(82, 82, 82); // - -pub fn pwd() -> ANSIGenericString<'static, str> { - let path = current_dir().map_or_else( - |_| "~~".to_string(), - |path| path.to_string_lossy().to_string(), - ); - PATH_COL.italic().paint(path) -} diff --git a/src/indicators/pwd.rs b/src/indicators/pwd.rs new file mode 100644 index 0000000..149f460 --- /dev/null +++ b/src/indicators/pwd.rs @@ -0,0 +1,33 @@ +use ansi_term::ANSIGenericString; +use ansi_term::Colour::RGB; +use std::env::current_dir; + +pub const UNKNOWN_PATH: &str = "\u{2248}"; // "≈" +pub const PATH_COL: ansi_term::Colour = RGB(82, 82, 82); + +fn home_dir() -> String { + std::env::var("HOME").map_or_else( + |_| "".to_string(), + |path| path.to_string(), + ) +} + +fn full_path() -> String { + current_dir().map_or_else( + |_| UNKNOWN_PATH.to_string(), + |path| path.to_string_lossy().to_string(), + ) +} + +fn replace_home(path: String) -> String { + let homedir = home_dir(); + path.replace(&homedir, "~") +} + +pub fn pwd(abbrev_home:bool) -> ANSIGenericString<'static, str> { + let mut path = full_path(); + if abbrev_home { + path = replace_home(path); + } + PATH_COL.italic().paint(path) +} \ No newline at end of file diff --git a/src/indicators/user.rs b/src/indicators/user.rs index a1193cb..8285977 100644 --- a/src/indicators/user.rs +++ b/src/indicators/user.rs @@ -1,6 +1,6 @@ use ansi_term::ANSIGenericString; use ansi_term::Colour::RGB; -use std::env::var_os; +use std::env::var; // User indicator symbol pub const USER_SYMBOL: &str = "\u{276F}"; // "❯" @@ -9,11 +9,8 @@ pub const NORMIE_COL: ansi_term::Colour = RGB(0, 255, 180); // A kind of green //Root user indicator pub fn username() -> String { - var_os("USER") - .expect("UnknownUser") - .to_str() - .expect("UnknownUser") - .to_string() + var("USER") + .unwrap_or_else(|_| "UnknownUser".to_string()) } pub fn indicator() -> ANSIGenericString<'static, str> { diff --git a/src/main.rs b/src/main.rs index 9d4094b..a5ddcb7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,14 @@ mod indicators { pub mod error; pub mod git; - pub mod path; + pub mod pwd; pub mod ssh; pub mod user; } use crate::indicators::error; use crate::indicators::git; -use crate::indicators::path; +use crate::indicators::pwd; use crate::indicators::ssh; use crate::indicators::user; @@ -21,9 +21,10 @@ fn main() { let indicate_err: bool = true; let indicate_git_branch: bool = true; let pwd: bool = true; + let abbrev_home:bool = true; if pwd { - prompt += &path::pwd().to_string(); + prompt += &pwd::pwd(abbrev_home).to_string(); } if indicate_ssh { prompt += &ssh::indicator().to_string();