Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 160 additions & 0 deletions app/console/commands/console_color_command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package commands

import (
"errors"
"fmt"
"strings"

"github.com/goravel/framework/contracts/console"
"github.com/goravel/framework/contracts/console/command"
"github.com/goravel/framework/support/color"
supportconsole "github.com/goravel/framework/support/console"
)

type ConsoleColorCommand struct{}

func (r *ConsoleColorCommand) Signature() string {
return "test:console-color"
}

func (r *ConsoleColorCommand) Description() string {
return "Verify color tag rendering through gookit/color (framework#1571)"
}

func (r *ConsoleColorCommand) Extend() command.Extend {
return command.Extend{Category: "console"}
}

const (
// redTestText is the plain text the red test tag must render to.
redTestText = "test"
// redTestTag is the red tag used by the probe and the sample rows.
redTestTag = "<red>" + redTestText + "</>"
)

// tagEscaper swaps tag delimiters for lookalikes in display copies only, so
// ctx.Line prints them literally instead of rendering or clearing them.
var tagEscaper = strings.NewReplacer("<", "‹", ">", "›")

// colorEnabled reports the process-global color state. The framework exposes no
// getter, so probe a tag: gookit clears tags when disabled and emits ANSI when
// enabled.
func colorEnabled() bool {
return color.Sprint(redTestTag) != redTestText
}

func escapeTags(s string) string {
return tagEscaper.Replace(s)
}

// Handle prints every color-tag case in labelled sections so one run verifies
// framework#1571 by hand: prefix printers, tag forms, StripColor,
// TwoColumnDetail width measurement, and the Disable/Enable toggle. Each case
// shows a visual render, and most add the exact rendered bytes with %q. Where
// the unrendered form matters, tag delimiters are shown as ‹ › because ctx.Line
// would otherwise render or clear them.
//
// Whether color is on is detected with a probe, since the framework exposes no
// getter. When the probe says color is off at entry, Handle forces color on,
// re-probes, and restores the entry state before printing anything: a tag that
// is still cleared means the renderer is broken, so it reports framework#1571
// as an error, while a tag that renders once forced on means color was disabled
// by policy or by a latched --no-ansi, which is only reported and returns nil.
// The process-global color state is snapshotted first and restored on return,
// because the toggle section changes it.
func (r *ConsoleColorCommand) Handle(ctx console.Context) error {
wasEnabled := colorEnabled()
defer func() {
if wasEnabled {
color.Enable()
} else {
color.Disable()
}
}()

if !wasEnabled {
// Force color on to tell a policy/latch disable (for example --no-ansi)
// apart from a renderer that is actually broken, then restore the entry
// state so this branch never prints ANSI.
color.Enable()
broken := color.Sprint(redTestTag) == redTestText
color.Disable()

if broken {
ctx.Error("FAIL: tags are still cleared after color.Enable(); rendering is broken (framework#1571).")
ctx.Line(fmt.Sprintf(" input %q -> %q (delimiters shown as ‹ ›)",
escapeTags(redTestTag), color.Sprint(redTestTag)))
return errors.New("tag rendering is off even after color.Enable() (framework#1571)")
}

ctx.Line("Color rendering is disabled by --no-ansi or a prior --no-ansi run; " +
"re-run in a fresh process to verify framework#1571.")
ctx.Line(fmt.Sprintf(" input %q -> %q (delimiters shown as ‹ ›; tag cleared, no ANSI)",
escapeTags(redTestTag), color.Sprint(redTestTag)))
return nil
}

ctx.Line("<fg=yellow;op=bold>Color tag rendering check (framework#1571)</>")
ctx.NewLine()

// 1. Prefix printers render tags through gookit as well; the rendered output
// is the evidence, so this section stays visual.
ctx.Line("<fg=cyan>1. Prefix printers</>")
ctx.Info("<fg=green>Application is healthy</>")
ctx.Warning("<fg=yellow>Queue worker is not running</>")
ctx.Success("<green>Migration finished</>")
ctx.Error("<fg=red>Cache connection failed</>")
ctx.Comment("<fg=gray>Checking cache store</>")
ctx.NewLine()

// 2. Tag forms, each rendered visually and then as exact bytes.
ctx.Line("<fg=cyan>2. Tag forms (visual, then exact rendered bytes)</>")
ctx.Line(fmt.Sprintf(" style tag (red): %s", redTestTag))
ctx.Line(fmt.Sprintf(" style tag bytes: %q", color.Sprint(redTestTag)))
ctx.Line(" fg attribute (green): <fg=green>ok</>")
ctx.Line(fmt.Sprintf(" fg attribute bytes: %q", color.Sprint("<fg=green>ok</>")))
ctx.Line(" bg attribute (blue): <bg=blue>MSG</>")
ctx.Line(fmt.Sprintf(" bg attribute bytes (Sprintf): %q", color.Sprintf("<bg=blue>%s</>", "MSG")))
ctx.Line(fmt.Sprintf(" nested (outer red, inner green): %s", color.Red().Sprint("outer <fg=green>inner</> end")))
ctx.Line(fmt.Sprintf(" nested bytes: %q", color.Red().Sprint("outer <fg=green>inner</> end")))
ctx.NewLine()

// 3. StripColor consumes tags and ANSI alike, including from mixed input.
// Display copies replace the tag delimiters with lookalikes so the raw syntax
// survives ctx.Line; StripColor still receives the real values.
ctx.Line("<fg=cyan>3. color.StripColor (tag delimiters shown as ‹ › -> stripped result)</>")
ctx.Line(fmt.Sprintf(" tagged input: %q -> %q", escapeTags(redTestTag), color.StripColor(redTestTag)))
raw := "\x1b[1mplain\x1b[0m"
ctx.Line(fmt.Sprintf(" raw ANSI input: %q -> %q", raw, color.StripColor(raw)))
mixed := color.Green().Sprint("a ") + "<fg=yellow>b</>"
ctx.Line(fmt.Sprintf(" mixed ANSI + tag: %q -> %q", escapeTags(mixed), color.StripColor(mixed)))
ctx.NewLine()

// 4. TwoColumnDetail measures rendered width, so the tagged second column
// lines up with the pre-colored one and with plain text. Color is guaranteed
// enabled here, so both the rendered bytes and the stripped layout must agree.
ctx.Line("<fg=cyan>4. TwoColumnDetail width measurement</>")
taggedDetail := supportconsole.TwoColumnDetail("<fg=cyan>cache</>", "<fg=green>OK</>")
coloredDetail := supportconsole.TwoColumnDetail(color.Cyan().Sprint("cache"), color.Green().Sprint("OK"))
plainDetail := supportconsole.TwoColumnDetail("cache", "OK")
ctx.Line(fmt.Sprintf(" tagged: %s", taggedDetail))
ctx.Line(fmt.Sprintf(" colored: %s", coloredDetail))
ctx.Line(fmt.Sprintf(" plain: %s", plainDetail))
match := color.Sprint(taggedDetail) == coloredDetail &&
color.StripColor(taggedDetail) == color.StripColor(plainDetail)
ctx.Line(fmt.Sprintf(" layout match: %t", match))
ctx.NewLine()

// 5. Disable/Enable toggle. Kept last so the explicit Enable() cannot change
// how the earlier sections render; the deferred restore fixes the global
// state again on return. This only runs when color was enabled at entry.
ctx.Line("<fg=cyan>5. color.Disable() and color.Enable() toggle</>")
color.Disable()
ctx.Line(fmt.Sprintf(" disabled bytes: %q", color.Sprint(redTestTag)))
ctx.Line(fmt.Sprintf(" disabled visual: %s", redTestTag))
color.Enable()
ctx.Line(fmt.Sprintf(" enabled bytes: %q", color.Sprint(redTestTag)))
ctx.Line(fmt.Sprintf(" enabled visual: %s", redTestTag))

return nil
}
1 change: 1 addition & 0 deletions bootstrap/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ func Commands() []console.Command {
&commands.ConsoleSingleCommand{},
&commands.ConsoleSliceCommand{},
&commands.ConsoleShutdownableCommand{},
&commands.ConsoleColorCommand{},
}
}
80 changes: 40 additions & 40 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/goravel/cos v1.18.0
github.com/goravel/example-proto v0.0.1
github.com/goravel/fiber v1.18.1-0.20260901101548-72e06e6c5635
github.com/goravel/framework v1.18.1-0.20260925125002-93850001fe6f
github.com/goravel/framework v1.18.1-0.20261013093243-4da928651f19
github.com/goravel/gemini v1.18.0
github.com/goravel/gin v1.18.1-0.20260926005436-467f03c48058
github.com/goravel/inertia v1.18.1-0.20260824090129-ac3d472d028c
Expand All @@ -31,20 +31,20 @@ require (
github.com/swaggo/swag v1.16.2
github.com/uber/jaeger-client-go v2.30.0+incompatible
github.com/vektah/gqlparser/v2 v2.5.19
go.opentelemetry.io/otel v1.46.0
go.opentelemetry.io/otel/metric v1.46.0
go.opentelemetry.io/otel/trace v1.46.0
google.golang.org/grpc v1.85.0-dev.0.20260825072537-93e31b48545e
go.opentelemetry.io/otel v1.47.0
go.opentelemetry.io/otel/metric v1.47.0
go.opentelemetry.io/otel/trace v1.47.0
google.golang.org/grpc v1.86.0-dev.0.20261012190323-de20bed5c055
google.golang.org/protobuf v1.36.12
)

require (
atomicgo.dev/cursor v0.2.0 // indirect
atomicgo.dev/keyboard v0.2.9 // indirect
atomicgo.dev/keyboard v0.2.10 // indirect
atomicgo.dev/schedule v0.1.0 // indirect
cloud.google.com/go v0.116.0 // indirect
cloud.google.com/go/auth v0.23.1 // indirect
cloud.google.com/go/compute/metadata v0.9.0 // indirect
cloud.google.com/go/compute/metadata v0.10.0 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/HdrHistogram/hdrhistogram-go v1.1.2 // indirect
github.com/KyleBanks/depth v1.2.1 // indirect
Expand Down Expand Up @@ -134,7 +134,7 @@ require (
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.21 // indirect
github.com/googleapis/gax-go/v2 v2.23.0 // indirect
github.com/gookit/color v1.6.0 // indirect
github.com/gookit/color v1.6.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.30.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/invopop/jsonschema v0.13.0 // indirect
Expand All @@ -158,7 +158,7 @@ require (
github.com/mattn/go-colorable v0.1.15 // indirect
github.com/mattn/go-isatty v0.0.24 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.30 // indirect
github.com/mattn/go-runewidth v0.0.31 // indirect
github.com/microsoft/go-mssqldb v1.9.1 // indirect
github.com/minio/crc64nvme v1.1.1 // indirect
github.com/minio/md5-simd v1.1.2 // indirect
Expand All @@ -177,7 +177,7 @@ require (
github.com/pelletier/go-toml/v2 v2.4.3 // indirect
github.com/petaki/inertia-go v1.15.0 // indirect
github.com/philhofer/fwd v1.2.0 // indirect
github.com/pterm/pterm v0.12.83 // indirect
github.com/pterm/pterm v0.12.84 // indirect
github.com/quic-go/qpack v0.6.0 // indirect
github.com/quic-go/quic-go v0.60.0 // indirect
github.com/redis/go-redis/v9 v9.21.0 // indirect
Expand Down Expand Up @@ -211,7 +211,7 @@ require (
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
github.com/ugorji/go/codec v1.3.1 // indirect
github.com/unrolled/secure v1.17.0 // indirect
github.com/urfave/cli/v3 v3.13.0 // indirect
github.com/urfave/cli/v3 v3.14.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.73.0 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
Expand All @@ -220,41 +220,41 @@ require (
github.com/zeebo/xxh3 v1.1.0 // indirect
go.mongodb.org/mongo-driver/v2 v2.8.0 // indirect
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.71.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.71.0 // indirect
go.opentelemetry.io/contrib/propagators/b3 v1.46.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.22.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.22.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.46.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.46.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.46.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.46.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.46.0 // indirect
go.opentelemetry.io/otel/exporters/stdout/stdoutlog v0.22.0 // indirect
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.46.0 // indirect
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.46.0 // indirect
go.opentelemetry.io/otel/log v0.22.0 // indirect
go.opentelemetry.io/otel/sdk v1.46.0 // indirect
go.opentelemetry.io/otel/sdk/log v0.22.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.46.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.72.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.72.0 // indirect
go.opentelemetry.io/contrib/propagators/b3 v1.47.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.23.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.23.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.47.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.47.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.47.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.47.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.47.0 // indirect
go.opentelemetry.io/otel/exporters/stdout/stdoutlog v0.23.0 // indirect
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.47.0 // indirect
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.47.0 // indirect
go.opentelemetry.io/otel/log v1.47.0 // indirect
go.opentelemetry.io/otel/sdk v1.47.0 // indirect
go.opentelemetry.io/otel/sdk/log v1.47.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.47.0 // indirect
go.opentelemetry.io/proto/otlp v1.11.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.yaml.in/yaml/v3 v3.0.5 // indirect
golang.org/x/arch v0.29.0 // indirect
golang.org/x/crypto v0.57.0 // indirect
golang.org/x/exp v0.0.0-20260908205506-85c1c2202aba // indirect
golang.org/x/mod v0.41.0 // indirect
golang.org/x/net v0.59.0 // indirect
golang.org/x/sync v0.23.0 // indirect
golang.org/x/sys v0.48.0 // indirect
golang.org/x/term v0.46.0 // indirect
golang.org/x/text v0.42.0 // indirect
golang.org/x/crypto v0.58.0 // indirect
golang.org/x/exp v0.0.0-20261011100332-faecfb24eb49 // indirect
golang.org/x/mod v0.42.0 // indirect
golang.org/x/net v0.61.0 // indirect
golang.org/x/sync v0.24.0 // indirect
golang.org/x/sys v0.49.0 // indirect
golang.org/x/term v0.47.0 // indirect
golang.org/x/text v0.43.0 // indirect
golang.org/x/time v0.15.0 // indirect
golang.org/x/tools v0.50.0 // indirect
golang.org/x/tools v0.52.0 // indirect
google.golang.org/api v0.293.0 // indirect
google.golang.org/genai v1.58.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20260819154853-08b0e4226688 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20260825221802-da73d73af1c5 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20260928230214-8a89bd6388cc // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20260928230214-8a89bd6388cc // indirect
gopkg.in/ini.v1 v1.67.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/mysql v1.6.0 // indirect
Expand All @@ -264,4 +264,4 @@ require (
gorm.io/plugin/dbresolver v1.6.2 // indirect
)

replace github.com/goravel/framework => github.com/goravel/framework v1.18.1-0.20260925125002-93850001fe6f
replace github.com/goravel/framework => github.com/goravel/framework v1.18.1-0.20261013093243-4da928651f19
Loading
Loading