CardSimEngine/cardsim/messages.go

84 lines
2.1 KiB
Go
Raw Normal View History

package cardsim
2023-04-02 07:20:37 +00:00
import (
"fmt"
"strings"
)
// Message is an opaque interface representing a displayable message.
// Using an interface here allows for implementation of new message display
// and formatting features without rewriting all existing callers.
type Message interface {
fmt.Stringer
}
// Titled desccribes any type that returns a Message as a title, given a Player
// (which it may ignore).
type Titled[C StatsCollection] interface {
Title(*Player[C]) Message
}
type stringMessage string
func (s stringMessage) String() string {
return string(s)
}
// MsgStr returns a Message representing a fixed string.
func MsgStr(s string) Message {
return stringMessage(s)
}
// Msgf is a Sprintf-like function that produces a Message equivalent to the
// one created by MsgStr.
func Msgf(f string, args ...any) Message {
return stringMessage(fmt.Sprintf(f, args...))
}
2023-04-02 03:52:46 +00:00
// A SpecialMessage is a specific, uniquely identifiable message.
type SpecialMessage struct {
msg Message
}
// String implements Message.
func (s *SpecialMessage) String() string {
if s == nil {
return ""
}
return s.msg.String()
}
// Messages that various display surfaces or other components may have a special interpretation of
// and identify specifically. These are largely sentinel values. Nil is a paragraph break.
var (
SectionBreak = &SpecialMessage{MsgStr(" -------------------------------------------------------------------- ")}
ChapterBreak = &SpecialMessage{MsgStr(" ==================================================================== ")}
)
// IsSpecialMessage returns whether a provided Message is a specific SpecialMessage.
func IsSpecialMessage(m Message, s *SpecialMessage) bool {
if m == nil {
return s == nil
}
if s2, ok := m.(*SpecialMessage); ok {
return s == s2
}
return false
}
2023-04-02 07:20:37 +00:00
// MultiMessage is a sequence of messages treated like one message.
type MultiMessage []Message
func (m MultiMessage) String() string {
s := make([]string, len(m))
for i, msg := range m {
if msg == nil {
s[i] = ""
continue
}
s[i] = msg.String()
continue
}
return strings.Join(s, "\n")
}