Accept spaces in paths for url-handlers config

Fixes #214
This commit is contained in:
makeworld 2021-12-07 15:56:12 -05:00
parent 97ee1aa368
commit 043242392c
4 changed files with 67 additions and 25 deletions

View File

@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Redirects occur automatically if it only adds a trailing slash (#271)
- Non-gemini links are underlined by default to help color blind users (#189)
- Text and element colors of default theme change to be black on white terminals (#181)
- Support paths with spaces in `[url-handlers]` config settings (#214)
- Display info modal when opening URL with custom application
### Changed
- Bookmarks are stored using XML in the XBEL format, old bookmarks are transferred (#68)

View File

@ -37,7 +37,7 @@ auto_redirect = false
# If a command is set, than the URL will be added (in quotes) to the end of the command.
# A space will be prepended to the URL.
#
# The best to define a command is using a string array.
# The best way to define a command is using a string array.
# Examples:
# http = ['firefox']
# http = ['custom-browser', '--flag', '--option=2']
@ -185,19 +185,28 @@ underline = true
[url-handlers]
# Allows setting the commands to run for various URL schemes.
# E.g. to open FTP URLs with FileZilla set the following key:
# ftp = 'filezilla'
# You can set any scheme to "off" or "" to disable handling it, or
# ftp = ['filezilla']
# You can set any scheme to 'off' or '' to disable handling it, or
# just leave the key unset.
#
# DO NOT use this for setting the HTTP command.
# Use the http setting in the "a-general" section above.
#
# NOTE: These settings are overrided by the ones in the proxies section.
#
# The best way to define a command is using a string array.
# Examples:
# magnet = ['transmission']
# foo = ['custom-browser', '--flag', '--option=2']
# tel = ['/path/with spaces/in it/telephone']
#
# Note the use of single quotes, so that backslashes will not be escaped.
# Using just a string will also work, but it is deprecated, and will degrade if
# you use paths with spaces.
# This is a special key that defines the handler for all URL schemes for which
# no handler is defined.
# It uses the special value "default", which will try and use the default
# It uses the special value 'default', which will try and use the default
# application on your computer for opening this kind of URI.
other = 'default'

View File

@ -34,7 +34,7 @@ auto_redirect = false
# If a command is set, than the URL will be added (in quotes) to the end of the command.
# A space will be prepended to the URL.
#
# The best to define a command is using a string array.
# The best way to define a command is using a string array.
# Examples:
# http = ['firefox']
# http = ['custom-browser', '--flag', '--option=2']
@ -182,19 +182,28 @@ underline = true
[url-handlers]
# Allows setting the commands to run for various URL schemes.
# E.g. to open FTP URLs with FileZilla set the following key:
# ftp = 'filezilla'
# You can set any scheme to "off" or "" to disable handling it, or
# ftp = ['filezilla']
# You can set any scheme to 'off' or '' to disable handling it, or
# just leave the key unset.
#
# DO NOT use this for setting the HTTP command.
# Use the http setting in the "a-general" section above.
#
# NOTE: These settings are overrided by the ones in the proxies section.
#
# The best way to define a command is using a string array.
# Examples:
# magnet = ['transmission']
# foo = ['custom-browser', '--flag', '--option=2']
# tel = ['/path/with spaces/in it/telephone']
#
# Note the use of single quotes, so that backslashes will not be escaped.
# Using just a string will also work, but it is deprecated, and will degrade if
# you use paths with spaces.
# This is a special key that defines the handler for all URL schemes for which
# no handler is defined.
# It uses the special value "default", which will try and use the default
# It uses the special value 'default', which will try and use the default
# application on your computer for opening this kind of URI.
other = 'default'

View File

@ -57,6 +57,7 @@ func handleHTTP(u string, showInfo bool) bool {
Error("HTTP Error", "Error executing custom browser command: "+err.Error())
return false
}
Info("Opened with: " + config.HTTPCommand[0])
App.Draw()
return true
@ -69,28 +70,49 @@ func handleOther(u string) {
parsed, _ := url.Parse(u)
// Search for a handler for the URL scheme
handler := strings.TrimSpace(viper.GetString("url-handlers." + parsed.Scheme))
handler := viper.GetStringSlice("url-handlers." + parsed.Scheme)
if len(handler) == 0 {
handler = strings.TrimSpace(viper.GetString("url-handlers.other"))
// A string and not a list of strings, use old method of parsing
// #214
handler = strings.Fields(viper.GetString("url-handlers." + parsed.Scheme))
if len(handler) == 0 {
handler = viper.GetStringSlice("url-handlers.other")
if len(handler) == 0 {
handler = strings.Fields(viper.GetString("url-handlers.other"))
}
}
}
switch handler {
case "", "off":
Error("URL Error", "Opening "+parsed.Scheme+" URLs is turned off.")
case "default":
_, err := sysopen.Open(u)
if err != nil {
Error("Application Error", err.Error())
if len(handler) == 1 {
// Maybe special key
switch strings.TrimSpace(handler[0]) {
case "", "off":
Error("URL Error", "Opening "+parsed.Scheme+" URLs is turned off.")
return
case "default":
_, err := sysopen.Open(u)
if err != nil {
Error("Application Error", err.Error())
return
}
Info("Opened in default application")
return
}
Info("Opened in default application")
default:
// The config has a custom command to execute for URLs
fields := strings.Fields(handler)
err := exec.Command(fields[0], append(fields[1:], u)...).Start()
if err != nil {
Error("URL Error", "Error executing custom command: "+err.Error())
}
}
// Custom application command
var err error
if len(handler) > 1 {
err = exec.Command(handler[0], append(handler[1:], u)...).Start()
} else {
err = exec.Command(handler[0], u).Start()
}
if err != nil {
Error("URL Error", "Error executing custom command: "+err.Error())
}
Info("Opened with: " + handler[0])
App.Draw()
}