fix: suppress banner in completion and redirected output

This commit is contained in:
XYSK-lilong007 2026-03-12 01:21:29 +08:00
parent 4a8a2e9c23
commit df19b8c708
2 changed files with 59 additions and 4 deletions

View file

@ -9,8 +9,10 @@ package main
import ( import (
"fmt" "fmt"
"os" "os"
"strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"golang.org/x/term"
"github.com/sipeed/picoclaw/cmd/picoclaw/internal" "github.com/sipeed/picoclaw/cmd/picoclaw/internal"
"github.com/sipeed/picoclaw/cmd/picoclaw/internal/agent" "github.com/sipeed/picoclaw/cmd/picoclaw/internal/agent"
@ -52,6 +54,7 @@ func NewPicoclawCommand() *cobra.Command {
const ( const (
colorBlue = "\033[1;38;2;62;93;185m" colorBlue = "\033[1;38;2;62;93;185m"
colorRed = "\033[1;38;2;213;70;70m" colorRed = "\033[1;38;2;213;70;70m"
noBannerEnv = "PICOCLAW_NO_BANNER"
banner = "\r\n" + banner = "\r\n" +
colorBlue + "██████╗ ██╗ ██████╗ ██████╗ " + colorRed + " ██████╗██╗ █████╗ ██╗ ██╗\n" + colorBlue + "██████╗ ██╗ ██████╗ ██████╗ " + colorRed + " ██████╗██╗ █████╗ ██╗ ██╗\n" +
colorBlue + "██╔══██╗██║██╔════╝██╔═══██╗" + colorRed + "██╔════╝██║ ██╔══██╗██║ ██║\n" + colorBlue + "██╔══██╗██║██╔════╝██╔═══██╗" + colorRed + "██╔════╝██║ ██╔══██╗██║ ██║\n" +
@ -62,8 +65,35 @@ const (
"\033[0m\r\n" "\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() { func main() {
if shouldPrintBanner(os.Args, term.IsTerminal(int(os.Stdout.Fd()))) {
fmt.Printf("%s", banner) fmt.Printf("%s", banner)
}
cmd := NewPicoclawCommand() cmd := NewPicoclawCommand()
if err := cmd.Execute(); err != nil { if err := cmd.Execute(); err != nil {
os.Exit(1) os.Exit(1)

View file

@ -5,6 +5,7 @@ import (
"slices" "slices"
"testing" "testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -55,3 +56,27 @@ func TestNewPicoclawCommand(t *testing.T) {
assert.False(t, subcmd.Hidden) 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))
})
}