Support flags when using the editor

Added --editorflags alongside --editor
$VISUAL and $EDITOR are split on whitespace
This commit is contained in:
morganamilo 2018-04-09 20:50:18 +01:00
parent 13ef6f66ab
commit f1e98e45a6
No known key found for this signature in database
GPG key ID: 6FE9E7996B0B082E
4 changed files with 23 additions and 11 deletions

2
cmd.go
View file

@ -261,6 +261,8 @@ func handleConfig(option, value string) bool {
config.BuildDir = value
case "editor":
config.Editor = value
case "editorflags":
config.EditorFlags = value
case "makepkg":
config.MakepkgBin = value
case "pacman":

View file

@ -7,6 +7,7 @@ import (
"fmt"
"os"
"os/exec"
"strings"
alpm "github.com/jguer/go-alpm"
)
@ -28,6 +29,7 @@ const (
type Configuration struct {
BuildDir string `json:"buildDir"`
Editor string `json:"editor"`
EditorFlags string `json:"editorflags"`
MakepkgBin string `json:"makepkgbin"`
PacmanBin string `json:"pacmanbin"`
PacmanConf string `json:"pacmanconf"`
@ -131,6 +133,7 @@ func defaultSettings(config *Configuration) {
config.BuildDir = cacheHome + "/"
config.CleanAfter = false
config.Editor = ""
config.EditorFlags = ""
config.Devel = false
config.MakepkgBin = "makepkg"
config.NoConfirm = false
@ -154,30 +157,32 @@ func defaultSettings(config *Configuration) {
}
// Editor returns the preferred system editor.
func editor() string {
func editor() (string, []string) {
switch {
case config.Editor != "":
editor, err := exec.LookPath(config.Editor)
if err != nil {
fmt.Println(err)
} else {
return editor
return editor, strings.Fields(config.EditorFlags)
}
fallthrough
case os.Getenv("EDITOR") != "":
editor, err := exec.LookPath(os.Getenv("EDITOR"))
editorArgs := strings.Fields(os.Getenv("EDITOR"))
editor, err := exec.LookPath(editorArgs[0])
if err != nil {
fmt.Println(err)
} else {
return editor
return editor, editorArgs[1:]
}
fallthrough
case os.Getenv("VISUAL") != "":
editor, err := exec.LookPath(os.Getenv("VISUAL"))
editorArgs := strings.Fields(os.Getenv("VISUAL"))
editor, err := exec.LookPath(editorArgs[0])
if err != nil {
fmt.Println(err)
} else {
return editor
return editor, editorArgs[1:]
}
fallthrough
default:
@ -187,19 +192,20 @@ func editor() string {
editorLoop:
fmt.Print(green("Edit PKGBUILD with:"))
var editorInput string
_, err := fmt.Scanln(&editorInput)
editorInput, err := getInput("")
if err != nil {
fmt.Println(err)
goto editorLoop
}
editor, err := exec.LookPath(editorInput)
editorArgs := strings.Fields(editorInput)
editor, err := exec.LookPath(editorArgs[0])
if err != nil {
fmt.Println(err)
goto editorLoop
}
return editor
return editor, editorArgs[1:]
}
}

View file

@ -462,7 +462,9 @@ func editPkgBuilds(pkgs []*rpc.Pkg) error {
pkgbuilds = append(pkgbuilds, dir+"PKGBUILD")
}
editcmd := exec.Command(editor(), pkgbuilds...)
editor, editorArgs := editor()
editorArgs = append(editorArgs, pkgbuilds...)
editcmd := exec.Command(editor, editorArgs...)
editcmd.Stdin, editcmd.Stdout, editcmd.Stderr = os.Stdin, os.Stdout, os.Stderr
err := editcmd.Run()
if err != nil {

View file

@ -433,6 +433,8 @@ func hasParam(arg string) bool {
return true
case "editor":
return true
case "editorflags":
return true
case "makepkg":
return true
case "pacman":