From 6490775f28fe36228280ac57b3a5a9e1fed9d004 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Fri, 13 Feb 2026 02:17:20 +0800 Subject: Bump version to 0.1.1 and make strip-ansi optional --- src/fmt_path.rs | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/fmt_path.rs b/src/fmt_path.rs index 4b15fe4..d7c5fd3 100644 --- a/src/fmt_path.rs +++ b/src/fmt_path.rs @@ -4,6 +4,7 @@ use std::path::{Path, PathBuf}; pub struct PathFormatConfig { /// Whether to strip ANSI escape sequences (e.g., `\x1b[31m`, `\x1b[0m`). /// When the path string may contain terminal color codes, enabling this option will clean them up. + #[cfg(feature = "strip-ansi")] pub strip_ansi: bool, /// Whether to strip characters disallowed in Windows filenames (`*`, `?`, `"`, `<`, `>`, `|`). @@ -27,6 +28,7 @@ pub struct PathFormatConfig { impl Default for PathFormatConfig { fn default() -> Self { Self { + #[cfg(feature = "strip-ansi")] strip_ansi: true, strip_unfriendly_chars: true, resolve_parent_dirs: true, @@ -72,7 +74,7 @@ impl Default for PathFormatConfig { /// # Ok(()) /// # } /// ``` -pub fn fmt_path_str(path: impl Into) -> Result { +pub fn fmt_path_str(path: impl Into) -> Result { fmt_path_str_custom(path, &PathFormatConfig::default()) } @@ -88,27 +90,28 @@ pub fn fmt_path_str(path: impl Into) -> Result pub fn fmt_path_str_custom( path: impl Into, config: &PathFormatConfig, -) -> Result { - let path_str = path.into(); - let ends_with_slash = path_str.ends_with('/'); +) -> Result { + let path_result = path.into(); + let ends_with_slash = path_result.ends_with('/'); // ANSI Strip - let cleaned = if config.strip_ansi { - strip_ansi_escapes::strip(&path_str) + #[cfg(feature = "strip-ansi")] + let path_result = if config.strip_ansi { + let cleaned = strip_ansi_escapes::strip(&path_result); + String::from_utf8(cleaned).map_err(PathFormatError::InvalidUtf8)? } else { - path_str.as_bytes().to_vec() + path_result }; - let path_without_ansi = String::from_utf8(cleaned).map_err(FormatPathError::InvalidUtf8)?; - let path_with_forward_slash = if config.escape_backslashes { - path_without_ansi.replace('\\', "/") + let path_result = if config.escape_backslashes { + path_result.replace('\\', "/") } else { - path_without_ansi + path_result }; let mut result = String::new(); let mut prev_char = '\0'; - for c in path_with_forward_slash.chars() { + for c in path_result.chars() { if config.collapse_consecutive_slashes && c == '/' && prev_char == '/' { continue; } @@ -182,7 +185,7 @@ fn normalize_path(path: &Path) -> PathBuf { /// - strip ANSI escape sequences /// - remove unfriendly characters (`*`, `?`, etc.) /// - resolve simple `..` segments -pub fn fmt_path(path: impl Into) -> Result { +pub fn fmt_path(path: impl Into) -> Result { let path_str = fmt_path_str(path.into().display().to_string())?; Ok(PathBuf::from(path_str)) } @@ -195,38 +198,38 @@ pub fn fmt_path(path: impl Into) -> Result { pub fn fmt_path_custom( path: impl Into, config: &PathFormatConfig, -) -> Result { +) -> Result { let path_str = fmt_path_str_custom(path.into().display().to_string(), config)?; Ok(PathBuf::from(path_str)) } /// Error type for path formatting operations. #[derive(Debug)] -pub enum FormatPathError { +pub enum PathFormatError { /// The input string contained invalid UTF-8 after stripping ANSI escape sequences. InvalidUtf8(std::string::FromUtf8Error), } -impl std::fmt::Display for FormatPathError { +impl std::fmt::Display for PathFormatError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - FormatPathError::InvalidUtf8(e) => { + PathFormatError::InvalidUtf8(e) => { write!(f, "Invalid UTF-8 after ANSI stripping: {}", e) } } } } -impl std::error::Error for FormatPathError { +impl std::error::Error for PathFormatError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { match self { - FormatPathError::InvalidUtf8(e) => Some(e), + PathFormatError::InvalidUtf8(e) => Some(e), } } } -impl From for FormatPathError { +impl From for PathFormatError { fn from(e: std::string::FromUtf8Error) -> Self { - FormatPathError::InvalidUtf8(e) + PathFormatError::InvalidUtf8(e) } } -- cgit