mirror of
https://github.com/BurntSushi/ripgrep.git
synced 2025-04-19 09:02:15 +02:00
Add colour choice
This commit is contained in:
parent
b1c52b52d6
commit
b3935935cb
@ -37,6 +37,36 @@ pub struct Printer<W> {
|
|||||||
replace: Option<Vec<u8>>,
|
replace: Option<Vec<u8>>,
|
||||||
/// Whether to prefix each match with the corresponding file name.
|
/// Whether to prefix each match with the corresponding file name.
|
||||||
with_filename: bool,
|
with_filename: bool,
|
||||||
|
|
||||||
|
/// The choice of Colours
|
||||||
|
color_choice: ColorChoice
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ColorChoice {
|
||||||
|
matched_line: color::Color,
|
||||||
|
heading: color::Color,
|
||||||
|
line_number: color::Color
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ColorChoice {
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
pub fn new() -> ColorChoice {
|
||||||
|
ColorChoice {
|
||||||
|
matched_line: color::RED,
|
||||||
|
heading: color::GREEN,
|
||||||
|
line_number: color::BLUE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(unix))]
|
||||||
|
pub fn new() -> ColorChoice {
|
||||||
|
ColorChoice {
|
||||||
|
matched_line: color::BRIGHT_RED,
|
||||||
|
heading: color::BRIGHT_GREEN,
|
||||||
|
line_number: color::BRIGHT_BLUE
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: Terminal + Send> Printer<W> {
|
impl<W: Terminal + Send> Printer<W> {
|
||||||
@ -53,6 +83,7 @@ impl<W: Terminal + Send> Printer<W> {
|
|||||||
quiet: false,
|
quiet: false,
|
||||||
replace: None,
|
replace: None,
|
||||||
with_filename: false,
|
with_filename: false,
|
||||||
|
color_choice: ColorChoice::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -243,7 +274,7 @@ impl<W: Terminal + Send> Printer<W> {
|
|||||||
let mut last_written = 0;
|
let mut last_written = 0;
|
||||||
for (s, e) in re.find_iter(buf) {
|
for (s, e) in re.find_iter(buf) {
|
||||||
self.write(&buf[last_written..s]);
|
self.write(&buf[last_written..s]);
|
||||||
let _ = self.wtr.fg(color::BRIGHT_RED);
|
let _ = self.wtr.fg(self.color_choice.matched_line);
|
||||||
let _ = self.wtr.attr(Attr::Bold);
|
let _ = self.wtr.attr(Attr::Bold);
|
||||||
self.write(&buf[s..e]);
|
self.write(&buf[s..e]);
|
||||||
let _ = self.wtr.reset();
|
let _ = self.wtr.reset();
|
||||||
@ -277,7 +308,7 @@ impl<W: Terminal + Send> Printer<W> {
|
|||||||
|
|
||||||
fn write_heading<P: AsRef<Path>>(&mut self, path: P) {
|
fn write_heading<P: AsRef<Path>>(&mut self, path: P) {
|
||||||
if self.wtr.supports_color() {
|
if self.wtr.supports_color() {
|
||||||
let _ = self.wtr.fg(color::BRIGHT_GREEN);
|
let _ = self.wtr.fg(self.color_choice.heading);
|
||||||
let _ = self.wtr.attr(Attr::Bold);
|
let _ = self.wtr.attr(Attr::Bold);
|
||||||
}
|
}
|
||||||
self.write_path(path.as_ref());
|
self.write_path(path.as_ref());
|
||||||
@ -289,7 +320,7 @@ impl<W: Terminal + Send> Printer<W> {
|
|||||||
|
|
||||||
fn line_number(&mut self, n: u64, sep: u8) {
|
fn line_number(&mut self, n: u64, sep: u8) {
|
||||||
if self.wtr.supports_color() {
|
if self.wtr.supports_color() {
|
||||||
let _ = self.wtr.fg(color::BRIGHT_BLUE);
|
let _ = self.wtr.fg(self.color_choice.line_number);
|
||||||
let _ = self.wtr.attr(Attr::Bold);
|
let _ = self.wtr.attr(Attr::Bold);
|
||||||
}
|
}
|
||||||
self.write(n.to_string().as_bytes());
|
self.write(n.to_string().as_bytes());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user