PrettyPrompt/src/indicators/git.rs
2024-11-29 16:38:19 +05:30

48 lines
1.6 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use ansi_term::ANSIGenericString;
use ansi_term::Colour::RGB;
use std::path::Path;
use std::process::Command;
// SSH indicator symbol
pub const GIT_SYMBOL: &str = "\u{276F}"; // ""
pub const MAIN_COL: ansi_term::Colour = RGB(178, 98, 44); //
pub const DEV_COL: ansi_term::Colour = RGB(54, 159, 150); //
pub const OTHER_COL: ansi_term::Colour = RGB(82, 82, 82); //
pub const NORMIE_COL: ansi_term::Colour = RGB(255, 255, 255); // White
pub fn info() -> Option<(String, String)> {
let output = Command::new("git")
.args(["rev-parse", "--show-toplevel", "--abbrev-ref", "HEAD"])
.output()
.ok()?;
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 {
return Some((parts[0].to_string(), parts[1].to_string()));
}
}
None
}
pub fn repo_name(path: &str) -> String {
Path::new(path)
.file_name() // Extracts the last component of the path.
.and_then(|name| name.to_str()) // Converts the `OsStr` to `&str`.
.unwrap_or("") // Default value(empty string) if None(no valid name)
.to_string() // Converts &str to String
}
//Git branch indicator
pub fn indicator(branch: Option<String>) -> ANSIGenericString<'static, str> {
match branch {
Some(b) => match b.as_str() {
"main" => MAIN_COL.paint(GIT_SYMBOL),
"dev" => DEV_COL.paint(GIT_SYMBOL),
_ => OTHER_COL.paint(GIT_SYMBOL),
},
None => NORMIE_COL.paint(GIT_SYMBOL),
}
}