Repository: sharkdp/fd
File: src/output.rs (Rust)

Code before:
    if r.is_err() {
        // Probably a broken pipe. Exit gracefully.
        process::exit(ExitCode::GeneralError.into());
    }
}

fn print_entry_colorized(
    stdout: &mut StdoutLock,
    path: &Path,
    config: &FdOptions,
    ls_colors: &LsColors,
    wants_to_quit: &Arc<AtomicBool>,
) -> io::Result<()> {
    let default_style = ansi_term::Style::default();

    // Traverse the path and colorize each component
    for (component, style) in ls_colors.style_for_path_components(path) {
        let style = style
            .map(Style::to_ansi_term_style)
            .unwrap_or(default_style);

        let path_string = component.to_string_lossy();

        match config.path_separator {
            None => write!(stdout, "{}", style.paint(path_string))?,
            Some(sep) => {
                let mut path_bytes = path_string.as_bytes().to_vec();
                for b in &mut path_bytes {
                    if *b == b'/' || (cfg!(windows) && *b == b'\\') {
                        *b = sep;
                    }
                }
                let path_string = String::from_utf8_lossy(&path_bytes);
                write!(stdout, "{}", style.paint(path_string))?
            }
        }

        if wants_to_quit.load(Ordering::Relaxed) {
            writeln!(stdout)?;
            process::exit(ExitCode::KilledBySigint.into());
        }
    }

    if config.null_separator {
        write!(stdout, "\0")
    } else {
        writeln!(stdout)
    }
}

fn print_entry_uncolorized(

Review comment (suggestion):
A couple of things:
* This should probably be in a separate function
* I don't think this is the best way to solve this (convert to byte vector, loop over vector, convert to string). Why not use a string-replacement function?
* Instead of checking for `cfg!(windows)`, this code should replace `std::path::MAIN_SEPARATOR` with `config.path_separator`. Otherwise, on Windows, you would replace both `/` and `\` with the new separator.

Code after:

    if r.is_err() {
        // Probably a broken pipe. Exit gracefully.
        process::exit(ExitCode::GeneralError.into());
    }
}

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,
    config: &FdOptions,
    ls_colors: &LsColors,
    wants_to_quit: &Arc<AtomicBool>,
) -> io::Result<()> {
    let default_style = ansi_term::Style::default();

    // Traverse the path and colorize each component
    for (component, style) in ls_colors.style_for_path_components(path) {
        let style = style
            .map(Style::to_ansi_term_style)
            .unwrap_or(default_style);

        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)?;
            process::exit(ExitCode::KilledBySigint.into());
        }
    }

    if config.null_separator {
        write!(stdout, "\0")
    } else {
        writeln!(stdout)
    }
}

fn print_entry_uncolorized(
    stdout: &mut StdoutLock,
    path: &Path,