mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-07-03 04:04:34 +02:00
/timestamp, /theme: Fix rendering, add tests
This commit is contained in:
parent
d72aaf6bae
commit
c058f72fe4
@ -174,6 +174,9 @@ var Themes []Theme
|
|||||||
// Default theme to use
|
// Default theme to use
|
||||||
var DefaultTheme *Theme
|
var DefaultTheme *Theme
|
||||||
|
|
||||||
|
// MonoTheme is a simple theme without colors, useful for testing and bots.
|
||||||
|
var MonoTheme *Theme
|
||||||
|
|
||||||
func allColors256() *Palette {
|
func allColors256() *Palette {
|
||||||
colors := []uint8{}
|
colors := []uint8{}
|
||||||
var i uint8
|
var i uint8
|
||||||
@ -225,6 +228,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
DefaultTheme = &Themes[0]
|
DefaultTheme = &Themes[0]
|
||||||
|
MonoTheme = &Themes[3]
|
||||||
|
|
||||||
/* Some debug helpers for your convenience:
|
/* Some debug helpers for your convenience:
|
||||||
|
|
||||||
|
@ -182,7 +182,7 @@ func (u *User) render(m Message) string {
|
|||||||
} else {
|
} else {
|
||||||
ts = ts.UTC()
|
ts = ts.UTC()
|
||||||
}
|
}
|
||||||
return cfg.Theme.Timestamp(ts.Format(*cfg.Timeformat) + " " + out + Newline)
|
return cfg.Theme.Timestamp(ts.Format(*cfg.Timeformat)) + " " + out + Newline
|
||||||
}
|
}
|
||||||
return out + Newline
|
return out + Newline
|
||||||
}
|
}
|
||||||
@ -238,6 +238,7 @@ func init() {
|
|||||||
DefaultUserConfig = UserConfig{
|
DefaultUserConfig = UserConfig{
|
||||||
Bell: true,
|
Bell: true,
|
||||||
Quiet: false,
|
Quiet: false,
|
||||||
|
Theme: DefaultTheme,
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Seed random?
|
// TODO: Seed random?
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package message
|
package message
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math/rand"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@ -10,6 +11,11 @@ func TestMakeUser(t *testing.T) {
|
|||||||
|
|
||||||
s := &MockScreen{}
|
s := &MockScreen{}
|
||||||
u := NewUserScreen(SimpleID("foo"), s)
|
u := NewUserScreen(SimpleID("foo"), s)
|
||||||
|
|
||||||
|
cfg := u.Config()
|
||||||
|
cfg.Theme = MonoTheme // Mono
|
||||||
|
u.SetConfig(cfg)
|
||||||
|
|
||||||
m := NewAnnounceMsg("hello")
|
m := NewAnnounceMsg("hello")
|
||||||
|
|
||||||
defer u.Close()
|
defer u.Close()
|
||||||
@ -22,3 +28,33 @@ func TestMakeUser(t *testing.T) {
|
|||||||
t.Errorf("Got: `%s`; Expected: `%s`", actual, expected)
|
t.Errorf("Got: `%s`; Expected: `%s`", actual, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRenderTimestamp(t *testing.T) {
|
||||||
|
var actual, expected []byte
|
||||||
|
|
||||||
|
// Reset seed for username color
|
||||||
|
rand.Seed(1)
|
||||||
|
s := &MockScreen{}
|
||||||
|
u := NewUserScreen(SimpleID("foo"), s)
|
||||||
|
|
||||||
|
cfg := u.Config()
|
||||||
|
timefmt := "AA:BB"
|
||||||
|
cfg.Timeformat = &timefmt
|
||||||
|
u.SetConfig(cfg)
|
||||||
|
|
||||||
|
if got, want := cfg.Theme.Timestamp("foo"), `[38;05;245mfoo`+Reset; got != want {
|
||||||
|
t.Errorf("Wrong timestamp formatting:\n got: %q\nwant: %q", got, want)
|
||||||
|
}
|
||||||
|
|
||||||
|
m := NewPublicMsg("hello", u)
|
||||||
|
|
||||||
|
defer u.Close()
|
||||||
|
u.Send(m)
|
||||||
|
u.HandleMsg(u.ConsumeOne())
|
||||||
|
|
||||||
|
s.Read(&actual)
|
||||||
|
expected = []byte(`[38;05;245mAA:BB` + Reset + ` [[38;05;88mfoo[0m] hello` + Newline)
|
||||||
|
if !reflect.DeepEqual(actual, expected) {
|
||||||
|
t.Errorf("Wrong screen output:\n Got: `%q`;\nWant: `%q`", actual, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user