|
| 1 | +package chat |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "runtime" |
| 8 | + "strings" |
| 9 | + |
| 10 | + tea "charm.land/bubbletea/v2" |
| 11 | + |
| 12 | + "github.com/docker/cagent/pkg/tui/components/notification" |
| 13 | + "github.com/docker/cagent/pkg/tui/core" |
| 14 | +) |
| 15 | + |
| 16 | +// openExternalEditor opens the current editor content in an external editor. |
| 17 | +// It suspends the TUI, runs the editor, and returns the result. |
| 18 | +func (p *chatPage) openExternalEditor() tea.Cmd { |
| 19 | + content := p.editor.Value() |
| 20 | + |
| 21 | + // Create a temporary file with the current content |
| 22 | + tmpFile, err := os.CreateTemp("", "cagent-*.md") |
| 23 | + if err != nil { |
| 24 | + return core.CmdHandler(notification.ShowMsg{ |
| 25 | + Text: fmt.Sprintf("Failed to create temp file: %v", err), |
| 26 | + Type: notification.TypeError, |
| 27 | + }) |
| 28 | + } |
| 29 | + tmpPath := tmpFile.Name() |
| 30 | + |
| 31 | + if _, err := tmpFile.WriteString(content); err != nil { |
| 32 | + tmpFile.Close() |
| 33 | + os.Remove(tmpPath) |
| 34 | + return core.CmdHandler(notification.ShowMsg{ |
| 35 | + Text: fmt.Sprintf("Failed to write temp file: %v", err), |
| 36 | + Type: notification.TypeError, |
| 37 | + }) |
| 38 | + } |
| 39 | + tmpFile.Close() |
| 40 | + |
| 41 | + // Get the editor command (VISUAL, EDITOR, or platform default) |
| 42 | + editorCmd := os.Getenv("VISUAL") |
| 43 | + if editorCmd == "" { |
| 44 | + editorCmd = os.Getenv("EDITOR") |
| 45 | + } |
| 46 | + if editorCmd == "" { |
| 47 | + if runtime.GOOS == "windows" { |
| 48 | + editorCmd = "notepad" |
| 49 | + } else { |
| 50 | + editorCmd = "vim" |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // Parse editor command (may include arguments like "code --wait") |
| 55 | + parts := strings.Fields(editorCmd) |
| 56 | + args := append(parts[1:], tmpPath) |
| 57 | + cmd := exec.Command(parts[0], args...) |
| 58 | + |
| 59 | + // Use tea.ExecProcess to properly suspend the TUI and run the editor |
| 60 | + return tea.ExecProcess(cmd, func(err error) tea.Msg { |
| 61 | + if err != nil { |
| 62 | + os.Remove(tmpPath) |
| 63 | + return core.CmdHandler(notification.ShowMsg{ |
| 64 | + Type: notification.TypeError, |
| 65 | + Text: fmt.Sprintf("Editor error: %v", err), |
| 66 | + }) |
| 67 | + } |
| 68 | + |
| 69 | + updatedContent, readErr := os.ReadFile(tmpPath) |
| 70 | + os.Remove(tmpPath) |
| 71 | + |
| 72 | + if readErr != nil { |
| 73 | + return core.CmdHandler(notification.ShowMsg{ |
| 74 | + Text: fmt.Sprintf("Failed to read edited file: %v", readErr), |
| 75 | + Type: notification.TypeError, |
| 76 | + }) |
| 77 | + } |
| 78 | + |
| 79 | + // Trim trailing newline that editors often add |
| 80 | + content := strings.TrimSuffix(string(updatedContent), "\n") |
| 81 | + |
| 82 | + // If content is empty, just clear the editor |
| 83 | + if strings.TrimSpace(content) == "" { |
| 84 | + p.editor.SetValue("") |
| 85 | + return nil |
| 86 | + } |
| 87 | + |
| 88 | + // Clear the editor and automatically submit the content |
| 89 | + p.editor.SetValue(content) |
| 90 | + return nil |
| 91 | + }) |
| 92 | +} |
0 commit comments