Merge PR #1376
This commit is contained in:
commit
bc9c2f4d41
2 changed files with 76 additions and 1 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue