mirror of
https://github.com/fatih/color.git
synced 2024-11-24 08:02:14 +02:00
commit
1c78dedd6a
36
color.go
36
color.go
@ -65,6 +65,29 @@ const (
|
||||
CrossedOut
|
||||
)
|
||||
|
||||
const (
|
||||
ResetBold Attribute = iota + 22
|
||||
ResetItalic
|
||||
ResetUnderline
|
||||
ResetBlinking
|
||||
_
|
||||
ResetReversed
|
||||
ResetConcealed
|
||||
ResetCrossedOut
|
||||
)
|
||||
|
||||
var mapResetAttributes map[Attribute]Attribute = map[Attribute]Attribute{
|
||||
Bold: ResetBold,
|
||||
Faint: ResetBold,
|
||||
Italic: ResetItalic,
|
||||
Underline: ResetUnderline,
|
||||
BlinkSlow: ResetBlinking,
|
||||
BlinkRapid: ResetBlinking,
|
||||
ReverseVideo: ResetReversed,
|
||||
Concealed: ResetConcealed,
|
||||
CrossedOut: ResetCrossedOut,
|
||||
}
|
||||
|
||||
// Foreground text colors
|
||||
const (
|
||||
FgBlack Attribute = iota + 30
|
||||
@ -377,7 +400,18 @@ func (c *Color) format() string {
|
||||
}
|
||||
|
||||
func (c *Color) unformat() string {
|
||||
return fmt.Sprintf("%s[%dm", escape, Reset)
|
||||
//return fmt.Sprintf("%s[%dm", escape, Reset)
|
||||
//for each element in sequence let's use the speficic reset escape, ou the generic one if not found
|
||||
format := make([]string, len(c.params))
|
||||
for i, v := range c.params {
|
||||
format[i] = strconv.Itoa(int(Reset))
|
||||
ra, ok := mapResetAttributes[v]
|
||||
if ok {
|
||||
format[i] = strconv.Itoa(int(ra))
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s[%sm", escape, strings.Join(format, ";"))
|
||||
}
|
||||
|
||||
// DisableColor disables the color output. Useful to not change any existing
|
||||
|
@ -469,3 +469,38 @@ func readRaw(t *testing.T, r io.Reader) string {
|
||||
|
||||
return string(out)
|
||||
}
|
||||
|
||||
func TestIssue206_1(t *testing.T) {
|
||||
//visual test, go test -v .
|
||||
//to see the string with escape codes, use go test -v . > c:\temp\test.txt
|
||||
var underline = New(Underline).Sprint
|
||||
|
||||
var line = fmt.Sprintf("%s %s %s %s", "word1", underline("word2"), "word3", underline("word4"))
|
||||
|
||||
line = CyanString(line)
|
||||
|
||||
fmt.Println(line)
|
||||
|
||||
var result = fmt.Sprintf("%v", line)
|
||||
const expectedResult = "\x1b[36mword1 \x1b[4mword2\x1b[24m word3 \x1b[4mword4\x1b[24m\x1b[0m"
|
||||
|
||||
if !bytes.Equal([]byte(result), []byte(expectedResult)) {
|
||||
t.Errorf("Expecting %v, got '%v'\n", expectedResult, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIssue206_2(t *testing.T) {
|
||||
var underline = New(Underline).Sprint
|
||||
var bold = New(Bold).Sprint
|
||||
|
||||
var line = fmt.Sprintf("%s %s", GreenString(underline("underlined regular green")), RedString(bold("bold red")))
|
||||
|
||||
fmt.Println(line)
|
||||
|
||||
var result = fmt.Sprintf("%v", line)
|
||||
const expectedResult = "\x1b[32m\x1b[4munderlined regular green\x1b[24m\x1b[0m \x1b[31m\x1b[1mbold red\x1b[22m\x1b[0m"
|
||||
|
||||
if !bytes.Equal([]byte(result), []byte(expectedResult)) {
|
||||
t.Errorf("Expecting %v, got '%v'\n", expectedResult, result)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user