extern crate clap;
use ansi_term::Colour::{Green, Red, Yellow};
use clap::{Arg, Command};
use git2::Repository;
use git_branch_status::branch_status::BranchStatus;
use git_branch_status::repository_ext::RepositoryExt;
fn main() {
let matches = Command::new("git-branch-status")
.version(env!("CARGO_PKG_VERSION"))
.author(env!("CARGO_PKG_AUTHORS"))
.about("Show git branch name colored by status")
.arg(
Arg::new("mode")
.short('m')
.long("mode")
.default_value("stdout")
.value_parser(["zsh", "stdout"])
.help("Sets a mode"),
)
.get_matches();
let repo = match Repository::discover(".") {
Ok(repo) => repo,
Err(_) => std::process::exit(1),
};
let branch = match repo.branch_name() {
Ok(branch) => branch,
Err(_) => std::process::exit(1),
};
let status = match repo.branch_status() {
Ok(status) => status,
Err(_) => std::process::exit(1),
};
let mode = matches.get_one::<String>("mode").unwrap().as_str();
match mode {
"zsh" => {
match status {
BranchStatus::NotChanged => print!("%F{{green}}{}%f", branch),
BranchStatus::Staged => print!("%F{{yellow}}{}%f", branch),
BranchStatus::Unstaged => print!("%F{{red}}{}%f", branch),
BranchStatus::Conflicted => print!("%F{{red}}{}%f", branch),
};
}
_ => {
match status {
BranchStatus::NotChanged => print!("{}", Green.paint(branch)),
BranchStatus::Staged => print!("{}", Yellow.paint(branch)),
BranchStatus::Unstaged => print!("{}", Red.paint(branch)),
BranchStatus::Conflicted => print!("{}", Red.paint(branch)),
};
}
};
}