mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-04-12 15:17:16 +03:00
chat: Add /focus command
Only show messages from focused users
This commit is contained in:
parent
8cd06e33b5
commit
aa78d0eb22
@ -411,4 +411,51 @@ func InitCommands(c *Commands) {
|
||||
return nil
|
||||
},
|
||||
})
|
||||
|
||||
c.Add(Command{
|
||||
Prefix: "/focus",
|
||||
PrefixHelp: "[USER ...]",
|
||||
Help: "Only show messages from focused users, or $ to reset.",
|
||||
Handler: func(room *Room, msg message.CommandMsg) error {
|
||||
ids := strings.TrimSpace(strings.TrimLeft(msg.Body(), "/focus"))
|
||||
if ids == "" {
|
||||
// Print focused names, if any.
|
||||
var names []string
|
||||
msg.From().Focused.Each(func(_ string, item set.Item) error {
|
||||
names = append(names, item.Key())
|
||||
return nil
|
||||
})
|
||||
|
||||
var systemMsg string
|
||||
if len(names) == 0 {
|
||||
systemMsg = "Unfocused."
|
||||
} else {
|
||||
systemMsg = fmt.Sprintf("Focusing on %d users: %s", len(names), strings.Join(names, ", "))
|
||||
}
|
||||
|
||||
room.Send(message.NewSystemMsg(systemMsg, msg.From()))
|
||||
return nil
|
||||
}
|
||||
|
||||
n := msg.From().Focused.Clear()
|
||||
if ids == "$" {
|
||||
room.Send(message.NewSystemMsg(fmt.Sprintf("Removed focus from %d users.", n), msg.From()))
|
||||
return nil
|
||||
}
|
||||
|
||||
var focused []string
|
||||
for _, name := range strings.Split(ids, " ") {
|
||||
id := sanitize.Name(name)
|
||||
if id == "" {
|
||||
continue // Skip
|
||||
}
|
||||
focused = append(focused, id)
|
||||
if err := msg.From().Focused.Set(set.Itemize(id, set.ZeroValue)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
room.Send(message.NewSystemMsg(fmt.Sprintf("Focusing: %s", strings.Join(focused, ", ")), msg.From()))
|
||||
return nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
@ -22,7 +22,8 @@ var ErrUserClosed = errors.New("user closed")
|
||||
// User definition, implemented set Item interface and io.Writer
|
||||
type User struct {
|
||||
Identifier
|
||||
Ignored *set.Set
|
||||
Ignored set.Interface
|
||||
Focused set.Interface
|
||||
colorIdx int
|
||||
joined time.Time
|
||||
msg chan Message
|
||||
@ -45,6 +46,7 @@ func NewUser(identity Identifier) *User {
|
||||
msg: make(chan Message, messageBuffer),
|
||||
done: make(chan struct{}),
|
||||
Ignored: set.New(),
|
||||
Focused: set.New(),
|
||||
}
|
||||
u.setColorIdx(rand.Int())
|
||||
|
||||
@ -175,6 +177,9 @@ func (u *User) render(m Message) string {
|
||||
return ""
|
||||
}
|
||||
out += m.RenderSelf(cfg)
|
||||
} else if u.Focused.Len() > 0 && !u.Focused.In(m.From().ID()) {
|
||||
// Skip message during focus
|
||||
return ""
|
||||
} else {
|
||||
out += m.RenderFor(cfg)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user