add support for editors that need arguments
This commit is contained in:
parent
e96bac3d9d
commit
f4df6f35eb
4 changed files with 45 additions and 20 deletions
|
@ -1,7 +1,6 @@
|
|||
package editor
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
@ -9,14 +8,17 @@ import (
|
|||
"git.burning.moe/celediel/agedit/pkg/tmpfile"
|
||||
)
|
||||
|
||||
// EditFile opens the specified file in the configured editor
|
||||
func EditFile(editor, filename string) error {
|
||||
if editor == "" {
|
||||
return errors.New("editor not set")
|
||||
}
|
||||
type Editor struct {
|
||||
Command string
|
||||
Args []string
|
||||
generator tmpfile.Generator
|
||||
}
|
||||
|
||||
// TODO: handle editors that require arguments
|
||||
cmd := exec.Command(editor, filename)
|
||||
// EditFile opens the specified file in the configured editor
|
||||
func (e *Editor) EditFile(filename string) error {
|
||||
args := append(e.Args, filename)
|
||||
|
||||
cmd := exec.Command(e.Command, args...)
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
@ -30,7 +32,7 @@ func EditFile(editor, filename string) error {
|
|||
|
||||
// EditTempFile creates a temporary file with a random name, opens it in the
|
||||
// editor, and returns the byte slice of its contents.
|
||||
func EditTempFile(editor, start, prefix, suffix string, filename_length int) ([]byte, error) {
|
||||
func (e *Editor) EditTempFile(start string) ([]byte, error) {
|
||||
var (
|
||||
filename string
|
||||
bytes []byte
|
||||
|
@ -38,10 +40,7 @@ func EditTempFile(editor, start, prefix, suffix string, filename_length int) ([]
|
|||
file *os.File
|
||||
)
|
||||
|
||||
// generator := tmpfile.NewGenerator("agedit_", ".txt", 13)
|
||||
generator := tmpfile.NewGenerator(prefix, suffix, filename_length)
|
||||
|
||||
filename = generator.GenerateFullPath()
|
||||
filename = e.generator.GenerateFullPath()
|
||||
if file, err = os.Create(filename); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -50,7 +49,7 @@ func EditTempFile(editor, start, prefix, suffix string, filename_length int) ([]
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if err = EditFile(editor, filename); err != nil {
|
||||
if err = e.EditFile(filename); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
@ -68,3 +67,14 @@ func EditTempFile(editor, start, prefix, suffix string, filename_length int) ([]
|
|||
|
||||
return bytes, nil
|
||||
}
|
||||
|
||||
// New returns an Editor configured to open files with `command` + `args`.
|
||||
// The prefix and suffix will be added to the randomly generated
|
||||
// filename of `length` characters.
|
||||
func New(command string, args []string, prefix, suffix string, length int) Editor {
|
||||
return Editor{
|
||||
Command: command,
|
||||
Args: args,
|
||||
generator: tmpfile.NewGenerator(prefix, suffix, length),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue