summaryrefslogtreecommitdiff
path: root/crates/utils/string_proc
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2025-11-07 13:24:43 +0800
committer魏曹先生 <1992414357@qq.com>2025-11-07 13:24:43 +0800
commit9e5a374164aca71ec99aa8b46e7932a1b74f68cc (patch)
tree04d72a86b09b5d434e8317f2e2cfafe5e6522d36 /crates/utils/string_proc
parent898969ffe4a7ac007e53fe1fc1cb121971d6d8ed (diff)
Add path formatting utility and local sheet management
- Implement format_path_str function to clean and normalize file paths - Add LocalSheet struct for tracking local file metadata - Support CRUD operations on local sheet mappings - Integrate path formatting into local sheet operations
Diffstat (limited to 'crates/utils/string_proc')
-rw-r--r--crates/utils/string_proc/src/format_path.rs64
-rw-r--r--crates/utils/string_proc/src/lib.rs1
2 files changed, 65 insertions, 0 deletions
diff --git a/crates/utils/string_proc/src/format_path.rs b/crates/utils/string_proc/src/format_path.rs
new file mode 100644
index 0000000..2d8927b
--- /dev/null
+++ b/crates/utils/string_proc/src/format_path.rs
@@ -0,0 +1,64 @@
+use std::path::PathBuf;
+
+/// Format path str
+pub fn format_path_str(path: impl Into<String>) -> Result<String, std::io::Error> {
+ let path_str = path.into();
+
+ // ANSI Strip
+ let cleaned = strip_ansi_escapes::strip(&path_str);
+ let path_without_ansi = String::from_utf8(cleaned)
+ .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
+
+ let path_with_forward_slash = path_without_ansi.replace('\\', "/");
+ let mut result = String::new();
+ let mut prev_char = '\0';
+
+ for c in path_with_forward_slash.chars() {
+ if c == '/' && prev_char == '/' {
+ continue;
+ }
+ result.push(c);
+ prev_char = c;
+ }
+
+ let unfriendly_chars = ['*', '?', '"', '<', '>', '|'];
+ result = result
+ .chars()
+ .filter(|c| !unfriendly_chars.contains(c))
+ .collect();
+
+ if result.ends_with('/') {
+ Ok(result)
+ } else {
+ Ok(result)
+ }
+}
+
+pub fn format_path(path: impl Into<PathBuf>) -> Result<PathBuf, std::io::Error> {
+ let path_str = format_path_str(path.into().display().to_string())?;
+ Ok(PathBuf::from(path_str))
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_format_path() -> Result<(), std::io::Error> {
+ assert_eq!(format_path_str("C:\\Users\\\\test")?, "C:/Users/test");
+
+ assert_eq!(
+ format_path_str("/path/with/*unfriendly?chars")?,
+ "/path/with/unfriendlychars"
+ );
+
+ assert_eq!(format_path_str("\x1b[31m/path\x1b[0m")?, "/path");
+ assert_eq!(format_path_str("/home/user/dir/")?, "/home/user/dir/");
+ assert_eq!(
+ format_path_str("/home/user/file.txt")?,
+ "/home/user/file.txt"
+ );
+
+ Ok(())
+ }
+}
diff --git a/crates/utils/string_proc/src/lib.rs b/crates/utils/string_proc/src/lib.rs
index e5559b9..76588c1 100644
--- a/crates/utils/string_proc/src/lib.rs
+++ b/crates/utils/string_proc/src/lib.rs
@@ -1,3 +1,4 @@
+pub mod format_path;
pub mod format_processer;
pub mod macros;
pub mod simple_processer;