aboutsummaryrefslogtreecommitdiff
path: root/room-view.go
diff options
context:
space:
mode:
authorTulir Asokan <tulir@maunium.net>2018-03-17 01:27:30 +0200
committerTulir Asokan <tulir@maunium.net>2018-03-17 01:27:30 +0200
commit85fd5f8d55e2ece0602c89159cac5665e21373e5 (patch)
tree65436fac24b47affbe1897169bc0945899504a46 /room-view.go
parent7be1d708d32e070cfc4fa9dcc4a46dd7fd091d5e (diff)
Fix bugs and add MessageView widget
Diffstat (limited to 'room-view.go')
-rw-r--r--room-view.go40
1 files changed, 21 insertions, 19 deletions
diff --git a/room-view.go b/room-view.go
index d5b9ca2..6764e85 100644
--- a/room-view.go
+++ b/room-view.go
@@ -19,9 +19,8 @@ package main
import (
"fmt"
"hash/fnv"
- "regexp"
+ "sort"
"strings"
- "time"
"github.com/gdamore/tcell"
"maunium.net/go/gomatrix"
@@ -32,10 +31,12 @@ type RoomView struct {
*tview.Box
topic *tview.TextView
- content *tview.TextView
+ content *MessageView
status *tview.TextView
userList *tview.TextView
room *gomatrix.Room
+
+ debug DebugPrinter
}
var colorNames []string
@@ -47,27 +48,30 @@ func init() {
colorNames[i] = name
i++
}
+ sort.Sort(sort.StringSlice(colorNames))
}
-func NewRoomView(room *gomatrix.Room) *RoomView {
+func NewRoomView(debug DebugPrinter, room *gomatrix.Room) *RoomView {
view := &RoomView{
Box: tview.NewBox(),
topic: tview.NewTextView(),
- content: tview.NewTextView(),
+ content: NewMessageView(debug),
status: tview.NewTextView(),
userList: tview.NewTextView(),
room: room,
+ debug: debug,
}
view.topic.
SetText(strings.Replace(room.GetTopic(), "\n", " ", -1)).
SetBackgroundColor(tcell.ColorDarkGreen)
view.status.SetBackgroundColor(tcell.ColorDimGray)
view.userList.SetDynamicColors(true)
- view.content.SetDynamicColors(true)
return view
}
func (view *RoomView) Draw(screen tcell.Screen) {
+ view.Box.Draw(screen)
+
x, y, width, height := view.GetRect()
view.topic.SetRect(x, y, width, 1)
view.content.SetRect(x, y+1, width-30, height-2)
@@ -104,26 +108,24 @@ func (view *RoomView) SetTyping(users []string) {
}
}
-var colorPattern = regexp.MustCompile(`\[([a-zA-Z]+|#[0-9a-zA-Z]{6})\]`)
+func (view *RoomView) MessageView() *MessageView {
+ return view.content
+}
-func color(s string) string {
+func getColorName(s string) string {
h := fnv.New32a()
h.Write([]byte(s))
- color := colorNames[int(h.Sum32())%len(colorNames)]
- return fmt.Sprintf("[%s]%s[white]", color, s)
+ return colorNames[int(h.Sum32())%len(colorNames)]
}
-func escapeColor(s string) string {
- return colorPattern.ReplaceAllString(s, "[$1[]")
+func getColor(s string) tcell.Color {
+ h := fnv.New32a()
+ h.Write([]byte(s))
+ return tcell.ColorNames[colorNames[int(h.Sum32())%len(colorNames)]]
}
-func (view *RoomView) AddMessage(sender, message string, timestamp time.Time) {
- member := view.room.GetMember(sender)
- if member != nil {
- sender = member.DisplayName
- }
- fmt.Fprintf(view.content, "[%s] %s: %s\n",
- timestamp.Format("15:04:05"), color(sender), escapeColor(message))
+func color(s string) string {
+ return fmt.Sprintf("[%s]%s[white]", getColorName(s), s)
}
func (view *RoomView) UpdateUserList() {