Debian Codegraph @

ui/rainbow.go

raw symbols SCIP
1 // gomuks - A terminal Matrix client written in Go.
2 // Copyright (C) 2022 Tulir Asokan
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
13 //
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17 package ui
18
19 import (
20 "fmt"
21 "math/rand"
22 "unicode"
23
24 "github.com/rivo/uniseg"
25 "github.com/yuin/goldmark"
26 "github.com/yuin/goldmark/ast"
27 "github.com/yuin/goldmark/renderer"
28 "github.com/yuin/goldmark/renderer/html"
29 "github.com/yuin/goldmark/util"
30 )
31
32 func Rand(n int) (str string) {
33 b := make([]byte, n)
34 rand.Read(b)
35 str = fmt.Sprintf("%x", b)
36 return
37 }
38
39 type extRainbow struct{}
40 type rainbowRenderer struct {
41 HardWraps bool
42 ColorID string
43 }
44
45 var ExtensionRainbow = &extRainbow{}
46 var defaultRB = &rainbowRenderer{HardWraps: true, ColorID: Rand(16)}
47
48 func (er *extRainbow) Extend(m goldmark.Markdown) {
49 m.Renderer().AddOptions(renderer.WithNodeRenderers(util.Prioritized(defaultRB, 0)))
50 }
51
52 func (rb *rainbowRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
53 reg.Register(ast.KindText, rb.renderText)
54 reg.Register(ast.KindString, rb.renderString)
55 }
56
57 type rainbowBufWriter struct {
58 util.BufWriter
59 ColorID string
60 }
61
62 func (rbw rainbowBufWriter) WriteString(s string) (int, error) {
63 i := 0
64 graphemes := uniseg.NewGraphemes(s)
65 for graphemes.Next() {
66 runes := graphemes.Runes()
67 if len(runes) == 1 && unicode.IsSpace(runes[0]) {
68 i2, err := rbw.BufWriter.WriteRune(runes[0])
69 i += i2
70 if err != nil {
71 return i, err
72 }
73 continue
74 }
75 i2, err := fmt.Fprintf(rbw.BufWriter, "<font color=\"%s\">%s</font>", rbw.ColorID, graphemes.Str())
76 i += i2
77 if err != nil {
78 return i, err
79 }
80 }
81 return i, nil
82 }
83
84 func (rbw rainbowBufWriter) Write(data []byte) (int, error) {
85 return rbw.WriteString(string(data))
86 }
87
88 func (rbw rainbowBufWriter) WriteByte(c byte) error {
89 _, err := rbw.WriteRune(rune(c))
90 return err
91 }
92
93 func (rbw rainbowBufWriter) WriteRune(r rune) (int, error) {
94 if unicode.IsSpace(r) {
95 return rbw.BufWriter.WriteRune(r)
96 } else {
97 return fmt.Fprintf(rbw.BufWriter, "<font color=\"%s\">%c</font>", rbw.ColorID, r)
98 }
99 }
100
101 func (rb *rainbowRenderer) renderText(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
102 if !entering {
103 return ast.WalkContinue, nil
104 }
105 n := node.(*ast.Text)
106 segment := n.Segment
107 if n.IsRaw() {
108 html.DefaultWriter.RawWrite(rainbowBufWriter{w, rb.ColorID}, segment.Value(source))
109 } else {
110 html.DefaultWriter.Write(rainbowBufWriter{w, rb.ColorID}, segment.Value(source))
111 if n.HardLineBreak() || (n.SoftLineBreak() && rb.HardWraps) {
112 _, _ = w.WriteString("<br>\n")
113 } else if n.SoftLineBreak() {
114 _ = w.WriteByte('\n')
115 }
116 }
117 return ast.WalkContinue, nil
118 }
119
120 func (rb *rainbowRenderer) renderString(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
121 if !entering {
122 return ast.WalkContinue, nil
123 }
124 n := node.(*ast.String)
125 if n.IsCode() {
126 _, _ = w.Write(n.Value)
127 } else {
128 if n.IsRaw() {
129 html.DefaultWriter.RawWrite(rainbowBufWriter{w, rb.ColorID}, n.Value)
130 } else {
131 html.DefaultWriter.Write(rainbowBufWriter{w, rb.ColorID}, n.Value)
132 }
133 }
134 return ast.WalkContinue, nil
135 }