From e9df420d2f653519e266969f8a91a6c64aaa0b41 Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Sun, 9 Apr 2017 09:08:49 -0400 Subject: Add ability to colorize column numbers. Fixes #377 --- doc/rg.1 | 2 +- doc/rg.1.md | 6 +++--- src/app.rs | 12 ++++++------ src/printer.rs | 30 ++++++++++++++++++++++++------ 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/doc/rg.1 b/doc/rg.1 index 49023417..5e068c28 100644 --- a/doc/rg.1 +++ b/doc/rg.1 @@ -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}. diff --git a/doc/rg.1.md b/doc/rg.1.md index 42191024..1cd9c771 100644 --- a/doc/rg.1.md +++ b/doc/rg.1.md @@ -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: diff --git a/src/app.rs b/src/app.rs index 94804828..b8f25b87 100644 --- a/src/app.rs +++ b/src/app.rs @@ -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.", diff --git a/src/printer.rs b/src/printer.rs index c624da2a..48492fe8 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -428,7 +428,7 @@ impl Printer { } 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 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] -- cgit v1.2.3