aboutsummaryrefslogtreecommitdiff
path: root/ui/messages/textbase.go
diff options
context:
space:
mode:
authorTulir Asokan <tulir@maunium.net>2018-05-22 22:06:48 +0300
committerTulir Asokan <tulir@maunium.net>2018-05-22 22:06:49 +0300
commit14903e0cdcd3ba78face2cbe0ad0287da269a1ea (patch)
tree664fd5cec84d1728bc704a31955ae03bfe9d32ea /ui/messages/textbase.go
parent4849ef08b861ee5886f879c320ee6e1d8169f139 (diff)
Add bare mode and fix terminal resize bug. Fixes #48
Diffstat (limited to 'ui/messages/textbase.go')
-rw-r--r--ui/messages/textbase.go39
1 files changed, 19 insertions, 20 deletions
diff --git a/ui/messages/textbase.go b/ui/messages/textbase.go
index f805067..faf53e6 100644
--- a/ui/messages/textbase.go
+++ b/ui/messages/textbase.go
@@ -17,35 +17,26 @@
package messages
import (
- "encoding/gob"
"regexp"
- "time"
-
"maunium.net/go/gomuks/ui/messages/tstring"
+ "fmt"
)
-func init() {
- gob.Register(BaseTextMessage{})
-}
-
-type BaseTextMessage struct {
- BaseMessage
-}
-
-func newBaseTextMessage(id, sender, displayname, msgtype string, timestamp time.Time) BaseTextMessage {
- return BaseTextMessage{newBaseMessage(id, sender, displayname, msgtype, timestamp)}
-}
-
// Regular expressions used to split lines when calculating the buffer.
//
// From tview/textview.go
var (
- boundaryPattern = regexp.MustCompile("([[:punct:]]\\s*|\\s+)")
+ boundaryPattern = regexp.MustCompile(`([[:punct:]]\s*|\s+)`)
+ bareBoundaryPattern = regexp.MustCompile(`(\s+)`)
spacePattern = regexp.MustCompile(`\s+`)
)
-func matchBoundaryPattern(extract tstring.TString) tstring.TString {
- matches := boundaryPattern.FindAllStringIndex(extract.String(), -1)
+func matchBoundaryPattern(bare bool, extract tstring.TString) tstring.TString {
+ regex := boundaryPattern
+ if bare {
+ regex = bareBoundaryPattern
+ }
+ matches := regex.FindAllStringIndex(extract.String(), -1)
if len(matches) > 0 {
if match := matches[len(matches)-1]; len(match) >= 2 {
if until := match[1]; until < len(extract) {
@@ -59,13 +50,20 @@ func matchBoundaryPattern(extract tstring.TString) tstring.TString {
// CalculateBuffer generates the internal buffer for this message that consists
// of the text of this message split into lines at most as wide as the width
// parameter.
-func (msg *BaseTextMessage) calculateBufferWithText(text tstring.TString, width int) {
+func (msg *BaseMessage) calculateBufferWithText(bare bool, text tstring.TString, width int) {
if width < 2 {
return
}
msg.buffer = []tstring.TString{}
+ if bare {
+ text = tstring.
+ NewTString(msg.FormatTime()).
+ AppendTString(tstring.NewColorTString(fmt.Sprintf(" <%s> ", msg.Sender()), msg.SenderColor())).
+ AppendTString(text)
+ }
+
forcedLinebreaks := text.Split('\n')
newlines := 0
for _, str := range forcedLinebreaks {
@@ -82,11 +80,12 @@ func (msg *BaseTextMessage) calculateBufferWithText(text tstring.TString, width
if spaces := spacePattern.FindStringIndex(str[len(extract):].String()); spaces != nil && spaces[0] == 0 {
extract = str[:len(extract)+spaces[1]]
}
- extract = matchBoundaryPattern(extract)
+ extract = matchBoundaryPattern(bare, extract)
}
msg.buffer = append(msg.buffer, extract)
str = str[len(extract):]
}
}
msg.prevBufferWidth = width
+ msg.prevBareMode = bare
}