Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add path-separator option. #429

Merged
merged 8 commits into from
Sep 15, 2019
Merged
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
11 changes: 11 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ pub fn build_app() -> App<'static, 'static> {
.hidden_short_help(true),
)
.arg(arg("pattern"))
.arg(
arg("path-separator")
sharkdp marked this conversation as resolved.
Show resolved Hide resolved
.takes_value(true)
sharkdp marked this conversation as resolved.
Show resolved Hide resolved
.value_name("separator")
.long("path-separator")
.hidden_short_help(true),
)
.arg(arg("path").multiple(true))
.arg(
arg("search-path")
Expand Down Expand Up @@ -249,6 +256,10 @@ fn usage() -> HashMap<&'static str, Help> {
doc!(h, "absolute-path"
, "Show absolute instead of relative paths"
, "Shows the full path starting from the root as opposed to relative paths.");
doc!(h, "path-separator"
, "Set the path separator to use when printing file paths."
, "Set the path separator to use when printing file paths. The default is the OS-specific \
separator ('/' on Unix, '\\' on Windows).");
doc!(h, "follow"
, "Follow symbolic links"
, "By default, fd does not descend into symlinked directories. Using this flag, symbolic \
Expand Down
3 changes: 3 additions & 0 deletions src/internal/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,7 @@ pub struct FdOptions {

/// Whether or not to display filesystem errors
pub show_filesystem_errors: bool,

/// The separator used to print file paths.
pub path_separator: Option<String>,
}
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ fn main() {
_ => atty::is(Stream::Stdout),
};

let path_separator = matches.value_of("path-separator").map(|str| str.to_owned());

#[cfg(windows)]
let colored_output = colored_output && ansi_term::enable_ansi_support().is_ok();

Expand Down Expand Up @@ -243,6 +245,7 @@ fn main() {
size_constraints: size_limits,
time_constraints,
show_filesystem_errors: matches.is_present("show-errors"),
path_separator,
};

match RegexBuilder::new(&pattern_regex)
Expand Down
17 changes: 15 additions & 2 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::exit_codes::ExitCode;
use crate::internal::opts::FdOptions;
use lscolors::{LsColors, Style};

use std::borrow::Cow;
use std::io::{self, StdoutLock, Write};
use std::path::{Component, Path, PathBuf};
use std::process;
Expand Down Expand Up @@ -52,6 +53,15 @@ pub fn print_entry(
}
}

fn replace_path_separator<'a>(config: &FdOptions, path: &mut Cow<'a, str>) {
match &config.path_separator {
None => {}
Some(sep) => {
*path.to_mut() = path.replace(std::path::MAIN_SEPARATOR, &sep);
}
}
}

fn print_entry_colorized(
stdout: &mut StdoutLock,
path: &Path,
Expand All @@ -67,7 +77,9 @@ fn print_entry_colorized(
.map(Style::to_ansi_term_style)
.unwrap_or(default_style);

write!(stdout, "{}", style.paint(component.to_string_lossy()))?;
let mut path_string = component.to_string_lossy();
replace_path_separator(&config, &mut path_string);
write!(stdout, "{}", style.paint(path_string))?;

if wants_to_quit.load(Ordering::Relaxed) {
writeln!(stdout)?;
Expand All @@ -89,6 +101,7 @@ fn print_entry_uncolorized(
) -> io::Result<()> {
let separator = if config.null_separator { "\0" } else { "\n" };

let path_str = path.to_string_lossy();
let mut path_str = path.to_string_lossy();
replace_path_separator(&config, &mut path_str);
write!(stdout, "{}{}", path_str, separator)
}
14 changes: 14 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1231,3 +1231,17 @@ fn test_modified_asolute() {
"30dec2017",
);
}

#[test]
fn test_custom_path_separator() {
let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);

te.assert_output(
&["foo", "one", "--path-separator", "="],
"one=b.foo
one=two=c.foo
one=two=C.Foo2
one=two=three=d.foo
one=two=three=directory_foo",
);
}