From 7ed36ce4711b620ff89148d5ed3621fb2b72e7ae Mon Sep 17 00:00:00 2001 From: candifloss Date: Thu, 21 Nov 2024 20:04:16 +0530 Subject: [PATCH] modularity --- .idea/workspace.xml | 102 +++++++++++++++++++++++++++++++++++++++++ src/indicators/path.rs | 31 +++++++++++++ src/main.rs | 30 ++---------- 3 files changed, 136 insertions(+), 27 deletions(-) create mode 100644 .idea/workspace.xml create mode 100644 src/indicators/path.rs diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..aaf8a4a --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,102 @@ + + + + + + + + + + + + + + + + { + "associatedIndex": 2 +} + + + + + + + + + + + + + + + + + 1723662281778 + + + + + + \ No newline at end of file diff --git a/src/indicators/path.rs b/src/indicators/path.rs new file mode 100644 index 0000000..ff76519 --- /dev/null +++ b/src/indicators/path.rs @@ -0,0 +1,31 @@ +use std::env::{args, current_dir, var_os}; + +pub fn abrev_path(path: &str) -> String { + let mut short_dir = path.to_string(); + + let slashes = path.matches('/').count(); + + if slashes > 3 { + let parts: Vec<&str> = path.split('/').collect(); + let len = parts.len() - 1; + let mut ch1: String; + + for part in &parts[0..len] { + if part.to_string() != "" { + // to avoid the 1st "/" + ch1 = part.chars().next().expect(part).to_string(); // 1st char of each part + short_dir = short_dir.replace(part, &ch1); + } + } + } + short_dir +} + +//pwd +pub fn homedir() -> String { + var_os("HOME") + .expect("UnknownDir") + .to_str() + .expect("UnknownDir") + .to_string() +} diff --git a/src/main.rs b/src/main.rs index 4bb9529..df3d877 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,32 +5,12 @@ use std::env::{args, current_dir, var_os}; mod indicators { pub mod error; pub mod git; + pub mod path; pub mod shell; pub mod ssh; pub mod user; } -use crate::indicators::{error::*, git::*, shell::*, ssh::*, user::*}; - -fn abrev_path(path: &str) -> String { - let mut short_dir = path.to_string(); - - let slashes = path.matches('/').count(); - - if slashes > 3 { - let parts: Vec<&str> = path.split('/').collect(); - let len = parts.len() - 1; - let mut ch1: String; - - for part in &parts[0..len] { - if part.to_string() != "" { - // to avoid the 1st "/" - ch1 = part.chars().next().expect(part).to_string(); // 1st char of each part - short_dir = short_dir.replace(part, &ch1); - } - } - } - short_dir -} +use crate::indicators::{error::*, git::*, path::*, shell::*, ssh::*, user::*}; fn main() -> std::io::Result<()> { //let angle = "❯"; @@ -53,11 +33,7 @@ fn main() -> std::io::Result<()> { let git_char = get_git_char(&git_branch); //pwd - let homedir = var_os("HOME") - .expect("UnknownDir") - .to_str() - .expect("UnknownDir") - .to_string(); + let homedir = homedir(); let pwd = current_dir()?; let mut cur_dir = pwd.display().to_string(); cur_dir = cur_dir.replace(&git_repo_root, ""); // Remove git repo root