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..c4eed450ab0878c196d33f1f5510ac02a4d02aa4 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" @@ -54,7 +55,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) }, }