diff --git a/cmd/picoclaw/main.go b/cmd/picoclaw/main.go index b82475905..020adcce8 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" @@ -50,9 +52,10 @@ func NewPicoclawCommand() *cobra.Command { } const ( - colorBlue = "\033[1;38;2;62;93;185m" - colorRed = "\033[1;38;2;213;70;70m" - banner = "\r\n" + + colorBlue = "\033[1;38;2;62;93;185m" + colorRed = "\033[1;38;2;213;70;70m" + noBannerEnv = "PICOCLAW_NO_BANNER" + banner = "\r\n" + colorBlue + "██████╗ ██╗ ██████╗ ██████╗ " + colorRed + " ██████╗██╗ █████╗ ██╗ ██╗\n" + colorBlue + "██╔══██╗██║██╔════╝██╔═══██╗" + colorRed + "██╔════╝██║ ██╔══██╗██║ ██║\n" + colorBlue + "██████╔╝██║██║ ██║ ██║" + colorRed + "██║ ██║ ███████║██║ █╗ ██║\n" + @@ -62,8 +65,35 @@ const ( "\033[0m\r\n" ) +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 e622675ee..0096988af 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" @@ -55,3 +56,27 @@ 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)) + }) +}