mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-04-13 15:47:17 +03:00
Merge pull request #356 from shazow/focus-cmd
/focus: Add command to only show messages from focused users
This commit is contained in:
commit
ab46dd9a98
@ -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)
|
||||
}
|
||||
|
19
set/set.go
19
set/set.go
@ -15,6 +15,25 @@ var ErrMissing = errors.New("item does not exist")
|
||||
// Returned when a nil item is added. Nil values are considered expired and invalid.
|
||||
var ErrNil = errors.New("item value must not be nil")
|
||||
|
||||
// ZeroValue can be used when we only care about the key, not about the value.
|
||||
var ZeroValue = struct{}{}
|
||||
|
||||
// Interface is the Set interface
|
||||
type Interface interface {
|
||||
Clear() int
|
||||
Each(fn IterFunc) error
|
||||
// Add only if the item does not already exist
|
||||
Add(item Item) error
|
||||
// Set item, override if it already exists
|
||||
Set(item Item) error
|
||||
Get(key string) (Item, error)
|
||||
In(key string) bool
|
||||
Len() int
|
||||
ListPrefix(prefix string) []Item
|
||||
Remove(key string) error
|
||||
Replace(oldKey string, item Item) error
|
||||
}
|
||||
|
||||
type IterFunc func(key string, item Item) error
|
||||
|
||||
type Set struct {
|
||||
|
Loading…
x
Reference in New Issue
Block a user