fix: improve editor handling (#449)

* fix: improve editor handling

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* test: add tests

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
Carlos Alexandro Becker 2023-01-24 14:51:11 -03:00 committed by GitHub
commit 8b5468468a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 33 deletions

View file

@ -1,14 +1,12 @@
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"github.com/charmbracelet/glow/editor"
gap "github.com/muesli/go-app-paths"
"github.com/spf13/cobra"
)
@ -32,11 +30,6 @@ var configCmd = &cobra.Command{
Example: paragraph("glow config\nglow config --config path/to/config.yml"),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
editor := strings.Fields(os.Getenv("EDITOR"))
if len(editor) == 0 {
return errors.New("no EDITOR environment variable set")
}
if configFile == "" {
scope := gap.NewScope(gap.User, "glow")
@ -71,11 +64,7 @@ var configCmd = &cobra.Command{
return err
}
var eargs []string
if len(editor) > 1 {
eargs = editor[1:]
}
c := exec.Command(editor[0], append(eargs, configFile)...) // nolint: gosec
c := editor.Cmd(configFile)
c.Stdin = os.Stdin
c.Stdout = os.Stdout
c.Stderr = os.Stderr

27
editor/editor.go Normal file
View file

@ -0,0 +1,27 @@
package editor
import (
"os"
"os/exec"
"strings"
)
const defaultEditor = "nano"
// Cmd returns a *exec.Cmd editing the given path with $EDITOR or nano if no
// $EDITOR is set.
func Cmd(path string) *exec.Cmd {
editor, args := getEditor()
return exec.Command(editor, append(args, path)...)
}
func getEditor() (string, []string) {
editor := strings.Fields(os.Getenv("EDITOR"))
if len(editor) > 1 {
return editor[0], editor[1:]
}
if len(editor) == 1 {
return editor[0], []string{}
}
return defaultEditor, []string{}
}

26
editor/editor_test.go Normal file
View file

@ -0,0 +1,26 @@
package editor
import (
"reflect"
"testing"
)
func TestEditor(t *testing.T) {
filename := "README.md"
for k, v := range map[string][]string{
"": {"nano", filename},
"nvim": {"nvim", filename},
"vim": {"vim", filename},
"vscode --foo": {"vscode", "--foo", filename},
"nvim -a -b": {"nvim", "-a", "-b", filename},
} {
t.Run(k, func(t *testing.T) {
t.Setenv("EDITOR", k)
cmd := Cmd("README.md")
got := cmd.Args
if !reflect.DeepEqual(got, v) {
t.Fatalf("expected %v; got %v", v, got)
}
})
}
}

View file

@ -1,33 +1,15 @@
package ui
import (
"os"
"os/exec"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/glow/editor"
)
const defaultEditor = "nano"
type editorFinishedMsg struct{ err error }
func openEditor(path string) tea.Cmd {
editor, args := getEditor()
cmd := exec.Command(editor, append(args, path)...)
cb := func(err error) tea.Msg {
return editorFinishedMsg{err}
}
return tea.ExecProcess(cmd, cb)
}
func getEditor() (string, []string) {
editor := strings.Fields(os.Getenv("EDITOR"))
if len(editor) > 1 {
return editor[0], editor[1:]
}
if len(editor) == 1 {
return editor[0], []string{}
}
return defaultEditor, []string{}
return tea.ExecProcess(editor.Cmd(path), cb)
}