summaryrefslogtreecommitdiffstats
path: root/grep-printer
diff options
context:
space:
mode:
authorAndrew Gallant <jamslam@gmail.com>2018-08-29 20:53:52 -0400
committerAndrew Gallant <jamslam@gmail.com>2018-09-04 23:18:55 -0400
commit4846d63539690047fa58ec582d94bcba16da1c09 (patch)
tree61a2cf9de3d62ea6524659893ab9a2c7800c3286 /grep-printer
parent13c47530a6e685d2dee1953a64f055936e6a2ba8 (diff)
grep-cli: introduce new grep-cli crate
This commit moves a lot of "utility" code from ripgrep core into grep-cli. Any one of these things might not be worth creating a new crate, but combining everything together results in a fair number of a convenience routines that make up a decent sized crate. There is potentially more we could move into the crate, but much of what remains in ripgrep core is almost entirely dealing with the number of flags we support. In the course of doing moving things to the grep-cli crate, we clean up a lot of gunk and improve failure modes in a number of cases. In particular, we've fixed a bug where other processes could deadlock if they write too much to stderr. Fixes #990
Diffstat (limited to 'grep-printer')
-rw-r--r--grep-printer/src/color.rs28
-rw-r--r--grep-printer/src/lib.rs2
2 files changed, 29 insertions, 1 deletions
diff --git a/grep-printer/src/color.rs b/grep-printer/src/color.rs
index dcaca59d..394f5ccf 100644
--- a/grep-printer/src/color.rs
+++ b/grep-printer/src/color.rs
@@ -4,6 +4,25 @@ use std::str::FromStr;
use termcolor::{Color, ColorSpec, ParseColorError};
+/// Returns a default set of color specifications.
+///
+/// This may change over time, but the color choices are meant to be fairly
+/// conservative that work across terminal themes.
+///
+/// Additional color specifications can be added to the list returned. More
+/// recently added specifications override previously added specifications.
+pub fn default_color_specs() -> Vec<UserColorSpec> {
+ vec![
+ #[cfg(unix)]
+ "path:fg:magenta".parse().unwrap(),
+ #[cfg(windows)]
+ "path:fg:cyan".parse().unwrap(),
+ "line:fg:green".parse().unwrap(),
+ "match:fg:red".parse().unwrap(),
+ "match:style:bold".parse().unwrap(),
+ ]
+}
+
/// An error that can occur when parsing color specifications.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ColorError {
@@ -227,6 +246,15 @@ impl ColorSpecs {
merged
}
+ /// Create a default set of specifications that have color.
+ ///
+ /// This is distinct from `ColorSpecs`'s `Default` implementation in that
+ /// this provides a set of default color choices, where as the `Default`
+ /// implementation provides no color choices.
+ pub fn default_with_color() -> ColorSpecs {
+ ColorSpecs::new(&default_color_specs())
+ }
+
/// Return the color specification for coloring file paths.
pub fn path(&self) -> &ColorSpec {
&self.path
diff --git a/grep-printer/src/lib.rs b/grep-printer/src/lib.rs
index 128b0bdf..6ef11c72 100644
--- a/grep-printer/src/lib.rs
+++ b/grep-printer/src/lib.rs
@@ -83,7 +83,7 @@ extern crate serde_derive;
extern crate serde_json;
extern crate termcolor;
-pub use color::{ColorError, ColorSpecs, UserColorSpec};
+pub use color::{ColorError, ColorSpecs, UserColorSpec, default_color_specs};
#[cfg(feature = "serde1")]
pub use json::{JSON, JSONBuilder, JSONSink};
pub use standard::{Standard, StandardBuilder, StandardSink};