use crate::opts::Opts;
use std::io::Error;
fn has_color(s: &str) -> bool {
s.contains("\x1b[31m") || s.contains("\x1b[32m")
}
fn print_line(line: &String, count: &mut u16, limit: u16) {
if !has_color(&line) {
println!("{}", line);
return;
}
*count += 1;
if *count > limit {
return;
}
if line.starts_with('\t') {
println!("{}{}", count, line);
} else {
println!("{: <4}{}", count, line);
}
}
pub fn run(opts: Opts) -> Result<(), Error> {
let mut git = opts.cmd()?;
git.args(["-c", "status.color=always", "status"]);
git.stdout(std::process::Stdio::piped());
let mut git = git.spawn()?;
let output = git.stdout.as_mut().ok_or(Error::new(
std::io::ErrorKind::NotFound,
"Unable to get stdout",
))?;
let mut count = 0;
let limit = 50;
use std::io::{BufRead, BufReader};
BufReader::new(output)
.lines()
.filter_map(|line| line.ok())
.for_each(|line| print_line(&line, &mut count, limit));
if count > limit {
println!("... {} hidden items (gitnu)", count - limit);
}
git.wait().map(|v| ())
}