use std::path::Path;
use super::super::color::{cyan, green, red, yellow};
pub(super) fn current_git_branch_at(cwd: &Path) -> Option<String> {
let repo = git2::Repository::discover(cwd).ok()?;
let head = repo.head().ok()?;
head.shorthand().map(|s| s.to_string())
}
fn git_file_status_counts_at(cwd: &Path) -> Option<(usize, usize, usize)> {
let repo = git2::Repository::discover(cwd).ok()?;
let statuses = repo.statuses(None).ok()?;
let mut added = 0usize;
let mut modified = 0usize;
let mut deleted = 0usize;
for entry in statuses.iter() {
let s = entry.status();
if s.intersects(git2::Status::INDEX_NEW | git2::Status::WT_NEW) {
added += 1;
}
if s.intersects(git2::Status::INDEX_MODIFIED | git2::Status::WT_MODIFIED) {
modified += 1;
}
if s.intersects(git2::Status::INDEX_DELETED | git2::Status::WT_DELETED) {
deleted += 1;
}
}
Some((added, modified, deleted))
}
pub(super) fn format_git_status_at(cwd: &Path, nerd_font: bool) -> String {
let (added, modified, deleted) = match git_file_status_counts_at(cwd) {
Some(counts) => counts,
None => return String::new(),
};
if added == 0 && modified == 0 && deleted == 0 {
return String::new();
}
let (modified_prefix, added_prefix, deleted_prefix) = if nerd_font {
("\u{ea73} ", "\u{f067} ", "\u{f068} ") } else {
("~", "+", "-")
};
let mut parts = Vec::new();
if modified > 0 {
parts.push(yellow(&format!("{modified_prefix}{modified}")));
}
if added > 0 {
parts.push(green(&format!("{added_prefix}{added}")));
}
if deleted > 0 {
parts.push(red(&format!("{deleted_prefix}{deleted}")));
}
parts.join(" ")
}
pub(super) fn format_branch_label(branch: &str, nerd_font: bool) -> String {
if nerd_font {
cyan(&format!("\u{e725} {branch}"))
} else {
cyan(branch)
}
}