From 7459ed35baede33c27a71de775425b5d3e68e7ae Mon Sep 17 00:00:00 2001 From: Ole Andre Birkedal Date: Fri, 18 Sep 2020 21:40:18 +0200 Subject: [PATCH] add special highlight for `code blocks` like this --- ui/buffer_events.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/ui/buffer_events.go b/ui/buffer_events.go index 58bbe2e..65b8302 100644 --- a/ui/buffer_events.go +++ b/ui/buffer_events.go @@ -3,6 +3,8 @@ package ui import ( "fmt" "github.com/rivo/tview" + "regexp" + "strings" "time" ) @@ -69,10 +71,11 @@ 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) { @@ -80,3 +83,23 @@ func (v *View) writeToBuffer(line string, c *channel) { //_, _ = 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 +} \ No newline at end of file