Skip to content

Commit

Permalink
add special highlight for code blocks like this
Browse files Browse the repository at this point in the history
  • Loading branch information
Ole Andre Birkedal committed Sep 18, 2020
1 parent e0c2dfb commit 7459ed3
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion ui/buffer_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package ui
import (
"fmt"
"github.com/rivo/tview"
"regexp"
"strings"
"time"
)

Expand Down Expand Up @@ -69,14 +71,35 @@ func (v *View) AddBufferMsg(channel, from, msg string, time int64, bid int) {
if c != nil {
ts := getTimestamp(time)
line := fmt.Sprintf("[-:-:d]%s[-:-:-] <[-:-:b]%s[-:-:-]> %s", ts, tview.Escape(from), tview.Escape(msg))

line = tagifyCodeQuotes(line, `[grey:black:b]`, `[-:-:-]`)
v.writeToBuffer(line, c)
}
})

}

func (v *View) writeToBuffer(line string, c *channel) {
c.SendToChannel(line)
//_, _ = c.chat.Write([]byte("\n" + line))
//c.chat.ScrollToEnd()
}

func trimString(s string) string {
if len(s) < 2 {
return s
}

return s[1 : len(s)-1]
}

func tagifyCodeQuotes(input, startTag, endTag string) string {
regex := regexp.MustCompile("`(.*?)`")
found := regex.FindAllString(input, -1)

for _, f := range found {
tag := fmt.Sprintf("%s%s%s", startTag, trimString(f), endTag)
input = strings.Replace(input, f, tag, -1)
}

return input
}

0 comments on commit 7459ed3

Please sign in to comment.