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 package message
import "fmt" import (
"fmt"
"time"
)
const ( const (
// Reset resets the color // Reset resets the color
@ -122,43 +125,49 @@ type Theme struct {
names *Palette names *Palette
} }
func (t Theme) ID() string { func (theme Theme) ID() string {
return t.id return theme.id
} }
// Colorize name string given some index // Colorize name string given some index
func (t Theme) ColorName(u *User) string { func (theme Theme) ColorName(u *User) string {
if t.names == nil { if theme.names == nil {
return u.Name() 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 // Colorize the PM string
func (t Theme) ColorPM(s string) string { func (theme Theme) ColorPM(s string) string {
if t.pm == nil { if theme.pm == nil {
return s return s
} }
return t.pm.Format(s) return theme.pm.Format(s)
} }
// Colorize the Sys message // Colorize the Sys message
func (t Theme) ColorSys(s string) string { func (theme Theme) ColorSys(s string) string {
if t.sys == nil { if theme.sys == nil {
return s return s
} }
return t.sys.Format(s) return theme.sys.Format(s)
} }
// Highlight a matched string, usually name // Highlight a matched string, usually name
func (t Theme) Highlight(s string) string { func (theme Theme) Highlight(s string) string {
if t.highlight == nil { if theme.highlight == nil {
return s 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 // List of initialzied themes

View File

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