From f1b0526977de0c8f9953093d756e41dbc32617f5 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Thu, 15 Mar 2018 21:38:43 +0200 Subject: [PATCH] Merge branch 'asdfasjkfdlas/alkdjf' into sdkjfls-final --- matrix.go | 11 ++++++++++- room-view.go | 15 +++++++++++---- view-main.go | 7 ++++--- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/matrix.go b/matrix.go index 9200649..a9b55b9 100644 --- a/matrix.go +++ b/matrix.go @@ -17,6 +17,7 @@ package main import ( + "encoding/json" "fmt" "time" @@ -149,7 +150,15 @@ func (c *MatrixContainer) Start() { func (c *MatrixContainer) HandleMessage(evt *gomatrix.Event) { message, _ := evt.Content["body"].(string) - c.ui.MainView().Append(evt.RoomID, evt.Sender, message) + + timestampNumber, _ := evt.Content["origin_server_ts"].(json.Number) + timestampInt64, _ := timestampNumber.Int64() + timestamp := time.Now() + if timestampInt64 != 0 { + timestamp = time.Unix(timestampInt64 / 1000, timestampInt64 % 1000 * 1000) + } + + c.ui.MainView().AddMessage(evt.RoomID, evt.Sender, message, timestamp) } func (c *MatrixContainer) HandleTyping(evt *gomatrix.Event) { diff --git a/room-view.go b/room-view.go index 597ed0c..b9b911e 100644 --- a/room-view.go +++ b/room-view.go @@ -22,6 +22,7 @@ import ( "regexp" "sort" "strings" + "time" "github.com/gdamore/tcell" "maunium.net/go/tview" @@ -60,6 +61,7 @@ func NewRoomView(topic string) *RoomView { SetText(strings.Replace(topic, "\n", " ", -1)). SetBackgroundColor(tcell.ColorDarkGreen) view.status.SetBackgroundColor(tcell.ColorDimGray) + view.userList.SetDynamicColors(true) view.content.SetDynamicColors(true) return view } @@ -108,15 +110,20 @@ func escapeColor(s string) string { return colorPattern.ReplaceAllString(s, "[$1[]") } -func (view *RoomView) AddMessage(sender, message string) { - fmt.Fprintf(view.content, "%s: %s\n", - color(sender), escapeColor(message)) +func (view *RoomView) AddMessage(sender, message string, timestamp time.Time) { + fmt.Fprintf(view.content, "[%s] %s: %s\n", + timestamp.Format("15:04:05"), color(sender), escapeColor(message)) } func (view *RoomView) SetUsers(users []string) { view.users = sort.StringSlice(users) view.users.Sort() - view.userList.SetText(strings.Join(view.users, "\n")) + var buf strings.Builder + for _, user := range view.users { + buf.WriteString(color(user)) + buf.WriteRune('\n') + } + view.userList.SetText(buf.String()) } func (view *RoomView) RemoveUser(user string) { diff --git a/view-main.go b/view-main.go index 21e209f..3c536c9 100644 --- a/view-main.go +++ b/view-main.go @@ -18,6 +18,7 @@ package main import ( "strings" + "time" "github.com/gdamore/tcell" "maunium.net/go/gomatrix" @@ -123,7 +124,7 @@ func (view *MainView) HandleCommand(room, command string, args []string) { view.matrix.client.LeaveRoom(room) case "/join": if len(args) == 0 { - view.Append(room, "*", "Usage: /join ") + view.AddMessage(room, "*", "Usage: /join ", time.Now()) break } mxid := args[0] @@ -206,10 +207,10 @@ func (view *MainView) SetTyping(room string, users []string) { } } -func (view *MainView) Append(room, sender, message string) { +func (view *MainView) AddMessage(room, sender, message string, timestamp time.Time) { roomView, ok := view.rooms[room] if ok { - roomView.AddMessage(sender, message) + roomView.AddMessage(sender, message, timestamp) view.parent.Render() } }