commit 0023f4c
Hubert Hirtz
·
2020-08-16 14:26:37 +0000 UTC
parent cb1f094
ui: Reverse color of the nickname on highlight
3 files changed,
+50,
-38
M
app.go
+30,
-25
1@@ -29,7 +29,7 @@ func NewApp(cfg Config) (app *App, err error) {
2 }
3
4 var conn *tls.Conn
5- app.win.AddLine(ui.Home, ui.NewLineNow("--", fmt.Sprintf("Connecting to %s...", cfg.Addr)), false)
6+ app.win.AddLine(ui.Home, ui.NewLineNow("--", fmt.Sprintf("Connecting to %s...", cfg.Addr)))
7 conn, err = tls.Dial("tcp", cfg.Addr, nil)
8 if err != nil {
9 return
10@@ -85,55 +85,46 @@ func (app *App) handleIRCEvent(ev irc.Event) {
11 if ev.Outgoing {
12 head = "DEBUG OUT --"
13 }
14- app.win.AddLine(ui.Home, ui.NewLineNow(head, ev.Message), false)
15+ app.win.AddLine(ui.Home, ui.NewLineNow(head, ev.Message))
16 case irc.RegisteredEvent:
17- app.win.AddLine(ui.Home, ui.NewLineNow("--", "Connected to the server"), false)
18+ app.win.AddLine(ui.Home, ui.NewLineNow("--", "Connected to the server"))
19 if app.cfg.Highlights == nil {
20 app.highlights[0] = app.s.NickCf()
21 }
22 case irc.SelfNickEvent:
23 line := fmt.Sprintf("\x0314%s\x03\u2192\x0314%s\x03", ev.FormerNick, ev.NewNick)
24- app.win.AddLine(ui.Home, ui.NewLine(ev.Time, "--", line, true), true)
25+ app.win.AddLine(ui.Home, ui.NewLine(ev.Time, "--", line, true, true))
26 case irc.UserNickEvent:
27 line := fmt.Sprintf("\x0314%s\x03\u2192\x0314%s\x03", ev.FormerNick, ev.NewNick)
28- app.win.AddLine(ui.Home, ui.NewLine(ev.Time, "--", line, true), false)
29+ app.win.AddLine(ui.Home, ui.NewLine(ev.Time, "--", line, true, false))
30 case irc.SelfJoinEvent:
31 app.win.AddBuffer(ev.Channel)
32 case irc.UserJoinEvent:
33 line := fmt.Sprintf("\x033+\x0314%s\x03", ev.Nick)
34- app.win.AddLine(ev.Channel, ui.NewLine(ev.Time, "--", line, true), false)
35+ app.win.AddLine(ev.Channel, ui.NewLine(ev.Time, "--", line, true, false))
36 case irc.SelfPartEvent:
37 app.win.RemoveBuffer(ev.Channel)
38 case irc.UserPartEvent:
39 line := fmt.Sprintf("\x034-\x0314%s\x03", ev.Nick)
40 for _, channel := range ev.Channels {
41- app.win.AddLine(channel, ui.NewLine(ev.Time, "--", line, true), false)
42+ app.win.AddLine(channel, ui.NewLine(ev.Time, "--", line, true, false))
43 }
44 case irc.QueryMessageEvent:
45 if ev.Command == "PRIVMSG" {
46- l := ui.LineFromIRCMessage(ev.Time, ev.Nick, ev.Content, false)
47- app.win.AddLine(ui.Home, l, true)
48+ l := ui.LineFromIRCMessage(ev.Time, ev.Nick, ev.Content, false, true)
49+ app.win.AddLine(ui.Home, l)
50 app.win.TypingStop(ui.Home, ev.Nick)
51 } else if ev.Command == "NOTICE" {
52- l := ui.LineFromIRCMessage(ev.Time, ev.Nick, ev.Content, true)
53- app.win.AddLine("", l, true)
54+ l := ui.LineFromIRCMessage(ev.Time, ev.Nick, ev.Content, true, false)
55+ app.win.AddLine("", l)
56 app.win.TypingStop("", ev.Nick)
57 } else {
58 log.Panicf("received unknown command for query event: %q\n", ev.Command)
59 }
60 case irc.ChannelMessageEvent:
61- l := ui.LineFromIRCMessage(ev.Time, ev.Nick, ev.Content, ev.Command == "NOTICE")
62-
63- lContent := strings.ToLower(ev.Content)
64- isHighlight := false
65- for _, h := range app.highlights {
66- if strings.Contains(lContent, h) {
67- isHighlight = true
68- break
69- }
70- }
71-
72- app.win.AddLine(ev.Channel, l, isHighlight)
73+ isHighlight := app.isHighlight(ev.Nick, ev.Content)
74+ l := ui.LineFromIRCMessage(ev.Time, ev.Nick, ev.Content, ev.Command == "NOTICE", isHighlight)
75+ app.win.AddLine(ev.Channel, l)
76 app.win.TypingStop(ev.Channel, ev.Nick)
77 case irc.QueryTagEvent:
78 if ev.Typing == irc.TypingActive || ev.Typing == irc.TypingPaused {
79@@ -152,7 +143,8 @@ func (app *App) handleIRCEvent(ev irc.Event) {
80 for _, m := range ev.Messages {
81 switch m := m.(type) {
82 case irc.ChannelMessageEvent:
83- l := ui.LineFromIRCMessage(m.Time, m.Nick, m.Content, m.Command == "NOTICE")
84+ isHighlight := app.isHighlight(m.Nick, m.Content)
85+ l := ui.LineFromIRCMessage(m.Time, m.Nick, m.Content, m.Command == "NOTICE", isHighlight)
86 lines = append(lines, l)
87 default:
88 panic("TODO")
89@@ -265,6 +257,19 @@ func (app *App) handleUIEvent(ev tcell.Event) {
90 }
91 }
92
93+func (app *App) isHighlight(nick, content string) bool {
94+ if app.s.NickCf() == strings.ToLower(nick) {
95+ return false
96+ }
97+ contentCf := strings.ToLower(content)
98+ for _, h := range app.highlights {
99+ if strings.Contains(contentCf, h) {
100+ return true
101+ }
102+ }
103+ return false
104+}
105+
106 func parseCommand(s string) (command, args string) {
107 if s == "" {
108 return
109@@ -297,7 +302,7 @@ func (app *App) handleInput(buffer, content string) {
110
111 app.s.PrivMsg(buffer, args)
112 if !app.s.HasCapability("echo-message") {
113- app.win.AddLine(buffer, ui.NewLineNow(app.s.Nick(), args), false)
114+ app.win.AddLine(buffer, ui.NewLineNow(app.s.Nick(), args))
115 }
116 case "QUOTE":
117 app.s.SendRaw(args)
+17,
-10
1@@ -31,22 +31,25 @@ type point struct {
2 }
3
4 type Line struct {
5- at time.Time
6- head string
7- body string
8- isStatus bool
9+ at time.Time
10+ head string
11+ body string
12+
13+ isStatus bool
14+ isHighlight bool
15
16 splitPoints []point
17 width int
18 newLines []int
19 }
20
21-func NewLine(at time.Time, head string, body string, isStatus bool) Line {
22+func NewLine(at time.Time, head string, body string, isStatus bool, isHighlight bool) Line {
23 l := Line{
24 at: at,
25 head: head,
26 body: body,
27 isStatus: isStatus,
28+ isHighlight: isHighlight,
29 splitPoints: []point{},
30 newLines: []int{},
31 }
32@@ -55,10 +58,10 @@ func NewLine(at time.Time, head string, body string, isStatus bool) Line {
33 }
34
35 func NewLineNow(head, body string) Line {
36- return NewLine(time.Now(), head, body, false)
37+ return NewLine(time.Now(), head, body, false, false)
38 }
39
40-func LineFromIRCMessage(at time.Time, nick string, content string, isNotice bool) Line {
41+func LineFromIRCMessage(at time.Time, nick string, content string, isNotice bool, isHighlight bool) Line {
42 if strings.HasPrefix(content, "\x01ACTION") {
43 c := ircColorCode(identColor(nick))
44 content = fmt.Sprintf("%s%s\x0F%s", c, nick, content[7:])
45@@ -68,7 +71,7 @@ func LineFromIRCMessage(at time.Time, nick string, content string, isNotice bool
46 content = fmt.Sprintf("(%s%s\x0F: %s)", c, nick, content)
47 nick = "*"
48 }
49- return NewLine(at, nick, content, false)
50+ return NewLine(at, nick, content, false, isHighlight)
51 }
52
53 func (l *Line) computeSplitPoints() {
54@@ -235,7 +238,11 @@ func (b *buffer) DrawLines(screen tcell.Screen, width int, height int) {
55 head := truncate(line.head, nickColWidth, "\u2026")
56 x := 6 + nickColWidth - StringWidth(head)
57 c := identColor(line.head)
58+ if line.isHighlight {
59+ st = st.Reverse(true)
60+ }
61 printString(screen, &x, y0, st.Foreground(colorFromCode(c)), head)
62+ st = st.Reverse(false)
63
64 x = x0
65 y := y0
66@@ -333,7 +340,7 @@ func (bs *bufferList) Remove(title string) (ok bool) {
67 return
68 }
69
70-func (bs *bufferList) AddLine(title string, line Line, isHighlight bool) {
71+func (bs *bufferList) AddLine(title string, line Line) {
72 idx := bs.idx(title)
73 if idx < 0 {
74 return
75@@ -358,7 +365,7 @@ func (bs *bufferList) AddLine(title string, line Line, isHighlight bool) {
76 if !line.isStatus && idx != bs.current {
77 b.unread = true
78 }
79- if isHighlight && idx != bs.current {
80+ if line.isHighlight && idx != bs.current {
81 b.highlights++
82 }
83 }
M
ui/ui.go
+3,
-3
1@@ -46,7 +46,7 @@ func New() (ui *UI, err error) {
2 hmIdx := rand.Intn(len(homeMessages))
3 ui.bs = newBufferList(w, h)
4 ui.bs.Add(Home)
5- ui.bs.AddLine("", NewLineNow("--", homeMessages[hmIdx]), false)
6+ ui.bs.AddLine("", NewLineNow("--", homeMessages[hmIdx]))
7
8 ui.e = newEditor(w)
9
10@@ -113,8 +113,8 @@ func (ui *UI) RemoveBuffer(title string) {
11 }
12 }
13
14-func (ui *UI) AddLine(buffer string, line Line, isHighlight bool) {
15- ui.bs.AddLine(buffer, line, isHighlight)
16+func (ui *UI) AddLine(buffer string, line Line) {
17+ ui.bs.AddLine(buffer, line)
18 ui.draw()
19 }
20