From 21b81ccb2716d73cde4eda805cf1f5ea1642412e Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 7 Apr 2019 03:22:51 +0300 Subject: Initial changes to do #91 --- ui/message-view.go | 8 ++ ui/messages/htmlmessage.go | 187 ++++++++++++++++++++++++++++++++ ui/messages/message.go | 1 - ui/messages/parser/htmlparser.go | 226 ++++++++++++++++++++------------------- ui/messages/parser/parser.go | 3 +- ui/view-login.go | 4 +- 6 files changed, 313 insertions(+), 116 deletions(-) create mode 100644 ui/messages/htmlmessage.go (limited to 'ui') diff --git a/ui/message-view.go b/ui/message-view.go index 91f4621..f2c7260 100644 --- a/ui/message-view.go +++ b/ui/message-view.go @@ -504,6 +504,14 @@ func (view *MessageView) Draw(screen mauview.Screen) { meta.SenderColor()) } prevMeta = meta + htmlMessage, ok := meta.(*messages.HTMLMessage) + if ok { + htmlMessage.Draw(mauview.NewProxyScreen(screen, 0, line, view.width, htmlMessage.Height())) + if ok { + line += htmlMessage.Height() + continue + } + } } text.Draw(screen, messageX, line) diff --git a/ui/messages/htmlmessage.go b/ui/messages/htmlmessage.go new file mode 100644 index 0000000..de4b30c --- /dev/null +++ b/ui/messages/htmlmessage.go @@ -0,0 +1,187 @@ +// gomuks - A terminal Matrix client written in Go. +// Copyright (C) 2019 Tulir Asokan +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package messages + +import ( + "time" + + "github.com/mattn/go-runewidth" + + "maunium.net/go/gomuks/config" + "maunium.net/go/gomuks/ui/widget" + "maunium.net/go/mautrix" + "maunium.net/go/mauview" + "maunium.net/go/tcell" +) + +type HTMLMessage struct { + BaseMessage + + Root *HTMLEntity +} + +func NewHTMLMessage(id, sender, displayname string, msgtype mautrix.MessageType, root *HTMLEntity, timestamp time.Time) UIMessage { + return &HTMLMessage{ + BaseMessage: newBaseMessage(id, sender, displayname, msgtype, timestamp), + Root: root, + } +} +func (hw *HTMLMessage) Draw(screen mauview.Screen) { + hw.Root.Draw(screen) +} + +func (hw *HTMLMessage) OnKeyEvent(event mauview.KeyEvent) bool { + return false +} + +func (hw *HTMLMessage) OnMouseEvent(event mauview.MouseEvent) bool { + return false +} + +func (hw *HTMLMessage) OnPasteEvent(event mauview.PasteEvent) bool { + return false +} + +func (hw *HTMLMessage) CalculateBuffer(preferences config.UserPreferences, width int) { + // TODO account for bare messages in initial startX + startX := 0 + hw.Root.calculateBuffer(width, startX, preferences.BareMessageView) +} + +func (hw *HTMLMessage) Height() int { + return hw.Root.height +} + +func (hw *HTMLMessage) PlainText() string { + return "Plaintext unavailable" +} + +func (hw *HTMLMessage) NotificationContent() string { + return "Notification content unavailable" +} + +type HTMLEntity struct { + // Permanent variables + Tag string + Text string + Style tcell.Style + Children []*HTMLEntity + Block bool + Indent int + + // Non-permanent variables (calculated buffer data) + buffer []string + prevWidth int + startX int + height int +} + +func (he *HTMLEntity) AdjustStyle(fn func(tcell.Style) tcell.Style) *HTMLEntity { + for _, child := range he.Children { + child.AdjustStyle(fn) + } + he.Style = fn(he.Style) + return he +} + +func (he *HTMLEntity) Draw(screen mauview.Screen) { + width, _ := screen.Size() + if len(he.buffer) > 0 { + x := he.startX + for y, line := range he.buffer { + widget.WriteLine(screen, mauview.AlignLeft, line, x, y, width, he.Style) + x = 0 + } + } + if len(he.Children) > 0 { + proxyScreen := &mauview.ProxyScreen{Parent: screen, OffsetX: he.Indent, Width: width - he.Indent} + for _, entity := range he.Children { + if entity.Block { + proxyScreen.OffsetY++ + } + proxyScreen.Height = entity.height + entity.Draw(proxyScreen) + proxyScreen.OffsetY += entity.height - 1 + } + } +} + +func (he *HTMLEntity) calculateBuffer(width, startX int, bare bool) int { + if len(he.Children) > 0 { + childStartX := 0 + for _, entity := range he.Children { + childStartX = entity.calculateBuffer(width-he.Indent, childStartX, bare) + he.height += entity.height - 1 + } + } + if len(he.Text) > 0 && width != he.prevWidth { + he.prevWidth = width + he.buffer = make([]string, 0, 1) + text := he.Text + if !he.Block { + he.startX = startX + } else { + startX = 0 + } + for { + extract := runewidth.Truncate(text, width-startX, "") + extract = trim(extract, text, bare) + he.buffer = append(he.buffer, extract) + text = text[len(extract):] + startX = 0 + if len(text) == 0 { + he.height += len(he.buffer) + // This entity is over, return the startX for the next entity + if he.Block { + // ...except if it's a block entity + return 0 + } + return runewidth.StringWidth(extract) + } + } + } + return 0 +} + +// Regular expressions used to split lines when calculating the buffer. +/*var ( + boundaryPattern = regexp.MustCompile(`([[:punct:]]\s*|\s+)`) + bareBoundaryPattern = regexp.MustCompile(`(\s+)`) + spacePattern = regexp.MustCompile(`\s+`) +)*/ + +func trim(extract, full string, bare bool) string { + if len(extract) == len(full) { + return extract + } + if spaces := spacePattern.FindStringIndex(full[len(extract):]); spaces != nil && spaces[0] == 0 { + extract = full[:len(extract)+spaces[1]] + } + regex := boundaryPattern + if bare { + regex = bareBoundaryPattern + } + matches := regex.FindAllStringIndex(extract, -1) + if len(matches) > 0 { + if match := matches[len(matches)-1]; len(match) >= 2 { + if until := match[1]; until < len(extract) { + extract = extract[:until] + } + } + } + return extract +} diff --git a/ui/messages/message.go b/ui/messages/message.go index 076cd87..e1888e6 100644 --- a/ui/messages/message.go +++ b/ui/messages/message.go @@ -27,7 +27,6 @@ type UIMessage interface { ifc.Message CalculateBuffer(preferences config.UserPreferences, width int) - RecalculateBuffer() Buffer() []tstring.TString Height() int PlainText() string diff --git a/ui/messages/parser/htmlparser.go b/ui/messages/parser/htmlparser.go index f01d3cb..3d1548b 100644 --- a/ui/messages/parser/htmlparser.go +++ b/ui/messages/parser/htmlparser.go @@ -26,11 +26,11 @@ import ( "github.com/lucasb-eyer/go-colorful" "golang.org/x/net/html" + "maunium.net/go/gomuks/ui/messages" "maunium.net/go/mautrix" "maunium.net/go/tcell" "maunium.net/go/gomuks/matrix/rooms" - "maunium.net/go/gomuks/ui/messages/tstring" "maunium.net/go/gomuks/ui/widget" ) @@ -40,11 +40,6 @@ type htmlParser struct { room *rooms.Room } -type taggedTString struct { - tstring.TString - tag string -} - func AdjustStyleBold(style tcell.Style) tcell.Style { return style.Bold(true) } @@ -89,9 +84,9 @@ func digits(num int) int { return int(math.Floor(math.Log10(float64(num))) + 1) } -func (parser *htmlParser) listToTString(node *html.Node, stripLinebreak bool) tstring.TString { +func (parser *htmlParser) listToTString(node *html.Node, stripLinebreak bool) *messages.HTMLEntity { ordered := node.Data == "ol" - taggedChildren := parser.nodeToTaggedTStrings(node.FirstChild, stripLinebreak) + listItems := parser.nodeToEntities(node.FirstChild, stripLinebreak) counter := 1 indentLength := 0 if ordered { @@ -100,13 +95,12 @@ func (parser *htmlParser) listToTString(node *html.Node, stripLinebreak bool) ts counter, _ = strconv.Atoi(start) } - longestIndex := (counter - 1) + len(taggedChildren) + longestIndex := (counter - 1) + len(listItems) indentLength = digits(longestIndex) } - indent := strings.Repeat(" ", indentLength+2) - var children []tstring.TString - for _, child := range taggedChildren { - if child.tag != "li" { + var children []*messages.HTMLEntity + for _, child := range listItems { + if child.Tag != "li" { continue } var prefix string @@ -116,31 +110,47 @@ func (parser *htmlParser) listToTString(node *html.Node, stripLinebreak bool) ts } else { prefix = "● " } - str := child.TString.Prepend(prefix) + child.Text = prefix + child.Text + child.Block = true + child.Indent = indentLength + 2 + children = append(children, child) counter++ - parts := str.Split('\n') - for i, part := range parts[1:] { - parts[i+1] = part.Prepend(indent) - } - str = tstring.Join(parts, "\n") - children = append(children, str) } - return tstring.Join(children, "\n") + return &messages.HTMLEntity{ + Tag: node.Data, + Text: "", + Style: tcell.StyleDefault, + Children: children, + Block: true, + Indent: 0, + } } -func (parser *htmlParser) basicFormatToTString(node *html.Node, stripLinebreak bool) tstring.TString { - str := parser.nodeToTagAwareTString(node.FirstChild, stripLinebreak) +func (parser *htmlParser) basicFormatToEntity(node *html.Node, stripLinebreak bool) *messages.HTMLEntity { + entity := &messages.HTMLEntity{ + Tag: node.Data, + Children: parser.nodeToEntities(node.FirstChild, stripLinebreak), + } switch node.Data { case "b", "strong": - str.AdjustStyleFull(AdjustStyleBold) + entity.AdjustStyle(AdjustStyleBold) case "i", "em": - str.AdjustStyleFull(AdjustStyleItalic) + entity.AdjustStyle(AdjustStyleItalic) case "s", "del": - str.AdjustStyleFull(AdjustStyleStrikethrough) + entity.AdjustStyle(AdjustStyleStrikethrough) case "u", "ins": - str.AdjustStyleFull(AdjustStyleUnderline) + entity.AdjustStyle(AdjustStyleUnderline) + case "font": + fgColor, ok := parser.parseColor(node, "data-mx-color", "color") + if ok { + entity.AdjustStyle(AdjustStyleTextColor(fgColor)) + } + bgColor, ok := parser.parseColor(node, "data-mx-bg-color", "background-color") + if ok { + entity.AdjustStyle(AdjustStyleBackgroundColor(bgColor)) + } } - return str + return entity } func (parser *htmlParser) parseColor(node *html.Node, mainName, altName string) (color tcell.Color, ok bool) { @@ -165,98 +175,112 @@ func (parser *htmlParser) parseColor(node *html.Node, mainName, altName string) return tcell.NewRGBColor(int32(r), int32(g), int32(b)), true } -func (parser *htmlParser) fontToTString(node *html.Node, stripLinebreak bool) tstring.TString { - str := parser.nodeToTagAwareTString(node.FirstChild, stripLinebreak) - fgColor, ok := parser.parseColor(node, "data-mx-color", "color") - if ok { - str.AdjustStyleFull(AdjustStyleTextColor(fgColor)) - } - bgColor, ok := parser.parseColor(node, "data-mx-bg-color", "background-color") - if ok { - str.AdjustStyleFull(AdjustStyleBackgroundColor(bgColor)) - } - return str -} - -func (parser *htmlParser) headerToTString(node *html.Node, stripLinebreak bool) tstring.TString { - children := parser.nodeToTStrings(node.FirstChild, stripLinebreak) +func (parser *htmlParser) headerToEntity(node *html.Node, stripLinebreak bool) *messages.HTMLEntity { length := int(node.Data[1] - '0') prefix := strings.Repeat("#", length) + " " - return tstring.Join(children, "").Prepend(prefix) + return (&messages.HTMLEntity{ + Tag: node.Data, + Text: prefix, + Children: parser.nodeToEntities(node.FirstChild, stripLinebreak), + }).AdjustStyle(AdjustStyleBold) } -func (parser *htmlParser) blockquoteToTString(node *html.Node, stripLinebreak bool) tstring.TString { - str := parser.nodeToTagAwareTString(node.FirstChild, stripLinebreak) - childrenArr := str.TrimSpace().Split('\n') - for index, child := range childrenArr { - childrenArr[index] = child.Prepend("> ") +func (parser *htmlParser) blockquoteToEntity(node *html.Node, stripLinebreak bool) *messages.HTMLEntity { + return &messages.HTMLEntity{ + Tag: "blockquote", + Text: ">", + Children: parser.nodeToEntities(node.FirstChild, stripLinebreak), + Block: true, + Indent: 2, } - return tstring.Join(childrenArr, "\n") } -func (parser *htmlParser) linkToTString(node *html.Node, stripLinebreak bool) tstring.TString { - str := parser.nodeToTagAwareTString(node.FirstChild, stripLinebreak) +func (parser *htmlParser) linkToEntity(node *html.Node, stripLinebreak bool) *messages.HTMLEntity { + entity := &messages.HTMLEntity{ + Tag: "a", + Children: parser.nodeToEntities(node.FirstChild, stripLinebreak), + } href := parser.getAttribute(node, "href") if len(href) == 0 { - return str + return entity } match := matrixToURL.FindStringSubmatch(href) if len(match) == 2 { + entity.Children = nil pillTarget := match[1] + entity.Text = pillTarget if pillTarget[0] == '@' { if member := parser.room.GetMember(pillTarget); member != nil { - return tstring.NewColorTString(member.Displayname, widget.GetHashColor(pillTarget)) + entity.Text = member.Displayname + entity.Style = entity.Style.Foreground(widget.GetHashColor(pillTarget)) } } - return tstring.NewTString(pillTarget) } - return str.Append(fmt.Sprintf(" (%s)", href)) + // TODO add click action for links + return entity } -func (parser *htmlParser) tagToTString(node *html.Node, stripLinebreak bool) tstring.TString { +func (parser *htmlParser) codeblockToEntity(node *html.Node) *messages.HTMLEntity { + return &messages.HTMLEntity{ + Tag: "pre", + Children: parser.nodeToEntities(node.FirstChild, false), + Block: true, + } +} + +func (parser *htmlParser) tagNodeToEntity(node *html.Node, stripLinebreak bool) *messages.HTMLEntity { switch node.Data { case "blockquote": - return parser.blockquoteToTString(node, stripLinebreak) + return parser.blockquoteToEntity(node, stripLinebreak) case "ol", "ul": return parser.listToTString(node, stripLinebreak) case "h1", "h2", "h3", "h4", "h5", "h6": - return parser.headerToTString(node, stripLinebreak) + return parser.headerToEntity(node, stripLinebreak) case "br": - return tstring.NewTString("\n") - case "b", "strong", "i", "em", "s", "del", "u", "ins": - return parser.basicFormatToTString(node, stripLinebreak) - case "font": - return parser.fontToTString(node, stripLinebreak) + return &messages.HTMLEntity{Tag: "br", Block: true} + case "b", "strong", "i", "em", "s", "del", "u", "ins", "font": + return parser.basicFormatToEntity(node, stripLinebreak) case "a": - return parser.linkToTString(node, stripLinebreak) - case "p": - return parser.nodeToTagAwareTString(node.FirstChild, stripLinebreak).Append("\n") + return parser.linkToEntity(node, stripLinebreak) case "pre": - return parser.nodeToTString(node.FirstChild, false) + return parser.codeblockToEntity(node) default: - return parser.nodeToTagAwareTString(node.FirstChild, stripLinebreak) + return &messages.HTMLEntity{ + Tag: node.Data, + Children: parser.nodeToEntities(node.FirstChild, stripLinebreak), + Block: parser.isBlockTag(node.Data), + } } } -func (parser *htmlParser) singleNodeToTString(node *html.Node, stripLinebreak bool) taggedTString { +func (parser *htmlParser) singleNodeToEntity(node *html.Node, stripLinebreak bool) *messages.HTMLEntity { switch node.Type { case html.TextNode: if stripLinebreak { node.Data = strings.Replace(node.Data, "\n", "", -1) } - return taggedTString{tstring.NewTString(node.Data), "text"} + return &messages.HTMLEntity{ + Tag: "text", + Text: node.Data, + } case html.ElementNode: - return taggedTString{parser.tagToTString(node, stripLinebreak), node.Data} + return parser.tagNodeToEntity(node, stripLinebreak) case html.DocumentNode: - return taggedTString{parser.nodeToTagAwareTString(node.FirstChild, stripLinebreak), "html"} + return &messages.HTMLEntity{ + Tag: "html", + Children: parser.nodeToEntities(node.FirstChild, stripLinebreak), + Block: true, + } default: - return taggedTString{tstring.NewBlankTString(), "unknown"} + return nil } } -func (parser *htmlParser) nodeToTaggedTStrings(node *html.Node, stripLinebreak bool) (strs []taggedTString) { +func (parser *htmlParser) nodeToEntities(node *html.Node, stripLinebreak bool) (entities []*messages.HTMLEntity) { for ; node != nil; node = node.NextSibling { - strs = append(strs, parser.singleNodeToTString(node, stripLinebreak)) + if entity := parser.singleNodeToEntity(node, stripLinebreak); entity != nil { + entities = append(entities, entity) + } } return } @@ -272,51 +296,31 @@ func (parser *htmlParser) isBlockTag(tag string) bool { return false } -func (parser *htmlParser) nodeToTagAwareTString(node *html.Node, stripLinebreak bool) tstring.TString { - strs := parser.nodeToTaggedTStrings(node, stripLinebreak) - output := tstring.NewBlankTString() - for _, str := range strs { - tstr := str.TString - if parser.isBlockTag(str.tag) { - tstr = tstr.Prepend("\n").Append("\n") - } - output = output.AppendTString(tstr) - } - return output.TrimSpace() -} - -func (parser *htmlParser) nodeToTStrings(node *html.Node, stripLinebreak bool) (strs []tstring.TString) { - for ; node != nil; node = node.NextSibling { - strs = append(strs, parser.singleNodeToTString(node, stripLinebreak).TString) - } - return -} - -func (parser *htmlParser) nodeToTString(node *html.Node, stripLinebreak bool) tstring.TString { - return tstring.Join(parser.nodeToTStrings(node, stripLinebreak), "") -} - -func (parser *htmlParser) Parse(htmlData string) tstring.TString { +func (parser *htmlParser) Parse(htmlData string) *messages.HTMLEntity { node, _ := html.Parse(strings.NewReader(htmlData)) - return parser.nodeToTagAwareTString(node, true) + return parser.singleNodeToEntity(node, true) } // ParseHTMLMessage parses a HTML-formatted Matrix event into a UIMessage. -func ParseHTMLMessage(room *rooms.Room, evt *mautrix.Event, senderDisplayname string) tstring.TString { +func ParseHTMLMessage(room *rooms.Room, evt *mautrix.Event, senderDisplayname string) *messages.HTMLEntity { htmlData := evt.Content.FormattedBody htmlData = strings.Replace(htmlData, "\t", " ", -1) parser := htmlParser{room} - str := parser.Parse(htmlData) + root := parser.Parse(htmlData) + root.Block = false if evt.Content.MsgType == mautrix.MsgEmote { - str = tstring.Join([]tstring.TString{ - tstring.NewTString("* "), - tstring.NewColorTString(senderDisplayname, widget.GetHashColor(evt.Sender)), - tstring.NewTString(" "), - str, - }, "") + root = &messages.HTMLEntity{ + Tag: "emote", + Children: []*messages.HTMLEntity{ + {Text: "* "}, + {Text: senderDisplayname, Style: tcell.StyleDefault.Foreground(widget.GetHashColor(evt.Sender))}, + {Text: " "}, + root, + }, + } } - return str + return root } diff --git a/ui/messages/parser/parser.go b/ui/messages/parser/parser.go index 79e628a..4181c09 100644 --- a/ui/messages/parser/parser.go +++ b/ui/messages/parser/parser.go @@ -125,8 +125,7 @@ func ParseMessage(matrix ifc.MatrixContainer, room *rooms.Room, evt *mautrix.Eve switch evt.Content.MsgType { case "m.text", "m.notice", "m.emote": if evt.Content.Format == mautrix.FormatHTML { - text := ParseHTMLMessage(room, evt, displayname) - return messages.NewExpandedTextMessage(evt.ID, evt.Sender, displayname, evt.Content.MsgType, text, ts) + return messages.NewHTMLMessage(evt.ID, evt.Sender, displayname, evt.Content.MsgType, ParseHTMLMessage(room, evt, displayname), ts) } evt.Content.Body = strings.Replace(evt.Content.Body, "\t", " ", -1) return messages.NewTextMessage(evt.ID, evt.Sender, displayname, evt.Content.MsgType, evt.Content.Body, ts) diff --git a/ui/view-login.go b/ui/view-login.go index e0268d0..b929d62 100644 --- a/ui/view-login.go +++ b/ui/view-login.go @@ -74,8 +74,8 @@ func (ui *GomuksUI) NewLoginView() mauview.Component { view.username.SetText(ui.gmx.Config().UserID) view.password.SetMaskCharacter('*') - view.quitButton.SetOnClick(ui.gmx.Stop).SetBackgroundColor(tcell.ColorBlue) - view.loginButton.SetOnClick(view.Login).SetBackgroundColor(tcell.ColorBlue) + view.quitButton.SetOnClick(ui.gmx.Stop).SetBackgroundColor(tcell.ColorDarkCyan) + view.loginButton.SetOnClick(view.Login).SetBackgroundColor(tcell.ColorDarkCyan) view.SetColumns([]int{1, 10, 1, 9, 1, 9, 1, 10, 1}) view.SetRows([]int{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}) -- cgit v1.2.3 From b0c4ef81e9a1c7d9376685875d795ad3b5d8db01 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 7 Apr 2019 18:21:38 +0300 Subject: More changes to do #91 --- ui/command-processor.go | 13 +++--- ui/commands.go | 15 ++++++ ui/message-view.go | 38 ++++++++++------ ui/messages/base.go | 51 +++++++++++---------- ui/messages/expandedtextmessage.go | 5 -- ui/messages/htmlmessage.go | 93 ++++++++++++++++++++++++++++++-------- ui/messages/imagemessage.go | 7 --- ui/messages/message.go | 4 +- ui/messages/parser/htmlparser.go | 3 ++ ui/messages/textbase.go | 2 - ui/messages/textmessage.go | 5 -- 11 files changed, 154 insertions(+), 82 deletions(-) (limited to 'ui') diff --git a/ui/command-processor.go b/ui/command-processor.go index 77a7b3d..7a4c2c1 100644 --- a/ui/command-processor.go +++ b/ui/command-processor.go @@ -76,12 +76,12 @@ func NewCommandProcessor(parent *MainView) *CommandProcessor { Gomuks: parent.gmx, }, aliases: map[string]*Alias{ - "part": {"leave"}, - "send": {"sendevent"}, - "msend": {"msendevent"}, - "state": {"setstate"}, - "mstate":{"msetstate"}, - "rb": {"rainbow"}, + "part": {"leave"}, + "send": {"sendevent"}, + "msend": {"msendevent"}, + "state": {"setstate"}, + "mstate": {"msetstate"}, + "rb": {"rainbow"}, }, commands: map[string]CommandHandler{ "unknown-command": cmdUnknownCommand, @@ -102,6 +102,7 @@ func NewCommandProcessor(parent *MainView) *CommandProcessor { "msetstate": cmdMSetState, "rainbow": cmdRainbow, "invite": cmdInvite, + "hprof": cmdHeapProfile, }, } } diff --git a/ui/commands.go b/ui/commands.go index a8b1faa..a652518 100644 --- a/ui/commands.go +++ b/ui/commands.go @@ -19,6 +19,9 @@ package ui import ( "encoding/json" "fmt" + "os" + "runtime" + "runtime/pprof" "strings" "unicode" @@ -69,6 +72,18 @@ var rainbow = GradientTable{ {colorful.LinearRgb(1, 0, 0.5), 1}, } +func cmdHeapProfile(cmd *Command) { + runtime.GC() + memProfile, err := os.Create("gomuks.prof") + if err != nil { + debug.Print(err) + } + defer memProfile.Close() + if err := pprof.WriteHeapProfile(memProfile); err != nil { + debug.Print(err) + } +} + // TODO this command definitely belongs in a plugin once we have a plugin system. func cmdRainbow(cmd *Command) { text := strings.Join(cmd.Args, " ") diff --git a/ui/message-view.go b/ui/message-view.go index f2c7260..20831f1 100644 --- a/ui/message-view.go +++ b/ui/message-view.go @@ -75,6 +75,7 @@ func NewMessageView(parent *RoomView) *MessageView { textBuffer: make([]tstring.TString, 0), metaBuffer: make([]ifc.MessageMeta, 0), + width: 80, widestSender: 5, prevWidth: -1, prevHeight: -1, @@ -159,8 +160,8 @@ func (view *MessageView) appendBuffer(message messages.UIMessage) { } } - view.textBuffer = append(view.textBuffer, message.Buffer()...) - for range message.Buffer() { + for i := 0; i < message.Height(); i++ { + view.textBuffer = append(view.textBuffer, nil) view.metaBuffer = append(view.metaBuffer, message) } view.prevMsgCount++ @@ -200,10 +201,15 @@ func (view *MessageView) replaceBuffer(original messages.UIMessage, new messages end++ } - view.textBuffer = append(append(view.textBuffer[0:start], new.Buffer()...), view.textBuffer[end:]...) - if len(new.Buffer()) != end-start { + if new.Height() == 0 { + new.CalculateBuffer(view.prevPrefs, view.prevWidth) + } + + textBuf := make([]tstring.TString, new.Height()) + view.textBuffer = append(append(view.textBuffer[0:start], textBuf...), view.textBuffer[end:]...) + if new.Height() != end-start { metaBuffer := view.metaBuffer[0:start] - for range new.Buffer() { + for i := 0; i < new.Height(); i++ { metaBuffer = append(metaBuffer, new) } view.metaBuffer = append(metaBuffer, view.metaBuffer[end:]...) @@ -504,16 +510,22 @@ func (view *MessageView) Draw(screen mauview.Screen) { meta.SenderColor()) } prevMeta = meta - htmlMessage, ok := meta.(*messages.HTMLMessage) - if ok { - htmlMessage.Draw(mauview.NewProxyScreen(screen, 0, line, view.width, htmlMessage.Height())) - if ok { - line += htmlMessage.Height() - continue + } + + message, ok := meta.(messages.UIMessage) + if ok { + for i := index - 1; i >= 0 && view.metaBuffer[i] == meta; i-- { + line-- + } + message.Draw(mauview.NewProxyScreen(screen, messageX, line, view.width-messageX, message.Height())) + if !bareMode { + for i := line; i < line+message.Height(); i++ { + screen.SetContent(separatorX, i, borderChar, nil, borderStyle) } } + line += message.Height() - 1 + } else { + text.Draw(screen, messageX, line) } - - text.Draw(screen, messageX, line) } } diff --git a/ui/messages/base.go b/ui/messages/base.go index ba1902d..d045e42 100644 --- a/ui/messages/base.go +++ b/ui/messages/base.go @@ -21,9 +21,9 @@ import ( "time" "maunium.net/go/mautrix" + "maunium.net/go/mauview" "maunium.net/go/tcell" - "maunium.net/go/gomuks/config" "maunium.net/go/gomuks/interface" "maunium.net/go/gomuks/ui/messages/tstring" "maunium.net/go/gomuks/ui/widget" @@ -34,33 +34,30 @@ func init() { } type BaseMessage struct { - MsgID string - MsgType mautrix.MessageType - MsgSenderID string - MsgSender string - MsgSenderColor tcell.Color - MsgTimestamp time.Time - MsgState ifc.MessageState - MsgIsHighlight bool - MsgIsService bool - buffer []tstring.TString - plainBuffer []tstring.TString - prevBufferWidth int - prevPrefs config.UserPreferences + MsgID string + MsgType mautrix.MessageType + MsgSenderID string + MsgSender string + MsgSenderColor tcell.Color + MsgTimestamp time.Time + MsgState ifc.MessageState + MsgIsHighlight bool + MsgIsService bool + buffer []tstring.TString + plainBuffer []tstring.TString } func newBaseMessage(id, sender, displayname string, msgtype mautrix.MessageType, timestamp time.Time) BaseMessage { return BaseMessage{ - MsgSenderID: sender, - MsgSender: displayname, - MsgTimestamp: timestamp, - MsgSenderColor: widget.GetHashColor(sender), - MsgType: msgtype, - MsgID: id, - prevBufferWidth: 0, - MsgState: ifc.MessageStateDefault, - MsgIsHighlight: false, - MsgIsService: false, + MsgSenderID: sender, + MsgSender: displayname, + MsgTimestamp: timestamp, + MsgSenderColor: widget.GetHashColor(sender), + MsgType: msgtype, + MsgID: id, + MsgState: ifc.MessageStateDefault, + MsgIsHighlight: false, + MsgIsService: false, } } @@ -227,3 +224,9 @@ func (msg *BaseMessage) IsService() bool { func (msg *BaseMessage) SetIsService(isService bool) { msg.MsgIsService = isService } + +func (msg *BaseMessage) Draw(screen mauview.Screen) { + for y, line := range msg.buffer { + line.Draw(screen, 0, y) + } +} diff --git a/ui/messages/expandedtextmessage.go b/ui/messages/expandedtextmessage.go index d889771..044613f 100644 --- a/ui/messages/expandedtextmessage.go +++ b/ui/messages/expandedtextmessage.go @@ -58,8 +58,3 @@ func (msg *ExpandedTextMessage) PlainText() string { func (msg *ExpandedTextMessage) CalculateBuffer(prefs config.UserPreferences, width int) { msg.calculateBufferWithText(prefs, msg.MsgText, width) } - -// RecalculateBuffer calculates the buffer again with the previously provided width. -func (msg *ExpandedTextMessage) RecalculateBuffer() { - msg.CalculateBuffer(msg.prevPrefs, msg.prevBufferWidth) -} diff --git a/ui/messages/htmlmessage.go b/ui/messages/htmlmessage.go index de4b30c..33e4d87 100644 --- a/ui/messages/htmlmessage.go +++ b/ui/messages/htmlmessage.go @@ -17,6 +17,8 @@ package messages import ( + "fmt" + "strings" "time" "github.com/mattn/go-runewidth" @@ -57,9 +59,13 @@ func (hw *HTMLMessage) OnPasteEvent(event mauview.PasteEvent) bool { } func (hw *HTMLMessage) CalculateBuffer(preferences config.UserPreferences, width int) { + if width <= 0 { + panic("Negative width in CalculateBuffer") + } // TODO account for bare messages in initial startX startX := 0 hw.Root.calculateBuffer(width, startX, preferences.BareMessageView) + //debug.Print(hw.Root.String()) } func (hw *HTMLMessage) Height() int { @@ -109,8 +115,8 @@ func (he *HTMLEntity) Draw(screen mauview.Screen) { } if len(he.Children) > 0 { proxyScreen := &mauview.ProxyScreen{Parent: screen, OffsetX: he.Indent, Width: width - he.Indent} - for _, entity := range he.Children { - if entity.Block { + for i, entity := range he.Children { + if i != 0 && entity.startX == 0 { proxyScreen.OffsetY++ } proxyScreen.Height = entity.height @@ -120,38 +126,87 @@ func (he *HTMLEntity) Draw(screen mauview.Screen) { } } +func (he *HTMLEntity) String() string { + var buf strings.Builder + buf.WriteString("&HTMLEntity{\n") + _, _ = fmt.Fprintf(&buf, ` Tag="%s", Style=%d, Block=%t, Indent=%d, startX=%d, height=%d,\n`, + he.Tag, he.Style, he.Block, he.Indent, he.startX, he.height) + _, _ = fmt.Fprintf(&buf, ` Buffer=["%s"]`, strings.Join(he.buffer, "\", \"")) + if len(he.Text) > 0 { + buf.WriteString(",\n") + _, _ = fmt.Fprintf(&buf, ` Text="%s"`, he.Text) + } + if len(he.Children) > 0 { + buf.WriteString(",\n") + buf.WriteString(" Children={") + for _, child := range he.Children { + buf.WriteString("\n ") + buf.WriteString(strings.Join(strings.Split(strings.TrimRight(child.String(), "\n"), "\n"), "\n ")) + } + buf.WriteString("\n },") + } + buf.WriteString("\n},\n") + return buf.String() +} + func (he *HTMLEntity) calculateBuffer(width, startX int, bare bool) int { + he.startX = startX + if he.Block { + he.startX = 0 + } + he.height = 0 if len(he.Children) > 0 { - childStartX := 0 + childStartX := he.startX for _, entity := range he.Children { + if entity.Block || childStartX == 0 || he.height == 0 { + he.height++ + } childStartX = entity.calculateBuffer(width-he.Indent, childStartX, bare) he.height += entity.height - 1 } + if len(he.Text) == 0 && !he.Block { + return childStartX + } } - if len(he.Text) > 0 && width != he.prevWidth { + if len(he.Text) > 0 { he.prevWidth = width - he.buffer = make([]string, 0, 1) - text := he.Text - if !he.Block { - he.startX = startX - } else { - startX = 0 + if he.buffer == nil { + he.buffer = []string{} } + bufPtr := 0 + text := he.Text + textStartX := he.startX for { - extract := runewidth.Truncate(text, width-startX, "") - extract = trim(extract, text, bare) - he.buffer = append(he.buffer, extract) + extract := runewidth.Truncate(text, width-textStartX, "") + extract, wordWrapped := trim(extract, text, bare) + if !wordWrapped && textStartX > 0 { + if bufPtr < len(he.buffer) { + he.buffer[bufPtr] = "" + } else { + he.buffer = append(he.buffer, "") + } + bufPtr++ + textStartX = 0 + continue + } + if bufPtr < len(he.buffer) { + he.buffer[bufPtr] = extract + } else { + he.buffer = append(he.buffer, extract) + } + bufPtr++ text = text[len(extract):] - startX = 0 if len(text) == 0 { + he.buffer = he.buffer[:bufPtr] he.height += len(he.buffer) // This entity is over, return the startX for the next entity if he.Block { // ...except if it's a block entity return 0 } - return runewidth.StringWidth(extract) + return textStartX + runewidth.StringWidth(extract) } + textStartX = 0 } } return 0 @@ -164,12 +219,13 @@ func (he *HTMLEntity) calculateBuffer(width, startX int, bare bool) int { spacePattern = regexp.MustCompile(`\s+`) )*/ -func trim(extract, full string, bare bool) string { +func trim(extract, full string, bare bool) (string, bool) { if len(extract) == len(full) { - return extract + return extract, true } if spaces := spacePattern.FindStringIndex(full[len(extract):]); spaces != nil && spaces[0] == 0 { extract = full[:len(extract)+spaces[1]] + //return extract, true } regex := boundaryPattern if bare { @@ -180,8 +236,9 @@ func trim(extract, full string, bare bool) string { if match := matches[len(matches)-1]; len(match) >= 2 { if until := match[1]; until < len(extract) { extract = extract[:until] + return extract, true } } } - return extract + return extract, len(extract) > 0 && extract[len(extract)-1] == ' ' } diff --git a/ui/messages/imagemessage.go b/ui/messages/imagemessage.go index cad76a4..968f29e 100644 --- a/ui/messages/imagemessage.go +++ b/ui/messages/imagemessage.go @@ -112,11 +112,4 @@ func (msg *ImageMessage) CalculateBuffer(prefs config.UserPreferences, width int } msg.buffer = image.Render() - msg.prevBufferWidth = width - msg.prevPrefs = prefs -} - -// RecalculateBuffer calculates the buffer again with the previously provided width. -func (msg *ImageMessage) RecalculateBuffer() { - msg.CalculateBuffer(msg.prevPrefs, msg.prevBufferWidth) } diff --git a/ui/messages/message.go b/ui/messages/message.go index e1888e6..db93879 100644 --- a/ui/messages/message.go +++ b/ui/messages/message.go @@ -19,7 +19,7 @@ package messages import ( "maunium.net/go/gomuks/config" "maunium.net/go/gomuks/interface" - "maunium.net/go/gomuks/ui/messages/tstring" + "maunium.net/go/mauview" ) // UIMessage is a wrapper for the content and metadata of a Matrix message intended to be displayed. @@ -27,7 +27,7 @@ type UIMessage interface { ifc.Message CalculateBuffer(preferences config.UserPreferences, width int) - Buffer() []tstring.TString + Draw(screen mauview.Screen) Height() int PlainText() string diff --git a/ui/messages/parser/htmlparser.go b/ui/messages/parser/htmlparser.go index 3d1548b..9a9c2d1 100644 --- a/ui/messages/parser/htmlparser.go +++ b/ui/messages/parser/htmlparser.go @@ -266,6 +266,9 @@ func (parser *htmlParser) singleNodeToEntity(node *html.Node, stripLinebreak boo case html.ElementNode: return parser.tagNodeToEntity(node, stripLinebreak) case html.DocumentNode: + if node.FirstChild.Data == "html" && node.FirstChild.NextSibling == nil { + return parser.singleNodeToEntity(node.FirstChild, stripLinebreak) + } return &messages.HTMLEntity{ Tag: "html", Children: parser.nodeToEntities(node.FirstChild, stripLinebreak), diff --git a/ui/messages/textbase.go b/ui/messages/textbase.go index 01e7b5c..321d998 100644 --- a/ui/messages/textbase.go +++ b/ui/messages/textbase.go @@ -92,6 +92,4 @@ func (msg *BaseMessage) calculateBufferWithText(prefs config.UserPreferences, te str = str[len(extract):] } } - msg.prevBufferWidth = width - msg.prevPrefs = prefs } diff --git a/ui/messages/textmessage.go b/ui/messages/textmessage.go index 8ce9482..6364659 100644 --- a/ui/messages/textmessage.go +++ b/ui/messages/textmessage.go @@ -90,8 +90,3 @@ func (msg *TextMessage) PlainText() string { func (msg *TextMessage) CalculateBuffer(prefs config.UserPreferences, width int) { msg.calculateBufferWithText(prefs, msg.getCache(), width) } - -// RecalculateBuffer calculates the buffer again with the previously provided width. -func (msg *TextMessage) RecalculateBuffer() { - msg.CalculateBuffer(msg.prevPrefs, msg.prevBufferWidth) -} -- cgit v1.2.3 From 083ae8bd44ad34859781ea88cae9f57d9404c7e5 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 7 Apr 2019 18:25:13 +0300 Subject: Remove commented code --- ui/messages/htmlmessage.go | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) (limited to 'ui') diff --git a/ui/messages/htmlmessage.go b/ui/messages/htmlmessage.go index 33e4d87..51678ba 100644 --- a/ui/messages/htmlmessage.go +++ b/ui/messages/htmlmessage.go @@ -65,7 +65,6 @@ func (hw *HTMLMessage) CalculateBuffer(preferences config.UserPreferences, width // TODO account for bare messages in initial startX startX := 0 hw.Root.calculateBuffer(width, startX, preferences.BareMessageView) - //debug.Print(hw.Root.String()) } func (hw *HTMLMessage) Height() int { @@ -73,10 +72,12 @@ func (hw *HTMLMessage) Height() int { } func (hw *HTMLMessage) PlainText() string { + // FIXME return "Plaintext unavailable" } func (hw *HTMLMessage) NotificationContent() string { + // FIXME return "Notification content unavailable" } @@ -212,20 +213,12 @@ func (he *HTMLEntity) calculateBuffer(width, startX int, bare bool) int { return 0 } -// Regular expressions used to split lines when calculating the buffer. -/*var ( - boundaryPattern = regexp.MustCompile(`([[:punct:]]\s*|\s+)`) - bareBoundaryPattern = regexp.MustCompile(`(\s+)`) - spacePattern = regexp.MustCompile(`\s+`) -)*/ - func trim(extract, full string, bare bool) (string, bool) { if len(extract) == len(full) { return extract, true } if spaces := spacePattern.FindStringIndex(full[len(extract):]); spaces != nil && spaces[0] == 0 { extract = full[:len(extract)+spaces[1]] - //return extract, true } regex := boundaryPattern if bare { -- cgit v1.2.3 From cf93671ecdebc96cfd45fefaeb9fc95f62393a33 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 7 Apr 2019 20:13:23 +0300 Subject: Add syntax highlighting. Fixes #28 --- ui/messages/htmlmessage.go | 50 ++++++++++++++++----- ui/messages/parser/htmlparser.go | 95 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 130 insertions(+), 15 deletions(-) (limited to 'ui') diff --git a/ui/messages/htmlmessage.go b/ui/messages/htmlmessage.go index 51678ba..0ffb6c9 100644 --- a/ui/messages/htmlmessage.go +++ b/ui/messages/htmlmessage.go @@ -72,23 +72,23 @@ func (hw *HTMLMessage) Height() int { } func (hw *HTMLMessage) PlainText() string { - // FIXME - return "Plaintext unavailable" + return hw.Root.PlainText() } func (hw *HTMLMessage) NotificationContent() string { - // FIXME - return "Notification content unavailable" + return hw.Root.PlainText() } type HTMLEntity struct { // Permanent variables - Tag string - Text string - Style tcell.Style - Children []*HTMLEntity - Block bool - Indent int + Tag string + Text string + Style tcell.Style + Children []*HTMLEntity + Block bool + Indent int + + DefaultHeight int // Non-permanent variables (calculated buffer data) buffer []string @@ -130,8 +130,9 @@ func (he *HTMLEntity) Draw(screen mauview.Screen) { func (he *HTMLEntity) String() string { var buf strings.Builder buf.WriteString("&HTMLEntity{\n") - _, _ = fmt.Fprintf(&buf, ` Tag="%s", Style=%d, Block=%t, Indent=%d, startX=%d, height=%d,\n`, + _, _ = fmt.Fprintf(&buf, ` Tag="%s", Style=%d, Block=%t, Indent=%d, startX=%d, height=%d,`, he.Tag, he.Style, he.Block, he.Indent, he.startX, he.height) + buf.WriteRune('\n') _, _ = fmt.Fprintf(&buf, ` Buffer=["%s"]`, strings.Join(he.buffer, "\", \"")) if len(he.Text) > 0 { buf.WriteString(",\n") @@ -150,6 +151,27 @@ func (he *HTMLEntity) String() string { return buf.String() } +func (he *HTMLEntity) PlainText() string { + if len(he.Children) == 0 { + return he.Text + } + var buf strings.Builder + buf.WriteString(he.Text) + newlined := false + for _, child := range he.Children { + if child.Block && !newlined { + buf.WriteRune('\n') + } + newlined = false + buf.WriteString(child.PlainText()) + if child.Block { + buf.WriteRune('\n') + newlined = true + } + } + return buf.String() +} + func (he *HTMLEntity) calculateBuffer(width, startX int, bare bool) int { he.startX = startX if he.Block { @@ -178,6 +200,7 @@ func (he *HTMLEntity) calculateBuffer(width, startX int, bare bool) int { text := he.Text textStartX := he.startX for { + // TODO add option no wrap and character wrap options extract := runewidth.Truncate(text, width-textStartX, "") extract, wordWrapped := trim(extract, text, bare) if !wordWrapped && textStartX > 0 { @@ -210,7 +233,10 @@ func (he *HTMLEntity) calculateBuffer(width, startX int, bare bool) int { textStartX = 0 } } - return 0 + if len(he.Text) == 0 && len(he.Children) == 0 { + he.height = he.DefaultHeight + } + return he.startX } func trim(extract, full string, bare bool) (string, bool) { diff --git a/ui/messages/parser/htmlparser.go b/ui/messages/parser/htmlparser.go index 9a9c2d1..688deb3 100644 --- a/ui/messages/parser/htmlparser.go +++ b/ui/messages/parser/htmlparser.go @@ -23,6 +23,9 @@ import ( "strconv" "strings" + "github.com/alecthomas/chroma" + "github.com/alecthomas/chroma/lexers" + "github.com/alecthomas/chroma/styles" "github.com/lucasb-eyer/go-colorful" "golang.org/x/net/html" @@ -216,18 +219,102 @@ func (parser *htmlParser) linkToEntity(node *html.Node, stripLinebreak bool) *me } } } - // TODO add click action for links + // TODO add click action and underline on hover for links return entity } -func (parser *htmlParser) codeblockToEntity(node *html.Node) *messages.HTMLEntity { +func (parser *htmlParser) imageToEntity(node *html.Node) *messages.HTMLEntity { + alt := parser.getAttribute(node, "alt") + if len(alt) == 0 { + alt = parser.getAttribute(node, "title") + if len(alt) == 0 { + alt = "[inline image]" + } + } + entity := &messages.HTMLEntity{ + Tag: "img", + Text: alt, + } + // TODO add click action and underline on hover for inline images + return entity +} + +func colourToColor(colour chroma.Colour) tcell.Color { + if !colour.IsSet() { + return tcell.ColorDefault + } + return tcell.NewRGBColor(int32(colour.Red()), int32(colour.Green()), int32(colour.Blue())) +} + +func styleEntryToStyle(se chroma.StyleEntry) tcell.Style { + return tcell.StyleDefault. + Bold(se.Bold == chroma.Yes). + Italic(se.Italic == chroma.Yes). + Underline(se.Underline == chroma.Yes). + Foreground(colourToColor(se.Colour)). + Background(colourToColor(se.Background)) +} + +func (parser *htmlParser) syntaxHighlight(text, language string) *messages.HTMLEntity { + lexer := lexers.Get(language) + if lexer == nil { + return nil + } + iter, err := lexer.Tokenise(nil, text) + if err != nil { + return nil + } + style := styles.SolarizedDark + tokens := iter.Tokens() + children := make([]*messages.HTMLEntity, len(tokens)) + for i, token := range tokens { + if token.Value == "\n" { + children[i] = &messages.HTMLEntity{Block: true, Tag: "br"} + } else { + children[i] = &messages.HTMLEntity{ + Tag: token.Type.String(), + Text: token.Value, + Style: styleEntryToStyle(style.Get(token.Type)), + + DefaultHeight: 1, + } + } + } return &messages.HTMLEntity{ Tag: "pre", - Children: parser.nodeToEntities(node.FirstChild, false), Block: true, + Children: children, } } +func (parser *htmlParser) codeblockToEntity(node *html.Node) *messages.HTMLEntity { + entity := &messages.HTMLEntity{ + Tag: "pre", + Block: true, + } + // TODO allow disabling syntax highlighting + if node.FirstChild.Type == html.ElementNode && node.FirstChild.Data == "code" { + text := (&messages.HTMLEntity{ + Children: parser.nodeToEntities(node.FirstChild.FirstChild, false), + }).PlainText() + attr := parser.getAttribute(node.FirstChild, "class") + var lang string + for _, class := range strings.Split(attr, " ") { + if strings.HasPrefix(class, "language-") { + lang = class[len("language-"):] + break + } + } + if len(lang) != 0 { + if parsed := parser.syntaxHighlight(text, lang); parsed != nil { + return parsed + } + } + } + entity.Children = parser.nodeToEntities(node.FirstChild, false) + return entity +} + func (parser *htmlParser) tagNodeToEntity(node *html.Node, stripLinebreak bool) *messages.HTMLEntity { switch node.Data { case "blockquote": @@ -242,6 +329,8 @@ func (parser *htmlParser) tagNodeToEntity(node *html.Node, stripLinebreak bool) return parser.basicFormatToEntity(node, stripLinebreak) case "a": return parser.linkToEntity(node, stripLinebreak) + case "img": + return parser.imageToEntity(node) case "pre": return parser.codeblockToEntity(node) default: -- cgit v1.2.3 From e6180c9b6fb12686926cf8d3bc27f31f6489129b Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 7 Apr 2019 22:25:53 +0300 Subject: Move special list/blockquote prefixing to renderer --- ui/messages/htmlmessage.go | 175 +++++++++++++++++++++++++++++++++------ ui/messages/parser/htmlparser.go | 135 +++++++++++------------------- 2 files changed, 200 insertions(+), 110 deletions(-) (limited to 'ui') diff --git a/ui/messages/htmlmessage.go b/ui/messages/htmlmessage.go index 0ffb6c9..34bba2b 100644 --- a/ui/messages/htmlmessage.go +++ b/ui/messages/htmlmessage.go @@ -18,25 +18,27 @@ package messages import ( "fmt" + "math" "strings" "time" "github.com/mattn/go-runewidth" - "maunium.net/go/gomuks/config" - "maunium.net/go/gomuks/ui/widget" "maunium.net/go/mautrix" "maunium.net/go/mauview" "maunium.net/go/tcell" + + "maunium.net/go/gomuks/config" + "maunium.net/go/gomuks/ui/widget" ) type HTMLMessage struct { BaseMessage - Root *HTMLEntity + Root HTMLEntity } -func NewHTMLMessage(id, sender, displayname string, msgtype mautrix.MessageType, root *HTMLEntity, timestamp time.Time) UIMessage { +func NewHTMLMessage(id, sender, displayname string, msgtype mautrix.MessageType, root HTMLEntity, timestamp time.Time) UIMessage { return &HTMLMessage{ BaseMessage: newBaseMessage(id, sender, displayname, msgtype, timestamp), Root: root, @@ -68,7 +70,7 @@ func (hw *HTMLMessage) CalculateBuffer(preferences config.UserPreferences, width } func (hw *HTMLMessage) Height() int { - return hw.Root.height + return hw.Root.Height() } func (hw *HTMLMessage) PlainText() string { @@ -79,14 +81,106 @@ func (hw *HTMLMessage) NotificationContent() string { return hw.Root.PlainText() } -type HTMLEntity struct { +type AdjustStyleFunc func(tcell.Style) tcell.Style + +type HTMLEntity interface { + AdjustStyle(AdjustStyleFunc) HTMLEntity + Draw(screen mauview.Screen) + IsBlock() bool + GetTag() string + PlainText() string + String() string + Height() int + + calculateBuffer(width, startX int, bare bool) int + getStartX() int +} + +type BlockquoteEntity struct { + *BaseHTMLEntity +} + +func NewBlockquoteEntity(children []HTMLEntity) *BlockquoteEntity { + return &BlockquoteEntity{&BaseHTMLEntity{ + Tag: "blockquote", + Children: children, + Block: true, + Indent: 2, + }} +} + +func (be *BlockquoteEntity) Draw(screen mauview.Screen) { + be.BaseHTMLEntity.Draw(screen) + for y := 0; y < be.height; y++ { + screen.SetContent(0, y, '>', nil, be.Style) + } +} + +func (be *BlockquoteEntity) String() string { + return fmt.Sprintf("&BlockquoteEntity{%s},\n", be.BaseHTMLEntity) +} + +type ListEntity struct { + *BaseHTMLEntity + Ordered bool + Start int +} + +func digits(num int) int { + if num <= 0 { + return 0 + } + return int(math.Floor(math.Log10(float64(num))) + 1) +} + +func NewListEntity(ordered bool, start int, children []HTMLEntity) *ListEntity { + entity := &ListEntity{ + BaseHTMLEntity: &BaseHTMLEntity{ + Tag: "ul", + Children: children, + Block: true, + Indent: 2, + }, + Ordered: ordered, + Start: start, + } + if ordered { + entity.Tag = "ol" + entity.Indent += digits(start + len(children) - 1) + } + return entity +} + +func (le *ListEntity) Draw(screen mauview.Screen) { + width, _ := screen.Size() + + proxyScreen := &mauview.ProxyScreen{Parent: screen, OffsetX: le.Indent, Width: width - le.Indent} + for i, entity := range le.Children { + proxyScreen.Height = entity.Height() + if le.Ordered { + number := le.Start + i + line := fmt.Sprintf("%d. %s", number, strings.Repeat(" ", le.Indent-2-digits(number))) + widget.WriteLine(screen, mauview.AlignLeft, line, 0, proxyScreen.OffsetY, le.Indent, le.Style) + } else { + screen.SetContent(0, proxyScreen.OffsetY, '●', nil, le.Style) + } + entity.Draw(proxyScreen) + proxyScreen.OffsetY += entity.Height() + } +} + +func (le *ListEntity) String() string { + return fmt.Sprintf("&ListEntity{Ordered=%t, Start=%d, Base=%s},\n", le.Ordered, le.Start, le.BaseHTMLEntity) +} + +type BaseHTMLEntity struct { // Permanent variables - Tag string - Text string - Style tcell.Style - Children []*HTMLEntity - Block bool - Indent int + Tag string + Text string + Style tcell.Style + Children []HTMLEntity + Block bool + Indent int DefaultHeight int @@ -97,7 +191,22 @@ type HTMLEntity struct { height int } -func (he *HTMLEntity) AdjustStyle(fn func(tcell.Style) tcell.Style) *HTMLEntity { +func NewHTMLTextEntity(text string) *BaseHTMLEntity { + return &BaseHTMLEntity{ + Tag: "text", + Text: text, + } +} + +func NewHTMLEntity(tag string, children []HTMLEntity, block bool) *BaseHTMLEntity { + return &BaseHTMLEntity{ + Tag: tag, + Children: children, + Block: block, + } +} + +func (he *BaseHTMLEntity) AdjustStyle(fn AdjustStyleFunc) HTMLEntity { for _, child := range he.Children { child.AdjustStyle(fn) } @@ -105,7 +214,23 @@ func (he *HTMLEntity) AdjustStyle(fn func(tcell.Style) tcell.Style) *HTMLEntity return he } -func (he *HTMLEntity) Draw(screen mauview.Screen) { +func (he *BaseHTMLEntity) IsBlock() bool { + return he.Block +} + +func (he *BaseHTMLEntity) GetTag() string { + return he.Tag +} + +func (he *BaseHTMLEntity) Height() int { + return he.height +} + +func (he *BaseHTMLEntity) getStartX() int { + return he.startX +} + +func (he *BaseHTMLEntity) Draw(screen mauview.Screen) { width, _ := screen.Size() if len(he.buffer) > 0 { x := he.startX @@ -117,19 +242,19 @@ func (he *HTMLEntity) Draw(screen mauview.Screen) { if len(he.Children) > 0 { proxyScreen := &mauview.ProxyScreen{Parent: screen, OffsetX: he.Indent, Width: width - he.Indent} for i, entity := range he.Children { - if i != 0 && entity.startX == 0 { + if i != 0 && entity.getStartX() == 0 { proxyScreen.OffsetY++ } - proxyScreen.Height = entity.height + proxyScreen.Height = entity.Height() entity.Draw(proxyScreen) - proxyScreen.OffsetY += entity.height - 1 + proxyScreen.OffsetY += entity.Height() - 1 } } } -func (he *HTMLEntity) String() string { +func (he *BaseHTMLEntity) String() string { var buf strings.Builder - buf.WriteString("&HTMLEntity{\n") + buf.WriteString("&BaseHTMLEntity{\n") _, _ = fmt.Fprintf(&buf, ` Tag="%s", Style=%d, Block=%t, Indent=%d, startX=%d, height=%d,`, he.Tag, he.Style, he.Block, he.Indent, he.startX, he.height) buf.WriteRune('\n') @@ -151,7 +276,7 @@ func (he *HTMLEntity) String() string { return buf.String() } -func (he *HTMLEntity) PlainText() string { +func (he *BaseHTMLEntity) PlainText() string { if len(he.Children) == 0 { return he.Text } @@ -159,12 +284,12 @@ func (he *HTMLEntity) PlainText() string { buf.WriteString(he.Text) newlined := false for _, child := range he.Children { - if child.Block && !newlined { + if child.IsBlock() && !newlined { buf.WriteRune('\n') } newlined = false buf.WriteString(child.PlainText()) - if child.Block { + if child.IsBlock() { buf.WriteRune('\n') newlined = true } @@ -172,7 +297,7 @@ func (he *HTMLEntity) PlainText() string { return buf.String() } -func (he *HTMLEntity) calculateBuffer(width, startX int, bare bool) int { +func (he *BaseHTMLEntity) calculateBuffer(width, startX int, bare bool) int { he.startX = startX if he.Block { he.startX = 0 @@ -181,11 +306,11 @@ func (he *HTMLEntity) calculateBuffer(width, startX int, bare bool) int { if len(he.Children) > 0 { childStartX := he.startX for _, entity := range he.Children { - if entity.Block || childStartX == 0 || he.height == 0 { + if entity.IsBlock() || childStartX == 0 || he.height == 0 { he.height++ } childStartX = entity.calculateBuffer(width-he.Indent, childStartX, bare) - he.height += entity.height - 1 + he.height += entity.Height() - 1 } if len(he.Text) == 0 && !he.Block { return childStartX diff --git a/ui/messages/parser/htmlparser.go b/ui/messages/parser/htmlparser.go index 688deb3..464459e 100644 --- a/ui/messages/parser/htmlparser.go +++ b/ui/messages/parser/htmlparser.go @@ -17,8 +17,6 @@ package parser import ( - "fmt" - "math" "regexp" "strconv" "strings" @@ -29,11 +27,11 @@ import ( "github.com/lucasb-eyer/go-colorful" "golang.org/x/net/html" - "maunium.net/go/gomuks/ui/messages" "maunium.net/go/mautrix" "maunium.net/go/tcell" "maunium.net/go/gomuks/matrix/rooms" + "maunium.net/go/gomuks/ui/messages" "maunium.net/go/gomuks/ui/widget" ) @@ -80,57 +78,30 @@ func (parser *htmlParser) getAttribute(node *html.Node, attribute string) string return "" } -func digits(num int) int { - if num <= 0 { - return 0 - } - return int(math.Floor(math.Log10(float64(num))) + 1) -} - -func (parser *htmlParser) listToTString(node *html.Node, stripLinebreak bool) *messages.HTMLEntity { +func (parser *htmlParser) listToEntity(node *html.Node, stripLinebreak bool) messages.HTMLEntity { + children := parser.nodeToEntities(node.FirstChild, stripLinebreak) ordered := node.Data == "ol" - listItems := parser.nodeToEntities(node.FirstChild, stripLinebreak) - counter := 1 - indentLength := 0 + start := 1 if ordered { - start := parser.getAttribute(node, "start") - if len(start) > 0 { - counter, _ = strconv.Atoi(start) + if startRaw := parser.getAttribute(node, "start"); len(startRaw) > 0 { + var err error + start, err = strconv.Atoi(startRaw) + if err != nil { + start = 1 + } } - - longestIndex := (counter - 1) + len(listItems) - indentLength = digits(longestIndex) } - var children []*messages.HTMLEntity - for _, child := range listItems { - if child.Tag != "li" { - continue - } - var prefix string - if ordered { - indexPadding := indentLength - digits(counter) - prefix = fmt.Sprintf("%d. %s", counter, strings.Repeat(" ", indexPadding)) - } else { - prefix = "● " + listItems := children[:0] + for _, child := range children { + if child.GetTag() == "li" { + listItems = append(listItems, child) } - child.Text = prefix + child.Text - child.Block = true - child.Indent = indentLength + 2 - children = append(children, child) - counter++ - } - return &messages.HTMLEntity{ - Tag: node.Data, - Text: "", - Style: tcell.StyleDefault, - Children: children, - Block: true, - Indent: 0, } + return messages.NewListEntity(ordered, start, listItems) } -func (parser *htmlParser) basicFormatToEntity(node *html.Node, stripLinebreak bool) *messages.HTMLEntity { - entity := &messages.HTMLEntity{ +func (parser *htmlParser) basicFormatToEntity(node *html.Node, stripLinebreak bool) messages.HTMLEntity { + entity := &messages.BaseHTMLEntity{ Tag: node.Data, Children: parser.nodeToEntities(node.FirstChild, stripLinebreak), } @@ -178,28 +149,22 @@ func (parser *htmlParser) parseColor(node *html.Node, mainName, altName string) return tcell.NewRGBColor(int32(r), int32(g), int32(b)), true } -func (parser *htmlParser) headerToEntity(node *html.Node, stripLinebreak bool) *messages.HTMLEntity { +func (parser *htmlParser) headerToEntity(node *html.Node, stripLinebreak bool) messages.HTMLEntity { length := int(node.Data[1] - '0') prefix := strings.Repeat("#", length) + " " - return (&messages.HTMLEntity{ + return (&messages.BaseHTMLEntity{ Tag: node.Data, Text: prefix, Children: parser.nodeToEntities(node.FirstChild, stripLinebreak), }).AdjustStyle(AdjustStyleBold) } -func (parser *htmlParser) blockquoteToEntity(node *html.Node, stripLinebreak bool) *messages.HTMLEntity { - return &messages.HTMLEntity{ - Tag: "blockquote", - Text: ">", - Children: parser.nodeToEntities(node.FirstChild, stripLinebreak), - Block: true, - Indent: 2, - } +func (parser *htmlParser) blockquoteToEntity(node *html.Node, stripLinebreak bool) messages.HTMLEntity { + return messages.NewBlockquoteEntity(parser.nodeToEntities(node.FirstChild, stripLinebreak)) } -func (parser *htmlParser) linkToEntity(node *html.Node, stripLinebreak bool) *messages.HTMLEntity { - entity := &messages.HTMLEntity{ +func (parser *htmlParser) linkToEntity(node *html.Node, stripLinebreak bool) messages.HTMLEntity { + entity := &messages.BaseHTMLEntity{ Tag: "a", Children: parser.nodeToEntities(node.FirstChild, stripLinebreak), } @@ -223,7 +188,7 @@ func (parser *htmlParser) linkToEntity(node *html.Node, stripLinebreak bool) *me return entity } -func (parser *htmlParser) imageToEntity(node *html.Node) *messages.HTMLEntity { +func (parser *htmlParser) imageToEntity(node *html.Node) messages.HTMLEntity { alt := parser.getAttribute(node, "alt") if len(alt) == 0 { alt = parser.getAttribute(node, "title") @@ -231,7 +196,7 @@ func (parser *htmlParser) imageToEntity(node *html.Node) *messages.HTMLEntity { alt = "[inline image]" } } - entity := &messages.HTMLEntity{ + entity := &messages.BaseHTMLEntity{ Tag: "img", Text: alt, } @@ -255,7 +220,7 @@ func styleEntryToStyle(se chroma.StyleEntry) tcell.Style { Background(colourToColor(se.Background)) } -func (parser *htmlParser) syntaxHighlight(text, language string) *messages.HTMLEntity { +func (parser *htmlParser) syntaxHighlight(text, language string) messages.HTMLEntity { lexer := lexers.Get(language) if lexer == nil { return nil @@ -266,12 +231,12 @@ func (parser *htmlParser) syntaxHighlight(text, language string) *messages.HTMLE } style := styles.SolarizedDark tokens := iter.Tokens() - children := make([]*messages.HTMLEntity, len(tokens)) + children := make([]messages.HTMLEntity, len(tokens)) for i, token := range tokens { if token.Value == "\n" { - children[i] = &messages.HTMLEntity{Block: true, Tag: "br"} + children[i] = &messages.BaseHTMLEntity{Block: true, Tag: "br"} } else { - children[i] = &messages.HTMLEntity{ + children[i] = &messages.BaseHTMLEntity{ Tag: token.Type.String(), Text: token.Value, Style: styleEntryToStyle(style.Get(token.Type)), @@ -280,21 +245,21 @@ func (parser *htmlParser) syntaxHighlight(text, language string) *messages.HTMLE } } } - return &messages.HTMLEntity{ + return &messages.BaseHTMLEntity{ Tag: "pre", Block: true, Children: children, } } -func (parser *htmlParser) codeblockToEntity(node *html.Node) *messages.HTMLEntity { - entity := &messages.HTMLEntity{ +func (parser *htmlParser) codeblockToEntity(node *html.Node) messages.HTMLEntity { + entity := &messages.BaseHTMLEntity{ Tag: "pre", Block: true, } // TODO allow disabling syntax highlighting if node.FirstChild.Type == html.ElementNode && node.FirstChild.Data == "code" { - text := (&messages.HTMLEntity{ + text := (&messages.BaseHTMLEntity{ Children: parser.nodeToEntities(node.FirstChild.FirstChild, false), }).PlainText() attr := parser.getAttribute(node.FirstChild, "class") @@ -315,16 +280,16 @@ func (parser *htmlParser) codeblockToEntity(node *html.Node) *messages.HTMLEntit return entity } -func (parser *htmlParser) tagNodeToEntity(node *html.Node, stripLinebreak bool) *messages.HTMLEntity { +func (parser *htmlParser) tagNodeToEntity(node *html.Node, stripLinebreak bool) messages.HTMLEntity { switch node.Data { case "blockquote": return parser.blockquoteToEntity(node, stripLinebreak) case "ol", "ul": - return parser.listToTString(node, stripLinebreak) + return parser.listToEntity(node, stripLinebreak) case "h1", "h2", "h3", "h4", "h5", "h6": return parser.headerToEntity(node, stripLinebreak) case "br": - return &messages.HTMLEntity{Tag: "br", Block: true} + return &messages.BaseHTMLEntity{Tag: "br", Block: true} case "b", "strong", "i", "em", "s", "del", "u", "ins", "font": return parser.basicFormatToEntity(node, stripLinebreak) case "a": @@ -334,7 +299,7 @@ func (parser *htmlParser) tagNodeToEntity(node *html.Node, stripLinebreak bool) case "pre": return parser.codeblockToEntity(node) default: - return &messages.HTMLEntity{ + return &messages.BaseHTMLEntity{ Tag: node.Data, Children: parser.nodeToEntities(node.FirstChild, stripLinebreak), Block: parser.isBlockTag(node.Data), @@ -342,13 +307,13 @@ func (parser *htmlParser) tagNodeToEntity(node *html.Node, stripLinebreak bool) } } -func (parser *htmlParser) singleNodeToEntity(node *html.Node, stripLinebreak bool) *messages.HTMLEntity { +func (parser *htmlParser) singleNodeToEntity(node *html.Node, stripLinebreak bool) messages.HTMLEntity { switch node.Type { case html.TextNode: if stripLinebreak { node.Data = strings.Replace(node.Data, "\n", "", -1) } - return &messages.HTMLEntity{ + return &messages.BaseHTMLEntity{ Tag: "text", Text: node.Data, } @@ -358,7 +323,7 @@ func (parser *htmlParser) singleNodeToEntity(node *html.Node, stripLinebreak boo if node.FirstChild.Data == "html" && node.FirstChild.NextSibling == nil { return parser.singleNodeToEntity(node.FirstChild, stripLinebreak) } - return &messages.HTMLEntity{ + return &messages.BaseHTMLEntity{ Tag: "html", Children: parser.nodeToEntities(node.FirstChild, stripLinebreak), Block: true, @@ -368,7 +333,7 @@ func (parser *htmlParser) singleNodeToEntity(node *html.Node, stripLinebreak boo } } -func (parser *htmlParser) nodeToEntities(node *html.Node, stripLinebreak bool) (entities []*messages.HTMLEntity) { +func (parser *htmlParser) nodeToEntities(node *html.Node, stripLinebreak bool) (entities []messages.HTMLEntity) { for ; node != nil; node = node.NextSibling { if entity := parser.singleNodeToEntity(node, stripLinebreak); entity != nil { entities = append(entities, entity) @@ -377,7 +342,7 @@ func (parser *htmlParser) nodeToEntities(node *html.Node, stripLinebreak bool) ( return } -var BlockTags = []string{"p", "h1", "h2", "h3", "h4", "h5", "h6", "ol", "ul", "pre", "blockquote", "div", "hr", "table"} +var BlockTags = []string{"p", "h1", "h2", "h3", "h4", "h5", "h6", "ol", "ul", "li", "pre", "blockquote", "div", "hr", "table"} func (parser *htmlParser) isBlockTag(tag string) bool { for _, blockTag := range BlockTags { @@ -388,27 +353,27 @@ func (parser *htmlParser) isBlockTag(tag string) bool { return false } -func (parser *htmlParser) Parse(htmlData string) *messages.HTMLEntity { +func (parser *htmlParser) Parse(htmlData string) messages.HTMLEntity { node, _ := html.Parse(strings.NewReader(htmlData)) return parser.singleNodeToEntity(node, true) } // ParseHTMLMessage parses a HTML-formatted Matrix event into a UIMessage. -func ParseHTMLMessage(room *rooms.Room, evt *mautrix.Event, senderDisplayname string) *messages.HTMLEntity { +func ParseHTMLMessage(room *rooms.Room, evt *mautrix.Event, senderDisplayname string) messages.HTMLEntity { htmlData := evt.Content.FormattedBody htmlData = strings.Replace(htmlData, "\t", " ", -1) parser := htmlParser{room} root := parser.Parse(htmlData) - root.Block = false + root.(*messages.BaseHTMLEntity).Block = false if evt.Content.MsgType == mautrix.MsgEmote { - root = &messages.HTMLEntity{ + root = &messages.BaseHTMLEntity{ Tag: "emote", - Children: []*messages.HTMLEntity{ - {Text: "* "}, - {Text: senderDisplayname, Style: tcell.StyleDefault.Foreground(widget.GetHashColor(evt.Sender))}, - {Text: " "}, + Children: []messages.HTMLEntity{ + messages.NewHTMLTextEntity("* "), + messages.NewHTMLTextEntity("* ").AdjustStyle(AdjustStyleTextColor(widget.GetHashColor(evt.Sender))), + messages.NewHTMLTextEntity(" "), root, }, } -- cgit v1.2.3 From 5d7c1a4caab46f7e981aed7b9cc825b7602b4098 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 7 Apr 2019 22:54:55 +0300 Subject: Improve handling of multiple linebreaks --- ui/message-view.go | 13 ++++---- ui/messages/htmlmessage.go | 67 +++++++++++++++++++++++++++------------- ui/messages/parser/htmlparser.go | 2 +- ui/messages/parser/parser.go | 2 +- 4 files changed, 54 insertions(+), 30 deletions(-) (limited to 'ui') diff --git a/ui/message-view.go b/ui/message-view.go index 20831f1..144bfca 100644 --- a/ui/message-view.go +++ b/ui/message-view.go @@ -503,12 +503,13 @@ func (view *MessageView) Draw(screen mauview.Screen) { if len(meta.FormatTime()) > 0 { widget.WriteLineSimpleColor(screen, meta.FormatTime(), 0, line, meta.TimestampColor()) } - if !bareMode && (prevMeta == nil || meta.Sender() != prevMeta.Sender()) { - widget.WriteLineColor( - screen, mauview.AlignRight, meta.Sender(), - usernameX, line, view.widestSender, - meta.SenderColor()) - } + // TODO hiding senders might not be that nice after all, maybe an option? (disabled for now) + //if !bareMode && (prevMeta == nil || meta.Sender() != prevMeta.Sender()) { + widget.WriteLineColor( + screen, mauview.AlignRight, meta.Sender(), + usernameX, line, view.widestSender, + meta.SenderColor()) + //} prevMeta = meta } diff --git a/ui/messages/htmlmessage.go b/ui/messages/htmlmessage.go index 34bba2b..8685442 100644 --- a/ui/messages/htmlmessage.go +++ b/ui/messages/htmlmessage.go @@ -173,6 +173,17 @@ func (le *ListEntity) String() string { return fmt.Sprintf("&ListEntity{Ordered=%t, Start=%d, Base=%s},\n", le.Ordered, le.Start, le.BaseHTMLEntity) } +type BreakEntity struct { + *BaseHTMLEntity +} + +func NewBreakEntity() *BreakEntity { + return &BreakEntity{&BaseHTMLEntity{ + Tag: "br", + Block: true, + }} +} + type BaseHTMLEntity struct { // Permanent variables Tag string @@ -230,28 +241,6 @@ func (he *BaseHTMLEntity) getStartX() int { return he.startX } -func (he *BaseHTMLEntity) Draw(screen mauview.Screen) { - width, _ := screen.Size() - if len(he.buffer) > 0 { - x := he.startX - for y, line := range he.buffer { - widget.WriteLine(screen, mauview.AlignLeft, line, x, y, width, he.Style) - x = 0 - } - } - if len(he.Children) > 0 { - proxyScreen := &mauview.ProxyScreen{Parent: screen, OffsetX: he.Indent, Width: width - he.Indent} - for i, entity := range he.Children { - if i != 0 && entity.getStartX() == 0 { - proxyScreen.OffsetY++ - } - proxyScreen.Height = entity.Height() - entity.Draw(proxyScreen) - proxyScreen.OffsetY += entity.Height() - 1 - } - } -} - func (he *BaseHTMLEntity) String() string { var buf strings.Builder buf.WriteString("&BaseHTMLEntity{\n") @@ -297,6 +286,34 @@ func (he *BaseHTMLEntity) PlainText() string { return buf.String() } +func (he *BaseHTMLEntity) Draw(screen mauview.Screen) { + width, _ := screen.Size() + if len(he.buffer) > 0 { + x := he.startX + for y, line := range he.buffer { + widget.WriteLine(screen, mauview.AlignLeft, line, x, y, width, he.Style) + x = 0 + } + } + if len(he.Children) > 0 { + prevBreak := false + proxyScreen := &mauview.ProxyScreen{Parent: screen, OffsetX: he.Indent, Width: width - he.Indent} + for i, entity := range he.Children { + if i != 0 && entity.getStartX() == 0 { + proxyScreen.OffsetY++ + } + proxyScreen.Height = entity.Height() + entity.Draw(proxyScreen) + proxyScreen.OffsetY += entity.Height() - 1 + _, isBreak := entity.(*BreakEntity) + if prevBreak && isBreak { + proxyScreen.OffsetY++ + } + prevBreak = isBreak + } + } +} + func (he *BaseHTMLEntity) calculateBuffer(width, startX int, bare bool) int { he.startX = startX if he.Block { @@ -305,12 +322,18 @@ func (he *BaseHTMLEntity) calculateBuffer(width, startX int, bare bool) int { he.height = 0 if len(he.Children) > 0 { childStartX := he.startX + prevBreak := false for _, entity := range he.Children { if entity.IsBlock() || childStartX == 0 || he.height == 0 { he.height++ } childStartX = entity.calculateBuffer(width-he.Indent, childStartX, bare) he.height += entity.Height() - 1 + _, isBreak := entity.(*BreakEntity) + if prevBreak && isBreak { + he.height++ + } + prevBreak = isBreak } if len(he.Text) == 0 && !he.Block { return childStartX diff --git a/ui/messages/parser/htmlparser.go b/ui/messages/parser/htmlparser.go index 464459e..e658c61 100644 --- a/ui/messages/parser/htmlparser.go +++ b/ui/messages/parser/htmlparser.go @@ -289,7 +289,7 @@ func (parser *htmlParser) tagNodeToEntity(node *html.Node, stripLinebreak bool) case "h1", "h2", "h3", "h4", "h5", "h6": return parser.headerToEntity(node, stripLinebreak) case "br": - return &messages.BaseHTMLEntity{Tag: "br", Block: true} + return messages.NewBreakEntity() case "b", "strong", "i", "em", "s", "del", "u", "ins", "font": return parser.basicFormatToEntity(node, stripLinebreak) case "a": diff --git a/ui/messages/parser/parser.go b/ui/messages/parser/parser.go index 4181c09..e48bd5f 100644 --- a/ui/messages/parser/parser.go +++ b/ui/messages/parser/parser.go @@ -113,7 +113,7 @@ func ParseMessage(matrix ifc.MatrixContainer, room *rooms.Room, evt *mautrix.Eve replyToEvt.Content.FormattedBody = html.EscapeString(replyToEvt.Content.Body) } evt.Content.FormattedBody = fmt.Sprintf( - "In reply to %[1]s
%[2]s

%[3]s", + "In reply to %[1]s
%[2]s


%[3]s", replyToEvt.Sender, replyToEvt.Content.FormattedBody, evt.Content.FormattedBody) } else { evt.Content.FormattedBody = fmt.Sprintf( -- cgit v1.2.3