diff --git a/cmd/picoclaw/main.go b/cmd/picoclaw/main.go index bf9c0389f..c2ca4cba6 100644 --- a/cmd/picoclaw/main.go +++ b/cmd/picoclaw/main.go @@ -9,8 +9,10 @@ package main import ( "fmt" "os" + "strings" "github.com/spf13/cobra" + "golang.org/x/term" "github.com/sipeed/picoclaw/cmd/picoclaw/internal" "github.com/sipeed/picoclaw/cmd/picoclaw/internal/agent" @@ -64,8 +66,37 @@ const ( "\033[0m\r\n" ) +const noBannerEnv = "PICOCLAW_NO_BANNER" + +func bannerDisabledByEnv() bool { + value := strings.TrimSpace(strings.ToLower(os.Getenv(noBannerEnv))) + switch value { + case "", "0", "false", "no", "off": + return false + default: + return true + } +} + +func shouldPrintBanner(args []string, stdoutIsTerminal bool) bool { + if bannerDisabledByEnv() || !stdoutIsTerminal { + return false + } + + if len(args) > 1 { + switch args[1] { + case "completion", cobra.ShellCompRequestCmd, cobra.ShellCompNoDescRequestCmd: + return false + } + } + + return true +} + func main() { - fmt.Printf("%s", banner) + if shouldPrintBanner(os.Args, term.IsTerminal(int(os.Stdout.Fd()))) { + fmt.Printf("%s", banner) + } cmd := NewPicoclawCommand() if err := cmd.Execute(); err != nil { os.Exit(1) diff --git a/cmd/picoclaw/main_test.go b/cmd/picoclaw/main_test.go index ad18cb330..e0bff0fe3 100644 --- a/cmd/picoclaw/main_test.go +++ b/cmd/picoclaw/main_test.go @@ -5,6 +5,7 @@ import ( "slices" "testing" + "github.com/spf13/cobra" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -56,3 +57,46 @@ func TestNewPicoclawCommand(t *testing.T) { assert.False(t, subcmd.Hidden) } } + +func TestShouldPrintBanner(t *testing.T) { + t.Run("interactive command prints banner", func(t *testing.T) { + t.Setenv(noBannerEnv, "") + assert.True(t, shouldPrintBanner([]string{"picoclaw", "agent"}, true)) + }) + + t.Run("redirected stdout suppresses banner", func(t *testing.T) { + t.Setenv(noBannerEnv, "") + assert.False(t, shouldPrintBanner([]string{"picoclaw", "agent"}, false)) + }) + + t.Run("completion command suppresses banner", func(t *testing.T) { + t.Setenv(noBannerEnv, "") + assert.False(t, shouldPrintBanner([]string{"picoclaw", "completion", "zsh"}, true)) + assert.False(t, shouldPrintBanner([]string{"picoclaw", cobra.ShellCompRequestCmd}, true)) + assert.False(t, shouldPrintBanner([]string{"picoclaw", cobra.ShellCompNoDescRequestCmd}, true)) + }) + + t.Run("env disables banner", func(t *testing.T) { + t.Setenv(noBannerEnv, "1") + assert.False(t, shouldPrintBanner([]string{"picoclaw", "agent"}, true)) + }) + + t.Run("truthy env values disable banner after normalization", func(t *testing.T) { + for _, value := range []string{"true", "yes", "on", " TrUe ", "\tON\n"} { + t.Run(fmt.Sprintf("%q", value), func(t *testing.T) { + t.Setenv(noBannerEnv, value) + assert.False(t, shouldPrintBanner([]string{"picoclaw", "agent"}, true)) + }) + } + }) + + t.Run("root command still prints banner in terminal", func(t *testing.T) { + t.Setenv(noBannerEnv, "") + assert.True(t, shouldPrintBanner([]string{"picoclaw"}, true)) + }) + + t.Run("unknown subcommand still prints banner in terminal", func(t *testing.T) { + t.Setenv(noBannerEnv, "") + assert.True(t, shouldPrintBanner([]string{"picoclaw", "unknown"}, true)) + }) +}