Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
use std::error;
|
|
|
|
use std::fmt;
|
2016-08-28 01:37:12 -04:00
|
|
|
use std::path::Path;
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
use std::str::FromStr;
|
2016-09-05 17:36:41 -04:00
|
|
|
|
2017-10-06 22:15:57 -04:00
|
|
|
use regex::bytes::{Captures, Match, Regex, Replacer};
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
use termcolor::{Color, ColorSpec, ParseColorError, WriteColor};
|
2016-08-28 01:37:12 -04:00
|
|
|
|
2016-09-24 19:18:48 -04:00
|
|
|
use pathutil::strip_prefix;
|
2016-10-11 19:57:09 -04:00
|
|
|
use ignore::types::FileTypeDef;
|
2016-08-29 22:44:15 -04:00
|
|
|
|
2017-10-06 22:15:57 -04:00
|
|
|
/// Track the start and end of replacements to allow coloring them on output.
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Offset {
|
|
|
|
start: usize,
|
|
|
|
end: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Offset {
|
|
|
|
fn new(start: usize, end: usize) -> Offset {
|
|
|
|
Offset { start: start, end: end }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'m, 'r> From<&'m Match<'r>> for Offset {
|
|
|
|
fn from(m: &'m Match<'r>) -> Self {
|
|
|
|
Offset{ start: m.start(), end: m.end() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-02 15:29:50 +01:00
|
|
|
/// CountingReplacer implements the Replacer interface for Regex,
|
|
|
|
/// and counts how often replacement is being performed.
|
|
|
|
struct CountingReplacer<'r> {
|
|
|
|
replace: &'r [u8],
|
|
|
|
count: &'r mut usize,
|
2017-10-06 22:15:57 -04:00
|
|
|
offsets: &'r mut Vec<Offset>,
|
2017-02-02 15:29:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'r> CountingReplacer<'r> {
|
2017-10-06 22:15:57 -04:00
|
|
|
fn new(
|
|
|
|
replace: &'r [u8],
|
|
|
|
count: &'r mut usize,
|
|
|
|
offsets: &'r mut Vec<Offset>,
|
|
|
|
) -> CountingReplacer<'r> {
|
|
|
|
CountingReplacer { replace: replace, count: count, offsets: offsets, }
|
2017-02-02 15:29:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'r> Replacer for CountingReplacer<'r> {
|
|
|
|
fn replace_append(&mut self, caps: &Captures, dst: &mut Vec<u8>) {
|
|
|
|
*self.count += 1;
|
2017-10-06 22:15:57 -04:00
|
|
|
let start = dst.len();
|
2017-02-02 15:29:50 +01:00
|
|
|
caps.expand(self.replace, dst);
|
2017-10-06 22:15:57 -04:00
|
|
|
let end = dst.len();
|
|
|
|
if start != end {
|
|
|
|
self.offsets.push(Offset::new(start, end));
|
|
|
|
}
|
2017-02-02 15:29:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-05 00:52:23 -04:00
|
|
|
/// Printer encapsulates all output logic for searching.
|
|
|
|
///
|
|
|
|
/// Note that we currently ignore all write errors. It's probably worthwhile
|
|
|
|
/// to fix this, but printers are only ever used for writes to stdout or
|
|
|
|
/// writes to memory, neither of which commonly fail.
|
2016-08-28 01:37:12 -04:00
|
|
|
pub struct Printer<W> {
|
2016-09-05 00:52:23 -04:00
|
|
|
/// The underlying writer.
|
2016-09-08 21:46:14 -04:00
|
|
|
wtr: W,
|
2016-09-05 00:52:23 -04:00
|
|
|
/// Whether anything has been printed to wtr yet.
|
2016-09-01 21:56:23 -04:00
|
|
|
has_printed: bool,
|
2016-09-06 19:50:27 -04:00
|
|
|
/// Whether to show column numbers for the first match or not.
|
|
|
|
column: bool,
|
2016-09-05 00:52:23 -04:00
|
|
|
/// The string to use to separate non-contiguous runs of context lines.
|
|
|
|
context_separator: Vec<u8>,
|
|
|
|
/// The end-of-line terminator used by the printer. In general, eols are
|
|
|
|
/// printed via the match directly, but occasionally we need to insert them
|
|
|
|
/// ourselves (for example, to print a context separator).
|
|
|
|
eol: u8,
|
2016-09-26 19:57:23 -04:00
|
|
|
/// A file separator to show before any matches are printed.
|
|
|
|
file_separator: Option<Vec<u8>>,
|
2016-09-05 17:36:41 -04:00
|
|
|
/// Whether to show file name as a heading or not.
|
|
|
|
///
|
|
|
|
/// N.B. If with_filename is false, then this setting has no effect.
|
|
|
|
heading: bool,
|
2016-09-22 21:32:38 -04:00
|
|
|
/// Whether to show every match on its own line.
|
|
|
|
line_per_match: bool,
|
2016-09-26 19:21:17 -04:00
|
|
|
/// Whether to print NUL bytes after a file path instead of new lines
|
|
|
|
/// or `:`.
|
|
|
|
null: bool,
|
2017-03-28 21:14:32 +03:00
|
|
|
/// Print only the matched (non-empty) parts of a matching line
|
|
|
|
only_matching: bool,
|
2016-09-05 17:36:41 -04:00
|
|
|
/// A string to use as a replacement of each match in a matching line.
|
|
|
|
replace: Option<Vec<u8>>,
|
2016-09-05 00:52:23 -04:00
|
|
|
/// Whether to prefix each match with the corresponding file name.
|
|
|
|
with_filename: bool,
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
/// The color specifications.
|
|
|
|
colors: ColorSpecs,
|
2017-01-10 18:16:15 -05:00
|
|
|
/// The separator to use for file paths. If empty, this is ignored.
|
|
|
|
path_separator: Option<u8>,
|
2017-02-02 15:29:50 +01:00
|
|
|
/// Restrict lines to this many columns.
|
|
|
|
max_columns: Option<usize>
|
2016-09-26 12:12:46 +09:30
|
|
|
}
|
|
|
|
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
impl<W: WriteColor> Printer<W> {
|
|
|
|
/// Create a new printer that writes to wtr with the given color settings.
|
2016-09-08 21:46:14 -04:00
|
|
|
pub fn new(wtr: W) -> Printer<W> {
|
2016-08-28 01:37:12 -04:00
|
|
|
Printer {
|
2016-09-08 21:46:14 -04:00
|
|
|
wtr: wtr,
|
2016-09-01 21:56:23 -04:00
|
|
|
has_printed: false,
|
2016-09-06 19:50:27 -04:00
|
|
|
column: false,
|
2016-09-05 00:52:23 -04:00
|
|
|
context_separator: "--".to_string().into_bytes(),
|
|
|
|
eol: b'\n',
|
2016-09-26 19:57:23 -04:00
|
|
|
file_separator: None,
|
2016-09-05 17:36:41 -04:00
|
|
|
heading: false,
|
2016-09-22 21:32:38 -04:00
|
|
|
line_per_match: false,
|
2016-09-26 19:21:17 -04:00
|
|
|
null: false,
|
2017-03-28 21:14:32 +03:00
|
|
|
only_matching: false,
|
2016-09-05 17:36:41 -04:00
|
|
|
replace: None,
|
2016-09-05 00:52:23 -04:00
|
|
|
with_filename: false,
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
colors: ColorSpecs::default(),
|
2017-01-10 18:16:15 -05:00
|
|
|
path_separator: None,
|
2017-02-02 15:29:50 +01:00
|
|
|
max_columns: None,
|
2016-08-28 01:37:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
/// Set the color specifications.
|
|
|
|
pub fn colors(mut self, colors: ColorSpecs) -> Printer<W> {
|
|
|
|
self.colors = colors;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2016-09-06 19:50:27 -04:00
|
|
|
/// When set, column numbers will be printed for the first match on each
|
|
|
|
/// line.
|
|
|
|
pub fn column(mut self, yes: bool) -> Printer<W> {
|
|
|
|
self.column = yes;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2016-09-05 00:52:23 -04:00
|
|
|
/// Set the context separator. The default is `--`.
|
|
|
|
pub fn context_separator(mut self, sep: Vec<u8>) -> Printer<W> {
|
|
|
|
self.context_separator = sep;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the end-of-line terminator. The default is `\n`.
|
|
|
|
pub fn eol(mut self, eol: u8) -> Printer<W> {
|
|
|
|
self.eol = eol;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2016-09-26 19:57:23 -04:00
|
|
|
/// If set, the separator is printed before any matches. By default, no
|
|
|
|
/// separator is printed.
|
|
|
|
pub fn file_separator(mut self, sep: Vec<u8>) -> Printer<W> {
|
|
|
|
self.file_separator = Some(sep);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2016-09-05 17:36:41 -04:00
|
|
|
/// Whether to show file name as a heading or not.
|
|
|
|
///
|
|
|
|
/// N.B. If with_filename is false, then this setting has no effect.
|
|
|
|
pub fn heading(mut self, yes: bool) -> Printer<W> {
|
|
|
|
self.heading = yes;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2016-09-22 21:32:38 -04:00
|
|
|
/// Whether to show every match on its own line.
|
|
|
|
pub fn line_per_match(mut self, yes: bool) -> Printer<W> {
|
|
|
|
self.line_per_match = yes;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2016-09-26 19:21:17 -04:00
|
|
|
/// Whether to cause NUL bytes to follow file paths instead of other
|
|
|
|
/// visual separators (like `:`, `-` and `\n`).
|
|
|
|
pub fn null(mut self, yes: bool) -> Printer<W> {
|
|
|
|
self.null = yes;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-03-28 21:14:32 +03:00
|
|
|
/// Print only the matched (non-empty) parts of a matching line
|
|
|
|
pub fn only_matching(mut self, yes: bool) -> Printer<W> {
|
|
|
|
self.only_matching = yes;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-01-10 18:16:15 -05:00
|
|
|
/// A separator to use when printing file paths. When empty, use the
|
|
|
|
/// default separator for the current platform. (/ on Unix, \ on Windows.)
|
|
|
|
pub fn path_separator(mut self, sep: Option<u8>) -> Printer<W> {
|
|
|
|
self.path_separator = sep;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2016-09-05 17:36:41 -04:00
|
|
|
/// Replace every match in each matching line with the replacement string
|
|
|
|
/// given.
|
|
|
|
pub fn replace(mut self, replacement: Vec<u8>) -> Printer<W> {
|
|
|
|
self.replace = Some(replacement);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2016-09-05 00:52:23 -04:00
|
|
|
/// When set, each match is prefixed with the file name that it came from.
|
|
|
|
pub fn with_filename(mut self, yes: bool) -> Printer<W> {
|
|
|
|
self.with_filename = yes;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-02-02 15:29:50 +01:00
|
|
|
/// Configure the max. number of columns used for printing matching lines.
|
|
|
|
pub fn max_columns(mut self, max_columns: Option<usize>) -> Printer<W> {
|
|
|
|
self.max_columns = max_columns;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2016-09-05 00:52:23 -04:00
|
|
|
/// Returns true if and only if something has been printed.
|
2016-09-01 21:56:23 -04:00
|
|
|
pub fn has_printed(&self) -> bool {
|
|
|
|
self.has_printed
|
|
|
|
}
|
|
|
|
|
2016-09-05 00:52:23 -04:00
|
|
|
/// Flushes the underlying writer and returns it.
|
2016-11-05 21:44:15 -04:00
|
|
|
#[allow(dead_code)]
|
2016-09-08 21:46:14 -04:00
|
|
|
pub fn into_inner(mut self) -> W {
|
2016-09-05 00:52:23 -04:00
|
|
|
let _ = self.wtr.flush();
|
2016-09-07 21:54:28 -04:00
|
|
|
self.wtr
|
2016-08-28 01:37:12 -04:00
|
|
|
}
|
|
|
|
|
2016-09-05 00:52:23 -04:00
|
|
|
/// Prints a type definition.
|
|
|
|
pub fn type_def(&mut self, def: &FileTypeDef) {
|
|
|
|
self.write(def.name().as_bytes());
|
|
|
|
self.write(b": ");
|
|
|
|
let mut first = true;
|
2016-10-11 19:57:09 -04:00
|
|
|
for glob in def.globs() {
|
2016-09-05 00:52:23 -04:00
|
|
|
if !first {
|
|
|
|
self.write(b", ");
|
|
|
|
}
|
2016-10-11 19:57:09 -04:00
|
|
|
self.write(glob.as_bytes());
|
2016-09-05 00:52:23 -04:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
self.write_eol();
|
2016-08-28 01:37:12 -04:00
|
|
|
}
|
|
|
|
|
2016-09-05 00:52:23 -04:00
|
|
|
/// Prints the given path.
|
|
|
|
pub fn path<P: AsRef<Path>>(&mut self, path: P) {
|
2016-09-24 19:18:48 -04:00
|
|
|
let path = strip_prefix("./", path.as_ref()).unwrap_or(path.as_ref());
|
2016-09-25 21:23:26 -04:00
|
|
|
self.write_path(path);
|
2017-03-31 00:13:07 +03:00
|
|
|
self.write_path_eol();
|
2016-08-28 20:18:34 -04:00
|
|
|
}
|
|
|
|
|
2016-09-05 00:52:23 -04:00
|
|
|
/// Prints the given path and a count of the number of matches found.
|
|
|
|
pub fn path_count<P: AsRef<Path>>(&mut self, path: P, count: u64) {
|
|
|
|
if self.with_filename {
|
2016-09-25 21:23:26 -04:00
|
|
|
self.write_path(path);
|
2017-03-31 00:13:07 +03:00
|
|
|
self.write_path_sep(b':');
|
2016-09-05 00:52:23 -04:00
|
|
|
}
|
|
|
|
self.write(count.to_string().as_bytes());
|
|
|
|
self.write_eol();
|
2016-08-28 01:37:12 -04:00
|
|
|
}
|
|
|
|
|
2016-09-05 00:52:23 -04:00
|
|
|
/// Prints the context separator.
|
|
|
|
pub fn context_separate(&mut self) {
|
|
|
|
if self.context_separator.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let _ = self.wtr.write_all(&self.context_separator);
|
2017-03-31 00:13:07 +03:00
|
|
|
self.write_eol();
|
2016-09-01 21:56:23 -04:00
|
|
|
}
|
|
|
|
|
2016-08-28 01:37:12 -04:00
|
|
|
pub fn matched<P: AsRef<Path>>(
|
|
|
|
&mut self,
|
2016-09-05 17:36:41 -04:00
|
|
|
re: &Regex,
|
2016-08-28 01:37:12 -04:00
|
|
|
path: P,
|
|
|
|
buf: &[u8],
|
2016-08-29 22:44:15 -04:00
|
|
|
start: usize,
|
|
|
|
end: usize,
|
|
|
|
line_number: Option<u64>,
|
2016-09-22 21:32:38 -04:00
|
|
|
) {
|
2017-03-28 21:14:32 +03:00
|
|
|
if !self.line_per_match && !self.only_matching {
|
2017-05-29 09:51:58 -04:00
|
|
|
let mat = re
|
|
|
|
.find(&buf[start..end])
|
|
|
|
.map(|m| (m.start(), m.end()))
|
|
|
|
.unwrap_or((0, 0));
|
2016-09-22 21:32:38 -04:00
|
|
|
return self.write_match(
|
2017-05-29 09:51:58 -04:00
|
|
|
re, path, buf, start, end, line_number, mat.0, mat.1);
|
2016-09-22 21:32:38 -04:00
|
|
|
}
|
2016-12-30 16:24:09 -05:00
|
|
|
for m in re.find_iter(&buf[start..end]) {
|
2016-09-22 21:32:38 -04:00
|
|
|
self.write_match(
|
2017-05-29 09:51:58 -04:00
|
|
|
re, path.as_ref(), buf, start, end,
|
|
|
|
line_number, m.start(), m.end());
|
2016-09-22 21:32:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_match<P: AsRef<Path>>(
|
|
|
|
&mut self,
|
|
|
|
re: &Regex,
|
|
|
|
path: P,
|
|
|
|
buf: &[u8],
|
|
|
|
start: usize,
|
|
|
|
end: usize,
|
|
|
|
line_number: Option<u64>,
|
2017-05-29 09:51:58 -04:00
|
|
|
match_start: usize,
|
|
|
|
match_end: usize,
|
2016-08-28 01:37:12 -04:00
|
|
|
) {
|
2016-09-05 17:36:41 -04:00
|
|
|
if self.heading && self.with_filename && !self.has_printed {
|
2016-09-26 19:57:23 -04:00
|
|
|
self.write_file_sep();
|
2017-03-31 00:13:07 +03:00
|
|
|
self.write_path(path);
|
|
|
|
self.write_path_eol();
|
2016-09-05 17:36:41 -04:00
|
|
|
} else if !self.heading && self.with_filename {
|
2017-03-31 00:13:07 +03:00
|
|
|
self.write_path(path);
|
|
|
|
self.write_path_sep(b':');
|
2016-09-05 00:52:23 -04:00
|
|
|
}
|
2016-08-29 22:44:15 -04:00
|
|
|
if let Some(line_number) = line_number {
|
2016-09-05 17:36:41 -04:00
|
|
|
self.line_number(line_number, b':');
|
|
|
|
}
|
2017-04-20 18:02:52 +03:00
|
|
|
if self.column {
|
2017-05-29 09:51:58 -04:00
|
|
|
self.column_number(match_start as u64 + 1, b':');
|
2016-09-06 19:50:27 -04:00
|
|
|
}
|
2016-09-05 17:36:41 -04:00
|
|
|
if self.replace.is_some() {
|
2017-02-02 15:29:50 +01:00
|
|
|
let mut count = 0;
|
2017-10-06 22:15:57 -04:00
|
|
|
let mut offsets = Vec::new();
|
2017-02-02 15:29:50 +01:00
|
|
|
let line = {
|
|
|
|
let replacer = CountingReplacer::new(
|
2017-10-06 22:15:57 -04:00
|
|
|
self.replace.as_ref().unwrap(), &mut count, &mut offsets);
|
2017-09-01 20:13:44 +03:00
|
|
|
if self.only_matching {
|
|
|
|
re.replace_all(
|
|
|
|
&buf[start + match_start..start + match_end], replacer)
|
|
|
|
} else {
|
|
|
|
re.replace_all(&buf[start..end], replacer)
|
|
|
|
}
|
2017-02-02 15:29:50 +01:00
|
|
|
};
|
|
|
|
if self.max_columns.map_or(false, |m| line.len() > m) {
|
2017-03-31 15:59:04 -04:00
|
|
|
let msg = format!(
|
|
|
|
"[Omitted long line with {} replacements]", count);
|
2017-03-31 00:13:07 +03:00
|
|
|
self.write_colored(msg.as_bytes(), |colors| colors.matched());
|
2017-02-02 15:29:50 +01:00
|
|
|
self.write_eol();
|
|
|
|
return;
|
|
|
|
}
|
2017-10-06 22:15:57 -04:00
|
|
|
self.write_matched_line(offsets, &*line, false);
|
2016-09-05 17:36:41 -04:00
|
|
|
} else {
|
2017-10-06 22:15:57 -04:00
|
|
|
let buf = if self.only_matching {
|
|
|
|
&buf[start + match_start..start + match_end]
|
2017-03-28 21:14:32 +03:00
|
|
|
} else {
|
2017-10-06 22:15:57 -04:00
|
|
|
&buf[start..end]
|
|
|
|
};
|
|
|
|
if self.max_columns.map_or(false, |m| buf.len() > m) {
|
|
|
|
let count = re.find_iter(buf).count();
|
|
|
|
let msg = format!("[Omitted long line with {} matches]", count);
|
|
|
|
self.write_colored(msg.as_bytes(), |colors| colors.matched());
|
|
|
|
self.write_eol();
|
|
|
|
return;
|
2017-05-29 09:51:58 -04:00
|
|
|
}
|
2017-10-06 22:15:57 -04:00
|
|
|
let only_match = self.only_matching;
|
|
|
|
self.write_matched_line(
|
|
|
|
re.find_iter(buf).map(|x| Offset::from(&x)), buf, only_match);
|
2016-09-03 21:48:23 -04:00
|
|
|
}
|
2016-08-28 01:37:12 -04:00
|
|
|
}
|
|
|
|
|
2017-10-06 22:15:57 -04:00
|
|
|
fn write_matched_line<I>(&mut self, offsets: I, buf: &[u8], only_match: bool)
|
|
|
|
where I: IntoIterator<Item=Offset>,
|
|
|
|
{
|
2016-11-21 20:33:15 -05:00
|
|
|
if !self.wtr.supports_color() || self.colors.matched().is_none() {
|
2016-09-05 17:36:41 -04:00
|
|
|
self.write(buf);
|
2017-05-29 09:51:58 -04:00
|
|
|
} else if only_match {
|
|
|
|
self.write_colored(buf, |colors| colors.matched());
|
2017-02-02 15:29:50 +01:00
|
|
|
} else {
|
|
|
|
let mut last_written = 0;
|
2017-10-06 22:15:57 -04:00
|
|
|
for o in offsets {
|
|
|
|
self.write(&buf[last_written..o.start]);
|
2017-03-31 15:59:04 -04:00
|
|
|
self.write_colored(
|
2017-10-06 22:15:57 -04:00
|
|
|
&buf[o.start..o.end], |colors| colors.matched());
|
|
|
|
last_written = o.end;
|
2017-02-02 15:29:50 +01:00
|
|
|
}
|
|
|
|
self.write(&buf[last_written..]);
|
2016-09-05 17:36:41 -04:00
|
|
|
}
|
2017-02-02 15:29:50 +01:00
|
|
|
if buf.last() != Some(&self.eol) {
|
|
|
|
self.write_eol();
|
2016-09-05 17:36:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-31 20:02:59 -04:00
|
|
|
pub fn context<P: AsRef<Path>>(
|
|
|
|
&mut self,
|
|
|
|
path: P,
|
|
|
|
buf: &[u8],
|
|
|
|
start: usize,
|
|
|
|
end: usize,
|
|
|
|
line_number: Option<u64>,
|
|
|
|
) {
|
2016-09-05 17:36:41 -04:00
|
|
|
if self.heading && self.with_filename && !self.has_printed {
|
2016-09-26 19:57:23 -04:00
|
|
|
self.write_file_sep();
|
2017-03-31 00:13:07 +03:00
|
|
|
self.write_path(path);
|
|
|
|
self.write_path_eol();
|
2016-09-05 17:36:41 -04:00
|
|
|
} else if !self.heading && self.with_filename {
|
2017-03-31 00:13:07 +03:00
|
|
|
self.write_path(path);
|
|
|
|
self.write_path_sep(b'-');
|
2016-09-05 00:52:23 -04:00
|
|
|
}
|
2016-08-31 20:02:59 -04:00
|
|
|
if let Some(line_number) = line_number {
|
2016-09-05 17:36:41 -04:00
|
|
|
self.line_number(line_number, b'-');
|
2016-08-31 20:02:59 -04:00
|
|
|
}
|
2017-02-02 15:29:50 +01:00
|
|
|
if self.max_columns.map_or(false, |m| end - start > m) {
|
|
|
|
self.write(format!("[Omitted long context line]").as_bytes());
|
|
|
|
self.write_eol();
|
|
|
|
return;
|
|
|
|
}
|
2016-08-31 20:02:59 -04:00
|
|
|
self.write(&buf[start..end]);
|
2016-09-05 00:52:23 -04:00
|
|
|
if buf[start..end].last() != Some(&self.eol) {
|
|
|
|
self.write_eol();
|
2016-09-03 21:48:23 -04:00
|
|
|
}
|
2016-08-31 20:02:59 -04:00
|
|
|
}
|
|
|
|
|
2017-03-31 00:13:07 +03:00
|
|
|
fn separator(&mut self, sep: &[u8]) {
|
|
|
|
self.write(&sep);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_path_sep(&mut self, sep: u8) {
|
2016-09-26 19:21:17 -04:00
|
|
|
if self.null {
|
|
|
|
self.write(b"\x00");
|
|
|
|
} else {
|
2017-03-31 00:13:07 +03:00
|
|
|
self.separator(&[sep]);
|
2016-09-26 19:21:17 -04:00
|
|
|
}
|
2016-09-05 17:36:41 -04:00
|
|
|
}
|
|
|
|
|
2017-03-31 00:13:07 +03:00
|
|
|
fn write_path_eol(&mut self) {
|
2016-09-28 11:54:43 +08:00
|
|
|
if self.null {
|
|
|
|
self.write(b"\x00");
|
|
|
|
} else {
|
2017-03-31 00:13:07 +03:00
|
|
|
self.write_eol();
|
2016-09-28 11:54:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-25 21:23:26 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
fn write_path<P: AsRef<Path>>(&mut self, path: P) {
|
|
|
|
use std::os::unix::ffi::OsStrExt;
|
|
|
|
let path = path.as_ref().as_os_str().as_bytes();
|
2017-03-31 00:13:07 +03:00
|
|
|
self.write_path_replace_separator(path);
|
2016-09-25 21:23:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
2016-09-25 21:48:01 -04:00
|
|
|
fn write_path<P: AsRef<Path>>(&mut self, path: P) {
|
2017-01-10 18:16:15 -05:00
|
|
|
let path = path.as_ref().to_string_lossy();
|
2017-03-31 00:13:07 +03:00
|
|
|
self.write_path_replace_separator(path.as_bytes());
|
2017-01-10 18:16:15 -05:00
|
|
|
}
|
|
|
|
|
2017-03-31 00:13:07 +03:00
|
|
|
fn write_path_replace_separator(&mut self, path: &[u8]) {
|
|
|
|
match self.path_separator {
|
|
|
|
None => self.write_colored(path, |colors| colors.path()),
|
|
|
|
Some(sep) => {
|
|
|
|
let transformed_path: Vec<_> = path.iter().map(|&b| {
|
|
|
|
if b == b'/' || (cfg!(windows) && b == b'\\') {
|
|
|
|
sep
|
|
|
|
} else {
|
|
|
|
b
|
|
|
|
}
|
|
|
|
}).collect();
|
|
|
|
self.write_colored(&transformed_path, |colors| colors.path());
|
2017-01-10 18:16:15 -05:00
|
|
|
}
|
|
|
|
}
|
2017-03-31 00:13:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn line_number(&mut self, n: u64, sep: u8) {
|
|
|
|
self.write_colored(n.to_string().as_bytes(), |colors| colors.line());
|
|
|
|
self.separator(&[sep]);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn column_number(&mut self, n: u64, sep: u8) {
|
2017-04-09 09:08:49 -04:00
|
|
|
self.write_colored(n.to_string().as_bytes(), |colors| colors.column());
|
2017-03-31 00:13:07 +03:00
|
|
|
self.separator(&[sep]);
|
2016-09-25 21:23:26 -04:00
|
|
|
}
|
|
|
|
|
2016-08-29 22:44:15 -04:00
|
|
|
fn write(&mut self, buf: &[u8]) {
|
2016-09-01 21:56:23 -04:00
|
|
|
self.has_printed = true;
|
2016-08-29 22:44:15 -04:00
|
|
|
let _ = self.wtr.write_all(buf);
|
|
|
|
}
|
2016-09-05 00:52:23 -04:00
|
|
|
|
|
|
|
fn write_eol(&mut self) {
|
|
|
|
let eol = self.eol;
|
|
|
|
self.write(&[eol]);
|
|
|
|
}
|
2016-09-26 19:57:23 -04:00
|
|
|
|
2017-03-31 00:13:07 +03:00
|
|
|
fn write_colored<F>(&mut self, buf: &[u8], get_color: F)
|
|
|
|
where F: Fn(&ColorSpecs) -> &ColorSpec
|
|
|
|
{
|
|
|
|
let _ = self.wtr.set_color( get_color(&self.colors) );
|
|
|
|
self.write(buf);
|
|
|
|
let _ = self.wtr.reset();
|
|
|
|
}
|
|
|
|
|
2016-09-26 19:57:23 -04:00
|
|
|
fn write_file_sep(&mut self) {
|
|
|
|
if let Some(ref sep) = self.file_separator {
|
|
|
|
self.has_printed = true;
|
|
|
|
let _ = self.wtr.write_all(sep);
|
|
|
|
let _ = self.wtr.write_all(b"\n");
|
|
|
|
}
|
|
|
|
}
|
2016-08-28 01:37:12 -04:00
|
|
|
}
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
|
|
|
|
/// An error that can occur when parsing color specifications.
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
pub enum Error {
|
|
|
|
/// This occurs when an unrecognized output type is used.
|
|
|
|
UnrecognizedOutType(String),
|
|
|
|
/// This occurs when an unrecognized spec type is used.
|
|
|
|
UnrecognizedSpecType(String),
|
|
|
|
/// This occurs when an unrecognized color name is used.
|
|
|
|
UnrecognizedColor(String, String),
|
|
|
|
/// This occurs when an unrecognized style attribute is used.
|
|
|
|
UnrecognizedStyle(String),
|
|
|
|
/// This occurs when the format of a color specification is invalid.
|
|
|
|
InvalidFormat(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl error::Error for Error {
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
match *self {
|
|
|
|
Error::UnrecognizedOutType(_) => "unrecognized output type",
|
|
|
|
Error::UnrecognizedSpecType(_) => "unrecognized spec type",
|
|
|
|
Error::UnrecognizedColor(_, _) => "unrecognized color name",
|
|
|
|
Error::UnrecognizedStyle(_) => "unrecognized style attribute",
|
|
|
|
Error::InvalidFormat(_) => "invalid color spec",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cause(&self) -> Option<&error::Error> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
|
|
|
Error::UnrecognizedOutType(ref name) => {
|
|
|
|
write!(f, "Unrecognized output type '{}'. Choose from: \
|
2017-04-09 09:08:49 -04:00
|
|
|
path, line, column, match.", name)
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
}
|
|
|
|
Error::UnrecognizedSpecType(ref name) => {
|
|
|
|
write!(f, "Unrecognized spec type '{}'. Choose from: \
|
|
|
|
fg, bg, style, none.", name)
|
|
|
|
}
|
|
|
|
Error::UnrecognizedColor(_, ref msg) => {
|
|
|
|
write!(f, "{}", msg)
|
|
|
|
}
|
|
|
|
Error::UnrecognizedStyle(ref name) => {
|
|
|
|
write!(f, "Unrecognized style attribute '{}'. Choose from: \
|
2017-03-31 00:13:07 +03:00
|
|
|
nobold, bold, nointense, intense.", name)
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
}
|
|
|
|
Error::InvalidFormat(ref original) => {
|
2017-04-09 09:08:49 -04:00
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"Invalid color speci format: '{}'. Valid format \
|
|
|
|
is '(path|line|column|match):(fg|bg|style):(value)'.",
|
|
|
|
original)
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ParseColorError> for Error {
|
|
|
|
fn from(err: ParseColorError) -> Error {
|
|
|
|
Error::UnrecognizedColor(err.invalid().to_string(), err.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A merged set of color specifications.
|
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq)]
|
|
|
|
pub struct ColorSpecs {
|
|
|
|
path: ColorSpec,
|
|
|
|
line: ColorSpec,
|
2017-04-09 09:08:49 -04:00
|
|
|
column: ColorSpec,
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
matched: ColorSpec,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A single color specification provided by the user.
|
|
|
|
///
|
|
|
|
/// A `ColorSpecs` can be built by merging a sequence of `Spec`s.
|
|
|
|
///
|
|
|
|
/// ## Example
|
|
|
|
///
|
|
|
|
/// The only way to build a `Spec` is to parse it from a string. Once multiple
|
|
|
|
/// `Spec`s have been constructed, then can be merged into a single
|
|
|
|
/// `ColorSpecs` value.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use termcolor::{Color, ColorSpecs, Spec};
|
|
|
|
///
|
|
|
|
/// let spec1: Spec = "path:fg:blue".parse().unwrap();
|
|
|
|
/// let spec2: Spec = "match:bg:green".parse().unwrap();
|
|
|
|
/// let specs = ColorSpecs::new(&[spec1, spec2]);
|
|
|
|
///
|
|
|
|
/// assert_eq!(specs.path().fg(), Some(Color::Blue));
|
|
|
|
/// assert_eq!(specs.matched().bg(), Some(Color::Green));
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ## Format
|
|
|
|
///
|
|
|
|
/// The format of a `Spec` is a triple: `{type}:{attribute}:{value}`. Each
|
|
|
|
/// component is defined as follows:
|
|
|
|
///
|
2017-04-09 09:08:49 -04:00
|
|
|
/// * `{type}` can be one of `path`, `line`, `column` or `match`.
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
/// * `{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.
|
|
|
|
///
|
|
|
|
/// `{type}` controls which part of the output should be styled and is
|
|
|
|
/// application dependent.
|
|
|
|
///
|
|
|
|
/// When `{attribute}` is `none`, then this should cause any existing color
|
|
|
|
/// settings to be cleared.
|
|
|
|
///
|
|
|
|
/// `{value}` should be a color when `{attribute}` is `fg` or `bg`, or it
|
|
|
|
/// should be a style instruction when `{attribute}` is `style`. When
|
|
|
|
/// `{attribute}` is `none`, `{value}` must be omitted.
|
|
|
|
///
|
|
|
|
/// Valid colors are `black`, `blue`, `green`, `red`, `cyan`, `magenta`,
|
|
|
|
/// `yellow`, `white`.
|
|
|
|
///
|
2017-07-17 15:01:13 -07:00
|
|
|
/// Valid style instructions are `nobold`, `bold`, `intense`, `nointense`.
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
pub struct Spec {
|
|
|
|
ty: OutType,
|
|
|
|
value: SpecValue,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The actual value given by the specification.
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
enum SpecValue {
|
|
|
|
None,
|
|
|
|
Fg(Color),
|
|
|
|
Bg(Color),
|
|
|
|
Style(Style),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The set of configurable portions of ripgrep's output.
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
enum OutType {
|
|
|
|
Path,
|
|
|
|
Line,
|
2017-04-09 09:08:49 -04:00
|
|
|
Column,
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
Match,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The specification type.
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
enum SpecType {
|
|
|
|
Fg,
|
|
|
|
Bg,
|
|
|
|
Style,
|
|
|
|
None,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The set of available styles for use in the terminal.
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
enum Style {
|
|
|
|
Bold,
|
|
|
|
NoBold,
|
2017-01-06 20:07:29 -05:00
|
|
|
Intense,
|
|
|
|
NoIntense,
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ColorSpecs {
|
|
|
|
/// Create color specifications from a list of user supplied
|
|
|
|
/// specifications.
|
|
|
|
pub fn new(user_specs: &[Spec]) -> ColorSpecs {
|
|
|
|
let mut specs = ColorSpecs::default();
|
|
|
|
for user_spec in user_specs {
|
|
|
|
match user_spec.ty {
|
|
|
|
OutType::Path => user_spec.merge_into(&mut specs.path),
|
|
|
|
OutType::Line => user_spec.merge_into(&mut specs.line),
|
2017-04-09 09:08:49 -04:00
|
|
|
OutType::Column => user_spec.merge_into(&mut specs.column),
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
OutType::Match => user_spec.merge_into(&mut specs.matched),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
specs
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the color specification for coloring file paths.
|
|
|
|
fn path(&self) -> &ColorSpec {
|
|
|
|
&self.path
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the color specification for coloring line numbers.
|
|
|
|
fn line(&self) -> &ColorSpec {
|
|
|
|
&self.line
|
|
|
|
}
|
|
|
|
|
2017-04-09 09:08:49 -04:00
|
|
|
/// Return the color specification for coloring column numbers.
|
|
|
|
fn column(&self) -> &ColorSpec {
|
|
|
|
&self.column
|
|
|
|
}
|
|
|
|
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
/// Return the color specification for coloring matched text.
|
|
|
|
fn matched(&self) -> &ColorSpec {
|
|
|
|
&self.matched
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Spec {
|
|
|
|
/// Merge this spec into the given color specification.
|
|
|
|
fn merge_into(&self, cspec: &mut ColorSpec) {
|
|
|
|
self.value.merge_into(cspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SpecValue {
|
|
|
|
/// Merge this spec value into the given color specification.
|
|
|
|
fn merge_into(&self, cspec: &mut ColorSpec) {
|
|
|
|
match *self {
|
|
|
|
SpecValue::None => cspec.clear(),
|
|
|
|
SpecValue::Fg(ref color) => { cspec.set_fg(Some(color.clone())); }
|
|
|
|
SpecValue::Bg(ref color) => { cspec.set_bg(Some(color.clone())); }
|
|
|
|
SpecValue::Style(ref style) => {
|
|
|
|
match *style {
|
|
|
|
Style::Bold => { cspec.set_bold(true); }
|
|
|
|
Style::NoBold => { cspec.set_bold(false); }
|
2017-01-06 20:07:29 -05:00
|
|
|
Style::Intense => { cspec.set_intense(true); }
|
|
|
|
Style::NoIntense => { cspec.set_intense(false); }
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for Spec {
|
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Spec, Error> {
|
2016-12-23 17:53:35 -02:00
|
|
|
let pieces: Vec<&str> = s.split(':').collect();
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
if pieces.len() <= 1 || pieces.len() > 3 {
|
|
|
|
return Err(Error::InvalidFormat(s.to_string()));
|
|
|
|
}
|
|
|
|
let otype: OutType = try!(pieces[0].parse());
|
|
|
|
match try!(pieces[1].parse()) {
|
|
|
|
SpecType::None => Ok(Spec { ty: otype, value: SpecValue::None }),
|
|
|
|
SpecType::Style => {
|
|
|
|
if pieces.len() < 3 {
|
|
|
|
return Err(Error::InvalidFormat(s.to_string()));
|
|
|
|
}
|
|
|
|
let style: Style = try!(pieces[2].parse());
|
|
|
|
Ok(Spec { ty: otype, value: SpecValue::Style(style) })
|
|
|
|
}
|
|
|
|
SpecType::Fg => {
|
|
|
|
if pieces.len() < 3 {
|
|
|
|
return Err(Error::InvalidFormat(s.to_string()));
|
|
|
|
}
|
|
|
|
let color: Color = try!(pieces[2].parse());
|
|
|
|
Ok(Spec { ty: otype, value: SpecValue::Fg(color) })
|
|
|
|
}
|
|
|
|
SpecType::Bg => {
|
|
|
|
if pieces.len() < 3 {
|
|
|
|
return Err(Error::InvalidFormat(s.to_string()));
|
|
|
|
}
|
|
|
|
let color: Color = try!(pieces[2].parse());
|
|
|
|
Ok(Spec { ty: otype, value: SpecValue::Bg(color) })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for OutType {
|
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<OutType, Error> {
|
|
|
|
match &*s.to_lowercase() {
|
|
|
|
"path" => Ok(OutType::Path),
|
|
|
|
"line" => Ok(OutType::Line),
|
2017-04-09 09:08:49 -04:00
|
|
|
"column" => Ok(OutType::Column),
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
"match" => Ok(OutType::Match),
|
|
|
|
_ => Err(Error::UnrecognizedOutType(s.to_string())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for SpecType {
|
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<SpecType, Error> {
|
|
|
|
match &*s.to_lowercase() {
|
|
|
|
"fg" => Ok(SpecType::Fg),
|
|
|
|
"bg" => Ok(SpecType::Bg),
|
|
|
|
"style" => Ok(SpecType::Style),
|
|
|
|
"none" => Ok(SpecType::None),
|
|
|
|
_ => Err(Error::UnrecognizedSpecType(s.to_string())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for Style {
|
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Style, Error> {
|
|
|
|
match &*s.to_lowercase() {
|
|
|
|
"bold" => Ok(Style::Bold),
|
|
|
|
"nobold" => Ok(Style::NoBold),
|
2017-01-06 20:07:29 -05:00
|
|
|
"intense" => Ok(Style::Intense),
|
|
|
|
"nointense" => Ok(Style::NoIntense),
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
_ => Err(Error::UnrecognizedStyle(s.to_string())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use termcolor::{Color, ColorSpec};
|
|
|
|
use super::{ColorSpecs, Error, OutType, Spec, SpecValue, Style};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge() {
|
|
|
|
let user_specs: &[Spec] = &[
|
|
|
|
"match:fg:blue".parse().unwrap(),
|
|
|
|
"match:none".parse().unwrap(),
|
|
|
|
"match:style:bold".parse().unwrap(),
|
|
|
|
];
|
|
|
|
let mut expect_matched = ColorSpec::new();
|
|
|
|
expect_matched.set_bold(true);
|
|
|
|
assert_eq!(ColorSpecs::new(user_specs), ColorSpecs {
|
|
|
|
path: ColorSpec::default(),
|
|
|
|
line: ColorSpec::default(),
|
2017-04-09 09:08:49 -04:00
|
|
|
column: ColorSpec::default(),
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
matched: expect_matched,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn specs() {
|
|
|
|
let spec: Spec = "path:fg:blue".parse().unwrap();
|
|
|
|
assert_eq!(spec, Spec {
|
|
|
|
ty: OutType::Path,
|
|
|
|
value: SpecValue::Fg(Color::Blue),
|
|
|
|
});
|
|
|
|
|
|
|
|
let spec: Spec = "path:bg:red".parse().unwrap();
|
|
|
|
assert_eq!(spec, Spec {
|
|
|
|
ty: OutType::Path,
|
|
|
|
value: SpecValue::Bg(Color::Red),
|
|
|
|
});
|
|
|
|
|
|
|
|
let spec: Spec = "match:style:bold".parse().unwrap();
|
|
|
|
assert_eq!(spec, Spec {
|
|
|
|
ty: OutType::Match,
|
|
|
|
value: SpecValue::Style(Style::Bold),
|
|
|
|
});
|
|
|
|
|
2017-01-06 20:07:29 -05:00
|
|
|
let spec: Spec = "match:style:intense".parse().unwrap();
|
|
|
|
assert_eq!(spec, Spec {
|
|
|
|
ty: OutType::Match,
|
|
|
|
value: SpecValue::Style(Style::Intense),
|
|
|
|
});
|
|
|
|
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
let spec: Spec = "line:none".parse().unwrap();
|
|
|
|
assert_eq!(spec, Spec {
|
|
|
|
ty: OutType::Line,
|
|
|
|
value: SpecValue::None,
|
|
|
|
});
|
2017-04-09 09:08:49 -04:00
|
|
|
|
|
|
|
let spec: Spec = "column:bg:green".parse().unwrap();
|
|
|
|
assert_eq!(spec, Spec {
|
|
|
|
ty: OutType::Column,
|
|
|
|
value: SpecValue::Bg(Color::Green),
|
|
|
|
});
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 11:14:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn spec_errors() {
|
|
|
|
let err = "line:nonee".parse::<Spec>().unwrap_err();
|
|
|
|
assert_eq!(err, Error::UnrecognizedSpecType("nonee".to_string()));
|
|
|
|
|
|
|
|
let err = "".parse::<Spec>().unwrap_err();
|
|
|
|
assert_eq!(err, Error::InvalidFormat("".to_string()));
|
|
|
|
|
|
|
|
let err = "foo".parse::<Spec>().unwrap_err();
|
|
|
|
assert_eq!(err, Error::InvalidFormat("foo".to_string()));
|
|
|
|
|
|
|
|
let err = "line:style:italic".parse::<Spec>().unwrap_err();
|
|
|
|
assert_eq!(err, Error::UnrecognizedStyle("italic".to_string()));
|
|
|
|
|
|
|
|
let err = "line:fg:brown".parse::<Spec>().unwrap_err();
|
|
|
|
match err {
|
|
|
|
Error::UnrecognizedColor(name, _) => assert_eq!(name, "brown"),
|
|
|
|
err => assert!(false, "unexpected error: {:?}", err),
|
|
|
|
}
|
|
|
|
|
|
|
|
let err = "foo:fg:brown".parse::<Spec>().unwrap_err();
|
|
|
|
assert_eq!(err, Error::UnrecognizedOutType("foo".to_string()));
|
|
|
|
}
|
|
|
|
}
|