From e35c15693683af745ac94e49dccab183c1fbc889 Mon Sep 17 00:00:00 2001 From: Fl_GUI Date: Sun, 12 May 2024 18:37:03 +0200 Subject: [PATCH] generate tags from prototype --- twitch/core/messages/messages.go | 10 ++++ twitch/core/messages/privmsg.go | 19 -------- twitch/core/privmsg/privmsg.go | 27 +++++++++++ twitch/core/privmsg/privmsgtags.go | 32 +++++++++++++ x/corechatclient/main.go | 5 +- x/generate/tagprototype/main.go | 77 ++++++++++++++++++++++++++++++ 6 files changed, 149 insertions(+), 21 deletions(-) delete mode 100644 twitch/core/messages/privmsg.go create mode 100644 twitch/core/privmsg/privmsg.go create mode 100755 twitch/core/privmsg/privmsgtags.go create mode 100644 x/generate/tagprototype/main.go diff --git a/twitch/core/messages/messages.go b/twitch/core/messages/messages.go index 6605b72..b56fb6c 100644 --- a/twitch/core/messages/messages.go +++ b/twitch/core/messages/messages.go @@ -7,3 +7,13 @@ type Message irc.Message func (m Message) String() string { return irc.Message(m).String() } + +// Returns the tag of the corresponding tag key, or nil if not found +func (m Message) GetTag(key string) *irc.Tag { + for _, tag := range m.Tags { + if tag.Key == key { + return &tag + } + } + return nil +} diff --git a/twitch/core/messages/privmsg.go b/twitch/core/messages/privmsg.go deleted file mode 100644 index 3807571..0000000 --- a/twitch/core/messages/privmsg.go +++ /dev/null @@ -1,19 +0,0 @@ -package messages - -import ( - "twitchchat/twitch/core/commands" -) - -type PrivMsg Message - -func IsPrivateMessage(m Message) bool { - return string(m.Command) == commands.PrivMsg -} - -func (p PrivMsg) User() string { - return p.Prefix.Name -} - -func (p PrivMsg) Text() string { - return p.Params.Trailing -} diff --git a/twitch/core/privmsg/privmsg.go b/twitch/core/privmsg/privmsg.go new file mode 100644 index 0000000..aec04ea --- /dev/null +++ b/twitch/core/privmsg/privmsg.go @@ -0,0 +1,27 @@ +package privmsg + +import ( + "twitchchat/irc" + "twitchchat/twitch/core/commands" + "twitchchat/twitch/core/messages" +) + +type PrivMsg messages.Message + +//go:generate tagprototype -out "privmsgtags.go" -type PrivMsgTag -prototype "@badge-info=;badges=;bits=client-nonce=;color=;display-name=;emotes=;first-msg=;flags=;id=;mod=;room-id=;subscriber=;tmi-sent-ts=;turbo=;user-id=;user-type=;reply-parent-msg-id=;reply-parent-user-id=;reply-parent-user-login=;reply-parent-display-name=;reply-parent-msg-body=;reply-thread-parent-msg-id=;reply-thread-parent-user-login=;vip=" -package privmsg + +func IsPrivateMessage(m messages.Message) bool { + return string(m.Command) == commands.PrivMsg +} + +func (p PrivMsg) User() string { + return p.Prefix.Name +} + +func (p PrivMsg) Text() string { + return p.Params.Trailing +} + +func (p PrivMsg) GetTag(key PrivMsgTag) *irc.Tag { + return messages.Message(p).GetTag(string(key)) +} diff --git a/twitch/core/privmsg/privmsgtags.go b/twitch/core/privmsg/privmsgtags.go new file mode 100755 index 0000000..f9e3571 --- /dev/null +++ b/twitch/core/privmsg/privmsgtags.go @@ -0,0 +1,32 @@ +//Generated by twitch-chat/x/generate/tagprototype + +package privmsg + +type PrivMsgTag string + +const ( + BadgeInfo PrivMsgTag = "badge-info" + Badges PrivMsgTag = "badges" + Bits PrivMsgTag = "bits" + Color PrivMsgTag = "color" + DisplayName PrivMsgTag = "display-name" + Emotes PrivMsgTag = "emotes" + FirstMsg PrivMsgTag = "first-msg" + Flags PrivMsgTag = "flags" + Id PrivMsgTag = "id" + Mod PrivMsgTag = "mod" + RoomId PrivMsgTag = "room-id" + Subscriber PrivMsgTag = "subscriber" + TmiSentTs PrivMsgTag = "tmi-sent-ts" + Turbo PrivMsgTag = "turbo" + UserId PrivMsgTag = "user-id" + UserType PrivMsgTag = "user-type" + ReplyParentMsgId PrivMsgTag = "reply-parent-msg-id" + ReplyParentUserId PrivMsgTag = "reply-parent-user-id" + ReplyParentUserLogin PrivMsgTag = "reply-parent-user-login" + ReplyParentDisplayName PrivMsgTag = "reply-parent-display-name" + ReplyParentMsgBody PrivMsgTag = "reply-parent-msg-body" + ReplyThreadParentMsgId PrivMsgTag = "reply-thread-parent-msg-id" + ReplyThreadParentUserLogin PrivMsgTag = "reply-thread-parent-user-login" + Vip PrivMsgTag = "vip" +) diff --git a/x/corechatclient/main.go b/x/corechatclient/main.go index 1bb5f34..71a06cb 100644 --- a/x/corechatclient/main.go +++ b/x/corechatclient/main.go @@ -7,6 +7,7 @@ import ( "strings" "twitchchat/twitch/core" "twitchchat/twitch/core/messages" + "twitchchat/twitch/core/privmsg" ) var channel = flag.String("channel", "fl_gui", "channel to read") @@ -50,8 +51,8 @@ func main() { var format string rmax := NewRunningMax(50) for m := range msgs { - if messages.IsPrivateMessage(m) { - priv := messages.PrivMsg(m) + if privmsg.IsPrivateMessage(m) { + priv := privmsg.PrivMsg(m) u := priv.User() mx := rmax.Push(len(u)) format = fmt.Sprintf("%%%ds: %%s\n", mx) diff --git a/x/generate/tagprototype/main.go b/x/generate/tagprototype/main.go new file mode 100644 index 0000000..c449df9 --- /dev/null +++ b/x/generate/tagprototype/main.go @@ -0,0 +1,77 @@ +package main + +import ( + "flag" + "fmt" + "os" + "strings" + "unicode" +) + +var prototypeString = flag.String("prototype", "", "The prototype of tags in the form of\n@tag-name-1=;tag-name-2=;...") +var outFile = flag.String("out", "", "File to write to") +var ofType = flag.String("type", "", "Type to wrap tag values in") +var packageName = flag.String("package", "", "package for the file") + +func extractTagsFromPrototype() []string { + // trim leading "@", and split into key= + parts := strings.Split((*prototypeString)[1:], ";") + res := make([]string, len(parts)) + for i, p := range parts { + res[i] = p[0:strings.Index(p, "=")] + } + + return res +} + +var bob = strings.Builder{} + +func tagToName(t string) string { + bob.Reset() + var turnNextUpper = true + for _, r := range t { + if turnNextUpper { + bob.WriteRune(unicode.ToUpper(r)) + turnNextUpper = false + } else if r == '-' { + turnNextUpper = true + } else { + bob.WriteRune(r) + } + } + + return bob.String() +} + +func main() { + flag.Parse() + + // required arguments + if prototypeString == nil || outFile == nil || packageName == nil { + flag.Usage() + return + } + + f, err := os.OpenFile(*outFile, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0755) + if err != nil { + fmt.Fprintln(os.Stderr, err) + } + + fmt.Fprintf(f, "//Generated by twitch-chat/x/generate/tagprototype\n") + fmt.Fprintf(f, "\n") + fmt.Fprintf(f, "package %s\n", *packageName) + fmt.Fprintf(f, "\n") + + var typ = "string" + if *ofType != "" { + fmt.Fprintf(f, "type %s string\n", *ofType) + fmt.Fprintf(f, "\n") + typ = *ofType + } + + fmt.Fprintf(f, "const (\n") + for _, t := range extractTagsFromPrototype() { + fmt.Fprintf(f, "\t%s %s = %#v\n", tagToName(t), typ, t) + } + fmt.Fprintf(f, ")\n") +} -- 2.47.1