|
4 | 4 |
|
5 | 5 | //! Types and functions related to shell. |
6 | 6 |
|
| 7 | +use std::str::FromStr; |
| 8 | + |
| 9 | +/// Program to use on the [`open`] call. |
| 10 | +pub enum Program { |
| 11 | + /// Use the `open` program. |
| 12 | + Open, |
| 13 | + /// Use the `start` program. |
| 14 | + Start, |
| 15 | + /// Use the `xdg-open` program. |
| 16 | + XdgOpen, |
| 17 | + /// Use the `gio` program. |
| 18 | + Gio, |
| 19 | + /// Use the `gnome-open` program. |
| 20 | + GnomeOpen, |
| 21 | + /// Use the `kde-open` program. |
| 22 | + KdeOpen, |
| 23 | + /// Use the `wslview` program. |
| 24 | + WslView, |
| 25 | + /// Use the `Firefox` program. |
| 26 | + Firefox, |
| 27 | + /// Use the `Google Chrome` program. |
| 28 | + Chrome, |
| 29 | + /// Use the `Chromium` program. |
| 30 | + Chromium, |
| 31 | + /// Use the `Safari` program. |
| 32 | + Safari, |
| 33 | +} |
| 34 | + |
| 35 | +impl FromStr for Program { |
| 36 | + type Err = super::Error; |
| 37 | + |
| 38 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 39 | + let p = match s.to_lowercase().as_str() { |
| 40 | + "open" => Self::Open, |
| 41 | + "start" => Self::Start, |
| 42 | + "xdg-open" => Self::XdgOpen, |
| 43 | + "gio" => Self::Gio, |
| 44 | + "gnome-open" => Self::GnomeOpen, |
| 45 | + "kde-open" => Self::KdeOpen, |
| 46 | + "wslview" => Self::WslView, |
| 47 | + "firefox" => Self::Firefox, |
| 48 | + "chrome" | "google chrome" => Self::Chrome, |
| 49 | + "chromium" => Self::Chromium, |
| 50 | + "safari" => Self::Safari, |
| 51 | + _ => return Err(super::Error::UnknownProgramName(s.to_string())), |
| 52 | + }; |
| 53 | + Ok(p) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl Program { |
| 58 | + fn name(self) -> &'static str { |
| 59 | + match self { |
| 60 | + Self::Open => "open", |
| 61 | + Self::Start => "start", |
| 62 | + Self::XdgOpen => "xdg-open", |
| 63 | + Self::Gio => "gio", |
| 64 | + Self::GnomeOpen => "gnome-open", |
| 65 | + Self::KdeOpen => "kde-open", |
| 66 | + Self::WslView => "wslview", |
| 67 | + |
| 68 | + #[cfg(target_os = "macos")] |
| 69 | + Self::Firefox => "Firefox", |
| 70 | + #[cfg(not(target_os = "macos"))] |
| 71 | + Self::Firefox => "firefox", |
| 72 | + |
| 73 | + #[cfg(target_os = "macos")] |
| 74 | + Self::Chrome => "Google Chrome", |
| 75 | + #[cfg(not(target_os = "macos"))] |
| 76 | + Self::Chrome => "google-chrome", |
| 77 | + |
| 78 | + #[cfg(target_os = "macos")] |
| 79 | + Self::Chromium => "Chromium", |
| 80 | + #[cfg(not(target_os = "macos"))] |
| 81 | + Self::Chromium => "chromium", |
| 82 | + |
| 83 | + #[cfg(target_os = "macos")] |
| 84 | + Self::Safari => "Safari", |
| 85 | + #[cfg(not(target_os = "macos"))] |
| 86 | + Self::Safari => "safari", |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
7 | 91 | /// Opens path or URL with program specified in `with`, or system default if `None`. |
8 | | -pub fn open(path: String, with: Option<String>) -> crate::api::Result<()> { |
| 92 | +pub fn open(path: String, with: Option<Program>) -> crate::api::Result<()> { |
9 | 93 | { |
10 | 94 | let exit_status = if let Some(with) = with { |
11 | | - open::with(&path, &with) |
| 95 | + open::with(&path, with.name()) |
12 | 96 | } else { |
13 | 97 | open::that(&path) |
14 | 98 | }; |
|
0 commit comments