diff --git a/chat/chatMsg.go b/chat/chatMsg.go index 7c87855..43ca342 100644 --- a/chat/chatMsg.go +++ b/chat/chatMsg.go @@ -67,7 +67,32 @@ var colors = map[string]int{ "white": 97, } -// String return the message with escape sequence for ansi color. +// ClearString return the message without escape sequence for ansi color. +func (m Message) ClearString() string { + var msg strings.Builder + msg.WriteString(m.Text) + + //handle translate + if m.Translate != "" { + args := make([]interface{}, len(m.With)) + for i, v := range m.With { + var arg Message + arg.UnmarshalJSON(v) //ignore error + args[i] = arg.ClearString() + } + + fmt.Fprintf(&msg, data.EnUs[m.Translate], args...) + } + + if m.Extra != nil { + for i := range m.Extra { + msg.WriteString(Message(m.Extra[i]).ClearString()) + } + } + return msg.String() +} + +// String return the message string with escape sequence for ansi color. // On windows, you may want print this string using // github.com/mattn/go-colorable. func (m Message) String() string { diff --git a/chat/chatMsg_test.go b/chat/chatMsg_test.go index da0290a..49d49b3 100644 --- a/chat/chatMsg_test.go +++ b/chat/chatMsg_test.go @@ -39,6 +39,17 @@ var texts = []string{ "world", } +var ctexts = []string{ + "故我依然™ Kun_QwQ: 为什么想要用炼药锅灭火时总是跳不进去", + + " 好像是这个id。。", + "Hello, world!", + //"Prefix, str1str2 again str2 and str1 lastly str3 and also str1 again!", + "%s %str1 %%s %%str2", + "str1 str2", + "world", +} + func TestChatMsgFormatString(t *testing.T) { for i, v := range jsons { var cm Message @@ -46,8 +57,21 @@ func TestChatMsgFormatString(t *testing.T) { if err != nil { t.Error(err) } - if cm.String() != texts[i] { - t.Errorf("gets %q, wants %q", cm, texts[i]) + if str := cm.String(); str != texts[i] { + t.Errorf("gets %q, wants %q", str, texts[i]) + } + } +} + +func TestChatMsgClearString(t *testing.T) { + for i, v := range jsons { + var cm Message + err := cm.UnmarshalJSON([]byte(v)) + if err != nil { + t.Error(err) + } + if str := cm.ClearString(); str != ctexts[i] { + t.Errorf("gets %q, wants %q", str, texts[i]) } } }