From b0144c5d0755812e7e1c8ff1eb3b2ad5cf43890f Mon Sep 17 00:00:00 2001 From: candifloss Date: Wed, 14 Aug 2024 20:28:16 +0530 Subject: [PATCH] more sensible code --- src/main.rs | 96 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 60 insertions(+), 36 deletions(-) diff --git a/src/main.rs b/src/main.rs index 95f484d..a9ced4f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,15 +2,66 @@ use std::env::current_dir as current_dir; use std::env::var_os as var_os; use std::env::args as args; use colored::Colorize; +use std::path::PathBuf; +use std::ffi::OsString; + +fn get_shell_char (shell: String) -> String { + let shell_char = match shell.as_str() { + "bash"|"/bin/bash" + => "", + "zsh"|"/bin/zsh"|"/usr/bin/zsh" + => "󰰶", + "fish" + => "󰈺", + "nushell" + => "", + "ion" + => "", + "oursh" + => "󱢇", + _ + => "󱆃", + }; + shell_char.to_string() +} + +fn abrev_path (pwd: PathBuf, homedir: String) -> String { + let mut path = pwd.display().to_string(); + path = path.replace(&homedir, "~"); // Abreviate homedir with "~" + let mut short_dir = path.clone(); + + let slashes = path.matches("/").count(); + + if slashes > 3 { + let parts: Vec<&str> = path.split("/").collect(); + let len = parts.len() - 1; + let mut fch: String; + + for p in &parts[0..len] { + let part = p; + fch = part.chars().next().expect("REASON").to_string(); //1st char of p + short_dir = short_dir.replace(part, &fch); + } + } + short_dir +} + +fn root_user (user: Option) -> bool { + let u = user.expect("UnknownUser").to_str().expect("UnknownUser").to_string(); + if u == "root" + { true } + else + { false } +} fn main() -> std::io::Result<()> { let angle = "❯"; let pwd = current_dir()?; - let mut dir = pwd.display().to_string(); let args: Vec = args().collect(); let shell: String; - let user = var_os("USER").expect("noDir").to_str().expect("noDir").to_string(); - let homedir = var_os("HOME").expect("noDir").to_str().expect("noDir").to_string(); + let user = var_os("USER"); //.expect("UnknownUser").to_str().expect("UnknownUser").to_string(); + //let user = var_os("USER").expect("UnknownUser").to_str().expect("UnknownUser").to_string(); + let homedir = var_os("HOME").expect("UnknownDir").to_str().expect("UnknownDir").to_string(); if args.len() > 1 { shell = args[1].clone(); @@ -18,41 +69,14 @@ fn main() -> std::io::Result<()> { else { shell = "none".to_string(); } - - let shell_char = match shell.as_str() { - "bash" => "", - "zsh" => "󰰶", - "fish" => "󰈺", - "nushell" => "", - "ion" => "", - _ => "󱆃", - }; - - dir = str::replace(&dir, &homedir, "~"); // Abreviate homedir with "~" - - let mut dir0 = dir.clone(); - let slashes = dir.matches("/").count(); - //println!("{}",slashes); - - if slashes > 3 { - // let mut dir0 = dir; - let parts: Vec<&str> = dir.split("/").collect(); - let len = parts.len() - 1; - let mut fch: String; - - for p in &parts[0..len] { - let part = p; - fch = part.chars().next().expect("REASON").to_string(); //1st char of p - dir0 = str::replace(&dir0, part, &fch); - //println!("{}__{}__{}",p,fch,dir0); - } - } - //dir = dir0; + //if root_user(user) { + // println!("heh"); + //} print!("{} {}{} ", - shell_char.truecolor(75,75,75), - dir0.italic().truecolor(75,75,75), + get_shell_char(shell).truecolor(75,75,75), + abrev_path(pwd,homedir).italic().truecolor(75,75,75), angle.truecolor(0, 255, 180), ); Ok(()) -} +} \ No newline at end of file