mirror of
https://github.com/charmbracelet/glow
synced 2024-09-30 05:05:24 +00:00
feat: use x/editor (#543)
Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
parent
54dd62a2a4
commit
c991ec475d
|
@ -6,7 +6,7 @@ import (
|
|||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/charmbracelet/glow/editor"
|
||||
"github.com/charmbracelet/x/editor"
|
||||
gap "github.com/muesli/go-app-paths"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
@ -65,7 +65,7 @@ var configCmd = &cobra.Command{
|
|||
return err
|
||||
}
|
||||
|
||||
c, err := editor.Cmd(configFile)
|
||||
c, err := editor.Cmd("Glow", configFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not edit %s: %w", configFile, err)
|
||||
}
|
||||
|
|
|
@ -1,31 +0,0 @@
|
|||
package editor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"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, error) {
|
||||
if os.Getenv("SNAP_REVISION") != "" {
|
||||
return nil, fmt.Errorf("Did you install with Snap? Glow is sandboxed and unable to open an editor. Please install Glow with Go or another package manager to enable editing.")
|
||||
}
|
||||
editor, args := getEditor()
|
||||
return exec.Command(editor, append(args, path)...), nil
|
||||
}
|
||||
|
||||
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{}
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
t.Run("inside snap", func(t *testing.T) {
|
||||
t.Setenv("SNAP_REVISION", "10")
|
||||
got, err := Cmd("foo")
|
||||
if err == nil {
|
||||
t.Fatalf("expected an error, got nil")
|
||||
}
|
||||
if got != nil {
|
||||
t.Fatalf("should have returned nil, got %v", got)
|
||||
}
|
||||
})
|
||||
}
|
1
go.mod
1
go.mod
|
@ -9,6 +9,7 @@ require (
|
|||
github.com/charmbracelet/charm v0.8.7
|
||||
github.com/charmbracelet/glamour v0.6.0
|
||||
github.com/charmbracelet/lipgloss v0.6.0
|
||||
github.com/charmbracelet/x/editor v0.0.0-20231116172829-450eedbca1ab
|
||||
github.com/dustin/go-humanize v1.0.1-0.20200219035652-afde56e7acac
|
||||
github.com/mattn/go-runewidth v0.0.14
|
||||
github.com/meowgorithm/babyenv v1.3.1
|
||||
|
|
2
go.sum
2
go.sum
|
@ -234,6 +234,8 @@ github.com/charmbracelet/glamour v0.6.0/go.mod h1:taqWV4swIMMbWALc0m7AfE9JkPSU8o
|
|||
github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
|
||||
github.com/charmbracelet/lipgloss v0.6.0 h1:1StyZB9vBSOyuZxQUcUwGr17JmojPNm87inij9N3wJY=
|
||||
github.com/charmbracelet/lipgloss v0.6.0/go.mod h1:tHh2wr34xcHjC2HCXIlGSG1jaDF0S0atAUvBMP6Ppuk=
|
||||
github.com/charmbracelet/x/editor v0.0.0-20231116172829-450eedbca1ab h1:95WbogoQheYFuAUy1olU8OgxrHk2K86zA7mSNELiMfU=
|
||||
github.com/charmbracelet/x/editor v0.0.0-20231116172829-450eedbca1ab/go.mod h1:lrin7iXW742pX5pePBEWhLPDTp53YW15r/Lp4Rcfg0M=
|
||||
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
|
||||
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
|
||||
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
|
||||
|
|
|
@ -2,7 +2,7 @@ package ui
|
|||
|
||||
import (
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/glow/editor"
|
||||
"github.com/charmbracelet/x/editor"
|
||||
)
|
||||
|
||||
type editorFinishedMsg struct{ err error }
|
||||
|
@ -12,7 +12,7 @@ func openEditor(path string) tea.Cmd {
|
|||
return editorFinishedMsg{err}
|
||||
}
|
||||
|
||||
editor, err := editor.Cmd(path)
|
||||
editor, err := editor.Cmd("Glow", path)
|
||||
if err != nil {
|
||||
return func() tea.Msg {
|
||||
return errMsg{err}
|
||||
|
|
Loading…
Reference in a new issue