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:
32
vlib/os/open_uri_default.c.v
Normal file
32
vlib/os/open_uri_default.c.v
Normal 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')
|
||||
}
|
||||
}
|
13
vlib/os/open_uri_windows.c.v
Normal file
13
vlib/os/open_uri_windows.c.v
Normal 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)
|
||||
}
|
Reference in New Issue
Block a user