v / vlib / os
Raw file | 31 loc (30 sloc) | 862 bytes | Latest commit hash 017ace6ea
1module os
2
3// open_uri opens a given uri.
4pub fn open_uri(uri string) ! {
5 mut vopen_uri_cmd := getenv('VOPEN_URI_CMD')
6 if vopen_uri_cmd == '' {
7 $if macos {
8 vopen_uri_cmd = 'open'
9 } $else $if freebsd || openbsd {
10 vopen_uri_cmd = 'xdg-open'
11 } $else $if linux {
12 providers := ['xdg-open', 'x-www-browser', 'www-browser', 'wslview', 'exo-open']
13 // There are multiple possible providers to open a browser on linux
14 // One of them is xdg-open, another is x-www-browser, then there's www-browser, etc.
15 // Look for one that exists and run it
16 for provider in providers {
17 if exists_in_system_path(provider) {
18 vopen_uri_cmd = provider
19 break
20 }
21 }
22 }
23 }
24 if vopen_uri_cmd == '' {
25 return error('unsupported platform')
26 }
27 result := execute('${vopen_uri_cmd} "${uri}"')
28 if result.exit_code != 0 {
29 return error('unable to open url: ${result.output}')
30 }
31}