| 1 |
package toner |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"io" |
| 6 |
"slices" |
| 7 |
"strconv" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"github.com/charmbracelet/x/ansi" |
| 11 |
"github.com/charmbracelet/x/exp/charmtone" |
| 12 |
) |
| 13 |
|
| 14 |
// Strings returns a colorized string representation of the input byte slice or |
| 15 |
// string. |
| 16 |
func Strings(s string) string { |
| 17 |
var buf bytes.Buffer |
| 18 |
_, _ = writeColorize(&buf, s) |
| 19 |
return buf.String() |
| 20 |
} |
| 21 |
|
| 22 |
// Bytes returns a colorized byte slice representation of the input byte slice. |
| 23 |
func Bytes(b []byte) []byte { |
| 24 |
var buf bytes.Buffer |
| 25 |
_, _ = writeColorize(&buf, b) |
| 26 |
return buf.Bytes() |
| 27 |
} |
| 28 |
|
| 29 |
// Writer encapsulates a [io.Writer] that colorizes the output using charm tones. |
| 30 |
type Writer struct { |
| 31 |
io.Writer |
| 32 |
} |
| 33 |
|
| 34 |
// Write writes the colorized output to the underlying writer. |
| 35 |
func (w Writer) Write(p []byte) (n int, err error) { |
| 36 |
if len(p) == 0 { |
| 37 |
return 0, nil |
| 38 |
} |
| 39 |
|
| 40 |
return writeColorize(w, p) |
| 41 |
} |
| 42 |
|
| 43 |
// WriteString writes the colorized output of the input string to the underlying writer. |
| 44 |
func (w Writer) WriteString(s string) (n int, err error) { |
| 45 |
if len(s) == 0 { |
| 46 |
return 0, nil |
| 47 |
} |
| 48 |
|
| 49 |
return writeColorize(w, s) |
| 50 |
} |
| 51 |
|
| 52 |
var colors = func() []charmtone.Key { |
| 53 |
cols := charmtone.Keys() |
| 54 |
// Filter out these colors. |
| 55 |
filterOut := []charmtone.Key{ |
| 56 |
charmtone.Cumin, |
| 57 |
charmtone.Tang, |
| 58 |
charmtone.Paprika, |
| 59 |
charmtone.Pepper, |
| 60 |
charmtone.Charcoal, |
| 61 |
charmtone.Iron, |
| 62 |
charmtone.Oyster, |
| 63 |
charmtone.Squid, |
| 64 |
charmtone.Smoke, |
| 65 |
charmtone.Ash, |
| 66 |
charmtone.Salt, |
| 67 |
charmtone.Butter, |
| 68 |
} |
| 69 |
for _, k := range filterOut { |
| 70 |
cols = slices.DeleteFunc(cols, func(c charmtone.Key) bool { |
| 71 |
return c == k |
| 72 |
}) |
| 73 |
} |
| 74 |
return cols |
| 75 |
}() |
| 76 |
|
| 77 |
func writeColorize[T []byte | string](w io.Writer, p T) (n int, err error) { |
| 78 |
pa := ansi.NewParser() |
| 79 |
|
| 80 |
var state byte |
| 81 |
for len(p) > 0 { |
| 82 |
seq, width, nr, newState := ansi.DecodeSequence(p, state, pa) |
| 83 |
cmd := pa.Command() |
| 84 |
|
| 85 |
var st ansi.Style |
| 86 |
var s string |
| 87 |
if cmd == 0 && width > 0 { |
| 88 |
s = string(seq) |
| 89 |
} else { |
| 90 |
st = st.ForegroundColor(colors[cmd%len(colors)]) |
| 91 |
s = string(seq) |
| 92 |
} |
| 93 |
|
| 94 |
s = strconv.Quote(s) |
| 95 |
s = strings.TrimPrefix(s, "\"") |
| 96 |
s = strings.TrimSuffix(s, "\"") |
| 97 |
if len(st) > 0 { |
| 98 |
s = st.Styled(s) |
| 99 |
} |
| 100 |
|
| 101 |
m, err := io.WriteString(w, s) |
| 102 |
if err != nil { |
| 103 |
return n, err |
| 104 |
} |
| 105 |
|
| 106 |
n += m |
| 107 |
|
| 108 |
p = p[nr:] |
| 109 |
state = newState |
| 110 |
} |
| 111 |
|
| 112 |
return n, nil |
| 113 |
} |