diff --git a/internal/app/app.go b/internal/app/app.go index a49273a2ad96c24127fd14642c51a768d5e20dd0..1027512672d0f14cbc9572153aeedffba571accf 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -5,6 +5,7 @@ import ( "database/sql" "errors" "fmt" + "io" "log/slog" "sync" "time" @@ -103,7 +104,7 @@ func (app *App) Config() *config.Config { // RunNonInteractive runs the application in non-interactive mode with the // given prompt, printing to stdout. -func (app *App) RunNonInteractive(ctx context.Context, prompt string, quiet bool) error { +func (app *App) RunNonInteractive(ctx context.Context, output io.Writer, prompt string, quiet bool) error { slog.Info("Running in non-interactive mode") ctx, cancel := context.WithCancel(ctx) @@ -170,7 +171,7 @@ func (app *App) RunNonInteractive(ctx context.Context, prompt string, quiet bool // Always print a newline at the end. If output is a TTY this will // prevent the prompt from overwriting the last line of output. - _, _ = fmt.Print('\n') + _, _ = fmt.Fprintln(output) }() for { @@ -204,7 +205,7 @@ func (app *App) RunNonInteractive(ctx context.Context, prompt string, quiet bool } part := content[readBytes:] - fmt.Print(part) + fmt.Fprint(output, part) messageReadBytes[msg.ID] = len(content) } diff --git a/internal/cmd/run.go b/internal/cmd/run.go index 4340334712c76b3aac11e8c9fb47b663b8679597..d3748d0f327d0db0913384100a5f885eb19a8c2e 100644 --- a/internal/cmd/run.go +++ b/internal/cmd/run.go @@ -3,6 +3,7 @@ package cmd import ( "fmt" "log/slog" + "os" "strings" "github.com/spf13/cobra" @@ -18,10 +19,13 @@ The prompt can be provided as arguments or piped from stdin.`, crush run Explain the use of context in Go # Pipe input from stdin -echo "What is this code doing?" | crush run +curl https://charm.land | crush run "Summarize this website" -# Run with quiet mode (no spinner) -crush run -q "Generate a README for this project" +# Read from a file +crush run "What is this code doing?" <<< prrr.go + +# Run in quiet mode (hide the spinner) +crush run --quiet "Generate a README for this project" `, RunE: func(cmd *cobra.Command, args []string) error { quiet, _ := cmd.Flags().GetBool("quiet") @@ -54,7 +58,7 @@ crush run -q "Generate a README for this project" // echo "Do something fancy" | crush run > output.txt // // TODO: We currently need to press ^c twice to cancel. Fix that. - return app.RunNonInteractive(cmd.Context(), prompt, quiet) + return app.RunNonInteractive(cmd.Context(), os.Stdout, prompt, quiet) }, }