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

Cygwin forward slashes #176

Closed
Closed
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
47 changes: 45 additions & 2 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ use std::ops::Deref;
use std::path::{self, Path, PathBuf, Component};
#[cfg(any(unix, target_os = "redox"))]
use std::os::unix::fs::PermissionsExt;
#[cfg(windows)]
use std::env;
use std::borrow::Cow;

use ansi_term;

Expand All @@ -33,6 +36,23 @@ pub fn print_entry(entry: &PathBuf, config: &FdOptions) {
}
}

#[cfg(not(windows))]
fn get_separator() -> String {
path::MAIN_SEPARATOR.to_string()
}

#[cfg(windows)]
fn get_separator() -> String {
// The HOME environment variable is not available when using cmd
// and allows a way to determine if we're running fd with cmd
// or cygwin.
if let Some(_) = env::var_os("HOME") {
String::from("/")
} else {
path::MAIN_SEPARATOR.to_string()
}
}

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

Expand Down Expand Up @@ -61,7 +81,7 @@ fn print_entry_colorized(path: &Path, config: &FdOptions, ls_colors: &LsColors)
// RootDir is already a separator.
Component::RootDir => String::new(),
// Everything else uses a separator that is painted the same way as the component.
_ => style.paint(path::MAIN_SEPARATOR.to_string()).to_string(),
_ => style.paint(get_separator()).to_string(),
};
}

Expand All @@ -72,11 +92,34 @@ fn print_entry_colorized(path: &Path, config: &FdOptions, ls_colors: &LsColors)
}
}

#[cfg(not(windows))]
fn write_entry_uncolorized(entry: Cow<str>, separator: &'static str) -> io::Result<()> {
write!(&mut io::stdout(), "{}{}", entry, separator)
}

#[cfg(windows)]
fn write_entry_uncolorized(entry: Cow<str>, separator: &'static str) -> io::Result<()> {
// The HOME environment variable is not available when using cmd
// and allows a way to determine if we're running fd with cmd
// or cygwin.
// Replace back slashes with forward slashes when running on cygwin.
if let Some(_) = env::var_os("HOME") {
write!(
&mut io::stdout(),
"{}{}",
entry.replace("\\", "/"),
separator
)
} else {
write!(&mut io::stdout(), "{}{}", entry, separator)
}
}

fn print_entry_uncolorized(path: &Path, config: &FdOptions) -> io::Result<()> {
let separator = if config.null_separator { "\0" } else { "\n" };

let path_str = path.to_string_lossy();
write!(&mut io::stdout(), "{}{}", path_str, separator)
write_entry_uncolorized(path_str, separator)
}

fn get_path_style<'a>(path: &Path, ls_colors: &'a LsColors) -> Option<&'a ansi_term::Style> {
Expand Down