diff options
author | Tulir Asokan <tulir@maunium.net> | 2018-03-22 19:51:20 +0200 |
---|---|---|
committer | Tulir Asokan <tulir@maunium.net> | 2018-03-22 19:54:31 +0200 |
commit | 702a75a8c0355737e3e62735b59fe30bee7e42f4 (patch) | |
tree | 538b2acd579eabf893fd1f63bc7093b65b620f28 /ui/widget | |
parent | 232f7fe1be917bf91f6342946f6d001948b8559e (diff) |
Save history to disk. Fixes #1
Diffstat (limited to 'ui/widget')
-rw-r--r-- | ui/widget/message-view.go | 53 | ||||
-rw-r--r-- | ui/widget/room-view.go | 13 |
2 files changed, 62 insertions, 4 deletions
diff --git a/ui/widget/message-view.go b/ui/widget/message-view.go index f263350..fe906b5 100644 --- a/ui/widget/message-view.go +++ b/ui/widget/message-view.go @@ -17,7 +17,9 @@ package widget import ( + "encoding/gob" "fmt" + "os" "time" "github.com/gdamore/tcell" @@ -79,6 +81,45 @@ func (view *MessageView) NewMessage(id, sender, text string, timestamp time.Time GetHashColor(sender)) } +func (view *MessageView) SaveHistory(path string) error { + file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0600) + if err != nil { + return err + } + defer file.Close() + + enc := gob.NewEncoder(file) + err = enc.Encode(view.messages) + if err != nil { + return err + } + + return nil +} + +func (view *MessageView) LoadHistory(path string) (int, error) { + file, err := os.OpenFile(path, os.O_RDONLY, 0600) + if err != nil { + if os.IsNotExist(err) { + return 0, nil + } + return -1, err + } + defer file.Close() + + dec := gob.NewDecoder(file) + err = dec.Decode(&view.messages) + if err != nil { + return -1, err + } + + for _, message := range view.messages { + view.updateWidestSender(message.Sender) + } + + return len(view.messages), nil +} + func (view *MessageView) updateWidestSender(sender string) { if len(sender) > view.widestSender { view.widestSender = len(sender) @@ -103,8 +144,12 @@ func (view *MessageView) UpdateMessageID(message *types.Message, newID string) { } func (view *MessageView) AddMessage(message *types.Message, direction MessageDirection) { + if message == nil { + return + } + msg, messageExists := view.messageIDs[message.ID] - if messageExists { + if msg != nil && messageExists { message.CopyTo(msg) direction = IgnoreMessage } @@ -117,7 +162,7 @@ func (view *MessageView) AddMessage(message *types.Message, direction MessageDir if direction == AppendMessage { if view.ScrollOffset > 0 { - view.ScrollOffset += len(message.Buffer) + view.ScrollOffset += message.Height() } view.messages = append(view.messages, message) view.appendBuffer(message) @@ -137,8 +182,8 @@ func (view *MessageView) appendBuffer(message *types.Message) { } } - view.textBuffer = append(view.textBuffer, message.Buffer...) - for range message.Buffer { + view.textBuffer = append(view.textBuffer, message.Buffer()...) + for range message.Buffer() { view.metaBuffer = append(view.metaBuffer, message) } view.prevMsgCount++ diff --git a/ui/widget/room-view.go b/ui/widget/room-view.go index 433d5dd..141e993 100644 --- a/ui/widget/room-view.go +++ b/ui/widget/room-view.go @@ -18,6 +18,7 @@ package widget import ( "fmt" + "path/filepath" "strings" "time" @@ -67,6 +68,18 @@ func NewRoomView(room *rooms.Room) *RoomView { return view } +func (view *RoomView) logPath(dir string) string { + return filepath.Join(dir, fmt.Sprintf("%s.gmxlog", view.Room.ID)) +} + +func (view *RoomView) SaveHistory(dir string) error { + return view.MessageView().SaveHistory(view.logPath(dir)) +} + +func (view *RoomView) LoadHistory(dir string) (int, error) { + return view.MessageView().LoadHistory(view.logPath(dir)) +} + func (view *RoomView) SetTabCompleteFunc(fn func(room *RoomView, text string, cursorOffset int) string) *RoomView { view.input.SetTabCompleteFunc(func(text string, cursorOffset int) string { return fn(view, text, cursorOffset) |