gomuks/ui/view-main.go

460 lines
12 KiB
Go
Raw Normal View History

2018-03-13 20:58:43 +01:00
// 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 <http://www.gnu.org/licenses/>.
2018-03-18 20:24:03 +01:00
package ui
2018-03-13 20:58:43 +01:00
import (
2018-03-17 13:32:01 +01:00
"fmt"
2018-03-17 15:09:53 +01:00
"sort"
"strconv"
2018-03-13 20:58:43 +01:00
"strings"
"time"
2018-03-17 13:32:01 +01:00
"unicode"
2018-03-13 20:58:43 +01:00
"github.com/gdamore/tcell"
2018-03-17 13:32:01 +01:00
"github.com/mattn/go-runewidth"
2018-03-15 17:25:16 +01:00
"maunium.net/go/gomatrix"
2018-03-18 20:24:03 +01:00
"maunium.net/go/gomuks/config"
"maunium.net/go/gomuks/interface"
"maunium.net/go/gomuks/ui/debug"
"maunium.net/go/gomuks/ui/types"
"maunium.net/go/gomuks/ui/widget"
2018-03-14 21:19:26 +01:00
"maunium.net/go/tview"
2018-03-13 20:58:43 +01:00
)
2018-03-15 19:53:04 +01:00
type MainView struct {
*tview.Grid
roomList *tview.List
roomView *tview.Pages
2018-03-18 20:24:03 +01:00
rooms map[string]*widget.RoomView
input *widget.AdvancedInputField
2018-03-15 19:53:04 +01:00
currentRoomIndex int
roomIDs []string
2018-03-18 20:24:03 +01:00
matrix ifc.MatrixContainer
gmx ifc.Gomuks
config *config.Config
2018-03-15 19:53:04 +01:00
parent *GomuksUI
}
func (view *MainView) addItem(p tview.Primitive, x, y, w, h int) {
view.Grid.AddItem(p, x, y, w, h, 0, 0, false)
}
2018-03-15 20:28:21 +01:00
func (ui *GomuksUI) NewMainView() tview.Primitive {
mainView := &MainView{
2018-03-15 19:53:04 +01:00
Grid: tview.NewGrid(),
roomList: tview.NewList(),
roomView: tview.NewPages(),
2018-03-18 20:24:03 +01:00
rooms: make(map[string]*widget.RoomView),
input: widget.NewAdvancedInputField(),
2018-03-15 19:53:04 +01:00
2018-03-18 20:24:03 +01:00
matrix: ui.gmx.MatrixContainer(),
2018-03-15 19:53:04 +01:00
gmx: ui.gmx,
2018-03-18 20:24:03 +01:00
config: ui.gmx.Config(),
2018-03-15 19:53:04 +01:00
parent: ui,
}
2018-03-15 20:28:21 +01:00
mainView.SetColumns(30, 1, 0).SetRows(0, 1)
2018-03-15 19:53:04 +01:00
2018-03-15 20:28:21 +01:00
mainView.roomList.
2018-03-15 19:53:04 +01:00
ShowSecondaryText(false).
SetSelectedBackgroundColor(tcell.ColorDarkGreen).
SetSelectedTextColor(tcell.ColorWhite).
2018-03-15 19:53:04 +01:00
SetBorderPadding(0, 0, 1, 0)
2018-03-13 20:58:43 +01:00
2018-03-15 20:28:21 +01:00
mainView.input.
SetDoneFunc(mainView.InputDone).
SetChangedFunc(mainView.InputChanged).
SetTabCompleteFunc(mainView.InputTabComplete).
2018-03-15 20:28:21 +01:00
SetFieldBackgroundColor(tcell.ColorDefault).
2018-03-17 11:47:57 +01:00
SetPlaceholder("Send a message...").
SetPlaceholderExtColor(tcell.ColorGray).
2018-03-15 20:28:21 +01:00
SetInputCapture(mainView.InputCapture)
2018-03-13 20:58:43 +01:00
2018-03-15 20:28:21 +01:00
mainView.addItem(mainView.roomList, 0, 0, 2, 1)
2018-03-18 20:24:03 +01:00
mainView.addItem(widget.NewBorder(), 0, 1, 2, 1)
2018-03-15 20:28:21 +01:00
mainView.addItem(mainView.roomView, 0, 2, 1, 1)
mainView.AddItem(mainView.input, 1, 2, 1, 1, 0, 0, true)
2018-03-13 20:58:43 +01:00
2018-03-15 20:28:21 +01:00
ui.mainView = mainView
return mainView
2018-03-13 20:58:43 +01:00
}
2018-03-15 19:53:04 +01:00
func (view *MainView) InputChanged(text string) {
2018-03-15 20:28:21 +01:00
if len(text) == 0 {
2018-03-16 15:24:11 +01:00
go view.matrix.SendTyping(view.CurrentRoomID(), false)
2018-03-15 20:28:21 +01:00
} else if text[0] != '/' {
2018-03-16 15:24:11 +01:00
go view.matrix.SendTyping(view.CurrentRoomID(), true)
2018-03-15 20:28:21 +01:00
}
2018-03-15 19:53:04 +01:00
}
2018-03-17 13:32:01 +01:00
func findWordToTabComplete(text string) string {
output := ""
runes := []rune(text)
for i := len(runes) - 1; i >= 0; i-- {
if unicode.IsSpace(runes[i]) {
break
}
output = string(runes[i]) + output
}
return output
}
func (view *MainView) InputTabComplete(text string, cursorOffset int) string {
roomView, _ := view.rooms[view.CurrentRoomID()]
if roomView != nil {
2018-03-17 13:32:01 +01:00
str := runewidth.Truncate(text, cursorOffset, "")
word := findWordToTabComplete(str)
userCompletions := roomView.AutocompleteUser(word)
if len(userCompletions) == 1 {
startIndex := len(str) - len(word)
completion := userCompletions[0]
if startIndex == 0 {
completion = completion + ": "
}
text = str[0:startIndex] + completion + text[len(str):]
2018-03-17 13:32:01 +01:00
} else if len(userCompletions) > 1 && len(userCompletions) < 6 {
2018-03-18 20:24:03 +01:00
roomView.SetStatus(fmt.Sprintf("Completions: %s", strings.Join(userCompletions, ", ")))
2018-03-17 13:32:01 +01:00
}
}
2018-03-17 13:32:01 +01:00
return text
}
2018-03-15 19:53:04 +01:00
func (view *MainView) InputDone(key tcell.Key) {
if key != tcell.KeyEnter {
return
}
room, text := view.CurrentRoomID(), view.input.GetText()
if len(text) == 0 {
return
} else if text[0] == '/' {
args := strings.SplitN(text, " ", 2)
command := strings.ToLower(args[0])
args = args[1:]
go view.HandleCommand(room, command, args)
} else {
view.SendMessage(room, text)
}
view.input.SetText("")
}
func (view *MainView) SendMessage(room, text string) {
now := time.Now()
roomView := view.GetRoom(room)
tempMessage := roomView.NewMessage(
strconv.FormatInt(now.UnixNano(), 10),
"Sending...", text, now)
tempMessage.TimestampColor = tcell.ColorGray
tempMessage.TextColor = tcell.ColorGray
tempMessage.SenderColor = tcell.ColorGray
roomView.AddMessage(tempMessage, widget.AppendMessage)
go func() {
defer view.gmx.Recover()
eventID, err := view.matrix.SendMessage(room, text)
if err != nil {
tempMessage.TextColor = tcell.ColorRed
tempMessage.TimestampColor = tcell.ColorRed
tempMessage.SenderColor = tcell.ColorRed
tempMessage.Sender = "Error"
roomView.SetStatus(fmt.Sprintf("Failed to send message: %s", err))
2018-03-15 19:53:04 +01:00
} else {
roomView.MessageView().UpdateMessageID(tempMessage, eventID)
2018-03-15 19:53:04 +01:00
}
}()
2018-03-15 19:53:04 +01:00
}
func (view *MainView) HandleCommand(room, command string, args []string) {
defer view.gmx.Recover()
2018-03-18 20:24:03 +01:00
debug.Print("Handling command", command, args)
2018-03-13 20:58:43 +01:00
switch command {
2018-03-14 23:14:39 +01:00
case "/quit":
2018-03-15 19:53:04 +01:00
view.gmx.Stop()
2018-03-14 23:14:39 +01:00
case "/clearcache":
view.config.Session.Clear()
2018-03-15 19:53:04 +01:00
view.gmx.Stop()
case "/panic":
panic("This is a test panic.")
2018-03-14 23:14:39 +01:00
case "/part":
2018-03-16 15:24:11 +01:00
fallthrough
2018-03-14 23:14:39 +01:00
case "/leave":
2018-03-18 20:24:03 +01:00
debug.Print(view.matrix.LeaveRoom(room))
2018-03-14 23:14:39 +01:00
case "/join":
2018-03-13 20:58:43 +01:00
if len(args) == 0 {
view.AddServiceMessage(room, "Usage: /join <room>")
2018-03-15 19:53:04 +01:00
break
2018-03-13 20:58:43 +01:00
}
2018-03-18 20:24:03 +01:00
debug.Print(view.matrix.JoinRoom(args[0]))
2018-03-16 15:24:11 +01:00
default:
view.AddServiceMessage(room, "Unknown command.")
2018-03-13 20:58:43 +01:00
}
}
2018-03-15 19:53:04 +01:00
func (view *MainView) InputCapture(key *tcell.EventKey) *tcell.EventKey {
2018-03-17 00:27:30 +01:00
k := key.Key()
2018-03-20 11:16:32 +01:00
if key.Modifiers() == tcell.ModCtrl || key.Modifiers() == tcell.ModAlt {
2018-03-17 00:27:30 +01:00
if k == tcell.KeyDown {
2018-03-15 19:53:04 +01:00
view.SwitchRoom(view.currentRoomIndex + 1)
view.roomList.SetCurrentItem(view.currentRoomIndex)
2018-03-17 00:27:30 +01:00
} else if k == tcell.KeyUp {
2018-03-15 19:53:04 +01:00
view.SwitchRoom(view.currentRoomIndex - 1)
view.roomList.SetCurrentItem(view.currentRoomIndex)
2018-03-14 21:19:26 +01:00
} else {
return key
2018-03-13 20:58:43 +01:00
}
2018-03-20 11:16:32 +01:00
} else if k == tcell.KeyPgUp || k == tcell.KeyPgDn || k == tcell.KeyUp || k == tcell.KeyDown {
2018-03-17 00:27:30 +01:00
msgView := view.rooms[view.CurrentRoomID()].MessageView()
2018-03-20 11:16:32 +01:00
if k == tcell.KeyPgUp || k == tcell.KeyUp {
if msgView.IsAtTop() {
go view.LoadMoreHistory(view.CurrentRoomID())
} else {
msgView.MoveUp(k == tcell.KeyPgUp)
}
2018-03-17 00:27:30 +01:00
} else {
2018-03-20 11:16:32 +01:00
msgView.MoveDown(k == tcell.KeyPgDn)
2018-03-17 00:27:30 +01:00
}
2018-03-20 11:16:32 +01:00
view.parent.Render()
2018-03-13 20:58:43 +01:00
} else {
return key
}
return nil
}
2018-03-15 19:53:04 +01:00
func (view *MainView) CurrentRoomID() string {
if len(view.roomIDs) == 0 {
return ""
}
return view.roomIDs[view.currentRoomIndex]
}
func (view *MainView) SwitchRoom(roomIndex int) {
if roomIndex < 0 {
roomIndex = len(view.roomIDs) - 1
}
2018-03-19 14:19:44 +01:00
if len(view.roomIDs) == 0 {
return
}
2018-03-15 19:53:04 +01:00
view.currentRoomIndex = roomIndex % len(view.roomIDs)
view.roomView.SwitchToPage(view.CurrentRoomID())
2018-03-16 15:24:11 +01:00
view.roomList.SetCurrentItem(roomIndex)
2018-03-15 19:53:04 +01:00
view.parent.Render()
}
2018-03-16 15:24:11 +01:00
func (view *MainView) addRoom(index int, room string) {
roomStore := view.matrix.GetRoom(room)
view.roomList.AddItem(roomStore.GetTitle(), "", 0, func() {
view.SwitchRoom(index)
})
if !view.roomView.HasPage(room) {
2018-03-20 11:16:32 +01:00
roomView := widget.NewRoomView(roomStore)
2018-03-16 15:24:11 +01:00
view.rooms[room] = roomView
view.roomView.AddPage(room, roomView, true, false)
roomView.UpdateUserList()
2018-03-20 11:16:32 +01:00
go view.LoadInitialHistory(room)
2018-03-16 15:24:11 +01:00
}
}
2018-03-18 20:24:03 +01:00
func (view *MainView) GetRoom(id string) *widget.RoomView {
return view.rooms[id]
}
2018-03-16 15:24:11 +01:00
func (view *MainView) HasRoom(room string) bool {
for _, existingRoom := range view.roomIDs {
if existingRoom == room {
return true
}
}
return false
}
func (view *MainView) AddRoom(room string) {
if view.HasRoom(room) {
return
}
view.roomIDs = append(view.roomIDs, room)
view.addRoom(len(view.roomIDs)-1, room)
2018-03-16 15:24:11 +01:00
}
func (view *MainView) RemoveRoom(room string) {
if !view.HasRoom(room) {
return
}
2018-03-17 15:09:53 +01:00
removeIndex := 0
2018-03-16 15:24:11 +01:00
if view.CurrentRoomID() == room {
2018-03-17 15:09:53 +01:00
removeIndex = view.currentRoomIndex
2018-03-16 15:24:11 +01:00
view.SwitchRoom(view.currentRoomIndex - 1)
2018-03-17 15:09:53 +01:00
} else {
removeIndex = sort.StringSlice(view.roomIDs).Search(room)
2018-03-16 15:24:11 +01:00
}
2018-03-17 15:09:53 +01:00
view.roomList.RemoveItem(removeIndex)
view.roomIDs = append(view.roomIDs[:removeIndex], view.roomIDs[removeIndex+1:]...)
2018-03-16 15:24:11 +01:00
view.roomView.RemovePage(room)
delete(view.rooms, room)
2018-03-20 11:16:32 +01:00
view.parent.Render()
2018-03-16 15:24:11 +01:00
}
2018-03-18 20:24:03 +01:00
func (view *MainView) SetRooms(rooms []string) {
2018-03-15 19:53:04 +01:00
view.roomIDs = rooms
view.roomList.Clear()
2018-03-16 15:24:11 +01:00
view.roomView.Clear()
2018-03-18 20:24:03 +01:00
view.rooms = make(map[string]*widget.RoomView)
2018-03-13 20:58:43 +01:00
for index, room := range rooms {
2018-03-16 15:24:11 +01:00
view.addRoom(index, room)
2018-03-13 20:58:43 +01:00
}
2018-03-15 19:53:04 +01:00
view.SwitchRoom(0)
2018-03-13 20:58:43 +01:00
}
2018-03-15 19:53:04 +01:00
func (view *MainView) SetTyping(room string, users []string) {
roomView, ok := view.rooms[room]
2018-03-14 23:14:39 +01:00
if ok {
2018-03-15 18:45:52 +01:00
roomView.SetTyping(users)
2018-03-15 19:53:04 +01:00
view.parent.Render()
2018-03-14 23:14:39 +01:00
}
}
func (view *MainView) AddServiceMessage(room, message string) {
2018-03-15 19:53:04 +01:00
roomView, ok := view.rooms[room]
2018-03-13 20:58:43 +01:00
if ok {
2018-03-20 11:16:32 +01:00
message := roomView.NewMessage("", "*", message, time.Now())
roomView.AddMessage(message, widget.AppendMessage)
2018-03-15 19:53:04 +01:00
view.parent.Render()
2018-03-13 20:58:43 +01:00
}
}
2018-03-20 11:16:32 +01:00
func (view *MainView) LoadMoreHistory(room string) {
view.UpdateLogs(room, false)
}
2018-03-20 11:16:32 +01:00
func (view *MainView) LoadInitialHistory(room string) {
view.UpdateLogs(room, true)
}
func (view *MainView) UpdateLogs(room string, initial bool) {
defer view.gmx.Recover()
roomView := view.rooms[room]
2018-03-20 11:16:32 +01:00
batch := roomView.Room.PrevBatch
lockTime := time.Now().Unix() + 1
roomView.FetchHistoryLock.Lock()
roomView.MessageView().LoadingMessages = true
defer func() {
roomView.FetchHistoryLock.Unlock()
roomView.MessageView().LoadingMessages = false
}()
// There's no clean way to try to lock a mutex, so we just check if we still
// want to continue after we get the lock. This function should always be ran
// in a goroutine, so the blocking doesn't matter.
if time.Now().Unix() >= lockTime || batch != roomView.Room.PrevBatch {
return
}
if initial {
batch = view.config.Session.NextBatch
}
debug.Print("Loading history for", room, "starting from", batch, "(initial:", initial, ")")
history, prevBatch, err := view.matrix.GetHistory(roomView.Room.ID, batch, 50)
if err != nil {
2018-03-20 11:16:32 +01:00
view.AddServiceMessage(room, "Failed to fetch history")
2018-03-18 20:24:03 +01:00
debug.Print("Failed to fetch history for", roomView.Room.ID, err)
return
}
2018-03-20 11:16:32 +01:00
roomView.Room.PrevBatch = prevBatch
for _, evt := range history {
2018-03-18 20:24:03 +01:00
var room *widget.RoomView
var message *types.Message
if evt.Type == "m.room.message" {
room, message = view.ProcessMessageEvent(&evt)
} else if evt.Type == "m.room.member" {
room, message = view.ProcessMembershipEvent(&evt, false)
}
if room != nil && message != nil {
2018-03-18 20:24:03 +01:00
room.AddMessage(message, widget.PrependMessage)
}
}
2018-03-20 11:16:32 +01:00
view.parent.Render()
}
2018-03-18 20:24:03 +01:00
func (view *MainView) ProcessMessageEvent(evt *gomatrix.Event) (room *widget.RoomView, message *types.Message) {
room = view.GetRoom(evt.RoomID)
if room != nil {
2018-03-18 20:24:03 +01:00
text, _ := evt.Content["body"].(string)
message = room.NewMessage(evt.ID, evt.Sender, text, unixToTime(evt.Timestamp))
}
return
}
func (view *MainView) processOwnMembershipChange(evt *gomatrix.Event) {
membership, _ := evt.Content["membership"].(string)
prevMembership := "leave"
if evt.Unsigned.PrevContent != nil {
prevMembership, _ = evt.Unsigned.PrevContent["membership"].(string)
}
if membership == prevMembership {
return
}
if membership == "join" {
view.AddRoom(evt.RoomID)
} else if membership == "leave" {
view.RemoveRoom(evt.RoomID)
}
}
2018-03-18 20:24:03 +01:00
func (view *MainView) ProcessMembershipEvent(evt *gomatrix.Event, new bool) (room *widget.RoomView, message *types.Message) {
if new && evt.StateKey != nil && *evt.StateKey == view.config.Session.MXID {
view.processOwnMembershipChange(evt)
}
room = view.GetRoom(evt.RoomID)
if room != nil {
membership, _ := evt.Content["membership"].(string)
var sender, text string
if membership == "invite" {
sender = "---"
text = fmt.Sprintf("%s invited %s.", evt.Sender, *evt.StateKey)
} else if membership == "join" {
sender = "-->"
text = fmt.Sprintf("%s joined the room.", *evt.StateKey)
} else if membership == "leave" {
sender = "<--"
if evt.Sender != *evt.StateKey {
reason, _ := evt.Content["reason"].(string)
text = fmt.Sprintf("%s kicked %s: %s", evt.Sender, *evt.StateKey, reason)
} else {
text = fmt.Sprintf("%s left the room.", *evt.StateKey)
}
} else {
room = nil
return
}
message = room.NewMessage(evt.ID, sender, text, unixToTime(evt.Timestamp))
message.TextColor = tcell.ColorGreen
}
return
}
func unixToTime(unix int64) time.Time {
timestamp := time.Now()
if unix != 0 {
timestamp = time.Unix(unix/1000, unix%1000*1000)
}
return timestamp
}