summaryrefslogtreecommitdiffstats
path: root/src/path/escape.rs
blob: e5f6b69e903d089f72a3c4e5eb9762ae17f6d194 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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"'\''"))
    }
}