This commit is contained in:
Andrey Petrov 2015-01-17 15:56:29 -08:00
parent 838285ba43
commit 391a66a876
3 changed files with 16 additions and 7 deletions

View File

@ -2,7 +2,6 @@ package chat
import (
"fmt"
"regexp"
"strings"
"time"
)
@ -102,13 +101,20 @@ func (m *PublicMsg) Render(t *Theme) string {
return fmt.Sprintf("%s: %s", t.ColorName(m.from), m.body)
}
func (m *PublicMsg) RenderHighlighted(t *Theme, highlight *regexp.Regexp) string {
if highlight == nil || t == nil {
return m.Render(t)
func (m *PublicMsg) RenderFor(cfg UserConfig) string {
if cfg.Highlight == nil || cfg.Theme == nil {
return m.Render(cfg.Theme)
}
body := highlight.ReplaceAllString(m.body, t.Highlight("${1}"))
return fmt.Sprintf("%s: %s", t.ColorName(m.from), body)
if !cfg.Highlight.MatchString(m.body) {
return m.Render(cfg.Theme)
}
body := cfg.Highlight.ReplaceAllString(m.body, cfg.Theme.Highlight("${1}"))
if cfg.Bell {
body += Bel
}
return fmt.Sprintf("%s: %s", cfg.Theme.ColorName(m.from), body)
}
func (m *PublicMsg) String() string {

View File

@ -26,6 +26,9 @@ const (
// Newline
Newline = "\r\n"
// BEL
Bel = "\007"
)
// Interface for Styles

View File

@ -116,7 +116,7 @@ func (u *User) SetHighlight(s string) error {
func (u User) render(m Message) string {
switch m := m.(type) {
case *PublicMsg:
return m.RenderHighlighted(u.Config.Theme, u.Config.Highlight) + Newline
return m.RenderFor(u.Config) + Newline
default:
return m.Render(u.Config.Theme) + Newline
}