Skip to content

Commit

Permalink
Add --verbose option #32 #140
Browse files Browse the repository at this point in the history
  • Loading branch information
dalance committed Nov 29, 2023
1 parent 07d25b1 commit 1bb5517
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 33 deletions.
12 changes: 10 additions & 2 deletions src/ambr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ pub struct Opt {
#[structopt(long = "mmap-bytes", default_value = "1048576", value_name = "BYTES")]
pub mmap_bytes: u64,

/// Verbose message
#[structopt(long = "verbose")]
pub verbose: bool,

/// Enable regular expression search
#[structopt(short = "r", long = "regex", hidden = DEFAULT_FLAGS.regex)]
pub regex: bool,
Expand Down Expand Up @@ -419,7 +423,7 @@ fn main() {
finder.follow_symlink = opt.symlink;
finder.skip_vcs = opt.skip_vcs;
finder.skip_gitignore = opt.skip_gitignore;
finder.print_skipped = opt.skipped;
finder.print_skipped = opt.skipped | opt.verbose;
finder.find_parent_ignore = opt.parent_ignore;
sorter.through = !opt.fixed_order;
replacer.is_color = opt.color;
Expand All @@ -432,7 +436,8 @@ fn main() {
let use_regex = opt.regex;
let use_tbm = opt.tbm;
let skip_binary = !opt.binary;
let print_skipped = opt.skipped;
let print_skipped = opt.skipped | opt.verbose;
let print_search = opt.verbose;
let binary_check_bytes = opt.bin_check_bytes;
let mmap_bytes = opt.mmap_bytes;
let max_threads = opt.max_threads;
Expand All @@ -451,6 +456,7 @@ fn main() {
let mut matcher = PipelineMatcher::new(m, &keyword);
matcher.skip_binary = skip_binary;
matcher.print_skipped = print_skipped;
matcher.print_search = print_search;
matcher.binary_check_bytes = binary_check_bytes;
matcher.mmap_bytes = mmap_bytes;
matcher.setup(id_matcher + i, rx_in, tx_out);
Expand All @@ -461,6 +467,7 @@ fn main() {
let mut matcher = PipelineMatcher::new(m, &keyword);
matcher.skip_binary = skip_binary;
matcher.print_skipped = print_skipped;
matcher.print_search = print_search;
matcher.binary_check_bytes = binary_check_bytes;
matcher.mmap_bytes = mmap_bytes;
matcher.setup(id_matcher + i, rx_in, tx_out);
Expand All @@ -471,6 +478,7 @@ fn main() {
let mut matcher = PipelineMatcher::new(m, &keyword);
matcher.skip_binary = skip_binary;
matcher.print_skipped = print_skipped;
matcher.print_search = print_search;
matcher.binary_check_bytes = binary_check_bytes;
matcher.mmap_bytes = mmap_bytes;
matcher.setup(id_matcher + i, rx_in, tx_out);
Expand Down
12 changes: 10 additions & 2 deletions src/ambs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ pub struct Opt {
#[structopt(long = "mmap-bytes", default_value = "1048576", value_name = "BYTES")]
pub mmap_bytes: u64,

/// Verbose message
#[structopt(long = "verbose")]
pub verbose: bool,

/// Enable regular expression search
#[structopt(short = "r", long = "regex", hidden = DEFAULT_FLAGS.regex)]
pub regex: bool,
Expand Down Expand Up @@ -382,7 +386,7 @@ fn main() {
finder.follow_symlink = opt.symlink;
finder.skip_vcs = opt.skip_vcs;
finder.skip_gitignore = opt.skip_gitignore;
finder.print_skipped = opt.skipped;
finder.print_skipped = opt.skipped | opt.verbose;
finder.find_parent_ignore = opt.parent_ignore;
sorter.through = !opt.fixed_order;
printer.is_color = opt.color;
Expand All @@ -394,7 +398,8 @@ fn main() {
let use_regex = opt.regex;
let use_tbm = opt.tbm;
let skip_binary = !opt.binary;
let print_skipped = opt.skipped;
let print_skipped = opt.skipped | opt.verbose;
let print_search = opt.verbose;
let binary_check_bytes = opt.bin_check_bytes;
let mmap_bytes = opt.mmap_bytes;
let max_threads = opt.max_threads;
Expand All @@ -413,6 +418,7 @@ fn main() {
let mut matcher = PipelineMatcher::new(m, &keyword);
matcher.skip_binary = skip_binary;
matcher.print_skipped = print_skipped;
matcher.print_search = print_search;
matcher.binary_check_bytes = binary_check_bytes;
matcher.mmap_bytes = mmap_bytes;
matcher.setup(id_matcher + i, rx_in, tx_out);
Expand All @@ -423,6 +429,7 @@ fn main() {
let mut matcher = PipelineMatcher::new(m, &keyword);
matcher.skip_binary = skip_binary;
matcher.print_skipped = print_skipped;
matcher.print_search = print_search;
matcher.binary_check_bytes = binary_check_bytes;
matcher.mmap_bytes = mmap_bytes;
matcher.setup(id_matcher + i, rx_in, tx_out);
Expand All @@ -433,6 +440,7 @@ fn main() {
let mut matcher = PipelineMatcher::new(m, &keyword);
matcher.skip_binary = skip_binary;
matcher.print_skipped = print_skipped;
matcher.print_search = print_search;
matcher.binary_check_bytes = binary_check_bytes;
matcher.mmap_bytes = mmap_bytes;
matcher.setup(id_matcher + i, rx_in, tx_out);
Expand Down
32 changes: 6 additions & 26 deletions src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ pub struct Console {
pub is_color: bool,
term_stdout: Box<StdoutTerminal>,
term_stderr: Box<StderrTerminal>,
color_out: Color,
color_err: Color,
colored_out: bool,
colored_err: bool,
last_color: Color,
}

const CR: u8 = 0x0d;
Expand All @@ -49,10 +46,7 @@ impl Console {
process::exit(1);
}),
is_color: true,
color_out: term::color::BLACK,
color_err: term::color::BLACK,
colored_out: false,
colored_err: false,
last_color: term::color::BLACK,
}
}

Expand Down Expand Up @@ -177,42 +171,28 @@ impl Console {
}

fn write_stdout(&mut self, val: &str, color: Color) {
if self.is_color && self.color_out != color {
if self.is_color && self.last_color != color {
self.term_stdout.fg(color).unwrap_or_else(|_| {
process::exit(1);
});
self.color_out = color;
self.colored_out = true;
self.last_color = color;
}

write!(self.term_stdout, "{}", val).unwrap_or_else(|_| {
process::exit(1);
});

//if self.is_color {
// self.term_stdout.reset().unwrap_or_else( |_| { process::exit( 1 ); } );
//}

//let _ = io::stdout().flush();
}

fn write_stderr(&mut self, val: &str, color: Color) {
if self.is_color && self.color_err != color {
if self.is_color && self.last_color != color {
self.term_stderr.fg(color).unwrap_or_else(|_| {
process::exit(1);
});
self.color_err = color;
self.colored_err = true;
self.last_color = color;
}

write!(self.term_stderr, "{}", val).unwrap_or_else(|_| {
process::exit(1);
});

//if self.is_color {
// self.term_stderr.reset().unwrap_or_else( |_| { process::exit( 1 ); } );
//}

//let _ = io::stderr().flush();
}
}
1 change: 1 addition & 0 deletions src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub enum PipelineInfo<T> {
SeqDat(usize, T),
SeqEnd(usize),
MsgInfo(usize, String),
MsgDebug(usize, String),
MsgErr(usize, String),
MsgTime(usize, Duration, Duration),
}
Expand Down
7 changes: 5 additions & 2 deletions src/pipeline_finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,11 @@ impl PipelineFinder {
};

if !ok_vcs & self.print_skipped {
self.infos.push(format!("Skipped: {:?} ( vcs file )\n", path));
self.infos.push(format!("Skip (vcs file) : {:?}", path));
}

if !ok_git & self.print_skipped {
self.infos.push(format!("Skipped: {:?} ( .gitignore )\n", path));
self.infos.push(format!("Skip (.gitignore): {:?}", path));
}

ok_vcs && ok_git
Expand Down Expand Up @@ -264,6 +264,9 @@ impl PipelineFork<PathBuf, PathInfo> for PipelineFinder {
break;
}

Ok(PipelineInfo::MsgDebug(i, e)) => {
let _ = tx[0].send(PipelineInfo::MsgDebug(i, e));
}
Ok(PipelineInfo::MsgInfo(i, e)) => {
let _ = tx[0].send(PipelineInfo::MsgInfo(i, e));
}
Expand Down
18 changes: 17 additions & 1 deletion src/pipeline_matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct PathMatch {
pub struct PipelineMatcher<T: Matcher> {
pub skip_binary: bool,
pub print_skipped: bool,
pub print_search: bool,
pub binary_check_bytes: usize,
pub mmap_bytes: u64,
pub infos: Vec<String>,
Expand All @@ -42,6 +43,7 @@ impl<T: Matcher> PipelineMatcher<T> {
PipelineMatcher {
skip_binary: true,
print_skipped: false,
print_search: false,
binary_check_bytes: 128,
mmap_bytes: 1024 * 1024,
infos: Vec::new(),
Expand Down Expand Up @@ -91,7 +93,7 @@ impl<T: Matcher> PipelineMatcher<T> {
}
if is_binary {
if self.print_skipped {
self.infos.push(format!("Skipped: {:?} ( binary file )\n", info.path));
self.infos.push(format!("Skip (binary) : {:?}", info.path));
}
return Ok(PathMatch {
path: info.path.clone(),
Expand Down Expand Up @@ -131,10 +133,21 @@ impl<T: Matcher> Pipeline<PathInfo, PathMatch> for PipelineMatcher<T> {
loop {
match rx.recv() {
Ok(PipelineInfo::SeqDat(x, p)) => {
let mut path = None;
if self.print_search {
path = Some(p.path.clone());
let _ = tx.send(PipelineInfo::MsgDebug(id, format!("Search Start : {:?}", p.path)));
}
watch_time!(self.time_bsy, {
let ret = self.search_path(p);
let _ = tx.send(PipelineInfo::SeqDat(x, ret));
});
if self.print_search {
let _ = tx.send(PipelineInfo::MsgDebug(
id,
format!("Search Finish : {:?}", path.unwrap()),
));
}
}

Ok(PipelineInfo::SeqBeg(x)) => {
Expand All @@ -158,6 +171,9 @@ impl<T: Matcher> Pipeline<PathInfo, PathMatch> for PipelineMatcher<T> {
break;
}

Ok(PipelineInfo::MsgDebug(i, e)) => {
let _ = tx.send(PipelineInfo::MsgDebug(i, e));
}
Ok(PipelineInfo::MsgInfo(i, e)) => {
let _ = tx.send(PipelineInfo::MsgInfo(i, e));
}
Expand Down
3 changes: 3 additions & 0 deletions src/pipeline_printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ impl Pipeline<PathMatch, ()> for PipelinePrinter {
break;
}

Ok(PipelineInfo::MsgDebug(_, e)) => {
self.console.write(ConsoleTextKind::Info, &format!("{}\n", e));
}
Ok(PipelineInfo::MsgInfo(i, e)) => {
let _ = tx.send(PipelineInfo::MsgInfo(i, e));
}
Expand Down
3 changes: 3 additions & 0 deletions src/pipeline_replacer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ impl Pipeline<PathMatch, ()> for PipelineReplacer {
break;
}

Ok(PipelineInfo::MsgDebug(i, e)) => {
let _ = tx.send(PipelineInfo::MsgDebug(i, e));
}
Ok(PipelineInfo::MsgInfo(i, e)) => {
let _ = tx.send(PipelineInfo::MsgInfo(i, e));
}
Expand Down
3 changes: 3 additions & 0 deletions src/pipeline_sorter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ impl PipelineJoin<PathMatch, PathMatch> for PipelineSorter {
break;
}

Ok(PipelineInfo::MsgDebug(i, e)) => {
let _ = tx.send(PipelineInfo::MsgDebug(i, e));
}
Ok(PipelineInfo::MsgInfo(i, e)) => {
let _ = tx.send(PipelineInfo::MsgInfo(i, e));
}
Expand Down

0 comments on commit 1bb5517

Please sign in to comment.