Do the same for the Git column

This commit is contained in:
Ben S 2015-05-10 17:57:21 +01:00
parent 5a37d1b6b1
commit d9319c48b4
4 changed files with 42 additions and 23 deletions

View File

@ -13,6 +13,7 @@ pub struct Colours {
pub size: Size,
pub users: Users,
pub links: Links,
pub git: Git,
pub punctuation: Style,
pub date: Style,
@ -82,6 +83,15 @@ pub struct Links {
pub multi_link_file: Style,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Git {
pub new: Style,
pub modified: Style,
pub deleted: Style,
pub renamed: Style,
pub typechange: Style,
}
impl Colours {
pub fn plain() -> Colours {
Colours::default()
@ -138,6 +148,14 @@ impl Colours {
multi_link_file: Red.on(Yellow),
},
git: Git {
new: Green.normal(),
modified: Blue.normal(),
deleted: Red.normal(),
renamed: Yellow.normal(),
typechange: Purple.normal(),
},
punctuation: Fixed(244).normal(),
date: Blue.normal(),
inode: Purple.normal(),

View File

@ -1,3 +1,4 @@
use colours::Colours;
use feature::Git;
use file::{File, GREY};
@ -64,10 +65,10 @@ impl Dir {
}
/// Get a string describing the Git status of the given file.
pub fn git_status(&self, path: &Path, prefix_lookup: bool) -> String {
pub fn git_status(&self, path: &Path, colours: &Colours, prefix_lookup: bool) -> String {
match (&self.git, prefix_lookup) {
(&Some(ref git), false) => git.status(path),
(&Some(ref git), true) => git.dir_status(path),
(&Some(ref git), false) => git.status(colours, path),
(&Some(ref git), true) => git.dir_status(colours, path),
(&None, _) => GREY.paint("--").to_string(),
}
}

View File

@ -1,9 +1,9 @@
use std::path::{Path, PathBuf};
use ansi_term::{ANSIString, ANSIStrings};
use ansi_term::Colour::*;
use git2;
use colours::Colours;
use file::GREY;
/// Container of Git statuses for all the files in this folder's Git repository.
@ -30,11 +30,11 @@ impl Git {
}
/// Get the status for the file at the given path, if present.
pub fn status(&self, path: &Path) -> String {
pub fn status(&self, c: &Colours, path: &Path) -> String {
let status = self.statuses.iter()
.find(|p| p.0.as_path() == path);
match status {
Some(&(_, s)) => ANSIStrings( &[Git::index_status(s), Git::working_tree_status(s) ]).to_string(),
Some(&(_, s)) => ANSIStrings( &[Git::index_status(c, s), Git::working_tree_status(c, s) ]).to_string(),
None => GREY.paint("--").to_string(),
}
}
@ -42,35 +42,35 @@ impl Git {
/// Get the combined status for all the files whose paths begin with the
/// path that gets passed in. This is used for getting the status of
/// directories, which don't really have an 'official' status.
pub fn dir_status(&self, dir: &Path) -> String {
pub fn dir_status(&self, c: &Colours, dir: &Path) -> String {
let s = self.statuses.iter()
.filter(|p| p.0.starts_with(dir))
.fold(git2::Status::empty(), |a, b| a | b.1);
ANSIStrings( &[Git::index_status(s), Git::working_tree_status(s)] ).to_string()
ANSIStrings( &[Git::index_status(c, s), Git::working_tree_status(c, s)] ).to_string()
}
/// The character to display if the file has been modified, but not staged.
fn working_tree_status(status: git2::Status) -> ANSIString<'static> {
fn working_tree_status(colours: &Colours, status: git2::Status) -> ANSIString<'static> {
match status {
s if s.contains(git2::STATUS_WT_NEW) => Green.paint("A"),
s if s.contains(git2::STATUS_WT_MODIFIED) => Blue.paint("M"),
s if s.contains(git2::STATUS_WT_DELETED) => Red.paint("D"),
s if s.contains(git2::STATUS_WT_RENAMED) => Yellow.paint("R"),
s if s.contains(git2::STATUS_WT_TYPECHANGE) => Purple.paint("T"),
s if s.contains(git2::STATUS_WT_NEW) => colours.git.new.paint("A"),
s if s.contains(git2::STATUS_WT_MODIFIED) => colours.git.modified.paint("M"),
s if s.contains(git2::STATUS_WT_DELETED) => colours.git.deleted.paint("D"),
s if s.contains(git2::STATUS_WT_RENAMED) => colours.git.renamed.paint("R"),
s if s.contains(git2::STATUS_WT_TYPECHANGE) => colours.git.typechange.paint("T"),
_ => GREY.paint("-"),
}
}
/// The character to display if the file has been modified, and the change
/// has been staged.
fn index_status(status: git2::Status) -> ANSIString<'static> {
fn index_status(colours: &Colours, status: git2::Status) -> ANSIString<'static> {
match status {
s if s.contains(git2::STATUS_INDEX_NEW) => Green.paint("A"),
s if s.contains(git2::STATUS_INDEX_MODIFIED) => Blue.paint("M"),
s if s.contains(git2::STATUS_INDEX_DELETED) => Red.paint("D"),
s if s.contains(git2::STATUS_INDEX_RENAMED) => Yellow.paint("R"),
s if s.contains(git2::STATUS_INDEX_TYPECHANGE) => Purple.paint("T"),
s if s.contains(git2::STATUS_INDEX_NEW) => colours.git.new.paint("A"),
s if s.contains(git2::STATUS_INDEX_MODIFIED) => colours.git.modified.paint("M"),
s if s.contains(git2::STATUS_INDEX_DELETED) => colours.git.deleted.paint("D"),
s if s.contains(git2::STATUS_INDEX_RENAMED) => colours.git.renamed.paint("R"),
s if s.contains(git2::STATUS_INDEX_TYPECHANGE) => colours.git.typechange.paint("T"),
_ => GREY.paint("-"),
}
}

View File

@ -124,7 +124,7 @@ impl<'a> File<'a> {
Blocks => self.blocks(colours, &locale.numeric),
User => self.user(colours, users_cache),
Group => self.group(colours, users_cache),
GitStatus => self.git_status(),
GitStatus => self.git_status(colours),
}
}
@ -475,7 +475,7 @@ impl<'a> File<'a> {
choices.contains(&&self.name[..])
}
fn git_status(&self) -> Cell {
fn git_status(&self, colours: &Colours) -> Cell {
let status = match self.dir {
None => GREY.paint("--").to_string(),
Some(d) => {
@ -484,7 +484,7 @@ impl<'a> File<'a> {
Ok(dir) => dir.join(&self.path),
};
d.git_status(&cwd, self.is_directory())
d.git_status(&cwd, colours, self.is_directory())
},
};