1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

os: add open_uri/1, use it in v bug (#11450)

This commit is contained in:
Enzo
2021-09-09 09:48:53 +02:00
committed by GitHub
parent 4faa0f8487
commit e57b068df0
3 changed files with 46 additions and 42 deletions

View File

@@ -0,0 +1,32 @@
module os
pub fn open_uri(uri string) ? {
$if macos {
result := execute('open "$uri"')
if result.exit_code != 0 {
return error('unable to open url: $result.output')
}
} $else $if freebsd || openbsd {
result := execute('xdg-open "$uri"')
if result.exit_code != 0 {
return error('unable to open url: $result.output')
}
} $else $if linux {
providers := ['xdg-open', 'x-www-browser', 'www-browser', 'wslview']
// 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) {
result := execute('$provider "$uri"')
if result.exit_code != 0 {
return error('unable to open url: $result.output')
}
break
}
}
} $else {
return error('unsupported platform')
}
}

View File

@@ -0,0 +1,13 @@
module os
import dl
type ShellExecuteWin = fn (voidptr, &u16, &u16, &u16, &u16, int)
pub fn open_uri(uri string) ? {
handle := dl.open_opt('shell32', dl.rtld_now) ?
// https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutew
func := ShellExecuteWin(dl.sym_opt(handle, 'ShellExecuteW') ?)
func(C.NULL, 'open'.to_wide(), uri.to_wide(), C.NULL, C.NULL, C.SW_SHOWNORMAL)
dl.close(handle)
}