Handler: func(room *Room, msg message.CommandMsg) error {
user := msg.From()
args := msg.Args()
+ cfg := user.Config()
if len(args) == 0 {
theme := "plain"
- if user.Config.Theme != nil {
- theme = user.Config.Theme.ID()
+ if cfg.Theme != nil {
+ theme = cfg.Theme.ID()
}
body := fmt.Sprintf("Current theme: %s", theme)
room.Send(message.NewSystemMsg(body, user))
id := args[0]
for _, t := range message.Themes {
if t.ID() == id {
- user.Config.Theme = &t
+ cfg.Theme = &t
+ user.SetConfig(cfg)
body := fmt.Sprintf("Set theme: %s", id)
room.Send(message.NewSystemMsg(body, user))
return nil
Help: "Silence room announcements.",
Handler: func(room *Room, msg message.CommandMsg) error {
u := msg.From()
- u.ToggleQuietMode()
+ cfg := u.Config()
+ cfg.Quiet = !cfg.Quiet
+ u.SetConfig(cfg)
var body string
- if u.Config.Quiet {
+ if cfg.Quiet {
body = "Quiet mode is toggled ON"
} else {
body = "Quiet mode is toggled OFF"
closeOnce sync.Once
mu sync.Mutex
- Config UserConfig
+ config UserConfig
replyTo *User // Set when user gets a /msg, for replying.
}
func NewUser(identity Identifier) *User {
u := User{
Identifier: identity,
- Config: DefaultUserConfig,
+ config: DefaultUserConfig,
joined: time.Now(),
msg: make(chan Message, messageBuffer),
done: make(chan struct{}),
return u
}
+func (u *User) Config() UserConfig {
+ u.mu.Lock()
+ defer u.mu.Unlock()
+ return u.config
+}
+
+func (u *User) SetConfig(cfg UserConfig) {
+ u.mu.Lock()
+ u.config = cfg
+ u.mu.Unlock()
+}
+
// Rename the user with a new Identifier.
func (u *User) SetID(id string) {
u.Identifier.SetID(id)
u.replyTo = user
}
-// ToggleQuietMode will toggle whether or not quiet mode is enabled
-func (u *User) ToggleQuietMode() {
- u.mu.Lock()
- defer u.mu.Unlock()
- u.Config.Quiet = !u.Config.Quiet
-}
-
// setColorIdx will set the colorIdx to a specific value, primarily used for
// testing.
func (u *User) setColorIdx(idx int) {
u.colorIdx = idx
}
-// Block until user is closed
-func (u *User) Wait() {
- <-u.done
-}
-
// Disconnect user, stop accepting messages
func (u *User) Close() {
u.closeOnce.Do(func() {
return err
}
u.mu.Lock()
- u.Config.Highlight = re
+ u.config.Highlight = re
u.mu.Unlock()
return nil
}
func (u *User) render(m Message) string {
- u.mu.Lock()
- cfg := u.Config
- u.mu.Unlock()
+ cfg := u.Config()
switch m := m.(type) {
case PublicMsg:
return m.RenderFor(cfg) + Newline
return
}
if _, ok := m.(*message.AnnounceMsg); ok {
- if user.Config.Quiet {
+ if user.Config().Quiet {
// Skip announcements
return
}
func TestRoomDoesntBroadcastAnnounceMessagesWhenQuiet(t *testing.T) {
u := message.NewUser(message.SimpleID("foo"))
- u.Config = message.UserConfig{
+ u.SetConfig(message.UserConfig{
Quiet: true,
- }
+ })
ch := NewRoom()
defer ch.Close()
func TestRoomQuietToggleBroadcasts(t *testing.T) {
u := message.NewUser(message.SimpleID("foo"))
- u.Config = message.UserConfig{
+ u.SetConfig(message.UserConfig{
Quiet: true,
- }
+ })
ch := NewRoom()
defer ch.Close()
// Drain the initial Join message
<-ch.broadcast
- u.ToggleQuietMode()
+ u.SetConfig(message.UserConfig{
+ Quiet: false,
+ })
expectedMsg := message.NewAnnounceMsg("Ignored")
ch.HandleMsg(expectedMsg)
t.Errorf("Got: `%T`; Expected: `%T`", msg, expectedMsg)
}
- u.ToggleQuietMode()
+ u.SetConfig(message.UserConfig{
+ Quiet: true,
+ })
ch.HandleMsg(message.NewAnnounceMsg("Ignored"))
ch.HandleMsg(message.NewSystemMsg("hello", u))
// GetPrompt will render the terminal prompt string based on the user.
func GetPrompt(user *message.User) string {
name := user.Name()
- if user.Config.Theme != nil {
- name = user.Config.Theme.ColorName(user)
+ cfg := user.Config()
+ if cfg.Theme != nil {
+ name = cfg.Theme.ColorName(user)
}
return fmt.Sprintf("[%s] ", name)
}
func (h *Host) Connect(term *sshd.Terminal) {
id := NewIdentity(term.Conn)
user := message.NewUserScreen(id, term)
- user.Config.Theme = &h.theme
+ cfg := user.Config()
+ cfg.Theme = &h.theme
+ user.SetConfig(cfg)
go user.Consume()
// Close term once user is closed.
t.Errorf("Got: %q; Expected: %q", actual, expected)
}
- u.Config.Theme = &message.Themes[0]
+ u.SetConfig(message.UserConfig{
+ Theme: &message.Themes[0],
+ })
actual = GetPrompt(u)
expected = "[\033[38;05;88mfoo\033[0m] "
if actual != expected {