Skip to content

Commit 0766617

Browse files
committed
Refactor how coloring is done.
All in the name of appeasing Windows.
1 parent afd99c4 commit 0766617

File tree

6 files changed

+497
-380
lines changed

6 files changed

+497
-380
lines changed

src/args.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ use grep::{Grep, GrepBuilder};
99
use log;
1010
use num_cpus;
1111
use regex;
12+
use term::Terminal;
1213
use walkdir::WalkDir;
1314

1415
use gitignore::{Gitignore, GitignoreBuilder};
1516
use ignore::Ignore;
16-
use out::Out;
17+
use out::{Out, OutBuffer};
1718
use printer::Printer;
1819
use search::{InputBuffer, Searcher};
1920
use search_buffer::BufferSearcher;
@@ -438,8 +439,8 @@ impl Args {
438439

439440
/// Create a new printer of individual search results that writes to the
440441
/// writer given.
441-
pub fn printer<W: Send + io::Write>(&self, wtr: W) -> Printer<W> {
442-
let mut p = Printer::new(wtr, self.color)
442+
pub fn printer<W: Send + Terminal>(&self, wtr: W) -> Printer<W> {
443+
let mut p = Printer::new(wtr)
443444
.column(self.column)
444445
.context_separator(self.context_separator.clone())
445446
.eol(self.eol)
@@ -454,8 +455,8 @@ impl Args {
454455

455456
/// Create a new printer of search results for an entire file that writes
456457
/// to the writer given.
457-
pub fn out<W: io::Write>(&self, wtr: W) -> Out<W> {
458-
let mut out = Out::new(wtr);
458+
pub fn out(&self) -> Out {
459+
let mut out = Out::new(self.color);
459460
if self.heading && !self.count {
460461
out = out.file_separator(b"".to_vec());
461462
} else if self.before_context > 0 || self.after_context > 0 {
@@ -464,6 +465,11 @@ impl Args {
464465
out
465466
}
466467

468+
/// Create a new buffer for use with searching.
469+
pub fn outbuf(&self) -> OutBuffer {
470+
OutBuffer::new(self.color)
471+
}
472+
467473
/// Return the paths that should be searched.
468474
pub fn paths(&self) -> &[PathBuf] {
469475
&self.paths
@@ -472,7 +478,7 @@ impl Args {
472478
/// Create a new line based searcher whose configuration is taken from the
473479
/// command line. This searcher supports a dizzying array of features:
474480
/// inverted matching, line counting, context control and more.
475-
pub fn searcher<'a, R: io::Read, W: Send + io::Write>(
481+
pub fn searcher<'a, R: io::Read, W: Send + Terminal>(
476482
&self,
477483
inp: &'a mut InputBuffer,
478484
printer: &'a mut Printer<W>,
@@ -493,7 +499,7 @@ impl Args {
493499
/// Create a new line based searcher whose configuration is taken from the
494500
/// command line. This search operates on an entire file all once (which
495501
/// may have been memory mapped).
496-
pub fn searcher_buffer<'a, W: Send + io::Write>(
502+
pub fn searcher_buffer<'a, W: Send + Terminal>(
497503
&self,
498504
printer: &'a mut Printer<W>,
499505
grep: &'a Grep,

src/main.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use term::Terminal;
3939
use walkdir::DirEntry;
4040

4141
use args::Args;
42-
use out::Out;
42+
use out::{NoColorTerminal, Out, OutBuffer};
4343
use printer::Printer;
4444
use search::InputBuffer;
4545

@@ -90,7 +90,8 @@ fn run(args: Args) -> Result<u64> {
9090
return run_types(args);
9191
}
9292
let args = Arc::new(args);
93-
let out = Arc::new(Mutex::new(args.out(io::stdout())));
93+
let out = Arc::new(Mutex::new(args.out()));
94+
let outbuf = args.outbuf();
9495
let mut workers = vec![];
9596

9697
let mut workq = {
@@ -101,7 +102,7 @@ fn run(args: Args) -> Result<u64> {
101102
out: out.clone(),
102103
chan_work: stealer.clone(),
103104
inpbuf: args.input_buffer(),
104-
outbuf: Some(vec![]),
105+
outbuf: Some(outbuf.clone()),
105106
grep: args.grep(),
106107
match_count: 0,
107108
};
@@ -129,7 +130,8 @@ fn run(args: Args) -> Result<u64> {
129130
}
130131

131132
fn run_files(args: Args) -> Result<u64> {
132-
let mut printer = args.printer(io::BufWriter::new(io::stdout()));
133+
let term = NoColorTerminal::new(io::BufWriter::new(io::stdout()));
134+
let mut printer = args.printer(term);
133135
let mut file_count = 0;
134136
for p in args.paths() {
135137
if p == Path::new("-") {
@@ -146,7 +148,8 @@ fn run_files(args: Args) -> Result<u64> {
146148
}
147149

148150
fn run_types(args: Args) -> Result<u64> {
149-
let mut printer = args.printer(io::BufWriter::new(io::stdout()));
151+
let term = NoColorTerminal::new(io::BufWriter::new(io::stdout()));
152+
let mut printer = args.printer(term);
150153
let mut ty_count = 0;
151154
for def in args.type_defs() {
152155
printer.type_def(def);
@@ -168,10 +171,10 @@ enum WorkReady {
168171

169172
struct Worker {
170173
args: Arc<Args>,
171-
out: Arc<Mutex<Out<io::Stdout>>>,
174+
out: Arc<Mutex<Out>>,
172175
chan_work: Stealer<Work>,
173176
inpbuf: InputBuffer,
174-
outbuf: Option<Vec<u8>>,
177+
outbuf: Option<OutBuffer>,
175178
grep: Grep,
176179
match_count: u64,
177180
}
@@ -203,12 +206,12 @@ impl Worker {
203206
let mut out = self.out.lock().unwrap();
204207
out.write(&outbuf);
205208
}
206-
self.outbuf = Some(outbuf.into_inner());
209+
self.outbuf = Some(outbuf);
207210
}
208211
self.match_count
209212
}
210213

211-
fn do_work<W: Send + io::Write>(
214+
fn do_work<W: Send + Terminal>(
212215
&mut self,
213216
printer: &mut Printer<W>,
214217
work: WorkReady,
@@ -241,7 +244,7 @@ impl Worker {
241244
}
242245
}
243246

244-
fn search<R: io::Read, W: Send + io::Write>(
247+
fn search<R: io::Read, W: Send + Terminal>(
245248
&mut self,
246249
printer: &mut Printer<W>,
247250
path: &Path,
@@ -256,7 +259,7 @@ impl Worker {
256259
).run().map_err(From::from)
257260
}
258261

259-
fn search_mmap<W: Send + io::Write>(
262+
fn search_mmap<W: Send + Terminal>(
260263
&mut self,
261264
printer: &mut Printer<W>,
262265
path: &Path,

0 commit comments

Comments
 (0)