diff options
Diffstat (limited to 'src/path/escape.rs')
-rw-r--r-- | src/path/escape.rs | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/path/escape.rs b/src/path/escape.rs new file mode 100644 index 0000000..e5f6b69 --- /dev/null +++ b/src/path/escape.rs @@ -0,0 +1,16 @@ +use { + std::path::Path, +}; + +/// from a path, build a string usable in a shell command, wrapping +/// it in quotes if necessary (and then escaping internal quotes). +/// Don't do unnecessary transformation, so that the produced string +/// is prettier on screen. +pub fn escape_for_shell(path: &Path) -> String { + let path = path.to_string_lossy(); + if regex!(r"^[\w/.-]*$").is_match(&path) { + path.to_string() + } else { + format!("'{}'", &path.replace('\'', r"'\''")) + } +} |