os: implement support for VOPEN_URI_CMD env override for os.open_uri
							parent
							
								
									2526aca75f
								
							
						
					
					
						commit
						5bc8b4dadb
					
				|  | @ -1,32 +1,30 @@ | ||||||
| module os | module os | ||||||
| 
 | 
 | ||||||
| pub fn open_uri(uri string) ? { | pub fn open_uri(uri string) ? { | ||||||
| 	$if macos { | 	mut vopen_uri_cmd := getenv('VOPEN_URI_CMD') | ||||||
| 		result := execute('open "$uri"') | 	if vopen_uri_cmd == '' { | ||||||
| 		if result.exit_code != 0 { | 		$if macos { | ||||||
| 			return error('unable to open url: $result.output') | 			vopen_uri_cmd = 'open' | ||||||
| 		} | 		} $else $if freebsd || openbsd { | ||||||
| 	} $else $if freebsd || openbsd { | 			vopen_uri_cmd = 'xdg-open' | ||||||
| 		result := execute('xdg-open "$uri"') | 		} $else $if linux { | ||||||
| 		if result.exit_code != 0 { | 			providers := ['xdg-open', 'x-www-browser', 'www-browser', 'wslview'] | ||||||
| 			return error('unable to open url: $result.output') | 			// 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.
 | ||||||
| 	} $else $if linux { | 			// Look for one that exists and run it
 | ||||||
| 		providers := ['xdg-open', 'x-www-browser', 'www-browser', 'wslview'] | 			for provider in providers { | ||||||
| 
 | 				if exists_in_system_path(provider) { | ||||||
| 		// There are multiple possible providers to open a browser on linux
 | 					vopen_uri_cmd = provider | ||||||
| 		// One of them is xdg-open, another is x-www-browser, then there's www-browser, etc.
 | 					break | ||||||
| 		// 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 { | 	} | ||||||
|  | 	if vopen_uri_cmd == '' { | ||||||
| 		return error('unsupported platform') | 		return error('unsupported platform') | ||||||
| 	} | 	} | ||||||
|  | 	result := execute('$vopen_uri_cmd "$uri"') | ||||||
|  | 	if result.exit_code != 0 { | ||||||
|  | 		return error('unable to open url: $result.output') | ||||||
|  | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -5,6 +5,14 @@ import dl | ||||||
| type ShellExecuteWin = fn (voidptr, &u16, &u16, &u16, &u16, int) | type ShellExecuteWin = fn (voidptr, &u16, &u16, &u16, &u16, int) | ||||||
| 
 | 
 | ||||||
| pub fn open_uri(uri string) ? { | pub fn open_uri(uri string) ? { | ||||||
|  | 	mut vopen_uri_cmd := getenv('VOPEN_URI_CMD') | ||||||
|  | 	if vopen_uri_cmd != '' { | ||||||
|  | 		result := execute('$vopen_uri_cmd "$uri"') | ||||||
|  | 		if result.exit_code != 0 { | ||||||
|  | 			return error('unable to open url: $result.output') | ||||||
|  | 		} | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
| 	handle := dl.open_opt('shell32', dl.rtld_now) ? | 	handle := dl.open_opt('shell32', dl.rtld_now) ? | ||||||
| 	// https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutew
 | 	// https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutew
 | ||||||
| 	func := ShellExecuteWin(dl.sym_opt(handle, 'ShellExecuteW') ?) | 	func := ShellExecuteWin(dl.sym_opt(handle, 'ShellExecuteW') ?) | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue