package tui import ( "os" "os/exec" "strings" tea "github.com/charmbracelet/bubbletea" ) type msgTempfileEditorClosed string // openTempfileEditor opens an $EDITOR on a new temporary file with the given // content. Upon closing, the contents of the file are read back returned // wrapped in a msgTempfileEditorClosed returned by the tea.Cmd func openTempfileEditor(pattern string, content string, placeholder string) tea.Cmd { msgFile, _ := os.CreateTemp("/tmp", pattern) err := os.WriteFile(msgFile.Name(), []byte(placeholder+content), os.ModeAppend) if err != nil { return wrapError(err) } editor := os.Getenv("EDITOR") if editor == "" { editor = "vim" } c := exec.Command(editor, msgFile.Name()) return tea.ExecProcess(c, func(err error) tea.Msg { bytes, err := os.ReadFile(msgFile.Name()) if err != nil { return msgError(err) } os.Remove(msgFile.Name()) fileContents := string(bytes) if strings.HasPrefix(fileContents, placeholder) { fileContents = fileContents[len(placeholder):] } stripped := strings.Trim(fileContents, "\n \t") return msgTempfileEditorClosed(stripped) }) }