yazi_shared/shell/
mod.rs

1//! Escape characters that may have special meaning in a shell, including
2//! spaces. This is a modified version of the [`shell-escape`], [`shell-words`]
3//! and [`this PR`].
4//!
5//! [`shell-escape`]: https://crates.io/crates/shell-escape
6//! [`shell-words`]: https://crates.io/crates/shell-words
7//! [`this PR`]: https://github.com/sfackler/shell-escape/pull/9
8
9use std::{borrow::Cow, ffi::OsStr};
10
11mod unix;
12mod windows;
13
14#[inline]
15pub fn escape_unix(s: &str) -> Cow<str> { unix::escape_str(s) }
16
17#[inline]
18pub fn escape_windows(s: &str) -> Cow<str> { windows::escape_str(s) }
19
20#[inline]
21pub fn escape_native(s: &str) -> Cow<str> {
22	#[cfg(unix)]
23	{
24		escape_unix(s)
25	}
26	#[cfg(windows)]
27	{
28		escape_windows(s)
29	}
30}
31
32#[inline]
33pub fn escape_os_str(s: &OsStr) -> Cow<OsStr> {
34	#[cfg(unix)]
35	{
36		unix::escape_os_str(s)
37	}
38	#[cfg(windows)]
39	{
40		windows::escape_os_str(s)
41	}
42}
43
44#[inline]
45pub fn split_unix(s: &str, eoo: bool) -> anyhow::Result<(Vec<String>, Option<String>)> {
46	unix::split(s, eoo).map_err(|()| anyhow::anyhow!("missing closing quote"))
47}
48
49#[cfg(windows)]
50pub fn split_windows(s: &str) -> anyhow::Result<Vec<String>> { Ok(windows::split(s)?) }
51
52pub fn split_native(s: &str) -> anyhow::Result<Vec<String>> {
53	#[cfg(unix)]
54	{
55		Ok(split_unix(s, false)?.0)
56	}
57	#[cfg(windows)]
58	{
59		split_windows(s)
60	}
61}