diff --git a/cmd/root.go b/cmd/root.go index d741b859178e6c524b4b4e3a61863f144840812c..4ba90e0452a104c909ab0539ace93dbf07093e17 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,6 +3,7 @@ package cmd import ( "context" "fmt" + "io" "os" "sync" "time" @@ -18,6 +19,7 @@ import ( "github.com/charmbracelet/crush/internal/tui" "github.com/charmbracelet/crush/internal/version" "github.com/charmbracelet/fang" + "github.com/charmbracelet/x/term" "github.com/spf13/cobra" ) @@ -99,6 +101,12 @@ to assist developers in writing, debugging, and understanding code directly from // Initialize MCP tools early for both modes initMCPTools(ctx, app) + prompt, err = maybePrependStdin(prompt) + if err != nil { + logging.Error("Failed to read stdin: %v", err) + return err + } + // Non-interactive mode if prompt != "" { // Run non-interactive flow using the App method @@ -301,3 +309,21 @@ func init() { return format.SupportedFormats, cobra.ShellCompDirectiveNoFileComp }) } + +func maybePrependStdin(prompt string) (string, error) { + if term.IsTerminal(os.Stdin.Fd()) { + return prompt, nil + } + fi, err := os.Stdin.Stat() + if err != nil { + return prompt, err + } + if fi.Mode()&os.ModeNamedPipe == 0 { + return prompt, nil + } + bts, err := io.ReadAll(os.Stdin) + if err != nil { + return prompt, err + } + return string(bts) + "\n\n" + prompt, nil +} diff --git a/go.mod b/go.mod index 0fec782ed33932c077eef4c5b5fd4b3cca90bce4..075468b28f5b9de74f2c6a85e337f77e9b5b36f5 100644 --- a/go.mod +++ b/go.mod @@ -20,7 +20,7 @@ require ( github.com/charmbracelet/bubbletea/v2 v2.0.0-beta.1 github.com/charmbracelet/fang v0.1.0 github.com/charmbracelet/glamour/v2 v2.0.0-20250516160903-6f1e2c8f9ebe - github.com/charmbracelet/lipgloss/v2 v2.0.0-beta.1.0.20250513162854-28902d027c40 + github.com/charmbracelet/lipgloss/v2 v2.0.0-beta.2.0.20250703152125-8e1c474f8a71 github.com/charmbracelet/x/ansi v0.9.3 github.com/charmbracelet/x/exp/charmtone v0.0.0-20250627134340-c144409e381c github.com/charmbracelet/x/exp/golden v0.0.0-20250207160936-21c02780d27a @@ -81,7 +81,7 @@ require ( github.com/charmbracelet/colorprofile v0.3.1 // indirect github.com/charmbracelet/x/cellbuf v0.0.14-0.20250516160309-24eee56f89fa // indirect github.com/charmbracelet/x/exp/slice v0.0.0-20250611152503-f53cdd7e01ef - github.com/charmbracelet/x/term v0.2.1 // indirect + github.com/charmbracelet/x/term v0.2.1 github.com/charmbracelet/x/windows v0.2.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/disintegration/gift v1.1.2 // indirect