From bf4cf561cae801ab49ca74ac69e733a88ea77bce Mon Sep 17 00:00:00 2001 From: Kevin Date: Wed, 11 Mar 2020 23:05:12 -0500 Subject: [PATCH] Utility function to detect hidden folders. Implemented for Unix and Windows. --- crates/nu-cli/src/shell/filesystem_shell.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/crates/nu-cli/src/shell/filesystem_shell.rs b/crates/nu-cli/src/shell/filesystem_shell.rs index bfe7cec53..22ff18408 100644 --- a/crates/nu-cli/src/shell/filesystem_shell.rs +++ b/crates/nu-cli/src/shell/filesystem_shell.rs @@ -9,6 +9,7 @@ use crate::prelude::*; use crate::shell::completer::NuCompleter; use crate::shell::shell::Shell; use crate::utils::FileStructure; +use cfg_if::cfg_if; use nu_errors::ShellError; use nu_parser::ExpandContext; use nu_protocol::{Primitive, ReturnSuccess, UntaggedValue}; @@ -1135,3 +1136,23 @@ fn is_dir_empty(d: &PathBuf) -> bool { Ok(mut s) => s.next().is_none(), } } + +fn is_hidden_dir(dir: impl AsRef) -> bool { + cfg_if! { + if #[cfg(unix)] { + dir.as_ref().starts_with(".") + } else if #[cfg(windows)] { + use std::os::windows::fs::MetadataExt; + + if let Ok(metadata) = dir.as_ref().metadata() { + let attributes = metadata.file_attributes(); + // https://docs.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants + attributes & 0x2 + } else { + false + } + } else { + false + } + } +}