MCPcopy Create free account
hub / github.com/PerpetualSoftware/pad / OpenInEditor

Function OpenInEditor

internal/cli/editor.go:14–59  ·  view source on GitHub ↗

OpenInEditor opens a temp file with content in the user's editor, waits for the editor to close, and returns the (possibly modified) content.

(cfg *config.Config, content, suffix string)

Source from the content-addressed store, hash-verified

12// OpenInEditor opens a temp file with content in the user's editor,
13// waits for the editor to close, and returns the (possibly modified) content.
14func OpenInEditor(cfg *config.Config, content, suffix string) (string, error) {
15 editor := cfg.Editor
16 if editor == "" {
17 editor = os.Getenv("EDITOR")
18 }
19 if editor == "" {
20 editor = os.Getenv("VISUAL")
21 }
22 if editor == "" {
23 editor = "vi"
24 }
25
26 // Create temp file
27 tmpFile, err := os.CreateTemp("", "pad-*"+suffix)
28 if err != nil {
29 return "", fmt.Errorf("create temp file: %w", err)
30 }
31 tmpPath := tmpFile.Name()
32 defer os.Remove(tmpPath)
33
34 if _, err := tmpFile.WriteString(content); err != nil {
35 tmpFile.Close()
36 return "", fmt.Errorf("write temp file: %w", err)
37 }
38 tmpFile.Close()
39
40 // Parse editor command (may have flags like "code --wait")
41 parts := strings.Fields(editor)
42 args := append(parts[1:], tmpPath)
43 cmd := exec.Command(parts[0], args...)
44 cmd.Stdin = os.Stdin
45 cmd.Stdout = os.Stdout
46 cmd.Stderr = os.Stderr
47
48 if err := cmd.Run(); err != nil {
49 return "", fmt.Errorf("editor exited with error: %w", err)
50 }
51
52 // Read modified content
53 data, err := os.ReadFile(tmpPath)
54 if err != nil {
55 return "", fmt.Errorf("read temp file: %w", err)
56 }
57
58 return string(data), nil
59}
60
61// ParseFrontmatter splits content into YAML frontmatter and body.
62// Returns frontmatter map and body content.

Callers 1

editCmdFunction · 0.92

Calls 2

RunMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected