chat/message: Add timestamp prefix support

This commit is contained in:
Andrey Petrov 2019-03-06 12:25:02 -08:00
parent 924c6bd234
commit dca2cec67f
2 changed files with 33 additions and 19 deletions

View File

@ -1,6 +1,9 @@
package message
import "fmt"
import (
"fmt"
"time"
)
const (
// Reset resets the color
@ -122,43 +125,49 @@ type Theme struct {
names *Palette
}
func (t Theme) ID() string {
return t.id
func (theme Theme) ID() string {
return theme.id
}
// Colorize name string given some index
func (t Theme) ColorName(u *User) string {
if t.names == nil {
func (theme Theme) ColorName(u *User) string {
if theme.names == nil {
return u.Name()
}
return t.names.Get(u.colorIdx).Format(u.Name())
return theme.names.Get(u.colorIdx).Format(u.Name())
}
// Colorize the PM string
func (t Theme) ColorPM(s string) string {
if t.pm == nil {
func (theme Theme) ColorPM(s string) string {
if theme.pm == nil {
return s
}
return t.pm.Format(s)
return theme.pm.Format(s)
}
// Colorize the Sys message
func (t Theme) ColorSys(s string) string {
if t.sys == nil {
func (theme Theme) ColorSys(s string) string {
if theme.sys == nil {
return s
}
return t.sys.Format(s)
return theme.sys.Format(s)
}
// Highlight a matched string, usually name
func (t Theme) Highlight(s string) string {
if t.highlight == nil {
func (theme Theme) Highlight(s string) string {
if theme.highlight == nil {
return s
}
return t.highlight.Format(s)
return theme.highlight.Format(s)
}
// Timestamp formats and colorizes the timestamp.
func (theme Theme) Timestamp(t time.Time) string {
// TODO: Change this per-theme? Or config?
return theme.sys.Format(t.Format("2006-01-02 15:04 UTC"))
}
// List of initialzied themes

View File

@ -158,17 +158,22 @@ func (u *User) SetHighlight(s string) error {
func (u *User) render(m Message) string {
cfg := u.Config()
var out string
switch m := m.(type) {
case PublicMsg:
return m.RenderFor(cfg) + Newline
out += m.RenderFor(cfg)
case *PrivateMsg:
out += m.Render(cfg.Theme)
if cfg.Bell {
return m.Render(cfg.Theme) + Bel + Newline
out += Bel
}
return m.Render(cfg.Theme) + Newline
default:
return m.Render(cfg.Theme) + Newline
out += m.Render(cfg.Theme)
}
if cfg.Timestamp {
return cfg.Theme.Timestamp(m.Timestamp()) + " " + out + Newline
}
return out + Newline
}
// writeMsg renders the message and attempts to write it, will Close the user