commit 9a9a00d
Hubert Hirtz
·
2020-08-20 11:22:32 +0000 UTC
parent ba98736
ui: Strip formatting codes in notifications
3 files changed,
+20,
-5
M
app.go
+16,
-1
1@@ -275,7 +275,7 @@ func (app *App) notifyHighlight(context, nick, content string) {
2 command = strings.Replace(command, "%%", "%", -1)
3 command = strings.Replace(command, "%c", context, -1)
4 command = strings.Replace(command, "%n", nick, -1)
5- command = strings.Replace(command, "%m", content, -1)
6+ command = strings.Replace(command, "%m", cleanMessage(content), -1)
7 exec.Command(sh, "-c", command).Run()
8 }
9
10@@ -476,3 +476,18 @@ func (app *App) completions(cursorIdx int, text []rune) []ui.Completion {
11
12 return cs
13 }
14+
15+func cleanMessage(s string) string {
16+ var res strings.Builder
17+ var sb ui.StyleBuffer
18+ res.Grow(len(s))
19+ for _, r := range s {
20+ if _, ok := sb.WriteRune(r); ok != 0 {
21+ if 1 < ok {
22+ res.WriteRune(',')
23+ }
24+ res.WriteRune(r)
25+ }
26+ }
27+ return res.String()
28+}
+1,
-1
1@@ -245,7 +245,7 @@ func (b *buffer) DrawLines(screen tcell.Screen, width, height, nickColWidth int)
2 x = x0
3 y := y0
4
5- var sb styleBuffer
6+ var sb StyleBuffer
7 sb.Reset()
8 for i, r := range line.body {
9 if 0 < len(nls) && i == nls[0] {
+3,
-3
1@@ -45,7 +45,7 @@ func StringWidth(s string) int {
2 return wb.Width()
3 }
4
5-type styleBuffer struct {
6+type StyleBuffer struct {
7 st tcell.Style
8 color colorBuffer
9 bold bool
10@@ -54,7 +54,7 @@ type styleBuffer struct {
11 underline bool
12 }
13
14-func (sb *styleBuffer) Reset() {
15+func (sb *StyleBuffer) Reset() {
16 sb.color.Reset()
17 sb.st = tcell.StyleDefault
18 sb.bold = false
19@@ -63,7 +63,7 @@ func (sb *styleBuffer) Reset() {
20 sb.underline = false
21 }
22
23-func (sb *styleBuffer) WriteRune(r rune) (st tcell.Style, ok int) {
24+func (sb *StyleBuffer) WriteRune(r rune) (st tcell.Style, ok int) {
25 if r == 0x00 || r == 0x0F {
26 sb.Reset()
27 return sb.st, 0