From 043242392cd44f7a58710119751f879e8ea09b5e Mon Sep 17 00:00:00 2001 From: makeworld Date: Tue, 7 Dec 2021 15:56:12 -0500 Subject: [PATCH] Accept spaces in paths for url-handlers config Fixes #214 --- CHANGELOG.md | 2 ++ config/default.go | 17 ++++++++++---- default-config.toml | 17 ++++++++++---- display/handlers.go | 56 +++++++++++++++++++++++++++++++-------------- 4 files changed, 67 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 04d9db3..8eb47a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/config/default.go b/config/default.go index 9855480..b6587d1 100644 --- a/config/default.go +++ b/config/default.go @@ -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' diff --git a/default-config.toml b/default-config.toml index d701365..3369b92 100644 --- a/default-config.toml +++ b/default-config.toml @@ -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' diff --git a/display/handlers.go b/display/handlers.go index 48f2546..e926c6e 100644 --- a/display/handlers.go +++ b/display/handlers.go @@ -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() }