mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-13 09:57:51 +08:00
e89fe48870
* Add compatibility test with old implementation * Add color type * Add basic style structs and utilities * Add structs for styled segments and texts * Add default style transformers to reimplement $edit:styled~ * Add builtins to manipulate styled segments and texts * Rename style 'underline' -> 'underlined' * Fix test case * Add conversion from styled text to ansi sequences * Return errors rather than throwing * Validate the type of boolean options * Delegate old to new styled function * Rebase for new test framework api and expand test cases * Remove old builtin function $edit:styled~ * Use strings to represent colors * Convert bool pointers to simple bool values * Validate color strings * Do no longer expose builtin style transformers * Fix confusion about pointers * Make outputs more stable * Expand tests * Use pointers instead of passing setter functions * Unexport and rename color check * Use the empty string for default colors * Expand tests * Simplify styled transformers Now there are three transformers for each boolean style attribute that allow setting, unsetting and toggling the corresponding attribute. * Rework and add doc comments
92 lines
2.0 KiB
Go
92 lines
2.0 KiB
Go
package styled
|
|
|
|
import (
|
|
"strings"
|
|
)
|
|
|
|
// todo: Make string conversion variable to environment.
|
|
// E.g. the shell displays colors different than HTML.
|
|
func (t Text) String() string {
|
|
buf := make([]byte, 0, 64)
|
|
for _, segment := range t {
|
|
styleBuf := make([]string, 0, 8)
|
|
|
|
if segment.Bold {
|
|
styleBuf = append(styleBuf, "1")
|
|
}
|
|
if segment.Dim {
|
|
styleBuf = append(styleBuf, "2")
|
|
}
|
|
if segment.Italic {
|
|
styleBuf = append(styleBuf, "3")
|
|
}
|
|
if segment.Underlined {
|
|
styleBuf = append(styleBuf, "4")
|
|
}
|
|
if segment.Blink {
|
|
styleBuf = append(styleBuf, "5")
|
|
}
|
|
if segment.Inverse {
|
|
styleBuf = append(styleBuf, "7")
|
|
}
|
|
|
|
if segment.Foreground != "" {
|
|
if col, ok := colorTranslationTable[segment.Foreground]; ok {
|
|
styleBuf = append(styleBuf, col)
|
|
}
|
|
}
|
|
if segment.Background != "" {
|
|
if col, ok := colorTranslationTable["bg-"+segment.Background]; ok {
|
|
styleBuf = append(styleBuf, col)
|
|
}
|
|
}
|
|
|
|
if len(styleBuf) > 0 {
|
|
buf = append(buf, "\033["...)
|
|
buf = append(buf, strings.Join(styleBuf, ";")...)
|
|
buf = append(buf, 'm')
|
|
buf = append(buf, segment.Text...)
|
|
buf = append(buf, "\033[m"...)
|
|
} else {
|
|
buf = append(buf, segment.Text...)
|
|
}
|
|
}
|
|
return string(buf)
|
|
}
|
|
|
|
var colorTranslationTable = map[string]string{
|
|
"black": "30",
|
|
"red": "31",
|
|
"green": "32",
|
|
"yellow": "33",
|
|
"blue": "34",
|
|
"magenta": "35",
|
|
"cyan": "36",
|
|
"lightgray": "37",
|
|
"gray": "90",
|
|
"lightred": "91",
|
|
"lightgreen": "92",
|
|
"lightyellow": "93",
|
|
"lightblue": "94",
|
|
"lightmagenta": "95",
|
|
"lightcyan": "96",
|
|
"white": "97",
|
|
|
|
"bg-black": "40",
|
|
"bg-red": "41",
|
|
"bg-green": "42",
|
|
"bg-yellow": "43",
|
|
"bg-blue": "44",
|
|
"bg-magenta": "45",
|
|
"bg-cyan": "46",
|
|
"bg-lightgray": "47",
|
|
"bg-gray": "100",
|
|
"bg-lightred": "101",
|
|
"bg-lightgreen": "102",
|
|
"bg-lightyellow": "103",
|
|
"bg-lightblue": "104",
|
|
"bg-lightmagenta": "105",
|
|
"bg-lightcyan": "106",
|
|
"bg-white": "107",
|
|
}
|