package colorprofile
import (
)
func ( io.Writer, []string) *Writer {
return &Writer{
Forward: ,
Profile: Detect(, ),
}
}
type Writer struct {
Forward io.Writer
Profile Profile
}
func ( *Writer) ( []byte) (int, error) {
switch {
case .Profile == TrueColor:
return .Forward.Write()
case .Profile <= NoTTY:
, := io.WriteString(.Forward, ansi.Strip(string()))
return len(),
case .Profile == ASCII, .Profile == ANSI, .Profile == ANSI256:
, := .downsample()
return len(),
default:
return 0, fmt.Errorf("invalid profile: %v", .Profile)
}
}
func ( *Writer) ( []byte) (int, error) {
var bytes.Buffer
var byte
:= ansi.GetParser()
defer ansi.PutParser()
for len() > 0 {
.Reset()
, , , := ansi.DecodeSequence(, , )
switch {
case ansi.HasCsiPrefix() && .Command() == 'm':
handleSgr(, , &)
default:
if , := .Write(); != nil {
return ,
}
}
= [:]
=
}
return .Forward.Write(.Bytes())
}
func ( *Writer) ( string) ( int, error) {
return .Write([]byte())
}
func ( *Writer, *ansi.Parser, *bytes.Buffer) {
var ansi.Style
:= .Params()
for := 0; < len(); ++ {
:= []
switch := .Param(0); {
case 0:
= append(, "")
case 30, 31, 32, 33, 34, 35, 36, 37:
if .Profile < ANSI {
continue
}
= .ForegroundColor(
.Profile.Convert(ansi.BasicColor( - 30)))
case 38:
var color.Color
if := ansi.ReadStyleColor([:], &); > 0 {
+= - 1
}
if .Profile < ANSI {
continue
}
= .ForegroundColor(.Profile.Convert())
case 39:
if .Profile < ANSI {
continue
}
= .ForegroundColor(nil)
case 40, 41, 42, 43, 44, 45, 46, 47:
if .Profile < ANSI {
continue
}
= .BackgroundColor(
.Profile.Convert(ansi.BasicColor( - 40)))
case 48:
var color.Color
if := ansi.ReadStyleColor([:], &); > 0 {
+= - 1
}
if .Profile < ANSI {
continue
}
= .BackgroundColor(.Profile.Convert())
case 49:
if .Profile < ANSI {
continue
}
= .BackgroundColor(nil)
case 58:
var color.Color
if := ansi.ReadStyleColor([:], &); > 0 {
+= - 1
}
if .Profile < ANSI {
continue
}
= .UnderlineColor(.Profile.Convert())
case 59:
if .Profile < ANSI {
continue
}
= .UnderlineColor(nil)
case 90, 91, 92, 93, 94, 95, 96, 97:
if .Profile < ANSI {
continue
}
= .ForegroundColor(
.Profile.Convert(ansi.BasicColor( - 90 + 8)))
case 100, 101, 102, 103, 104, 105, 106, 107:
if .Profile < ANSI {
continue
}
= .BackgroundColor(
.Profile.Convert(ansi.BasicColor( - 100 + 8)))
default:
= append(, strconv.Itoa())
}
}
_, _ = .WriteString(.String())
}