summaryrefslogtreecommitdiff
path: root/app.go
diff options
context:
space:
mode:
authordelthas <delthas@dille.cc>2021-11-24 16:59:00 +0100
committerHubert Hirtz <hubert@hirtz.pm>2021-11-24 17:34:21 +0100
commitb813210eb6b7a50607985a14f969e33914ae0956 (patch)
treec110cb7cf453ef49ab57a9c8c41baf988a492e66 /app.go
parentShow the current channel topic at the top of the timeline (diff)
Add support for draft/event-playback
- Refactor formatting lines into a function - Store event times in the event - Refactor merging lines into a function - Always merge lines from the history, now that we will add mergeable lines with CHATHISTORY Successfully tested locally.
Diffstat (limited to 'app.go')
-rw-r--r--app.go186
1 files changed, 110 insertions, 76 deletions
diff --git a/app.go b/app.go
index 7b416c8..00c31d2 100644
--- a/app.go
+++ b/app.go
@@ -642,21 +642,9 @@ func (app *App) handleIRCEvent(netID string, ev interface{}) {
Highlight: true,
})
case irc.UserNickEvent:
- var body ui.StyledStringBuilder
- body.WriteString(fmt.Sprintf("%s\u2192%s", ev.FormerNick, ev.User))
- textStyle := tcell.StyleDefault.Foreground(tcell.ColorGray)
- arrowStyle := tcell.StyleDefault
- body.AddStyle(0, textStyle)
- body.AddStyle(len(ev.FormerNick), arrowStyle)
- body.AddStyle(body.Len()-len(ev.User), textStyle)
+ line := app.formatEvent(ev)
for _, c := range s.ChannelsSharedWith(ev.User) {
- app.win.AddLine(netID, c, ui.NotifyNone, ui.Line{
- At: msg.TimeOrNow(),
- Head: "--",
- HeadColor: tcell.ColorGray,
- Body: body.StyledString(),
- Mergeable: true,
- })
+ app.win.AddLine(netID, c, ui.NotifyNone, line)
}
case irc.SelfJoinEvent:
i, added := app.win.AddBuffer(netID, "", ev.Channel)
@@ -685,70 +673,27 @@ func (app *App) handleIRCEvent(netID string, ev interface{}) {
app.lastBuffer = ""
}
case irc.UserJoinEvent:
- var body ui.StyledStringBuilder
- body.Grow(len(ev.User) + 1)
- body.SetStyle(tcell.StyleDefault.Foreground(tcell.ColorGreen))
- body.WriteByte('+')
- body.SetStyle(tcell.StyleDefault.Foreground(tcell.ColorGray))
- body.WriteString(ev.User)
- app.win.AddLine(netID, ev.Channel, ui.NotifyNone, ui.Line{
- At: msg.TimeOrNow(),
- Head: "--",
- HeadColor: tcell.ColorGray,
- Body: body.StyledString(),
- Mergeable: true,
- })
+ line := app.formatEvent(ev)
+ app.win.AddLine(netID, ev.Channel, ui.NotifyNone, line)
case irc.SelfPartEvent:
app.win.RemoveBuffer(netID, ev.Channel)
delete(app.messageBounds, boundKey{netID, ev.Channel})
case irc.UserPartEvent:
- var body ui.StyledStringBuilder
- body.Grow(len(ev.User) + 1)
- body.SetStyle(tcell.StyleDefault.Foreground(tcell.ColorRed))
- body.WriteByte('-')
- body.SetStyle(tcell.StyleDefault.Foreground(tcell.ColorGray))
- body.WriteString(ev.User)
- app.win.AddLine(netID, ev.Channel, ui.NotifyNone, ui.Line{
- At: msg.TimeOrNow(),
- Head: "--",
- HeadColor: tcell.ColorGray,
- Body: body.StyledString(),
- Mergeable: true,
- })
+ line := app.formatEvent(ev)
+ app.win.AddLine(netID, ev.Channel, ui.NotifyNone, line)
case irc.UserQuitEvent:
- var body ui.StyledStringBuilder
- body.Grow(len(ev.User) + 1)
- body.SetStyle(tcell.StyleDefault.Foreground(tcell.ColorRed))
- body.WriteByte('-')
- body.SetStyle(tcell.StyleDefault.Foreground(tcell.ColorGray))
- body.WriteString(ev.User)
+ line := app.formatEvent(ev)
for _, c := range ev.Channels {
- app.win.AddLine(netID, c, ui.NotifyNone, ui.Line{
- At: msg.TimeOrNow(),
- Head: "--",
- HeadColor: tcell.ColorGray,
- Body: body.StyledString(),
- Mergeable: true,
- })
+ app.win.AddLine(netID, c, ui.NotifyNone, line)
}
case irc.TopicChangeEvent:
+ line := app.formatEvent(ev)
+ app.win.AddLine(netID, ev.Channel, ui.NotifyUnread, line)
topic := ui.IRCString(ev.Topic).String()
- body := fmt.Sprintf("Topic changed to: %s", topic)
app.win.SetTopic(netID, ev.Channel, topic)
- app.win.AddLine(netID, ev.Channel, ui.NotifyUnread, ui.Line{
- At: msg.TimeOrNow(),
- Head: "--",
- HeadColor: tcell.ColorGray,
- Body: ui.Styled(body, tcell.StyleDefault.Foreground(tcell.ColorGray)),
- })
case irc.ModeChangeEvent:
- body := fmt.Sprintf("Mode change: %s", ev.Mode)
- app.win.AddLine(netID, ev.Channel, ui.NotifyUnread, ui.Line{
- At: msg.TimeOrNow(),
- Head: "--",
- HeadColor: tcell.ColorGray,
- Body: ui.Styled(body, tcell.StyleDefault.Foreground(tcell.ColorGray)),
- })
+ line := app.formatEvent(ev)
+ app.win.AddLine(netID, ev.Channel, ui.NotifyUnread, line)
case irc.InviteEvent:
var buffer string
var notify ui.NotifyType
@@ -807,19 +752,25 @@ func (app *App) handleIRCEvent(netID string, ev interface{}) {
var linesAfter []ui.Line
bounds, hasBounds := app.messageBounds[boundKey{netID, ev.Target}]
for _, m := range ev.Messages {
+ var line ui.Line
switch ev := m.(type) {
case irc.MessageEvent:
- _, line, _ := app.formatMessage(s, ev)
- if hasBounds {
- c := bounds.Compare(&line)
- if c < 0 {
- linesBefore = append(linesBefore, line)
- } else if c > 0 {
- linesAfter = append(linesAfter, line)
- }
- } else {
+ _, line, _ = app.formatMessage(s, ev)
+ default:
+ line = app.formatEvent(ev)
+ }
+ if line.IsZero() {
+ continue
+ }
+ if hasBounds {
+ c := bounds.Compare(&line)
+ if c < 0 {
linesBefore = append(linesBefore, line)
+ } else if c > 0 {
+ linesAfter = append(linesAfter, line)
}
+ } else {
+ linesBefore = append(linesBefore, line)
}
}
app.win.AddLines(netID, ev.Target, linesBefore, linesAfter)
@@ -987,6 +938,89 @@ func (app *App) completions(cursorIdx int, text []rune) []ui.Completion {
return cs
}
+// formatEvent returns a formatted ui.Line for an irc.Event.
+func (app *App) formatEvent(ev irc.Event) ui.Line {
+ switch ev := ev.(type) {
+ case irc.UserNickEvent:
+ var body ui.StyledStringBuilder
+ body.WriteString(fmt.Sprintf("%s\u2192%s", ev.FormerNick, ev.User))
+ textStyle := tcell.StyleDefault.Foreground(tcell.ColorGray)
+ arrowStyle := tcell.StyleDefault
+ body.AddStyle(0, textStyle)
+ body.AddStyle(len(ev.FormerNick), arrowStyle)
+ body.AddStyle(body.Len()-len(ev.User), textStyle)
+
+ return ui.Line{
+ At: ev.Time,
+ Head: "--",
+ HeadColor: tcell.ColorGray,
+ Body: body.StyledString(),
+ Mergeable: true,
+ }
+ case irc.UserJoinEvent:
+ var body ui.StyledStringBuilder
+ body.Grow(len(ev.User) + 1)
+ body.SetStyle(tcell.StyleDefault.Foreground(tcell.ColorGreen))
+ body.WriteByte('+')
+ body.SetStyle(tcell.StyleDefault.Foreground(tcell.ColorGray))
+ body.WriteString(ev.User)
+ return ui.Line{
+ At: ev.Time,
+ Head: "--",
+ HeadColor: tcell.ColorGray,
+ Body: body.StyledString(),
+ Mergeable: true,
+ }
+ case irc.UserPartEvent:
+ var body ui.StyledStringBuilder
+ body.Grow(len(ev.User) + 1)
+ body.SetStyle(tcell.StyleDefault.Foreground(tcell.ColorRed))
+ body.WriteByte('-')
+ body.SetStyle(tcell.StyleDefault.Foreground(tcell.ColorGray))
+ body.WriteString(ev.User)
+ return ui.Line{
+ At: ev.Time,
+ Head: "--",
+ HeadColor: tcell.ColorGray,
+ Body: body.StyledString(),
+ Mergeable: true,
+ }
+ case irc.UserQuitEvent:
+ var body ui.StyledStringBuilder
+ body.Grow(len(ev.User) + 1)
+ body.SetStyle(tcell.StyleDefault.Foreground(tcell.ColorRed))
+ body.WriteByte('-')
+ body.SetStyle(tcell.StyleDefault.Foreground(tcell.ColorGray))
+ body.WriteString(ev.User)
+ return ui.Line{
+ At: ev.Time,
+ Head: "--",
+ HeadColor: tcell.ColorGray,
+ Body: body.StyledString(),
+ Mergeable: true,
+ }
+ case irc.TopicChangeEvent:
+ topic := ui.IRCString(ev.Topic).String()
+ body := fmt.Sprintf("Topic changed to: %s", topic)
+ return ui.Line{
+ At: ev.Time,
+ Head: "--",
+ HeadColor: tcell.ColorGray,
+ Body: ui.Styled(body, tcell.StyleDefault.Foreground(tcell.ColorGray)),
+ }
+ case irc.ModeChangeEvent:
+ body := fmt.Sprintf("Mode change: %s", ev.Mode)
+ return ui.Line{
+ At: ev.Time,
+ Head: "--",
+ HeadColor: tcell.ColorGray,
+ Body: ui.Styled(body, tcell.StyleDefault.Foreground(tcell.ColorGray)),
+ }
+ default:
+ return ui.Line{}
+ }
+}
+
// formatMessage sets how a given message must be formatted.
//
// It computes three things: