1 // Copyright 2022 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Opening Browser on Linux and MacOS
16
17 use std::ffi::OsStr;
18 #[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
19 use std::process::Command;
20
21 #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
open<T: AsRef<OsStr>>(path: T)22 pub fn open<T: AsRef<OsStr>>(path: T) {
23 let path = path.as_ref();
24 println!("Unsupported OS. Open this url:{:?}", path)
25 }
26
27 #[cfg(target_os = "linux")]
open<T: AsRef<OsStr>>(path: T)28 pub fn open<T: AsRef<OsStr>>(path: T) {
29 let path = path.as_ref();
30 let open_handlers = ["xdg-open", "gnome-open", "kde-open"];
31
32 for command in &open_handlers {
33 if let Ok(_output) = Command::new(command).arg(path).output() {
34 return;
35 }
36 }
37 println!("xdg-open, gnome-open, kde-open not working (linux). Open this url:{:?}", path);
38 }
39
40 #[cfg(target_os = "macos")]
open<T: AsRef<OsStr>>(path: T)41 pub fn open<T: AsRef<OsStr>>(path: T) {
42 let path = path.as_ref();
43 if let Ok(_output) = Command::new("/usr/bin/open").arg(path).output() {
44 return;
45 }
46 println!("/usr/bin/open not working (macos). Open this url:{:?}", path);
47 }
48
49 #[cfg(target_os = "windows")]
open<T: AsRef<OsStr>>(path: T)50 pub fn open<T: AsRef<OsStr>>(path: T) {
51 let path = path.as_ref();
52 if let Ok(_output) = Command::new("cmd.exe").args(["/C", "start"]).arg(path).output() {
53 return;
54 } else if let Ok(_output) = Command::new("explorer").arg(path).output() {
55 return;
56 }
57 println!("'start' and 'explorer' command not supported (windows). Open this url:{:?}", path);
58 }
59