CardSimEngine/cardsim/messages.go

59 lines
1.6 KiB
Go
Raw Normal View History

package cardsim
import "fmt"
// 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
}
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
}