From 062ba4b8ae3700c38602ada4ef33d1f9a673ea4e Mon Sep 17 00:00:00 2001 From: Dock O'Neal Date: Tue, 21 Nov 2017 22:28:06 -0500 Subject: [PATCH 1/5] Added windows specific logic to check if we're in a unix like shell --- src/output.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/output.rs b/src/output.rs index fd54c82cd..7386341a4 100644 --- a/src/output.rs +++ b/src/output.rs @@ -15,6 +15,8 @@ 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 ansi_term; @@ -33,6 +35,20 @@ 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 { + if let Some(_) = env::var_os("PATH") { + 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(); @@ -61,7 +77,9 @@ 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() + }, }; } From d94e2ae0270cd470f5729655e55862db6c49d7ec Mon Sep 17 00:00:00 2001 From: Dock ONeal Date: Tue, 21 Nov 2017 23:45:21 -0500 Subject: [PATCH 2/5] Adding windows specific method to change back slashes to forward slashes if running in cygwin when output is not colorized --- src/output.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/output.rs b/src/output.rs index 7386341a4..0e11d08b8 100644 --- a/src/output.rs +++ b/src/output.rs @@ -15,8 +15,8 @@ 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; @@ -42,7 +42,7 @@ fn get_separator() -> String { #[cfg(windows)] fn get_separator() -> String { - if let Some(_) = env::var_os("PATH") { + if let Some(_) = env::var_os("HOME") { String::from("/") } else { path::MAIN_SEPARATOR.to_string() @@ -78,7 +78,7 @@ fn print_entry_colorized(path: &Path, config: &FdOptions, ls_colors: &LsColors) Component::RootDir => String::new(), // Everything else uses a separator that is painted the same way as the component. _ => { - style.paint(get_separator()).to_string() + style.paint(get_separator()).to_string() }, }; } @@ -90,11 +90,25 @@ fn print_entry_colorized(path: &Path, config: &FdOptions, ls_colors: &LsColors) } } +#[cfg(not(windows))] +fn write_entry_uncolorized(entry: Cow, separator: str) -> io::Result<()> { + write!(&mut io::stdout(), "{}{}", entry, separator) +} + +#[cfg(windows)] +fn write_entry_uncolorized(entry: Cow, separator: &'static str) -> io::Result<()> { + 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> { From 24225ce7afcae44c11b96ce89afd5882ef7a3d61 Mon Sep 17 00:00:00 2001 From: Dock O'Neal Date: Tue, 21 Nov 2017 23:49:19 -0500 Subject: [PATCH 3/5] Fixed syntax for non windows. Added a precompiler guard for std::env --- src/output.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/output.rs b/src/output.rs index 0e11d08b8..095e1563d 100644 --- a/src/output.rs +++ b/src/output.rs @@ -15,6 +15,7 @@ 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; @@ -91,7 +92,7 @@ fn print_entry_colorized(path: &Path, config: &FdOptions, ls_colors: &LsColors) } #[cfg(not(windows))] -fn write_entry_uncolorized(entry: Cow, separator: str) -> io::Result<()> { +fn write_entry_uncolorized(entry: Cow, separator: &'static str) -> io::Result<()> { write!(&mut io::stdout(), "{}{}", entry, separator) } From b97d5af9d4ed21f78c80cbbdc7f2f247b290dfb3 Mon Sep 17 00:00:00 2001 From: doxterpepper Date: Wed, 22 Nov 2017 00:01:28 -0500 Subject: [PATCH 4/5] Adding comment. --- src/output.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/output.rs b/src/output.rs index 095e1563d..083ad3c02 100644 --- a/src/output.rs +++ b/src/output.rs @@ -43,6 +43,9 @@ fn get_separator() -> 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 { @@ -78,9 +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(get_separator()).to_string() - }, + _ => style.paint(get_separator()).to_string(), }; } @@ -98,6 +99,10 @@ fn write_entry_uncolorized(entry: Cow, separator: &'static str) -> io::Resu #[cfg(windows)] fn write_entry_uncolorized(entry: Cow, 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 { From ba56d4e88658331ad226c9f33337cdb6878ea966 Mon Sep 17 00:00:00 2001 From: doxterpepper Date: Wed, 22 Nov 2017 00:34:07 -0500 Subject: [PATCH 5/5] Changing formatting --- src/output.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/output.rs b/src/output.rs index 083ad3c02..01b8ec8ff 100644 --- a/src/output.rs +++ b/src/output.rs @@ -104,17 +104,22 @@ fn write_entry_uncolorized(entry: Cow, separator: &'static str) -> io::Resu // 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) - } + 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_entry_uncolorized(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> {