mirror of
https://github.com/diamondburned/cchat-mock.git
synced 2024-12-28 06:06:45 +00:00
Updated to match v0.0.15
This commit is contained in:
parent
5e0e8415d8
commit
de5384bd32
42
channel.go
42
channel.go
|
@ -10,15 +10,20 @@ import (
|
|||
|
||||
"github.com/Pallinder/go-randomdata"
|
||||
"github.com/diamondburned/cchat"
|
||||
"github.com/diamondburned/cchat-mock/segments"
|
||||
"github.com/diamondburned/cchat/text"
|
||||
)
|
||||
|
||||
func init() {}
|
||||
|
||||
type Channel struct {
|
||||
session *Session
|
||||
id uint32
|
||||
name string
|
||||
done chan struct{}
|
||||
send chan cchat.SendableMessage // ideally this should be another type
|
||||
lastID uint32
|
||||
id uint32
|
||||
name string
|
||||
username text.Rich
|
||||
|
||||
done chan struct{}
|
||||
send chan cchat.SendableMessage // ideally this should be another type
|
||||
lastID uint32
|
||||
}
|
||||
|
||||
var (
|
||||
|
@ -26,18 +31,25 @@ var (
|
|||
_ cchat.ServerMessage = (*Channel)(nil)
|
||||
_ cchat.ServerMessageSender = (*Channel)(nil)
|
||||
_ cchat.ServerMessageSendCompleter = (*Channel)(nil)
|
||||
_ cchat.ServerNickname = (*Channel)(nil)
|
||||
)
|
||||
|
||||
func (ch *Channel) ID() string {
|
||||
return strconv.Itoa(int(ch.id))
|
||||
}
|
||||
|
||||
func (ch *Channel) Name() (string, error) {
|
||||
return ch.name, nil
|
||||
func (ch *Channel) Name(labeler cchat.LabelContainer) error {
|
||||
labeler.SetLabel(text.Rich{Content: ch.name})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ch *Channel) Nickname(labeler cchat.LabelContainer) error {
|
||||
labeler.SetLabel(ch.username)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ch *Channel) JoinServer(container cchat.MessagesContainer) error {
|
||||
var lastAuthor string
|
||||
var lastAuthor text.Rich
|
||||
|
||||
var nextID = func() uint32 {
|
||||
id := ch.lastID
|
||||
|
@ -74,7 +86,7 @@ func (ch *Channel) JoinServer(container cchat.MessagesContainer) error {
|
|||
for {
|
||||
select {
|
||||
case msg := <-ch.send:
|
||||
container.CreateMessage(echoMessage(msg, nextID(), ch.session.username))
|
||||
container.CreateMessage(echoMessage(msg, nextID(), ch.username))
|
||||
case <-ticker.C:
|
||||
container.CreateMessage(randomMsg())
|
||||
case <-editTick.C:
|
||||
|
@ -127,9 +139,13 @@ func generateChannels(s *Session, amount int) []cchat.Server {
|
|||
var channels = make([]cchat.Server, amount)
|
||||
for i := range channels {
|
||||
channels[i] = &Channel{
|
||||
session: s,
|
||||
id: atomic.AddUint32(&s.lastid, 1),
|
||||
name: "#" + randomdata.Noun(),
|
||||
id: atomic.AddUint32(&s.lastid, 1),
|
||||
name: "#" + randomdata.Noun(),
|
||||
username: text.Rich{
|
||||
Content: s.username,
|
||||
// hot pink-ish colored
|
||||
Segments: []text.Segment{segments.NewColored(s.username, 0xE88AF8)},
|
||||
},
|
||||
}
|
||||
}
|
||||
return channels
|
||||
|
|
2
go.mod
2
go.mod
|
@ -4,5 +4,5 @@ go 1.14
|
|||
|
||||
require (
|
||||
github.com/Pallinder/go-randomdata v1.2.0
|
||||
github.com/diamondburned/cchat v0.0.13
|
||||
github.com/diamondburned/cchat v0.0.15
|
||||
)
|
||||
|
|
6
go.sum
6
go.sum
|
@ -26,4 +26,10 @@ github.com/diamondburned/cchat v0.0.12 h1:ahu5xTRGtMrsudbUy6G2cPIvu0s+3OvXzX5hCx
|
|||
github.com/diamondburned/cchat v0.0.12/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU=
|
||||
github.com/diamondburned/cchat v0.0.13 h1:p8SyFjiRVCTjvwSJ4FsICGVYVZ3g0Iu02FrwmLuKiKE=
|
||||
github.com/diamondburned/cchat v0.0.13/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU=
|
||||
github.com/diamondburned/cchat v0.0.14 h1:QpYRndVRBgg0DZHNrjbf+FNZ7dJlAsP7PlR+JATwauE=
|
||||
github.com/diamondburned/cchat v0.0.14/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU=
|
||||
github.com/diamondburned/cchat v0.0.15 h1:1o4OX8zw/CdSv3Idaylz7vjHVOZKEi/xkg8BpEvtsHY=
|
||||
github.com/diamondburned/cchat v0.0.15/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU=
|
||||
github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac=
|
||||
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
|
|
33
message.go
33
message.go
|
@ -7,13 +7,14 @@ import (
|
|||
|
||||
"github.com/Pallinder/go-randomdata"
|
||||
"github.com/diamondburned/cchat"
|
||||
"github.com/diamondburned/cchat-mock/segments"
|
||||
"github.com/diamondburned/cchat/text"
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
id uint32
|
||||
time time.Time
|
||||
author string
|
||||
author text.Rich
|
||||
content string
|
||||
nonce string
|
||||
}
|
||||
|
@ -26,14 +27,14 @@ var (
|
|||
_ cchat.MessageMentioned = (*Message)(nil)
|
||||
)
|
||||
|
||||
func newEmptyMessage(id uint32, author string) Message {
|
||||
func newEmptyMessage(id uint32, author text.Rich) Message {
|
||||
return Message{
|
||||
id: id,
|
||||
author: author,
|
||||
}
|
||||
}
|
||||
|
||||
func newRandomMessage(id uint32, author string) Message {
|
||||
func newRandomMessage(id uint32, author text.Rich) Message {
|
||||
return Message{
|
||||
id: id,
|
||||
time: time.Now(),
|
||||
|
@ -42,7 +43,7 @@ func newRandomMessage(id uint32, author string) Message {
|
|||
}
|
||||
}
|
||||
|
||||
func echoMessage(sendable cchat.SendableMessage, id uint32, author string) Message {
|
||||
func echoMessage(sendable cchat.SendableMessage, id uint32, author text.Rich) Message {
|
||||
var echo = Message{
|
||||
id: id,
|
||||
time: time.Now(),
|
||||
|
@ -57,10 +58,15 @@ func echoMessage(sendable cchat.SendableMessage, id uint32, author string) Messa
|
|||
|
||||
func randomMessage(id uint32) Message {
|
||||
var now = time.Now()
|
||||
var author = randomdata.SillyName()
|
||||
|
||||
return Message{
|
||||
id: id,
|
||||
time: now,
|
||||
author: randomdata.SillyName(),
|
||||
id: id,
|
||||
time: now,
|
||||
author: text.Rich{
|
||||
Content: author,
|
||||
Segments: []text.Segment{segments.NewRandomColored(author)},
|
||||
},
|
||||
content: randomdata.Paragraph(),
|
||||
}
|
||||
}
|
||||
|
@ -88,21 +94,24 @@ func (m Message) Nonce() string {
|
|||
// Mentioned is true when the message content contains the author's name.
|
||||
func (m Message) Mentioned() bool {
|
||||
// hack
|
||||
return strings.Contains(m.content, m.author)
|
||||
return strings.Contains(m.content, m.author.Content)
|
||||
}
|
||||
|
||||
type Author struct {
|
||||
name string
|
||||
name text.Rich
|
||||
}
|
||||
|
||||
var _ cchat.MessageAuthor = (*Author)(nil)
|
||||
var (
|
||||
_ cchat.MessageAuthor = (*Author)(nil)
|
||||
_ cchat.MessageAuthorAvatar = (*Author)(nil)
|
||||
)
|
||||
|
||||
func (a Author) ID() string {
|
||||
return a.name
|
||||
return a.name.Content
|
||||
}
|
||||
|
||||
func (a Author) Name() text.Rich {
|
||||
return text.Rich{Content: a.name}
|
||||
return a.name
|
||||
}
|
||||
|
||||
func (a Author) Avatar() string {
|
||||
|
|
69
segments/color.go
Normal file
69
segments/color.go
Normal file
|
@ -0,0 +1,69 @@
|
|||
package segments
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/diamondburned/cchat/text"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
}
|
||||
|
||||
type Colored struct {
|
||||
strlen int
|
||||
color uint32
|
||||
}
|
||||
|
||||
var (
|
||||
_ text.Colorer = (*Colored)(nil)
|
||||
_ text.Segment = (*Colored)(nil)
|
||||
)
|
||||
|
||||
func NewColored(str string, color uint32) Colored {
|
||||
return Colored{len(str), color}
|
||||
}
|
||||
|
||||
func NewRandomColored(str string) Colored {
|
||||
return Colored{len(str), RandomColor()}
|
||||
}
|
||||
|
||||
func (color Colored) Bounds() (start, end int) {
|
||||
return 0, color.strlen - 1
|
||||
}
|
||||
|
||||
func (color Colored) Color() uint32 {
|
||||
return color.color
|
||||
}
|
||||
|
||||
// var Colors = []uint32{
|
||||
// 0x55cdfc,
|
||||
// 0x609ffb,
|
||||
// 0x6b78fa,
|
||||
// 0x9375f9,
|
||||
// 0xc180f8,
|
||||
// 0xe88af8,
|
||||
// 0xf794e7,
|
||||
// 0xf79ecc,
|
||||
// 0xf7a8b8,
|
||||
// }
|
||||
|
||||
// func randomColor() uint32 {
|
||||
// return Colors[rand.Intn(len(Colors))]
|
||||
// }
|
||||
|
||||
var Colors = []uint32{
|
||||
0xF5ABBA,
|
||||
0x5ACFFA, // starts here
|
||||
0xF5ABBA,
|
||||
0xFFFFFF,
|
||||
}
|
||||
|
||||
var colorIndex uint32 = 0
|
||||
|
||||
func RandomColor() uint32 {
|
||||
i := atomic.AddUint32(&colorIndex, 1) % uint32(len(Colors))
|
||||
return Colors[i]
|
||||
}
|
14
server.go
14
server.go
|
@ -7,6 +7,7 @@ import (
|
|||
|
||||
"github.com/Pallinder/go-randomdata"
|
||||
"github.com/diamondburned/cchat"
|
||||
"github.com/diamondburned/cchat/text"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
|
@ -25,8 +26,9 @@ func (sv *Server) ID() string {
|
|||
return strconv.Itoa(int(sv.id))
|
||||
}
|
||||
|
||||
func (sv *Server) Name() (string, error) {
|
||||
return sv.name, nil
|
||||
func (sv *Server) Name(labeler cchat.LabelContainer) error {
|
||||
labeler.SetLabel(text.Rich{Content: sv.name})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sv *Server) Servers(container cchat.ServersContainer) error {
|
||||
|
@ -39,14 +41,14 @@ func GenerateServers(s *Session) []cchat.Server {
|
|||
}
|
||||
|
||||
func generateServers(s *Session, amount int) []cchat.Server {
|
||||
var channels = make([]cchat.Server, amount)
|
||||
for i := range channels {
|
||||
channels[i] = &Server{
|
||||
var servers = make([]cchat.Server, amount)
|
||||
for i := range servers {
|
||||
servers[i] = &Server{
|
||||
session: s,
|
||||
id: atomic.AddUint32(&s.lastid, 1),
|
||||
name: randomdata.Noun(),
|
||||
children: generateChannels(s, rand.Intn(12)),
|
||||
}
|
||||
}
|
||||
return channels
|
||||
return servers
|
||||
}
|
||||
|
|
10
service.go
10
service.go
|
@ -8,6 +8,7 @@ import (
|
|||
|
||||
"github.com/diamondburned/cchat"
|
||||
"github.com/diamondburned/cchat/services"
|
||||
"github.com/diamondburned/cchat/text"
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
@ -124,12 +125,13 @@ func newSession(username string) *Session {
|
|||
return ses
|
||||
}
|
||||
|
||||
func (s *Session) Name() (string, error) {
|
||||
return s.username, nil
|
||||
func (s *Session) ID() string {
|
||||
return s.username
|
||||
}
|
||||
|
||||
func (s *Session) UserID() string {
|
||||
return s.username
|
||||
func (s *Session) Name(labeler cchat.LabelContainer) error {
|
||||
labeler.SetLabel(text.Rich{Content: s.username})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Session) Servers(container cchat.ServersContainer) error {
|
||||
|
|
Loading…
Reference in a new issue