Kaynağa Gözat

feat(tui): support pipe into tui (#1230)

Aiden Cline 7 ay önce
ebeveyn
işleme
38ae7d60aa
1 değiştirilmiş dosya ile 25 ekleme ve 0 silme
  1. 25 0
      packages/tui/cmd/opencode/main.go

+ 25 - 0
packages/tui/cmd/opencode/main.go

@@ -3,6 +3,7 @@ package main
 import (
 	"context"
 	"encoding/json"
+	"io"
 	"log/slog"
 	"os"
 	"os/signal"
@@ -51,6 +52,30 @@ func main() {
 		os.Exit(1)
 	}
 
+	stat, err := os.Stdin.Stat()
+	if err != nil {
+		slog.Error("Failed to stat stdin", "error", err)
+		os.Exit(1)
+	}
+
+	// Check if there's data piped to stdin
+	if (stat.Mode() & os.ModeCharDevice) == 0 {
+		stdin, err := io.ReadAll(os.Stdin)
+		if err != nil {
+			slog.Error("Failed to read stdin", "error", err)
+			os.Exit(1)
+		}
+		stdinContent := strings.TrimSpace(string(stdin))
+		if stdinContent != "" {
+			if prompt == nil || *prompt == "" {
+				prompt = &stdinContent
+			} else {
+				combined := *prompt + "\n" + stdinContent
+				prompt = &combined
+			}
+		}
+	}
+
 	httpClient := opencode.NewClient(
 		option.WithBaseURL(url),
 	)