summaryrefslogtreecommitdiff
path: root/doEditor.go
diff options
context:
space:
mode:
authorCastor Regex <[email protected]>2025-08-25 11:49:30 -0500
committerJeff Carr <[email protected]>2025-08-25 11:49:30 -0500
commit4f215037a1d89d638aed35ab6805b97128ba8876 (patch)
treeec4d751cee821970ce10191b7847977138525ea6 /doEditor.go
parentf5b923f18047646968389e60cf412486ba85bd9c (diff)
feat(editor): wait for ready file and rename to doEditor.go
Diffstat (limited to 'doEditor.go')
-rw-r--r--doEditor.go81
1 files changed, 81 insertions, 0 deletions
diff --git a/doEditor.go b/doEditor.go
new file mode 100644
index 0000000..e928f5a
--- /dev/null
+++ b/doEditor.go
@@ -0,0 +1,81 @@
+package main
+
+import (
+ "io/ioutil"
+ "os"
+ "os/exec"
+ "strings"
+ "time"
+
+ "go.wit.com/log"
+)
+
+func doEditor() error {
+ for {
+ filename, err := doEditorOnce()
+ if err != nil {
+ return err
+ }
+ log.Info("filename:", filename)
+
+ for {
+ _, err := os.Stat("/tmp/regex.ready")
+ if err == nil {
+ break
+ }
+ time.Sleep(100 * time.Millisecond)
+ }
+ // read in regex.ready exists (should be SessionID)
+ // Println session ID
+ content, err := ioutil.ReadFile("/tmp/regex.ready")
+ if err != nil {
+ log.Error(err)
+ }
+ log.Infof("SessionID: %s", string(content))
+ }
+}
+
+func doEditorOnce() (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
+}