2021-09-09 10:48:53 +03:00
|
|
|
module os
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn open_uri(uri string) ! {
|
2021-10-06 06:29:32 +03:00
|
|
|
mut vopen_uri_cmd := getenv('VOPEN_URI_CMD')
|
|
|
|
if vopen_uri_cmd == '' {
|
|
|
|
$if macos {
|
|
|
|
vopen_uri_cmd = 'open'
|
|
|
|
} $else $if freebsd || openbsd {
|
|
|
|
vopen_uri_cmd = 'xdg-open'
|
|
|
|
} $else $if linux {
|
2022-06-29 11:59:25 +03:00
|
|
|
providers := ['xdg-open', 'x-www-browser', 'www-browser', 'wslview', 'exo-open']
|
2021-10-06 06:29:32 +03:00
|
|
|
// There are multiple possible providers to open a browser on linux
|
|
|
|
// One of them is xdg-open, another is x-www-browser, then there's www-browser, etc.
|
|
|
|
// Look for one that exists and run it
|
|
|
|
for provider in providers {
|
|
|
|
if exists_in_system_path(provider) {
|
|
|
|
vopen_uri_cmd = provider
|
|
|
|
break
|
2021-09-09 10:48:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-06 06:29:32 +03:00
|
|
|
}
|
|
|
|
if vopen_uri_cmd == '' {
|
2021-09-09 10:48:53 +03:00
|
|
|
return error('unsupported platform')
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
result := execute('${vopen_uri_cmd} "${uri}"')
|
2021-10-06 06:29:32 +03:00
|
|
|
if result.exit_code != 0 {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('unable to open url: ${result.output}')
|
2021-10-06 06:29:32 +03:00
|
|
|
}
|
2021-09-09 10:48:53 +03:00
|
|
|
}
|