1
0
mirror of https://github.com/BurntSushi/ripgrep.git synced 2025-05-13 21:26:27 +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 .RS
.PP .PP
The format of the flag is {type}:{attribute}:{value}. 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. {attribute} can be fg, bg or style.
Value is either a color (for fg and bg) or a text 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}. 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. black. Styles are limited to nobold, bold, nointense or intense.
The format of the flag is {type}:{attribute}:{value}. {type} should be one 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 of path, line, column or match. {attribute} can be fg, bg or style. Value
a color (for fg and bg) or a text style. A special format, {type}:none, is either a color (for fg and bg) or a text style. A special format,
will clear all color settings for {type}. {type}:none, will clear all color settings for {type}.
For example, the following command will change the match color to magenta For example, the following command will change the match color to magenta
and the background color for line numbers to yellow: 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. \ red, blue, green, cyan, magenta, yellow, white and black. \
Styles are limited to nobold, bold, nointense or intense.\n\n\ Styles are limited to nobold, bold, nointense or intense.\n\n\
The format of the flag is {type}:{attribute}:{value}. {type} \ The format of the flag is {type}:{attribute}:{value}. {type} \
should be one of path, line or match. {attribute} can be fg, bg \ should be one of path, line, column or match. {attribute} can \
or style. {value} is either a color (for fg and bg) or a text \ be fg, bg or style. {value} is either a color (for fg and bg) \
style. A special format, {type}:none, will clear all color \ or a text style. A special format, {type}:none, will clear all \
settings for {type}.\n\nFor example, the following command will \ color settings for {type}.\n\nFor example, the following \
change the match color to magenta and the background color for \ command will change the match color to magenta and the \
line numbers to yellow:\n\n\ background color for line numbers to yellow:\n\n\
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo."); rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo.");
doc!(h, "encoding", doc!(h, "encoding",
"Specify the text encoding of files to search.", "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) { 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]); self.separator(&[sep]);
} }
@ -495,7 +495,7 @@ impl fmt::Display for Error {
match *self { match *self {
Error::UnrecognizedOutType(ref name) => { Error::UnrecognizedOutType(ref name) => {
write!(f, "Unrecognized output type '{}'. Choose from: \ write!(f, "Unrecognized output type '{}'. Choose from: \
path, line, match.", name) path, line, column, match.", name)
} }
Error::UnrecognizedSpecType(ref name) => { Error::UnrecognizedSpecType(ref name) => {
write!(f, "Unrecognized spec type '{}'. Choose from: \ write!(f, "Unrecognized spec type '{}'. Choose from: \
@ -509,9 +509,11 @@ impl fmt::Display for Error {
nobold, bold, nointense, intense.", name) nobold, bold, nointense, intense.", name)
} }
Error::InvalidFormat(ref original) => { Error::InvalidFormat(ref original) => {
write!(f, "Invalid color speci format: '{}'. Valid format \ write!(
is '(path|line|match):(fg|bg|style):(value)'.", f,
original) "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 { pub struct ColorSpecs {
path: ColorSpec, path: ColorSpec,
line: ColorSpec, line: ColorSpec,
column: ColorSpec,
matched: ColorSpec, matched: ColorSpec,
} }
@ -557,7 +560,7 @@ pub struct ColorSpecs {
/// The format of a `Spec` is a triple: `{type}:{attribute}:{value}`. Each /// The format of a `Spec` is a triple: `{type}:{attribute}:{value}`. Each
/// component is defined as follows: /// 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 /// * `{attribute}` can be one of `fg`, `bg` or `style`. `{attribute}` may also
/// be the special value `none`, in which case, `{value}` can be omitted. /// 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. /// * `{value}` is either a color name (for `fg`/`bg`) or a style instruction.
@ -596,6 +599,7 @@ enum SpecValue {
enum OutType { enum OutType {
Path, Path,
Line, Line,
Column,
Match, Match,
} }
@ -626,6 +630,7 @@ impl ColorSpecs {
match user_spec.ty { match user_spec.ty {
OutType::Path => user_spec.merge_into(&mut specs.path), OutType::Path => user_spec.merge_into(&mut specs.path),
OutType::Line => user_spec.merge_into(&mut specs.line), 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), OutType::Match => user_spec.merge_into(&mut specs.matched),
} }
} }
@ -642,6 +647,11 @@ impl ColorSpecs {
&self.line &self.line
} }
/// Return the color specification for coloring column numbers.
fn column(&self) -> &ColorSpec {
&self.column
}
/// Return the color specification for coloring matched text. /// Return the color specification for coloring matched text.
fn matched(&self) -> &ColorSpec { fn matched(&self) -> &ColorSpec {
&self.matched &self.matched
@ -717,6 +727,7 @@ impl FromStr for OutType {
match &*s.to_lowercase() { match &*s.to_lowercase() {
"path" => Ok(OutType::Path), "path" => Ok(OutType::Path),
"line" => Ok(OutType::Line), "line" => Ok(OutType::Line),
"column" => Ok(OutType::Column),
"match" => Ok(OutType::Match), "match" => Ok(OutType::Match),
_ => Err(Error::UnrecognizedOutType(s.to_string())), _ => Err(Error::UnrecognizedOutType(s.to_string())),
} }
@ -768,6 +779,7 @@ mod tests {
assert_eq!(ColorSpecs::new(user_specs), ColorSpecs { assert_eq!(ColorSpecs::new(user_specs), ColorSpecs {
path: ColorSpec::default(), path: ColorSpec::default(),
line: ColorSpec::default(), line: ColorSpec::default(),
column: ColorSpec::default(),
matched: expect_matched, matched: expect_matched,
}); });
} }
@ -803,6 +815,12 @@ mod tests {
ty: OutType::Line, ty: OutType::Line,
value: SpecValue::None, value: SpecValue::None,
}); });
let spec: Spec = "column:bg:green".parse().unwrap();
assert_eq!(spec, Spec {
ty: OutType::Column,
value: SpecValue::Bg(Color::Green),
});
} }
#[test] #[test]