Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added away users to output #379

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions chat/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"sort"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -191,13 +192,27 @@ func InitCommands(c *Commands) {

names := room.Members.ListPrefix("")
sort.Slice(names, func(i, j int) bool { return names[i].Key() < names[j].Key() })
colNames := make([]string, len(names))
for i, uname := range names {
colNames[i] = colorize(uname.Value().(*Member).User)
activeColNames := []string{}
awayColNames := []string{}
for _, uname := range names {
user := uname.Value().(*Member).User
colUser := colorize(user)
if isAway, _, _ := user.GetAway(); isAway {
awayColNames = append(awayColNames, colUser)
} else {
activeColNames = append(activeColNames, colUser)
}
}
numPeople := strconv.Itoa(len(names))
activePeople := strings.Join(activeColNames, ", ")

if len(awayColNames) > 0 {
awayPeople := strings.Join(awayColNames, ",")
room.Send(message.NewSystemMsgP(msg.From(), numPeople, " connected: ", activePeople, "; away: ", awayPeople))
} else {
room.Send(message.NewSystemMsgP(msg.From(), numPeople, " connected: ", activePeople))
}

body := fmt.Sprintf("%d connected: %s", len(colNames), strings.Join(colNames, ", "))
room.Send(message.NewSystemMsg(body, msg.From()))
return nil
},
})
Expand Down
50 changes: 41 additions & 9 deletions chat/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,38 +202,70 @@ func (m PrivateMsg) String() string {
return m.Render(nil)
}

var _ Message = &SystemMsg{}

// SystemMsg is a response sent from the server directly to a user, not shown
// to anyone else. Usually in response to something, like /help.
type SystemMsg struct {
Msg
to *User
parts []string
timestamp time.Time
to *User
}

var systemMessagePrefix = []string{"-> "}

func NewSystemMsg(body string, to *User) *SystemMsg {
return &SystemMsg{
Msg: Msg{
body: body,
timestamp: time.Now(),
},
to: to,
parts: append(systemMessagePrefix, body),
timestamp: time.Now(),
to: to,
}
}

func NewSystemMsgP(to *User, parts ...string) *SystemMsg {
return &SystemMsg{
to: to,
parts: append(systemMessagePrefix, parts...),
timestamp: time.Now(),
}
}

func (m *SystemMsg) renderPlain() string {
spArgs := make([]interface{}, len(m.parts))
for i, arg := range m.parts {
spArgs[i] = arg
}
return fmt.Sprint(spArgs...)
}

func (m *SystemMsg) Render(t *Theme) string {
if t == nil {
return m.String()
}
return t.ColorSys(m.String())

colPart := make([]interface{}, len(m.parts))
for i, part := range m.parts {
colPart[i] = t.ColorSys(part)
}
return fmt.Sprint(colPart...)
}

func (m *SystemMsg) String() string {
return fmt.Sprintf("-> %s", m.body)
return fmt.Sprintf("-> %s", m.renderPlain())
}

func (m *SystemMsg) To() *User {
return m.to
}

func (m *SystemMsg) Command() string {
return ""
}

func (m *SystemMsg) Timestamp() time.Time {
return m.timestamp
}

// AnnounceMsg is a message sent from the server to everyone, like a join or
// leave event.
type AnnounceMsg struct {
Expand Down