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/>.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-03-17 13:32:01 +01:00
|
|
|
"fmt"
|
2018-03-17 15:09:53 +01:00
|
|
|
"sort"
|
2018-03-13 20:58:43 +01:00
|
|
|
"strings"
|
2018-03-15 20:38:43 +01:00
|
|
|
"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-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
|
|
|
|
rooms map[string]*RoomView
|
2018-03-17 11:32:00 +01:00
|
|
|
input *AdvancedInputField
|
2018-03-15 19:53:04 +01:00
|
|
|
currentRoomIndex int
|
|
|
|
roomIDs []string
|
|
|
|
|
|
|
|
matrix *MatrixContainer
|
|
|
|
debug DebugPrinter
|
|
|
|
gmx Gomuks
|
|
|
|
config *Config
|
|
|
|
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(),
|
|
|
|
rooms: make(map[string]*RoomView),
|
2018-03-17 11:32:00 +01:00
|
|
|
input: NewAdvancedInputField(),
|
2018-03-15 19:53:04 +01:00
|
|
|
|
|
|
|
matrix: ui.matrix,
|
|
|
|
debug: ui.debug,
|
|
|
|
gmx: ui.gmx,
|
|
|
|
config: ui.config,
|
|
|
|
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).
|
2018-03-17 13:00:02 +01:00
|
|
|
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).
|
2018-03-17 13:00:02 +01:00
|
|
|
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)
|
|
|
|
mainView.addItem(NewBorder(), 0, 1, 2, 1)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-03-17 14:48:31 +01:00
|
|
|
func (view *MainView) GetRoom(id string) *RoomView {
|
|
|
|
return view.rooms[id]
|
2018-03-17 13:32:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (view *MainView) InputTabComplete(text string, cursorOffset int) string {
|
2018-03-17 13:00:02 +01:00
|
|
|
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 {
|
|
|
|
text = str[0:len(str)-len(word)] + userCompletions[0] + text[len(str):]
|
|
|
|
} else if len(userCompletions) > 1 && len(userCompletions) < 6 {
|
|
|
|
roomView.status.Clear()
|
|
|
|
fmt.Fprintf(roomView.status, "Completions: %s", strings.Join(userCompletions, ", "))
|
|
|
|
}
|
2018-03-17 13:00:02 +01:00
|
|
|
}
|
2018-03-17 13:32:01 +01:00
|
|
|
return text
|
2018-03-17 13:00:02 +01:00
|
|
|
}
|
|
|
|
|
2018-03-15 19:53:04 +01:00
|
|
|
func (view *MainView) InputDone(key tcell.Key) {
|
|
|
|
if key == tcell.KeyEnter {
|
|
|
|
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:]
|
2018-03-16 15:24:11 +01:00
|
|
|
go view.HandleCommand(room, command, args)
|
2018-03-15 19:53:04 +01:00
|
|
|
} else {
|
2018-03-16 15:24:11 +01:00
|
|
|
go view.matrix.SendMessage(room, text)
|
2018-03-15 19:53:04 +01:00
|
|
|
}
|
|
|
|
view.input.SetText("")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (view *MainView) HandleCommand(room, command string, args []string) {
|
2018-03-16 15:24:11 +01:00
|
|
|
view.gmx.Recover()
|
2018-03-15 19:53:04 +01:00
|
|
|
view.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":
|
2018-03-15 19:53:04 +01:00
|
|
|
view.config.Session.Rooms = make(map[string]*gomatrix.Room)
|
|
|
|
view.config.Session.NextBatch = ""
|
|
|
|
view.config.Session.FilterID = ""
|
|
|
|
view.config.Session.Save()
|
|
|
|
view.gmx.Stop()
|
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-15 19:53:04 +01:00
|
|
|
view.matrix.client.LeaveRoom(room)
|
2018-03-14 23:14:39 +01:00
|
|
|
case "/join":
|
2018-03-13 20:58:43 +01:00
|
|
|
if len(args) == 0 {
|
2018-03-17 13:00:02 +01:00
|
|
|
view.AddMessage(room, "Usage: /join <room>")
|
2018-03-15 19:53:04 +01:00
|
|
|
break
|
2018-03-13 20:58:43 +01:00
|
|
|
}
|
2018-03-16 15:24:11 +01:00
|
|
|
view.debug.Print(view.matrix.JoinRoom(args[0]))
|
|
|
|
default:
|
2018-03-17 00:27:30 +01:00
|
|
|
view.AddMessage(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-13 20:58:43 +01:00
|
|
|
if key.Modifiers() == tcell.ModCtrl {
|
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-17 00:27:30 +01:00
|
|
|
} else if k == tcell.KeyPgUp || k == tcell.KeyPgDn {
|
|
|
|
msgView := view.rooms[view.CurrentRoomID()].MessageView()
|
|
|
|
if k == tcell.KeyPgUp {
|
|
|
|
msgView.PageUp()
|
|
|
|
} else {
|
|
|
|
msgView.PageDown()
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
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-17 14:48:31 +01:00
|
|
|
roomView := NewRoomView(view, roomStore)
|
2018-03-16 15:24:11 +01:00
|
|
|
view.rooms[room] = roomView
|
|
|
|
view.roomView.AddPage(room, roomView, true, false)
|
|
|
|
roomView.UpdateUserList()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2018-03-17 13:00:02 +01:00
|
|
|
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)
|
2018-03-17 15:09:53 +01:00
|
|
|
view.Render()
|
2018-03-16 15:24:11 +01:00
|
|
|
}
|
|
|
|
|
2018-03-15 19:53:04 +01:00
|
|
|
func (view *MainView) SetRoomList(rooms []string) {
|
|
|
|
view.roomIDs = rooms
|
|
|
|
view.roomList.Clear()
|
2018-03-16 15:24:11 +01:00
|
|
|
view.roomView.Clear()
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-17 00:27:30 +01:00
|
|
|
func (view *MainView) AddMessage(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-17 14:48:31 +01:00
|
|
|
roomView.content.AddMessage("", "*", message, time.Now())
|
2018-03-15 19:53:04 +01:00
|
|
|
view.parent.Render()
|
2018-03-13 20:58:43 +01:00
|
|
|
}
|
|
|
|
}
|
2018-03-17 14:48:31 +01:00
|
|
|
|
|
|
|
func (view *MainView) Render() {
|
|
|
|
view.parent.Render()
|
|
|
|
}
|