Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Commit

Permalink
Add spoiler feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Bios-Marcel committed Feb 8, 2019
1 parent 9675db3 commit 449e275
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions internal/ui/chatview.go
Expand Up @@ -29,8 +29,10 @@ var (
codeBlockRegex = regexp.MustCompile("(?s)\x60\x60\x60(.+?)\n(.+?)\x60\x60\x60(?:$|\n)")
channelMentionRegex = regexp.MustCompile("<#\\d*>")
urlRegex = regexp.MustCompile("<?https?://(.+?)(?:(?:/.*)|\\s|$|>)")
shortener = linkshortener.NewShortener(51726)
userColor = "green"
spoilerRegex = regexp.MustCompile("(?s)\\|\\|(.+?)\\|\\|")

shortener = linkshortener.NewShortener(51726)
userColor = "green"
)

// ChatView is using a tview.TextView in order to be able to display messages
Expand All @@ -46,17 +48,20 @@ type ChatView struct {
selection int
selectionMode bool

showSpoilerContent map[string]bool

onMessageAction func(message *discordgo.Message, event *tcell.EventKey) *tcell.EventKey
}

// NewChatView constructs a new ready to use ChatView.
func NewChatView(session *discordgo.Session, ownUserID string) *ChatView {
chatView := ChatView{
internalTextView: tview.NewTextView(),
session: session,
ownUserID: ownUserID,
selection: -1,
selectionMode: true,
internalTextView: tview.NewTextView(),
session: session,
ownUserID: ownUserID,
selection: -1,
selectionMode: true,
showSpoilerContent: make(map[string]bool, 0),
}

if config.GetConfig().ShortenLinks {
Expand Down Expand Up @@ -119,6 +124,18 @@ func NewChatView(session *discordgo.Session, ownUserID string) *ChatView {
return nil
}

if chatView.selection > 0 && chatView.selection < len(chatView.data) && event.Rune() == 's' {
messageID := chatView.data[chatView.selection].ID
currentValue, contains := chatView.showSpoilerContent[messageID]
if contains {
chatView.showSpoilerContent[messageID] = !currentValue
} else {
chatView.showSpoilerContent[messageID] = true
}
chatView.SetMessages(chatView.data)
return nil
}

if chatView.selection > 0 && chatView.selection < len(chatView.data) && chatView.onMessageAction != nil {
return chatView.onMessageAction(chatView.data[chatView.selection], event)
}
Expand Down Expand Up @@ -217,6 +234,11 @@ func (chatView *ChatView) AddMessages(messages []*discordgo.Message) {
messageText = "[gray]removed " + message.Mentions[0].Username + " from the group."
}

shouldShow, contains := chatView.showSpoilerContent[message.ID]
if !contains || !shouldShow {
messageText = spoilerRegex.ReplaceAllString(messageText, "[red]!SPOILER![white]")
}

messageText = channelMentionRegex.
ReplaceAllStringFunc(messageText, func(data string) string {
channelID := strings.TrimSuffix(strings.TrimPrefix(data, "<#"), ">")
Expand Down Expand Up @@ -336,11 +358,14 @@ func (chatView *ChatView) AddMessages(messages []*discordgo.Message) {
}
}

// ClearSelection clears the current selection of messages.
func (chatView *ChatView) ClearSelection() {
chatView.selection = -1
chatView.updateHighlights()
}

// SignalSelectionDeleted notifies the ChatView that its currently selected
// message doesn't exist anymore, moving the selection up by a row if possible.
func (chatView *ChatView) SignalSelectionDeleted() {
if chatView.selection > 0 {
chatView.selection--
Expand Down

0 comments on commit 449e275

Please sign in to comment.