]> Sergey Matveev's repositories - mmc.git/commitdiff
Show thread mention
authorSergey Matveev <stargrave@stargrave.org>
Sun, 12 Mar 2023 13:05:45 +0000 (16:05 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Sun, 12 Mar 2023 13:08:50 +0000 (16:08 +0300)
cmd/rd/colourized
cmd/rd/main.go

index 9ded197181d2e37a87b43ddf76682b98466e7f2c..6fb294565bd064b2e0569e2ac9022711d95738b0 100755 (executable)
@@ -4,4 +4,5 @@ dir="$(dirname "$(realpath -- "$0")")"
 "$dir"/rd $@ | spc \
     -e blu,"^.?[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}" \
     -e grn," <[^>]+> " \
+    -e red," \[RE\]: " \
     -e yel,"stargrave"
index 0d7e70b65e9280fef79d140c3ff71deb134e62fe..c7c68772c53b727203b36e7aa5a0204203eff1d6 100644 (file)
@@ -23,6 +23,8 @@ import (
        "log"
        "os"
        "path"
+       "strings"
+       "unicode/utf8"
 
        "github.com/fsnotify/fsnotify"
        "github.com/mattermost/mattermost-server/v6/model"
@@ -30,6 +32,20 @@ import (
        "go.stargrave.org/mmc"
 )
 
+var Threads = make(map[string]string)
+
+func rememberPost(post map[string][]string) {
+       text := strings.Split(post["Text"][0], "\n")[0]
+       i := 60
+       if len(text) > i {
+               for i > 0 && !utf8.Valid([]byte{text[i]}) {
+                       i--
+               }
+               text = text[:i] + "..."
+       }
+       Threads[post["Id"][0]] = text
+}
+
 func printPost(m map[string][]string) {
        var tag string
        if len(m["Event"]) > 0 {
@@ -40,7 +56,19 @@ func printPost(m map[string][]string) {
                        tag += "[DEL] "
                }
        }
-       fmt.Printf("\a%s <%s> %s%s\n", m["Created"][0], m["Sender"][0], tag, m["Text"][0])
+       var re string
+       if len(m["RootId"]) > 0 {
+               thread := Threads[m["RootId"][0]]
+               if thread == "" {
+                       thread = m["RootId"][0]
+               }
+               re = " [RE]: " + thread
+       }
+       fmt.Printf(
+               "\a%s <%s> %s%s%s\n",
+               m["Created"][0], m["Sender"][0],
+               tag, m["Text"][0], re,
+       )
        tag += "[FILE] "
        for i, fileId := range m["File"] {
                fmt.Printf(
@@ -82,6 +110,7 @@ func main() {
                ms = ms[len(ms)-*lastNum:]
        }
        for _, m := range ms {
+               rememberPost(m)
                printPost(m)
        }
 
@@ -114,6 +143,7 @@ func main() {
                                }
                                log.Fatalln(err)
                        }
+                       rememberPost(m)
                        printPost(m)
                }
                unlock()