diff --git a/Cargo.lock b/Cargo.lock index 482755226fac8457db0cf465cbfffaad748de067..aee8dd6e79dc5e101b57e22a932b91f50f5a3044 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,8 +2,10 @@ name = "git-series" version = "0.8.2" dependencies = [ + "ansi_term 0.7.5 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.9.2 (registry+https://github.com/rust-lang/crates.io-index)", + "colorparse 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "git2 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", "isatty 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "quick-error 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -56,6 +58,15 @@ dependencies = [ "gcc 0.3.31 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "colorparse" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ansi_term 0.7.5 (registry+https://github.com/rust-lang/crates.io-index)", + "quick-error 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "gcc" version = "0.3.31" diff --git a/Cargo.toml b/Cargo.toml index 57f8e1cffac0a6beaa03002720b13bf7e00a62b4..d00627202e2acb9be66b36242dae2d7769d51cb6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,8 +8,10 @@ repository = "https://github.com/git-series/git-series" description = "Track patch series in git" [dependencies] +ansi_term = "0.7.4" chrono = "0.2.22" clap = "2.7.0" +colorparse = "1" git2 = "0.4.4" isatty = "0.1.1" quick-error = "1.0" diff --git a/src/main.rs b/src/main.rs index 56a96423a9587a7f8048c44fd3de94c9cfa215f2..25dd275d09967cc4e65c078815311b3dd5130984 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ +extern crate ansi_term; extern crate chrono; #[macro_use] extern crate clap; +extern crate colorparse; extern crate git2; extern crate isatty; #[macro_use] @@ -14,6 +16,7 @@ use std::fs::File; use std::io::Read; use std::io::Write as IoWrite; use std::process::Command; +use ansi_term::Style; use chrono::offset::TimeZone; use clap::{App, AppSettings, Arg, ArgGroup, ArgMatches, SubCommand}; use git2::{Config, Commit, Diff, ObjectType, Oid, Reference, Repository, TreeBuilder}; @@ -314,16 +317,22 @@ fn series(out: &mut Output, repo: &Repository) -> Result<()> { refs.sort(); refs.dedup(); - let config = try!(repo.config()); + let config = try!(try!(repo.config()).snapshot()); try!(out.auto_pager(&config, "branch", false)); + let color_current = try!(out.get_color(&config, "branch", "current", "green")); + let color_plain = try!(out.get_color(&config, "branch", "plain", "normal")); for name in refs.iter() { - let star = if Some(name) == shead_target.as_ref() { '*' } else { ' ' }; + let (star, color) = if Some(name) == shead_target.as_ref() { + ('*', color_current) + } else { + (' ', color_plain) + }; let new = if try!(notfound_to_none(repo.refname_to_id(&format!("{}{}", SERIES_PREFIX, name)))).is_none() { " (new, no commits yet)" } else { "" }; - try!(writeln!(out, "{} {}{}", star, name, new)); + try!(writeln!(out, "{} {}{}", star, color.paint(name as &str), new)); } if refs.is_empty() { try!(writeln!(out, "No series; use \"git series start \" to start")); @@ -622,6 +631,34 @@ impl Output { Ok(()) } + // Get a color to write text with, taking git configuration into account. + // + // config: the configuration to determine the color from. + // command: the git command to act like. + // slot: the color "slot" of that git command to act like. + // default: the color to use if not configured. + fn get_color(&self, config: &Config, command: &str, slot: &str, default: &str) -> Result