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

pull/11430/head
Enzo 2021-09-09 09:48:53 +02:00 committed by GitHub
parent 4faa0f8487
commit e57b068df0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 42 deletions

View File

@ -1,4 +1,3 @@
import dl
import net.urllib
import os
import readline
@ -66,46 +65,6 @@ fn get_v_build_output(is_verbose bool, is_yes bool, file_path string) string {
return result.output
}
type ShellExecuteWin = fn (voidptr, &u16, &u16, &u16, &u16, int)
// open a uri using the default associated application
fn open_browser(uri string) ? {
$if macos {
result := os.execute('open "$uri"')
if result.exit_code != 0 {
return error('unable to open url: $result.output')
}
} $else $if freebsd || openbsd {
result := os.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 os.exists_in_system_path(provider) {
result := os.execute('$provider "$uri"')
if result.exit_code != 0 {
return error('unable to open url: $result.output')
}
break
}
}
} $else $if windows {
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)
} $else {
return error('unsupported platform')
}
}
fn ask(msg string) bool {
prompt := os.input_opt('$msg [Y/n] ') or { 'y' }
return prompt == '' || prompt[0].ascii_str().to_lower() != 'n'
@ -199,7 +158,7 @@ $build_output```'
println('Your file is too big to be submitted. Head over to the following URL and attach your file.')
println(generated_uri)
} else {
open_browser(generated_uri) or {
os.open_uri(generated_uri) or {
if is_verbose {
eprintln(err)
}

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)
}