Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ grep = "0.2.12"
headers = "0.3.8"
http = "0.2.9"
hyper-proxy = "0.9.1"
atty = "0.2.14"
memchr = "2.5.0"
num_cpus = "1.16.0"
indexmap = "2.0.0"
Expand Down
44 changes: 19 additions & 25 deletions src/bin/nix-locate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ struct Args {
package_pattern: Option<String>,
file_type: Vec<FileType>,
only_toplevel: bool,
color: bool,
minimal: bool,
}

Expand Down Expand Up @@ -122,27 +121,23 @@ fn locate(args: &Args) -> Result<()> {

let path = String::from_utf8_lossy(&path);

if args.color {
let mut prev = 0;
for mat in pattern.find_iter(path.as_bytes()) {
// if the match is empty, we need to make sure we don't use string
// indexing because the match may be "inside" a single multibyte character
// in that case (for example, the pattern may match the second byte of a multibyte character)
if mat.start() == mat.end() {
continue;
}
print!(
"{}{}",
&path[prev..mat.start()],
(&path[mat.start()..mat.end()])
.if_supports_color(Stream::Stdout, |txt| txt.red()),
);
prev = mat.end();
let mut prev = 0;
for mat in pattern.find_iter(path.as_bytes()) {
// if the match is empty, we need to make sure we don't use string
// indexing because the match may be "inside" a single multibyte character
// in that case (for example, the pattern may match the second byte of a multibyte character)
if mat.start() == mat.end() {
continue;
}
println!("{}", &path[prev..]);
} else {
println!("{}", path);
print!(
"{}{}",
&path[prev..mat.start()],
(&path[mat.start()..mat.end()])
.if_supports_color(Stream::Stdout, |txt| txt.red()),
);
prev = mat.end();
}
println!("{}", &path[prev..]);
}
}

Expand Down Expand Up @@ -172,10 +167,10 @@ fn process_args(matches: Opts) -> result::Result<Args, clap::Error> {
}
};

let color = match matches.color {
Color::Auto => atty::is(atty::Stream::Stdout),
Color::Always => true,
Color::Never => false,
match matches.color {
Color::Always => owo_colors::set_override(true),
Color::Never => owo_colors::set_override(false),
_ => {}
};

let args = Args {
Expand All @@ -188,7 +183,6 @@ fn process_args(matches: Opts) -> result::Result<Args, clap::Error> {
.r#type
.unwrap_or_else(|| files::ALL_FILE_TYPES.to_vec()),
only_toplevel: matches.top_level,
color,
minimal: matches.minimal,
};
Ok(args)
Expand Down