49 lines
1.8 KiB
Rust
49 lines
1.8 KiB
Rust
use ansi_term::ANSIGenericString;
|
||
use ansi_term::Colour::RGB;
|
||
use std::path::Path;
|
||
use std::process::Command;
|
||
|
||
pub const GIT_SYMBOL: &str = "\u{276F}"; // Git indicator symbol: "❯"
|
||
pub const MAIN_COL: ansi_term::Colour = RGB(178, 98, 44);
|
||
pub const DEV_COL: ansi_term::Colour = RGB(54, 159, 150);
|
||
pub const DEFAULT_BRANCH_COL: ansi_term::Colour = RGB(255, 255, 255);
|
||
pub const NORMIE_COL: ansi_term::Colour = RGB(82, 82, 82);
|
||
|
||
/// Returns the repo's root and branch, if present
|
||
pub fn info() -> Option<(String, String)> {
|
||
let output = Command::new("git") // Program/Command to execute
|
||
.args(["rev-parse", "--show-toplevel", "--abbrev-ref", "HEAD"]) // Arguments
|
||
.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())); // If the `git`` command returns a repo and branch
|
||
}
|
||
}
|
||
None // If the current directory is not in a git repo
|
||
}
|
||
|
||
/// The name of the repo
|
||
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),
|
||
_ => DEFAULT_BRANCH_COL.paint(GIT_SYMBOL),
|
||
},
|
||
None => NORMIE_COL.paint(GIT_SYMBOL),
|
||
}
|
||
}
|