1
0
mirror of https://github.com/BurntSushi/ripgrep.git synced 2025-01-29 22:01:04 +02:00

Add ability to colorize column numbers.

Fixes #377
This commit is contained in:
Andrew Gallant 2017-04-09 09:08:49 -04:00
parent 201b4fc757
commit e9df420d2f
4 changed files with 34 additions and 16 deletions

View File

@ -181,7 +181,7 @@ Styles are limited to nobold, bold, nointense or intense.
.RS
.PP
The format of the flag is {type}:{attribute}:{value}.
{type} should be one of path, line or match.
{type} should be one of path, line, column or match.
{attribute} can be fg, bg or style.
Value is either a color (for fg and bg) or a text style.
A special format, {type}:none, will clear all color settings for {type}.

View File

@ -123,9 +123,9 @@ Project home page: https://github.com/BurntSushi/ripgrep
black. Styles are limited to nobold, bold, nointense or intense.
The format of the flag is {type}:{attribute}:{value}. {type} should be one
of path, line or match. {attribute} can be fg, bg or style. Value is either
a color (for fg and bg) or a text style. A special format, {type}:none,
will clear all color settings for {type}.
of path, line, column or match. {attribute} can be fg, bg or style. Value
is either a color (for fg and bg) or a text style. A special format,
{type}:none, will clear all color settings for {type}.
For example, the following command will change the match color to magenta
and the background color for line numbers to yellow:

View File

@ -235,12 +235,12 @@ lazy_static! {
red, blue, green, cyan, magenta, yellow, white and black. \
Styles are limited to nobold, bold, nointense or intense.\n\n\
The format of the flag is {type}:{attribute}:{value}. {type} \
should be one of path, line or match. {attribute} can be fg, bg \
or style. {value} is either a color (for fg and bg) or a text \
style. A special format, {type}:none, will clear all color \
settings for {type}.\n\nFor example, the following command will \
change the match color to magenta and the background color for \
line numbers to yellow:\n\n\
should be one of path, line, column or match. {attribute} can \
be fg, bg or style. {value} is either a color (for fg and bg) \
or a text style. A special format, {type}:none, will clear all \
color settings for {type}.\n\nFor example, the following \
command will change the match color to magenta and the \
background color for line numbers to yellow:\n\n\
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo.");
doc!(h, "encoding",
"Specify the text encoding of files to search.",

View File

@ -428,7 +428,7 @@ impl<W: WriteColor> Printer<W> {
}
fn column_number(&mut self, n: u64, sep: u8) {
self.write(n.to_string().as_bytes());
self.write_colored(n.to_string().as_bytes(), |colors| colors.column());
self.separator(&[sep]);
}
@ -495,7 +495,7 @@ impl fmt::Display for Error {
match *self {
Error::UnrecognizedOutType(ref name) => {
write!(f, "Unrecognized output type '{}'. Choose from: \
path, line, match.", name)
path, line, column, match.", name)
}
Error::UnrecognizedSpecType(ref name) => {
write!(f, "Unrecognized spec type '{}'. Choose from: \
@ -509,9 +509,11 @@ impl fmt::Display for Error {
nobold, bold, nointense, intense.", name)
}
Error::InvalidFormat(ref original) => {
write!(f, "Invalid color speci format: '{}'. Valid format \
is '(path|line|match):(fg|bg|style):(value)'.",
original)
write!(
f,
"Invalid color speci format: '{}'. Valid format \
is '(path|line|column|match):(fg|bg|style):(value)'.",
original)
}
}
}
@ -528,6 +530,7 @@ impl From<ParseColorError> for Error {
pub struct ColorSpecs {
path: ColorSpec,
line: ColorSpec,
column: ColorSpec,
matched: ColorSpec,
}
@ -557,7 +560,7 @@ pub struct ColorSpecs {
/// The format of a `Spec` is a triple: `{type}:{attribute}:{value}`. Each
/// component is defined as follows:
///
/// * `{type}` can be one of `path`, `line` or `match`.
/// * `{type}` can be one of `path`, `line`, `column` or `match`.
/// * `{attribute}` can be one of `fg`, `bg` or `style`. `{attribute}` may also
/// be the special value `none`, in which case, `{value}` can be omitted.
/// * `{value}` is either a color name (for `fg`/`bg`) or a style instruction.
@ -596,6 +599,7 @@ enum SpecValue {
enum OutType {
Path,
Line,
Column,
Match,
}
@ -626,6 +630,7 @@ impl ColorSpecs {
match user_spec.ty {
OutType::Path => user_spec.merge_into(&mut specs.path),
OutType::Line => user_spec.merge_into(&mut specs.line),
OutType::Column => user_spec.merge_into(&mut specs.column),
OutType::Match => user_spec.merge_into(&mut specs.matched),
}
}
@ -642,6 +647,11 @@ impl ColorSpecs {
&self.line
}
/// Return the color specification for coloring column numbers.
fn column(&self) -> &ColorSpec {
&self.column
}
/// Return the color specification for coloring matched text.
fn matched(&self) -> &ColorSpec {
&self.matched
@ -717,6 +727,7 @@ impl FromStr for OutType {
match &*s.to_lowercase() {
"path" => Ok(OutType::Path),
"line" => Ok(OutType::Line),
"column" => Ok(OutType::Column),
"match" => Ok(OutType::Match),
_ => Err(Error::UnrecognizedOutType(s.to_string())),
}
@ -768,6 +779,7 @@ mod tests {
assert_eq!(ColorSpecs::new(user_specs), ColorSpecs {
path: ColorSpec::default(),
line: ColorSpec::default(),
column: ColorSpec::default(),
matched: expect_matched,
});
}
@ -803,6 +815,12 @@ mod tests {
ty: OutType::Line,
value: SpecValue::None,
});
let spec: Spec = "column:bg:green".parse().unwrap();
assert_eq!(spec, Spec {
ty: OutType::Column,
value: SpecValue::Bg(Color::Green),
});
}
#[test]