commit f720a01

Hubert Hirtz  ·  2021-10-23 17:26:13 +0000 UTC
parent a3fb513
Simplify string building logic in app.go
1 files changed,  +19, -32
M app.go
M app.go
+19, -32
 1@@ -545,26 +545,23 @@ func (app *App) handleIRCEvent(ev interface{}) {
 2 			// TODO: support autojoining channels with keys
 3 			app.s.Join(channel, "")
 4 		}
 5-		var body ui.StyledStringBuilder
 6-		body.WriteString("Connected to the server")
 7+		body := "Connected to the server"
 8 		if app.s.Nick() != app.cfg.Nick {
 9-			body.WriteString(" as ")
10-			body.WriteString(app.s.Nick())
11+			body = fmt.Sprintf("Connected to the server as %s", app.s.Nick())
12 		}
13 		app.win.AddLine(Home, ui.NotifyUnread, ui.Line{
14 			At:   msg.TimeOrNow(),
15 			Head: "--",
16-			Body: body.StyledString(),
17+			Body: ui.PlainString(body),
18 		})
19 	case irc.SelfNickEvent:
20 		var body ui.StyledStringBuilder
21-		body.Grow(len(ev.FormerNick) + 4 + len(app.s.Nick()))
22-		body.SetStyle(tcell.StyleDefault.Foreground(tcell.ColorGray))
23-		body.WriteString(ev.FormerNick)
24-		body.SetStyle(tcell.StyleDefault)
25-		body.WriteRune('\u2192') // right arrow
26-		body.SetStyle(tcell.StyleDefault.Foreground(tcell.ColorGray))
27-		body.WriteString(app.s.Nick())
28+		body.WriteString(fmt.Sprintf("%s\u2192%s", ev.FormerNick, app.s.Nick()))
29+		textStyle := tcell.StyleDefault.Foreground(tcell.ColorGray)
30+		arrowStyle := tcell.StyleDefault
31+		body.AddStyle(0, textStyle)
32+		body.AddStyle(len(ev.FormerNick), arrowStyle)
33+		body.AddStyle(body.Len()-len(app.s.Nick()), textStyle)
34 		app.addStatusLine(ui.Line{
35 			At:        msg.TimeOrNow(),
36 			Head:      "--",
37@@ -574,13 +571,12 @@ func (app *App) handleIRCEvent(ev interface{}) {
38 		})
39 	case irc.UserNickEvent:
40 		var body ui.StyledStringBuilder
41-		body.Grow(len(ev.FormerNick) + 4 + len(ev.User))
42-		body.SetStyle(tcell.StyleDefault.Foreground(tcell.ColorGray))
43-		body.WriteString(ev.FormerNick)
44-		body.SetStyle(tcell.StyleDefault)
45-		body.WriteRune('\u2192') // right arrow
46-		body.SetStyle(tcell.StyleDefault.Foreground(tcell.ColorGray))
47-		body.WriteString(ev.User)
48+		body.WriteString(fmt.Sprintf("%s\u2192%s", ev.FormerNick, ev.User))
49+		textStyle := tcell.StyleDefault.Foreground(tcell.ColorGray)
50+		arrowStyle := tcell.StyleDefault
51+		body.AddStyle(0, textStyle)
52+		body.AddStyle(len(ev.FormerNick), arrowStyle)
53+		body.AddStyle(body.Len()-len(ev.User), textStyle)
54 		for _, c := range app.s.ChannelsSharedWith(ev.User) {
55 			app.win.AddLine(c, ui.NotifyNone, ui.Line{
56 				At:        msg.TimeOrNow(),
57@@ -662,29 +658,20 @@ func (app *App) handleIRCEvent(ev interface{}) {
58 			})
59 		}
60 	case irc.TopicChangeEvent:
61-		var body ui.StyledStringBuilder
62-		body.Grow(len(ev.Topic) + 18)
63-		body.SetStyle(tcell.StyleDefault.Foreground(tcell.ColorGray))
64-		body.WriteString("Topic changed to: ")
65-		topic := ui.IRCString(ev.Topic)
66-		body.WriteString(topic.String())
67+		body := fmt.Sprintf("Topic changed to: %s", ev.Topic)
68 		app.win.AddLine(ev.Channel, ui.NotifyUnread, ui.Line{
69 			At:        msg.TimeOrNow(),
70 			Head:      "--",
71 			HeadColor: tcell.ColorGray,
72-			Body:      body.StyledString(),
73+			Body:      ui.Styled(body, tcell.StyleDefault.Foreground(tcell.ColorGray)),
74 		})
75 	case irc.ModeChangeEvent:
76-		var body ui.StyledStringBuilder
77-		body.Grow(len(ev.Mode) + 13)
78-		body.SetStyle(tcell.StyleDefault.Foreground(tcell.ColorGray))
79-		body.WriteString("Mode change: ")
80-		body.WriteString(ev.Mode)
81+		body := fmt.Sprintf("Mode change: %s", ev.Mode)
82 		app.win.AddLine(ev.Channel, ui.NotifyUnread, ui.Line{
83 			At:        msg.TimeOrNow(),
84 			Head:      "--",
85 			HeadColor: tcell.ColorGray,
86-			Body:      body.StyledString(),
87+			Body:      ui.Styled(body, tcell.StyleDefault.Foreground(tcell.ColorGray)),
88 		})
89 	case irc.InviteEvent:
90 		var buffer string