Iimproved efficiency and best practices
This commit is contained in:
parent
0510f9c34d
commit
e8216e7c08
@ -1,17 +1,16 @@
|
|||||||
use ansi_term::ANSIGenericString;
|
use ansi_term::ANSIGenericString;
|
||||||
use ansi_term::Colour::RGB;
|
use ansi_term::Colour::RGB;
|
||||||
|
|
||||||
// Error indicator symbol
|
pub const ERR_SYMBOL: &str = "\u{276F}"; // Error indicator symbol: "❯"
|
||||||
pub const ERR_SYMBOL: &str = "\u{276F}"; // "❯"
|
pub const ERR_COL: ansi_term::Colour = RGB(255, 53, 94); // Error
|
||||||
pub const ERR_COL: ansi_term::Colour = RGB(255, 53, 94); //
|
pub const NORMIE_COL: ansi_term::Colour = RGB(0, 255, 180); // Success
|
||||||
pub const NORMIE_COL: ansi_term::Colour = RGB(0, 255, 180); //
|
|
||||||
|
|
||||||
// Error indicator
|
// Error indicator
|
||||||
pub fn indicator(args: Vec<String>) -> ANSIGenericString<'static, str> {
|
pub fn indicator(args: &[String]) -> ANSIGenericString<'static, str> {
|
||||||
let default_exit_code = "0".to_string();
|
let exit_code = args.get(1).map_or("0", String::as_str); // Default to "0" (success, no error) if arg missing
|
||||||
let exit_code = args.get(1).unwrap_or(&default_exit_code); // Default to "0" if missing
|
if exit_code == "0" {
|
||||||
match exit_code.as_str() {
|
NORMIE_COL.paint(ERR_SYMBOL) // Success
|
||||||
"0" => NORMIE_COL.paint(ERR_SYMBOL),
|
} else {
|
||||||
_ => ERR_COL.paint(ERR_SYMBOL),
|
ERR_COL.paint(ERR_SYMBOL) // Error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,25 +12,18 @@ pub const NORMIE_COL: ansi_term::Colour = RGB(255, 255, 255); // White
|
|||||||
|
|
||||||
pub fn info() -> Option<(String, String)> {
|
pub fn info() -> Option<(String, String)> {
|
||||||
let output = Command::new("git")
|
let output = Command::new("git")
|
||||||
.arg("rev-parse")
|
.args(["rev-parse", "--show-toplevel", "--abbrev-ref", "HEAD"])
|
||||||
.arg("--show-toplevel") // Repo root
|
.output()
|
||||||
.arg("--abbrev-ref") // Git branch
|
.ok()?;
|
||||||
.arg("HEAD")
|
|
||||||
.output();
|
|
||||||
match output {
|
|
||||||
Ok(output) if output.status.success() => {
|
|
||||||
let stdout = String::from_utf8_lossy(&output.stdout); // Get cmd output
|
|
||||||
let output_string = stdout.trim();
|
|
||||||
let parts: Vec<&str> = output_string.split('\n').collect();
|
|
||||||
|
|
||||||
|
if output.status.success() {
|
||||||
|
let output_str = String::from_utf8_lossy(&output.stdout).trim().to_string();
|
||||||
|
let parts: Vec<&str> = output_str.split('\n').collect();
|
||||||
if parts.len() == 2 {
|
if parts.len() == 2 {
|
||||||
Some((parts[0].to_string(), parts[1].to_string()))
|
return Some((parts[0].to_string(), parts[1].to_string()));
|
||||||
} else {
|
}
|
||||||
|
}
|
||||||
None
|
None
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn repo_name(path: &str) -> String {
|
pub fn repo_name(path: &str) -> String {
|
||||||
|
@ -7,10 +7,12 @@ pub const UNKNOWN_PATH: &str = "\u{2248}"; // "≈"
|
|||||||
pub const PATH_COL: ansi_term::Colour = RGB(82, 82, 82);
|
pub const PATH_COL: ansi_term::Colour = RGB(82, 82, 82);
|
||||||
pub const REPO_COL: ansi_term::Colour = RGB(55, 120, 130);
|
pub const REPO_COL: ansi_term::Colour = RGB(55, 120, 130);
|
||||||
|
|
||||||
|
/// Find the current user's home directory.
|
||||||
fn home_dir() -> String {
|
fn home_dir() -> String {
|
||||||
std::env::var("HOME").map_or_else(|_| "".to_string(), |path| path.to_string())
|
std::env::var("HOME").map_or_else(|_| String::new(), |path| path.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the full path of the current directory as a string.
|
||||||
fn full_path() -> String {
|
fn full_path() -> String {
|
||||||
current_dir().map_or_else(
|
current_dir().map_or_else(
|
||||||
|_| UNKNOWN_PATH.to_string(),
|
|_| UNKNOWN_PATH.to_string(),
|
||||||
@ -18,10 +20,12 @@ fn full_path() -> String {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Remove the path of the repo's root from the path, to replace it with the repo's name.
|
||||||
fn remove_repo(pwd_path: &str, repo_path: &str) -> String {
|
fn remove_repo(pwd_path: &str, repo_path: &str) -> String {
|
||||||
pwd_path.replacen(repo_path, "", 1)
|
pwd_path.replacen(repo_path, "", 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Replace the 'home directory' part of the path with a the '~' symbol
|
||||||
fn replace_home(path: &str) -> String {
|
fn replace_home(path: &str) -> String {
|
||||||
let homedir = home_dir();
|
let homedir = home_dir();
|
||||||
path.replacen(&homedir, "~", 1)
|
path.replacen(&homedir, "~", 1)
|
||||||
@ -34,13 +38,13 @@ fn short(path: &str, slash_limit: u8) -> String {
|
|||||||
} else {
|
} else {
|
||||||
// Long path, shorten it
|
// Long path, shorten it
|
||||||
let parts: Vec<&str> = path.split('/').collect(); // Split the path into parts
|
let parts: Vec<&str> = path.split('/').collect(); // Split the path into parts
|
||||||
let first = if path.starts_with("~") { "~" } else { "" }; // Determine the first part correctly
|
let first = if path.starts_with('~') { "~" } else { "" }; // Determine the first part correctly
|
||||||
let last = parts[parts.len() - 1]; // The last part
|
let last = parts[parts.len() - 1]; // The last part
|
||||||
|
|
||||||
// Abbreviate middle parts (take the first character of each)
|
// Abbreviate middle parts (take the first character of each)
|
||||||
let abbreviated_middle: Vec<String> = parts[1..parts.len() - 1] // Skip the first and last part
|
let abbreviated_middle: Vec<String> = parts[1..parts.len() - 1] // Skip the first and last part
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|&&part| !part.is_empty()) // Avoid empty parts (like after "/")
|
.filter(|&&part| !part.is_empty()) // Avoid empty parts (like "//")
|
||||||
.map(|&part| part.chars().next().unwrap().to_string()) // Take the first letter
|
.map(|&part| part.chars().next().unwrap().to_string()) // Take the first letter
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
@ -46,7 +46,8 @@ fn main() {
|
|||||||
components.push(user::indicator());
|
components.push(user::indicator());
|
||||||
}
|
}
|
||||||
if indicate_err {
|
if indicate_err {
|
||||||
components.push(error::indicator(cmd_args));
|
//components.push(error::indicator(cmd_args));
|
||||||
|
components.push(error::indicator(&cmd_args));
|
||||||
}
|
}
|
||||||
if show_pwd {
|
if show_pwd {
|
||||||
let repo_path = match git_info {
|
let repo_path = match git_info {
|
||||||
|
@ -1,55 +0,0 @@
|
|||||||
//use colored::{ColoredString, Colorize};
|
|
||||||
use colored::Colorize;
|
|
||||||
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::*, path::*, shell::*, ssh::*, user::*};
|
|
||||||
|
|
||||||
fn main() -> std::io::Result<()> {
|
|
||||||
//let angle = "❯";
|
|
||||||
|
|
||||||
let args: Vec<String> = args().collect();
|
|
||||||
let shell: String = shell(args.clone());
|
|
||||||
|
|
||||||
let root_indicator = root_indicator();
|
|
||||||
|
|
||||||
let err: String = err(args.clone());
|
|
||||||
let err_indicator = err_indicator(err);
|
|
||||||
|
|
||||||
// SSH status
|
|
||||||
let ssh_char = ssh_char();
|
|
||||||
|
|
||||||
//Git status
|
|
||||||
let git_branch = get_git_branch();
|
|
||||||
let git_repo_root = get_git_root();
|
|
||||||
let git_repo_name = get_git_repo_name(&git_repo_root.clone()).truecolor(122, 68, 24);
|
|
||||||
let git_char = get_git_char(&git_branch);
|
|
||||||
|
|
||||||
//pwd
|
|
||||||
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
|
|
||||||
cur_dir = cur_dir.replace(&homedir, "~"); // Abreviate homedir with "~"
|
|
||||||
cur_dir = abrev_path(&cur_dir);
|
|
||||||
|
|
||||||
print!(
|
|
||||||
"{}{}{}{}{}{}{} ",
|
|
||||||
ssh_char,
|
|
||||||
get_shell_char(&shell).truecolor(75, 75, 75),
|
|
||||||
git_repo_name,
|
|
||||||
git_char,
|
|
||||||
cur_dir.italic().truecolor(82, 82, 82),
|
|
||||||
root_indicator,
|
|
||||||
err_indicator,
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
pub fn get_shell_char(shell: &str) -> String {
|
|
||||||
let shell_char = match shell {
|
|
||||||
"bash" | "/bin/bash" | "/usr/bin/bash" | "-bash" => " ",
|
|
||||||
"zsh" | "/bin/zsh" | "/usr/bin/zsh" | "-zsh" => " ",
|
|
||||||
"fish" => " ",
|
|
||||||
"nushell" => " ",
|
|
||||||
"ion" => " ",
|
|
||||||
"oursh" => " ",
|
|
||||||
_ => " ",
|
|
||||||
};
|
|
||||||
shell_char.to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn shell(args: Vec<String>) -> String {
|
|
||||||
if args.len() > 1 {
|
|
||||||
args[1].clone() // Shell symbol
|
|
||||||
} else {
|
|
||||||
"none".to_string()
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user