diff --git a/editor.go b/editor.go new file mode 100644 index 0000000..9186a16 --- /dev/null +++ b/editor.go @@ -0,0 +1,53 @@ +package main + +import ( + "io/ioutil" + "os" + "os/exec" + "strings" +) + +func doEditor() (string, error) { + // Create a temporary file + tmpfile, err := ioutil.TempFile("", "regex-*.txt") + if err != nil { + return "", err + } + tmpPath := tmpfile.Name() + // Defer removal in case of error, but we might move it + defer os.Remove(tmpPath) + tmpfile.Close() + + // Get the user's editor + editor := os.Getenv("EDITOR") + if editor == "" { + editor = "vim" // default to vim + } + + // Run the editor + cmd := exec.Command(editor, tmpPath) + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + if err := cmd.Run(); err != nil { + return "", err + } + + // Read the file content + content, err := ioutil.ReadFile(tmpPath) + if err != nil { + return "", err + } + + // Check if the file is not empty after trimming space + if strings.TrimSpace(string(content)) != "" { + // Move the file + if err := os.Rename(tmpPath, "/tmp/regex.txt"); err != nil { + return "", err + } + return "/tmp/regex.txt", nil + } + + return "", nil +} diff --git a/junk b/junk deleted file mode 100644 index 8b13789..0000000 --- a/junk +++ /dev/null @@ -1 +0,0 @@ - diff --git a/main.go b/main.go index 0e34bbf..4afcff4 100644 --- a/main.go +++ b/main.go @@ -59,7 +59,7 @@ func main() { } if argv.Editor != nil { - // doEditor() + doEditor() okExit("") }