parent
ea49ab09da
commit
5cb73a99a9
@ -0,0 +1,148 @@
|
||||
/* ——————————————————————————————————————————————————————————————————————————
|
||||
Proyecto WIAWAIW
|
||||
—————————————————————————————————————————————————————————————————————————————
|
||||
Fichero capabilities.go
|
||||
Package ansi2
|
||||
Autor Juan V. Navarro juanvnl@activething.com
|
||||
Creado 28/08/2025
|
||||
—————————————————————————————————————————————————————————————————————————————
|
||||
|
||||
LICENSES AND TERMS OF USE
|
||||
-------------------------
|
||||
|
||||
This software is licensed under the Elastic License v2.0 (the "License").
|
||||
For full terms and additional information regarding permitted and prohibited
|
||||
uses, please visit:
|
||||
https://activething.com/wiawiaw/licenses
|
||||
|
||||
You may use, copy, modify, and redistribute this software internally within
|
||||
your organization for any purpose, including research, development, and
|
||||
testing, subject to the terms of this License.
|
||||
|
||||
You may NOT, however, use, provide, distribute, or make this software
|
||||
available to any third party as part of a hosted service, SaaS offering, or
|
||||
commercial product without first obtaining a commercial license from
|
||||
Active Thing.
|
||||
|
||||
You may combine this software with other code, provided that such
|
||||
combination does not circumvent the restrictions of this License.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. In no event shall
|
||||
the authors or copyright holders be liable for any claim, damages, or other
|
||||
liability arising from the use of this software.
|
||||
|
||||
—————————————————————————————————————————————————————————————————————————————
|
||||
|
||||
Web : activething.com | wiawiaw.com
|
||||
git : g.activething.com | github.com/activething
|
||||
Correo : dev@activething.com
|
||||
|
||||
—————————————————————————————————————————————————————————————————————————————
|
||||
No deseo caminar sobre el agua", dijo Siddhartha.
|
||||
Que los antiguos chamanes se contenten con tales habilidades.
|
||||
—— Hermann Hesse, Siddhartha
|
||||
—————————————————————————————————————————————————————————————————————————————
|
||||
Copyright (c) 2025 Active Thing
|
||||
————————————————————————————————————————————————————————————————————————————— */
|
||||
|
||||
package ansi2
|
||||
|
||||
|
||||
import "strings"
|
||||
|
||||
const (
|
||||
maxCapabilities = 15
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
CapStyleBold Capabilities = 1 << iota // Soporte para texto en negrita.
|
||||
CapStyleDim
|
||||
CapStyleItalic // Soporte para texto en cursiva.
|
||||
CapStyleUnderline // Soporte para texto subrayado.
|
||||
CapStyleBlink
|
||||
CapStyleInverse
|
||||
CapStyleHidden
|
||||
CapStyleStrike
|
||||
|
||||
CapDoubleUnderline
|
||||
CapOverline
|
||||
CapFraktur
|
||||
CapProportional
|
||||
|
||||
CapBasicColor //Capabilities = 1 << 12
|
||||
Cap256Color //Capabilities = 1 << 13
|
||||
CapTrueColor //Capabilities = 1 << 14
|
||||
|
||||
CapNone Capabilities = 0 // El terminal no soporta nada.
|
||||
|
||||
|
||||
|
||||
CapAllColor = CapBasicColor | Cap256Color | CapTrueColor
|
||||
CapAllStyle = CapStyleBold | CapStyleDim | CapStyleItalic |
|
||||
CapStyleUnderline | CapStyleBlink | CapStyleInverse
|
||||
|
||||
CapAllExtended = CapStyleHidden | CapStyleStrike| CapDoubleUnderline|
|
||||
CapOverline | CapFraktur | CapProportional
|
||||
|
||||
CapAll = CapAllColor | CapAllStyle | CapAllExtended
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
type (
|
||||
|
||||
Capabilities uint16
|
||||
)
|
||||
|
||||
|
||||
|
||||
// Métodos para verificar capacidades de color
|
||||
|
||||
func (c Capabilities) HasBasicColor () bool { return c&CapBasicColor!= 0 }
|
||||
func (c Capabilities) Has256Color () bool { return c&Cap256Color != 0 }
|
||||
func (c Capabilities) HasTrueColor () bool { return c&CapTrueColor != 0 }
|
||||
func (c Capabilities) HasAnyColor () bool { return c&CapAllColor != 0 }
|
||||
|
||||
// Métodos para verificar capacidades de estilo
|
||||
|
||||
func (c Capabilities) HasBold () bool { return c&CapStyleBold != 0 }
|
||||
func (c Capabilities) HasDim () bool { return c&CapStyleDim != 0 }
|
||||
func (c Capabilities) HasItalic () bool { return c&CapStyleItalic != 0 }
|
||||
func (c Capabilities) HasUnderline () bool { return c&CapStyleUnderline!= 0 }
|
||||
func (c Capabilities) HasBlink () bool { return c&CapStyleBlink != 0 }
|
||||
func (c Capabilities) HasInverse () bool { return c&CapStyleInverse != 0 }
|
||||
func (c Capabilities) HasHidden () bool { return c&CapStyleHidden != 0 }
|
||||
func (c Capabilities) HasStrike () bool { return c&CapStyleStrike != 0 }
|
||||
|
||||
// Métodos para capacidades extendidas
|
||||
|
||||
func (c Capabilities) HasDoubleUnderline() bool { return c&CapDoubleUnderline != 0 }
|
||||
func (c Capabilities) HasOverline () bool { return c&CapOverline != 0 }
|
||||
func (c Capabilities) HasFraktur () bool { return c&CapFraktur != 0 }
|
||||
func (c Capabilities) HasProportional () bool { return c&CapProportional != 0 }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
func (c Capabilities) String () string {
|
||||
if c == 0 {
|
||||
return ""
|
||||
}
|
||||
cs := make([]string, 0, maxCapabilities)
|
||||
for i := 0; i < maxCapabilities; i++ {
|
||||
if c&1<<i != 0 {
|
||||
cs = append(cs, capNames[i])
|
||||
}
|
||||
}
|
||||
return strings.Join(cs, "|")
|
||||
}
|
||||
|
||||
@ -0,0 +1,147 @@
|
||||
/* ——————————————————————————————————————————————————————————————————————————
|
||||
Proyecto WIAWAIW
|
||||
—————————————————————————————————————————————————————————————————————————————
|
||||
Fichero z_build_benchmark_test.go
|
||||
Package ansi2
|
||||
Autor Juan V. Navarro juanvnl@activething.com
|
||||
Creado 28/08/2025
|
||||
—————————————————————————————————————————————————————————————————————————————
|
||||
|
||||
LICENSES AND TERMS OF USE
|
||||
-------------------------
|
||||
|
||||
This software is licensed under the Elastic License v2.0 (the "License").
|
||||
For full terms and additional information regarding permitted and prohibited
|
||||
uses, please visit:
|
||||
https://activething.com/wiawiaw/licenses
|
||||
|
||||
You may use, copy, modify, and redistribute this software internally within
|
||||
your organization for any purpose, including research, development, and
|
||||
testing, subject to the terms of this License.
|
||||
|
||||
You may NOT, however, use, provide, distribute, or make this software
|
||||
available to any third party as part of a hosted service, SaaS offering, or
|
||||
commercial product without first obtaining a commercial license from
|
||||
Active Thing.
|
||||
|
||||
You may combine this software with other code, provided that such
|
||||
combination does not circumvent the restrictions of this License.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. In no event shall
|
||||
the authors or copyright holders be liable for any claim, damages, or other
|
||||
liability arising from the use of this software.
|
||||
|
||||
—————————————————————————————————————————————————————————————————————————————
|
||||
|
||||
Web : activething.com | wiawiaw.com
|
||||
git : g.activething.com | github.com/activething
|
||||
Correo : dev@activething.com
|
||||
|
||||
—————————————————————————————————————————————————————————————————————————————
|
||||
No deseo caminar sobre el agua", dijo Siddhartha.
|
||||
Que los antiguos chamanes se contenten con tales habilidades.
|
||||
—— Hermann Hesse, Siddhartha
|
||||
—————————————————————————————————————————————————————————————————————————————
|
||||
Copyright (c) 2025 Active Thing
|
||||
————————————————————————————————————————————————————————————————————————————— */
|
||||
|
||||
package ansi2
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/logrusorgru/aurora"
|
||||
)
|
||||
|
||||
// Variable global para evitar optimizaciones del compilador
|
||||
var sink2 string
|
||||
|
||||
// Benchmarks para Style.Build (base/ansi2) vs aurora.Value.String (Aurora)
|
||||
func BenchmarkBuild(b *testing.B) {
|
||||
styles := []struct {
|
||||
name string
|
||||
style Style
|
||||
auroraValue aurora.Value
|
||||
skipAurora bool // Para casos RGB no soportados por Aurora
|
||||
}{
|
||||
{
|
||||
name: "BasicColor",
|
||||
style: MakeStyle(FgRed()), // \033[31m (rojo)
|
||||
auroraValue: aurora.Red(""), // aurora.Red() genera \033[31m
|
||||
skipAurora: false,
|
||||
},
|
||||
{
|
||||
name: "8BitColor",
|
||||
style: MakeStyle(FgIndexColor(255)), // \033[38;5;255m
|
||||
auroraValue: aurora.Index(255, ""), // aurora.Index(255) genera \033[38;5;255m
|
||||
skipAurora: false,
|
||||
},
|
||||
{
|
||||
name: "BasicColorWithBold",
|
||||
style: MakeStyle(Bold(), FgRed()), // \033[1;31m
|
||||
auroraValue: aurora.Red("").Bold(), // aurora.Red().Bold() genera \033[1;31m
|
||||
skipAurora: false,
|
||||
},
|
||||
{
|
||||
name: "8BitColorWithBoldUnderline",
|
||||
style: MakeStyle(Bold(), Underline(), FgIndexColor(255)), // \033[1;4;38;5;255m
|
||||
auroraValue: aurora.Index(255, "").Bold().Underline(), // aurora.Index(255).Bold().Underline() genera \033[1;4;38;5;255m
|
||||
skipAurora: false,
|
||||
},
|
||||
{
|
||||
name: "TrueFgColor",
|
||||
style: MakeStyle(FgTrueColor(255, 128, 0)), // \033[38;2;255;128;0m
|
||||
skipAurora: true, // Aurora no soporta RGB
|
||||
},
|
||||
{
|
||||
name: "RGBColorWithBoldUnderline",
|
||||
style: MakeStyle(Bold(), Underline(), FgTrueColor(255, 128, 0)), // \033[1;4;38;2;255;128;0m
|
||||
skipAurora: true, // Aurora no soporta RGB
|
||||
},
|
||||
}
|
||||
|
||||
// Benchmarks para base/ansi2
|
||||
for _, s := range styles {
|
||||
b.Run("base/ansi2/"+s.name, func(b *testing.B) {
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
sink2 = string(s.style.Build())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Benchmarks para Aurora (excluyendo casos RGB)
|
||||
for _, s := range styles {
|
||||
if s.skipAurora {
|
||||
continue
|
||||
}
|
||||
b.Run("aurora/"+s.name, func(b *testing.B) {
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
sink2 = s.auroraValue.String()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
func BenchmarkStylize(b *testing.B) {
|
||||
//style := MakeStyle(Bold(), FgRed())
|
||||
//style := Style(0).Bold().Red()
|
||||
//auroraStyle := aurora.Red("").Bold()
|
||||
text := "Hello, World!"
|
||||
b.Run("base/ansi2", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
//_ = style.Build().Stylize(text)
|
||||
_ = Style(0).Bold().Red().Build().Stylize(text)
|
||||
}
|
||||
})
|
||||
b.Run("aurora", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = aurora.Colorize(text, aurora.GreenFg|aurora.BoldFm).String()
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -1,13 +1,2 @@
|
||||
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
|
||||
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI=
|
||||
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
|
||||
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8=
|
||||
github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
|
||||
|
||||
Loading…
Reference in new issue