mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-06-07 19:03:17 +03:00
refactor: User.Config -> User.Config() and User.SetConfig(UserConfig)
This commit is contained in:
parent
454777448f
commit
cdcc4a9931
@ -184,10 +184,11 @@ func InitCommands(c *Commands) {
|
|||||||
Handler: func(room *Room, msg message.CommandMsg) error {
|
Handler: func(room *Room, msg message.CommandMsg) error {
|
||||||
user := msg.From()
|
user := msg.From()
|
||||||
args := msg.Args()
|
args := msg.Args()
|
||||||
|
cfg := user.Config()
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
theme := "plain"
|
theme := "plain"
|
||||||
if user.Config.Theme != nil {
|
if cfg.Theme != nil {
|
||||||
theme = user.Config.Theme.ID()
|
theme = cfg.Theme.ID()
|
||||||
}
|
}
|
||||||
body := fmt.Sprintf("Current theme: %s", theme)
|
body := fmt.Sprintf("Current theme: %s", theme)
|
||||||
room.Send(message.NewSystemMsg(body, user))
|
room.Send(message.NewSystemMsg(body, user))
|
||||||
@ -197,7 +198,8 @@ func InitCommands(c *Commands) {
|
|||||||
id := args[0]
|
id := args[0]
|
||||||
for _, t := range message.Themes {
|
for _, t := range message.Themes {
|
||||||
if t.ID() == id {
|
if t.ID() == id {
|
||||||
user.Config.Theme = &t
|
cfg.Theme = &t
|
||||||
|
user.SetConfig(cfg)
|
||||||
body := fmt.Sprintf("Set theme: %s", id)
|
body := fmt.Sprintf("Set theme: %s", id)
|
||||||
room.Send(message.NewSystemMsg(body, user))
|
room.Send(message.NewSystemMsg(body, user))
|
||||||
return nil
|
return nil
|
||||||
@ -212,10 +214,12 @@ func InitCommands(c *Commands) {
|
|||||||
Help: "Silence room announcements.",
|
Help: "Silence room announcements.",
|
||||||
Handler: func(room *Room, msg message.CommandMsg) error {
|
Handler: func(room *Room, msg message.CommandMsg) error {
|
||||||
u := msg.From()
|
u := msg.From()
|
||||||
u.ToggleQuietMode()
|
cfg := u.Config()
|
||||||
|
cfg.Quiet = !cfg.Quiet
|
||||||
|
u.SetConfig(cfg)
|
||||||
|
|
||||||
var body string
|
var body string
|
||||||
if u.Config.Quiet {
|
if cfg.Quiet {
|
||||||
body = "Quiet mode is toggled ON"
|
body = "Quiet mode is toggled ON"
|
||||||
} else {
|
} else {
|
||||||
body = "Quiet mode is toggled OFF"
|
body = "Quiet mode is toggled OFF"
|
||||||
|
@ -31,14 +31,14 @@ type User struct {
|
|||||||
closeOnce sync.Once
|
closeOnce sync.Once
|
||||||
|
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
Config UserConfig
|
config UserConfig
|
||||||
replyTo *User // Set when user gets a /msg, for replying.
|
replyTo *User // Set when user gets a /msg, for replying.
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUser(identity Identifier) *User {
|
func NewUser(identity Identifier) *User {
|
||||||
u := User{
|
u := User{
|
||||||
Identifier: identity,
|
Identifier: identity,
|
||||||
Config: DefaultUserConfig,
|
config: DefaultUserConfig,
|
||||||
joined: time.Now(),
|
joined: time.Now(),
|
||||||
msg: make(chan Message, messageBuffer),
|
msg: make(chan Message, messageBuffer),
|
||||||
done: make(chan struct{}),
|
done: make(chan struct{}),
|
||||||
@ -56,6 +56,18 @@ func NewUserScreen(identity Identifier, screen io.WriteCloser) *User {
|
|||||||
return u
|
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.
|
// Rename the user with a new Identifier.
|
||||||
func (u *User) SetID(id string) {
|
func (u *User) SetID(id string) {
|
||||||
u.Identifier.SetID(id)
|
u.Identifier.SetID(id)
|
||||||
@ -76,24 +88,12 @@ func (u *User) SetReplyTo(user *User) {
|
|||||||
u.replyTo = user
|
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
|
// setColorIdx will set the colorIdx to a specific value, primarily used for
|
||||||
// testing.
|
// testing.
|
||||||
func (u *User) setColorIdx(idx int) {
|
func (u *User) setColorIdx(idx int) {
|
||||||
u.colorIdx = idx
|
u.colorIdx = idx
|
||||||
}
|
}
|
||||||
|
|
||||||
// Block until user is closed
|
|
||||||
func (u *User) Wait() {
|
|
||||||
<-u.done
|
|
||||||
}
|
|
||||||
|
|
||||||
// Disconnect user, stop accepting messages
|
// Disconnect user, stop accepting messages
|
||||||
func (u *User) Close() {
|
func (u *User) Close() {
|
||||||
u.closeOnce.Do(func() {
|
u.closeOnce.Do(func() {
|
||||||
@ -144,15 +144,13 @@ func (u *User) SetHighlight(s string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
u.mu.Lock()
|
u.mu.Lock()
|
||||||
u.Config.Highlight = re
|
u.config.Highlight = re
|
||||||
u.mu.Unlock()
|
u.mu.Unlock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *User) render(m Message) string {
|
func (u *User) render(m Message) string {
|
||||||
u.mu.Lock()
|
cfg := u.Config()
|
||||||
cfg := u.Config
|
|
||||||
u.mu.Unlock()
|
|
||||||
switch m := m.(type) {
|
switch m := m.(type) {
|
||||||
case PublicMsg:
|
case PublicMsg:
|
||||||
return m.RenderFor(cfg) + Newline
|
return m.RenderFor(cfg) + Newline
|
||||||
|
@ -110,7 +110,7 @@ func (r *Room) HandleMsg(m message.Message) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if _, ok := m.(*message.AnnounceMsg); ok {
|
if _, ok := m.(*message.AnnounceMsg); ok {
|
||||||
if user.Config.Quiet {
|
if user.Config().Quiet {
|
||||||
// Skip announcements
|
// Skip announcements
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -213,9 +213,9 @@ func TestRoomJoin(t *testing.T) {
|
|||||||
|
|
||||||
func TestRoomDoesntBroadcastAnnounceMessagesWhenQuiet(t *testing.T) {
|
func TestRoomDoesntBroadcastAnnounceMessagesWhenQuiet(t *testing.T) {
|
||||||
u := message.NewUser(message.SimpleID("foo"))
|
u := message.NewUser(message.SimpleID("foo"))
|
||||||
u.Config = message.UserConfig{
|
u.SetConfig(message.UserConfig{
|
||||||
Quiet: true,
|
Quiet: true,
|
||||||
}
|
})
|
||||||
|
|
||||||
ch := NewRoom()
|
ch := NewRoom()
|
||||||
defer ch.Close()
|
defer ch.Close()
|
||||||
@ -252,9 +252,9 @@ func TestRoomDoesntBroadcastAnnounceMessagesWhenQuiet(t *testing.T) {
|
|||||||
|
|
||||||
func TestRoomQuietToggleBroadcasts(t *testing.T) {
|
func TestRoomQuietToggleBroadcasts(t *testing.T) {
|
||||||
u := message.NewUser(message.SimpleID("foo"))
|
u := message.NewUser(message.SimpleID("foo"))
|
||||||
u.Config = message.UserConfig{
|
u.SetConfig(message.UserConfig{
|
||||||
Quiet: true,
|
Quiet: true,
|
||||||
}
|
})
|
||||||
|
|
||||||
ch := NewRoom()
|
ch := NewRoom()
|
||||||
defer ch.Close()
|
defer ch.Close()
|
||||||
@ -267,7 +267,9 @@ func TestRoomQuietToggleBroadcasts(t *testing.T) {
|
|||||||
// Drain the initial Join message
|
// Drain the initial Join message
|
||||||
<-ch.broadcast
|
<-ch.broadcast
|
||||||
|
|
||||||
u.ToggleQuietMode()
|
u.SetConfig(message.UserConfig{
|
||||||
|
Quiet: false,
|
||||||
|
})
|
||||||
|
|
||||||
expectedMsg := message.NewAnnounceMsg("Ignored")
|
expectedMsg := message.NewAnnounceMsg("Ignored")
|
||||||
ch.HandleMsg(expectedMsg)
|
ch.HandleMsg(expectedMsg)
|
||||||
@ -276,7 +278,9 @@ func TestRoomQuietToggleBroadcasts(t *testing.T) {
|
|||||||
t.Errorf("Got: `%T`; Expected: `%T`", msg, 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.NewAnnounceMsg("Ignored"))
|
||||||
ch.HandleMsg(message.NewSystemMsg("hello", u))
|
ch.HandleMsg(message.NewSystemMsg("hello", u))
|
||||||
|
9
host.go
9
host.go
@ -21,8 +21,9 @@ const maxInputLength int = 1024
|
|||||||
// GetPrompt will render the terminal prompt string based on the user.
|
// GetPrompt will render the terminal prompt string based on the user.
|
||||||
func GetPrompt(user *message.User) string {
|
func GetPrompt(user *message.User) string {
|
||||||
name := user.Name()
|
name := user.Name()
|
||||||
if user.Config.Theme != nil {
|
cfg := user.Config()
|
||||||
name = user.Config.Theme.ColorName(user)
|
if cfg.Theme != nil {
|
||||||
|
name = cfg.Theme.ColorName(user)
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("[%s] ", name)
|
return fmt.Sprintf("[%s] ", name)
|
||||||
}
|
}
|
||||||
@ -91,7 +92,9 @@ func (h *Host) isOp(conn sshd.Connection) bool {
|
|||||||
func (h *Host) Connect(term *sshd.Terminal) {
|
func (h *Host) Connect(term *sshd.Terminal) {
|
||||||
id := NewIdentity(term.Conn)
|
id := NewIdentity(term.Conn)
|
||||||
user := message.NewUserScreen(id, term)
|
user := message.NewUserScreen(id, term)
|
||||||
user.Config.Theme = &h.theme
|
cfg := user.Config()
|
||||||
|
cfg.Theme = &h.theme
|
||||||
|
user.SetConfig(cfg)
|
||||||
go user.Consume()
|
go user.Consume()
|
||||||
|
|
||||||
// Close term once user is closed.
|
// Close term once user is closed.
|
||||||
|
@ -35,7 +35,9 @@ func TestHostGetPrompt(t *testing.T) {
|
|||||||
t.Errorf("Got: %q; Expected: %q", actual, expected)
|
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)
|
actual = GetPrompt(u)
|
||||||
expected = "[\033[38;05;88mfoo\033[0m] "
|
expected = "[\033[38;05;88mfoo\033[0m] "
|
||||||
if actual != expected {
|
if actual != expected {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user