-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for custom commands (#265)
* Adds support for custom commands Supports up to 10 custom commands, defined in the configuration file. The current or selected URL can be passed to the command by placing ${url} in the command string. Default hotkeys are Alt-1 through Alt-0 for triggering the command with the selected link URL as a parameter, or Alt-Shift-1 through Alt-Shift-0 for triggering the command with the current page URL as a parameter. This follows the convention set by the copy page URL (C) and copy selected URL (c) defaults. Custom commands must be available in the $PATH or use absolute paths. Relative paths are not supported, neither are pipes or shell redirections. * Fix linting issues * Fix typo * Further clarify comment * Use goroutines for Error/Info calls * Run go fmt to fix issue from merge --------- Co-authored-by: makeworld <[email protected]>
- Loading branch information
Showing
9 changed files
with
282 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package command | ||
|
||
import ( | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
// RunCommand runs `command`, replacing the string "${url}" with `url`. | ||
func RunCommand(command string, url string) (string, error) { | ||
cmdWithURL := strings.ReplaceAll(command, "${url}", url) | ||
cmdSplit := strings.SplitN(cmdWithURL, " ", 2) | ||
if len(cmdSplit) > 1 { | ||
if err := exec.Command(cmdSplit[0], cmdSplit[1]).Start(); err != nil { | ||
return "", err | ||
} | ||
return "Ran command " + cmdSplit[0] + " with args " + cmdSplit[1], nil | ||
} | ||
if err := exec.Command(cmdWithURL).Start(); err != nil { | ||
return "", err | ||
} | ||
return "Ran command " + cmdWithURL, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package display | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/makeworld-the-better-one/amfora/command" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// CustomCommand runs custom commands as defined in the app configuration. | ||
// Commands are zero-indexed, so 0 is command1 and 9 is command0 (10). | ||
func CustomCommand(num int, url string) { | ||
if num < 0 { | ||
num = 0 | ||
} | ||
num++ | ||
if num > 9 { | ||
num = 0 | ||
} | ||
|
||
cmd := viper.GetString("commands.command" + strconv.Itoa(num)) | ||
if len(cmd) > 0 { | ||
msg, err := command.RunCommand(cmd, url) | ||
if err != nil { | ||
go Error("Command Error", err.Error()) | ||
return | ||
} | ||
go Info(msg) | ||
} else { | ||
go Error("Command Error", "Command "+strconv.Itoa(num)+" not defined") | ||
return | ||
} | ||
|
||
App.Draw() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.