From ff7ee333a1028850337aa332532cbc23c7bdde14 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Wed, 11 Apr 2018 17:57:15 +0300 Subject: Rename UIString to TString, move ansimage to lib/ and switch to tcell fork --- ui/messages/cell.go | 51 ---------------- ui/messages/imagemessage.go | 26 +++----- ui/messages/message.go | 3 +- ui/messages/meta.go | 2 +- ui/messages/parser.go | 19 +++--- ui/messages/string.go | 138 ------------------------------------------ ui/messages/textmessage.go | 31 +++++----- ui/messages/tstring/cell.go | 51 ++++++++++++++++ ui/messages/tstring/string.go | 138 ++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 227 insertions(+), 232 deletions(-) delete mode 100644 ui/messages/cell.go delete mode 100644 ui/messages/string.go create mode 100644 ui/messages/tstring/cell.go create mode 100644 ui/messages/tstring/string.go (limited to 'ui/messages') diff --git a/ui/messages/cell.go b/ui/messages/cell.go deleted file mode 100644 index a919da7..0000000 --- a/ui/messages/cell.go +++ /dev/null @@ -1,51 +0,0 @@ -// gomuks - A terminal Matrix client written in Go. -// Copyright (C) 2018 Tulir Asokan -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU 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 General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -package messages - -import ( - "github.com/gdamore/tcell" - "github.com/mattn/go-runewidth" -) - -type Cell struct { - Char rune - Style tcell.Style -} - -func NewStyleCell(char rune, style tcell.Style) Cell { - return Cell{char, style} -} - -func NewColorCell(char rune, color tcell.Color) Cell { - return Cell{char, tcell.StyleDefault.Foreground(color)} -} - -func NewCell(char rune) Cell { - return Cell{char, tcell.StyleDefault} -} - -func (cell Cell) RuneWidth() int { - return runewidth.RuneWidth(cell.Char) -} - -func (cell Cell) Draw(screen tcell.Screen, x, y int) (chWidth int) { - chWidth = cell.RuneWidth() - for runeWidthOffset := 0; runeWidthOffset < chWidth; runeWidthOffset++ { - screen.SetContent(x+runeWidthOffset, y, cell.Char, nil, cell.Style) - } - return -} diff --git a/ui/messages/imagemessage.go b/ui/messages/imagemessage.go index 53c0588..7129e5a 100644 --- a/ui/messages/imagemessage.go +++ b/ui/messages/imagemessage.go @@ -23,11 +23,12 @@ import ( "image/color" - "github.com/gdamore/tcell" "maunium.net/go/gomuks/debug" "maunium.net/go/gomuks/interface" + "maunium.net/go/gomuks/ui/messages/tstring" "maunium.net/go/gomuks/ui/widget" - "maunium.net/go/pixterm/ansimage" + "maunium.net/go/gomuks/lib/ansimage" + "maunium.net/go/tcell" ) func init() { @@ -36,11 +37,12 @@ func init() { type UIImageMessage struct { UITextMessage + Path string data []byte } // NewImageMessage creates a new UIImageMessage object with the provided values and the default state. -func NewImageMessage(id, sender, msgtype string, data []byte, timestamp time.Time) UIMessage { +func NewImageMessage(id, sender, msgtype string, path string, data []byte, timestamp time.Time) UIMessage { return &UIImageMessage{ UITextMessage{ MsgSender: sender, @@ -53,6 +55,7 @@ func NewImageMessage(id, sender, msgtype string, data []byte, timestamp time.Tim MsgIsHighlight: false, MsgIsService: false, }, + path, data, } } @@ -90,24 +93,13 @@ func (msg *UIImageMessage) CalculateBuffer(width int) { return } - image, err := ansimage.NewScaledFromReader(bytes.NewReader(msg.data), -1, width, color.Black, ansimage.ScaleModeResize, ansimage.NoDithering) + image, err := ansimage.NewScaledFromReader(bytes.NewReader(msg.data), 0, width, color.Black) if err != nil { - msg.buffer = []UIString{NewColorUIString("Failed to display image", tcell.ColorRed)} + msg.buffer = []tstring.TString{tstring.NewColorTString("Failed to display image", tcell.ColorRed)} debug.Print("Failed to display image:", err) return } - msg.buffer = make([]UIString, image.Height()) - pixels := image.Pixmap() - for row, pixelRow := range pixels { - msg.buffer[row] = make(UIString, len(pixelRow)) - for column, pixel := range pixelRow { - pixelColor := tcell.NewRGBColor(int32(pixel.R), int32(pixel.G), int32(pixel.B)) - msg.buffer[row][column] = Cell{ - Char: ' ', - Style: tcell.StyleDefault.Background(pixelColor), - } - } - } + msg.buffer = image.Render() msg.prevBufferWidth = width } diff --git a/ui/messages/message.go b/ui/messages/message.go index f116d84..ac118ad 100644 --- a/ui/messages/message.go +++ b/ui/messages/message.go @@ -18,6 +18,7 @@ package messages import ( "maunium.net/go/gomuks/interface" + "maunium.net/go/gomuks/ui/messages/tstring" ) // Message is a wrapper for the content and metadata of a Matrix message intended to be displayed. @@ -26,7 +27,7 @@ type UIMessage interface { CalculateBuffer(width int) RecalculateBuffer() - Buffer() []UIString + Buffer() []tstring.TString Height() int RealSender() string diff --git a/ui/messages/meta.go b/ui/messages/meta.go index 3f9c9ab..7e2f29f 100644 --- a/ui/messages/meta.go +++ b/ui/messages/meta.go @@ -19,7 +19,7 @@ package messages import ( "time" - "github.com/gdamore/tcell" + "maunium.net/go/tcell" "maunium.net/go/gomuks/interface" ) diff --git a/ui/messages/parser.go b/ui/messages/parser.go index 4300c86..22ba4e7 100644 --- a/ui/messages/parser.go +++ b/ui/messages/parser.go @@ -20,12 +20,13 @@ import ( "fmt" "time" - "github.com/gdamore/tcell" "maunium.net/go/gomatrix" "maunium.net/go/gomuks/debug" "maunium.net/go/gomuks/interface" "maunium.net/go/gomuks/matrix/rooms" + "maunium.net/go/gomuks/ui/messages/tstring" "maunium.net/go/gomuks/ui/widget" + "maunium.net/go/tcell" ) func ParseEvent(mx ifc.MatrixContainer, room *rooms.Room, evt *gomatrix.Event) UIMessage { @@ -59,16 +60,16 @@ func ParseMessage(mx ifc.MatrixContainer, evt *gomatrix.Event) UIMessage { return NewTextMessage(evt.ID, evt.Sender, msgtype, text, ts) case "m.image": url, _ := evt.Content["url"].(string) - data, err := mx.Download(url) + data, path, err := mx.Download(url) if err != nil { debug.Printf("Failed to download %s: %v", url, err) } - return NewImageMessage(evt.ID, evt.Sender, msgtype, data, ts) + return NewImageMessage(evt.ID, evt.Sender, msgtype, path, data, ts) } return nil } -func getMembershipEventContent(evt *gomatrix.Event) (sender string, text UIString) { +func getMembershipEventContent(evt *gomatrix.Event) (sender string, text tstring.TString) { membership, _ := evt.Content["membership"].(string) displayname, _ := evt.Content["displayname"].(string) if len(displayname) == 0 { @@ -85,28 +86,28 @@ func getMembershipEventContent(evt *gomatrix.Event) (sender string, text UIStrin switch membership { case "invite": sender = "---" - text = NewColorUIString(fmt.Sprintf("%s invited %s.", evt.Sender, displayname), tcell.ColorYellow) + text = tstring.NewColorTString(fmt.Sprintf("%s invited %s.", evt.Sender, displayname), tcell.ColorYellow) text.Colorize(0, len(evt.Sender), widget.GetHashColor(evt.Sender)) text.Colorize(len(evt.Sender)+len(" invited "), len(displayname), widget.GetHashColor(displayname)) case "join": sender = "-->" - text = NewColorUIString(fmt.Sprintf("%s joined the room.", displayname), tcell.ColorGreen) + text = tstring.NewColorTString(fmt.Sprintf("%s joined the room.", displayname), tcell.ColorGreen) text.Colorize(0, len(displayname), widget.GetHashColor(displayname)) case "leave": sender = "<--" if evt.Sender != *evt.StateKey { reason, _ := evt.Content["reason"].(string) - text = NewColorUIString(fmt.Sprintf("%s kicked %s: %s", evt.Sender, displayname, reason), tcell.ColorRed) + text = tstring.NewColorTString(fmt.Sprintf("%s kicked %s: %s", evt.Sender, displayname, reason), tcell.ColorRed) text.Colorize(0, len(evt.Sender), widget.GetHashColor(evt.Sender)) text.Colorize(len(evt.Sender)+len(" kicked "), len(displayname), widget.GetHashColor(displayname)) } else { - text = NewColorUIString(fmt.Sprintf("%s left the room.", displayname), tcell.ColorRed) + text = tstring.NewColorTString(fmt.Sprintf("%s left the room.", displayname), tcell.ColorRed) text.Colorize(0, len(displayname), widget.GetHashColor(displayname)) } } } else if displayname != prevDisplayname { sender = "---" - text = NewColorUIString(fmt.Sprintf("%s changed their display name to %s.", prevDisplayname, displayname), tcell.ColorYellow) + text = tstring.NewColorTString(fmt.Sprintf("%s changed their display name to %s.", prevDisplayname, displayname), tcell.ColorYellow) text.Colorize(0, len(prevDisplayname), widget.GetHashColor(prevDisplayname)) text.Colorize(len(prevDisplayname)+len(" changed their display name to "), len(displayname), widget.GetHashColor(displayname)) } diff --git a/ui/messages/string.go b/ui/messages/string.go deleted file mode 100644 index 7c3143b..0000000 --- a/ui/messages/string.go +++ /dev/null @@ -1,138 +0,0 @@ -// gomuks - A terminal Matrix client written in Go. -// Copyright (C) 2018 Tulir Asokan -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU 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 General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -package messages - -import ( - "strings" - - "github.com/gdamore/tcell" - "github.com/mattn/go-runewidth" -) - -type UIString []Cell - -func NewUIString(str string) UIString { - newStr := make([]Cell, len(str)) - for i, char := range str { - newStr[i] = NewCell(char) - } - return newStr -} - -func NewColorUIString(str string, color tcell.Color) UIString { - newStr := make([]Cell, len(str)) - for i, char := range str { - newStr[i] = NewColorCell(char, color) - } - return newStr -} - -func NewStyleUIString(str string, style tcell.Style) UIString { - newStr := make([]Cell, len(str)) - for i, char := range str { - newStr[i] = NewStyleCell(char, style) - } - return newStr -} - -func (str UIString) Colorize(from, length int, color tcell.Color) { - for i := from; i < from+length; i++ { - str[i].Style = str[i].Style.Foreground(color) - } -} - -func (str UIString) Draw(screen tcell.Screen, x, y int) { - offsetX := 0 - for _, cell := range str { - offsetX += cell.Draw(screen, x+offsetX, y) - } -} - -func (str UIString) RuneWidth() (width int) { - for _, cell := range str { - width += runewidth.RuneWidth(cell.Char) - } - return width -} - -func (str UIString) String() string { - var buf strings.Builder - for _, cell := range str { - buf.WriteRune(cell.Char) - } - return buf.String() -} - -// Truncate return string truncated with w cells -func (str UIString) Truncate(w int) UIString { - if str.RuneWidth() <= w { - return str[:] - } - width := 0 - i := 0 - for ; i < len(str); i++ { - cw := runewidth.RuneWidth(str[i].Char) - if width+cw > w { - break - } - width += cw - } - return str[0:i] -} - -func (str UIString) IndexFrom(r rune, from int) int { - for i := from; i < len(str); i++ { - if str[i].Char == r { - return i - } - } - return -1 -} - -func (str UIString) Index(r rune) int { - return str.IndexFrom(r, 0) -} - -func (str UIString) Count(r rune) (counter int) { - index := 0 - for { - index = str.IndexFrom(r, index) - if index < 0 { - break - } - index++ - counter++ - } - return -} - -func (str UIString) Split(sep rune) []UIString { - a := make([]UIString, str.Count(sep)+1) - i := 0 - orig := str - for { - m := orig.Index(sep) - if m < 0 { - break - } - a[i] = orig[:m] - orig = orig[m+1:] - i++ - } - a[i] = orig - return a[:i+1] -} diff --git a/ui/messages/textmessage.go b/ui/messages/textmessage.go index 8ad3168..12b098d 100644 --- a/ui/messages/textmessage.go +++ b/ui/messages/textmessage.go @@ -22,7 +22,8 @@ import ( "regexp" "time" - "github.com/gdamore/tcell" + "maunium.net/go/gomuks/ui/messages/tstring" + "maunium.net/go/tcell" "maunium.net/go/gomuks/interface" "maunium.net/go/gomuks/ui/widget" ) @@ -34,11 +35,11 @@ func init() { type UIExpandedTextMessage struct { UITextMessage - MsgUIStringText UIString + MsgTStringText tstring.TString } // NewExpandedTextMessage creates a new UIExpandedTextMessage object with the provided values and the default state. -func NewExpandedTextMessage(id, sender, msgtype string, text UIString, timestamp time.Time) UIMessage { +func NewExpandedTextMessage(id, sender, msgtype string, text tstring.TString, timestamp time.Time) UIMessage { return &UIExpandedTextMessage{ UITextMessage{ MsgSender: sender, @@ -56,8 +57,8 @@ func NewExpandedTextMessage(id, sender, msgtype string, text UIString, timestamp } } -func (msg *UIExpandedTextMessage) GetUIStringText() UIString { - return msg.MsgUIStringText +func (msg *UIExpandedTextMessage) GetTStringText() tstring.TString { + return msg.MsgTStringText } // CopyFrom replaces the content of this message object with the content of the given object. @@ -78,9 +79,9 @@ func (msg *UIExpandedTextMessage) CopyFrom(from ifc.MessageMeta) { fromExpandedMsg, ok := from.(*UIExpandedTextMessage) if ok { - msg.MsgUIStringText = fromExpandedMsg.MsgUIStringText + msg.MsgTStringText = fromExpandedMsg.MsgTStringText } else { - msg.MsgUIStringText = NewColorUIString(fromMsg.Text(), from.TextColor()) + msg.MsgTStringText = tstring.NewColorTString(fromMsg.Text(), from.TextColor()) } msg.RecalculateBuffer() @@ -97,7 +98,7 @@ type UITextMessage struct { MsgState ifc.MessageState MsgIsHighlight bool MsgIsService bool - buffer []UIString + buffer []tstring.TString prevBufferWidth int } @@ -235,7 +236,7 @@ func (msg *UITextMessage) RecalculateBuffer() { // // N.B. This will NOT automatically calculate the buffer if it hasn't been // calculated already, as that requires the target width. -func (msg *UITextMessage) Buffer() []UIString { +func (msg *UITextMessage) Buffer() []tstring.TString { return msg.buffer } @@ -307,8 +308,8 @@ func (msg *UITextMessage) SetIsService(isService bool) { msg.MsgIsService = isService } -func (msg *UITextMessage) GetUIStringText() UIString { - return NewColorUIString(msg.Text(), msg.TextColor()) +func (msg *UITextMessage) GetTStringText() tstring.TString { + return tstring.NewColorTString(msg.Text(), msg.TextColor()) } // Regular expressions used to split lines when calculating the buffer. @@ -327,10 +328,10 @@ func (msg *UITextMessage) CalculateBuffer(width int) { return } - msg.buffer = []UIString{} - text := msg.GetUIStringText() + msg.buffer = []tstring.TString{} + text := msg.GetTStringText() if msg.MsgType == "m.emote" { - text = NewColorUIString(fmt.Sprintf("* %s %s", msg.MsgSender, text.String()), msg.TextColor()) + text = tstring.NewColorTString(fmt.Sprintf("* %s %s", msg.MsgSender, text.String()), msg.TextColor()) text.Colorize(2, len(msg.MsgSender), msg.SenderColor()) } @@ -338,7 +339,7 @@ func (msg *UITextMessage) CalculateBuffer(width int) { newlines := 0 for _, str := range forcedLinebreaks { if len(str) == 0 && newlines < 1 { - msg.buffer = append(msg.buffer, UIString{}) + msg.buffer = append(msg.buffer, tstring.TString{}) newlines++ } else { newlines = 0 diff --git a/ui/messages/tstring/cell.go b/ui/messages/tstring/cell.go new file mode 100644 index 0000000..8a400ee --- /dev/null +++ b/ui/messages/tstring/cell.go @@ -0,0 +1,51 @@ +// gomuks - A terminal Matrix client written in Go. +// Copyright (C) 2018 Tulir Asokan +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package tstring + +import ( + "maunium.net/go/tcell" + "github.com/mattn/go-runewidth" +) + +type Cell struct { + Char rune + Style tcell.Style +} + +func NewStyleCell(char rune, style tcell.Style) Cell { + return Cell{char, style} +} + +func NewColorCell(char rune, color tcell.Color) Cell { + return Cell{char, tcell.StyleDefault.Foreground(color)} +} + +func NewCell(char rune) Cell { + return Cell{char, tcell.StyleDefault} +} + +func (cell Cell) RuneWidth() int { + return runewidth.RuneWidth(cell.Char) +} + +func (cell Cell) Draw(screen tcell.Screen, x, y int) (chWidth int) { + chWidth = cell.RuneWidth() + for runeWidthOffset := 0; runeWidthOffset < chWidth; runeWidthOffset++ { + screen.SetContent(x+runeWidthOffset, y, cell.Char, nil, cell.Style) + } + return +} diff --git a/ui/messages/tstring/string.go b/ui/messages/tstring/string.go new file mode 100644 index 0000000..ad0b2c8 --- /dev/null +++ b/ui/messages/tstring/string.go @@ -0,0 +1,138 @@ +// gomuks - A terminal Matrix client written in Go. +// Copyright (C) 2018 Tulir Asokan +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package tstring + +import ( + "strings" + + "maunium.net/go/tcell" + "github.com/mattn/go-runewidth" +) + +type TString []Cell + +func NewTString(str string) TString { + newStr := make([]Cell, len(str)) + for i, char := range str { + newStr[i] = NewCell(char) + } + return newStr +} + +func NewColorTString(str string, color tcell.Color) TString { + newStr := make([]Cell, len(str)) + for i, char := range str { + newStr[i] = NewColorCell(char, color) + } + return newStr +} + +func NewStyleTString(str string, style tcell.Style) TString { + newStr := make([]Cell, len(str)) + for i, char := range str { + newStr[i] = NewStyleCell(char, style) + } + return newStr +} + +func (str TString) Colorize(from, length int, color tcell.Color) { + for i := from; i < from+length; i++ { + str[i].Style = str[i].Style.Foreground(color) + } +} + +func (str TString) Draw(screen tcell.Screen, x, y int) { + offsetX := 0 + for _, cell := range str { + offsetX += cell.Draw(screen, x+offsetX, y) + } +} + +func (str TString) RuneWidth() (width int) { + for _, cell := range str { + width += runewidth.RuneWidth(cell.Char) + } + return width +} + +func (str TString) String() string { + var buf strings.Builder + for _, cell := range str { + buf.WriteRune(cell.Char) + } + return buf.String() +} + +// Truncate return string truncated with w cells +func (str TString) Truncate(w int) TString { + if str.RuneWidth() <= w { + return str[:] + } + width := 0 + i := 0 + for ; i < len(str); i++ { + cw := runewidth.RuneWidth(str[i].Char) + if width+cw > w { + break + } + width += cw + } + return str[0:i] +} + +func (str TString) IndexFrom(r rune, from int) int { + for i := from; i < len(str); i++ { + if str[i].Char == r { + return i + } + } + return -1 +} + +func (str TString) Index(r rune) int { + return str.IndexFrom(r, 0) +} + +func (str TString) Count(r rune) (counter int) { + index := 0 + for { + index = str.IndexFrom(r, index) + if index < 0 { + break + } + index++ + counter++ + } + return +} + +func (str TString) Split(sep rune) []TString { + a := make([]TString, str.Count(sep)+1) + i := 0 + orig := str + for { + m := orig.Index(sep) + if m < 0 { + break + } + a[i] = orig[:m] + orig = orig[m+1:] + i++ + } + a[i] = orig + return a[:i+1] +} -- cgit v1.2.3