gomuks/view-main.go

253 lines
7.0 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/>.
package main
import (
"fmt"
"strings"
"github.com/gdamore/tcell"
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
)
type RoomView struct {
2018-03-15 17:21:14 +01:00
*tview.Box
topic *tview.TextView
content *tview.TextView
status *tview.TextView
userlist *tview.TextView
name string
2018-03-13 20:58:43 +01:00
}
2018-03-14 23:14:39 +01:00
func NewRoomView(name, topic string) *RoomView {
2018-03-13 20:58:43 +01:00
view := &RoomView{
2018-03-15 17:21:14 +01:00
Box: tview.NewBox(),
topic: tview.NewTextView(),
content: tview.NewTextView(),
status: tview.NewTextView(),
userlist: tview.NewTextView(),
name: name,
2018-03-13 20:58:43 +01:00
}
2018-03-15 17:21:14 +01:00
view.topic.SetText(topic).SetBackgroundColor(tcell.ColorDarkGreen)
view.status.SetBackgroundColor(tcell.ColorDimGray)
view.userlist.SetText("@tulir:maunium.net\n@tulir_test:maunium.net")
2018-03-13 20:58:43 +01:00
return view
}
2018-03-15 17:21:14 +01:00
func (view *RoomView) Draw(screen tcell.Screen) {
x, y, width, height := view.GetRect()
view.topic.SetRect(x, y, width, 1)
view.content.SetRect(x, y+1, width-30, height-2)
view.status.SetRect(x, y+height-1, width,1)
view.userlist.SetRect(x+width-29, y+1, 29, height - 2)
view.topic.Draw(screen)
view.content.Draw(screen)
view.status.Draw(screen)
borderX := x+width-30
background := tcell.StyleDefault.Background(view.GetBackgroundColor()).Foreground(view.GetBorderColor())
for borderY := y + 1; borderY < y + height - 1; borderY++ {
screen.SetContent(borderX, borderY, tview.GraphicsVertBar, nil, background)
}
view.userlist.Draw(screen)
}
type Border struct {
*tview.Box
}
func NewBorder() *Border {
return &Border{tview.NewBox()}
}
func (border *Border) Draw(screen tcell.Screen) {
background := tcell.StyleDefault.Background(border.GetBackgroundColor()).Foreground(border.GetBorderColor())
x, y, width, height := border.GetRect()
if width == 1 {
for borderY := y; borderY < y + height; borderY++ {
screen.SetContent(x, borderY, tview.GraphicsVertBar, nil, background)
}
} else if height == 1 {
for borderX := x; borderX < x + width; borderX++ {
screen.SetContent(borderX, y, tview.GraphicsHoriBar, nil, background)
}
}
}
2018-03-13 20:58:43 +01:00
func (ui *GomuksUI) MakeMainUI() tview.Primitive {
2018-03-15 17:21:14 +01:00
ui.mainView = tview.NewGrid()
ui.mainView.SetColumns(30, 1, 0).SetRows(0, 1)
2018-03-13 20:58:43 +01:00
ui.mainViewRoomList = tview.NewList().ShowSecondaryText(false)
2018-03-15 17:21:14 +01:00
ui.mainViewRoomList.SetBorderPadding(0, 0, 0, 1)
2018-03-14 23:14:39 +01:00
ui.mainView.AddItem(ui.mainViewRoomList, 0, 0, 2, 1, 0, 0, false)
2018-03-13 20:58:43 +01:00
2018-03-15 17:21:14 +01:00
ui.mainView.AddItem(NewBorder(), 0, 1, 2, 1, 0, 0, false)
2018-03-13 20:58:43 +01:00
ui.mainViewRoomView = tview.NewPages()
ui.mainViewRoomView.SetChangedFunc(ui.Render)
2018-03-15 17:21:14 +01:00
ui.mainView.AddItem(ui.mainViewRoomView, 0, 2, 1, 1, 0, 0, false)
2018-03-13 20:58:43 +01:00
ui.mainViewInput = tview.NewInputField()
2018-03-14 23:14:39 +01:00
ui.mainViewInput.SetChangedFunc(func(_ string) {
ui.matrix.SendTyping(ui.currentRoom())
})
2018-03-13 20:58:43 +01:00
ui.mainViewInput.SetDoneFunc(func(key tcell.Key) {
if key == tcell.KeyEnter {
room, text := ui.currentRoom(), ui.mainViewInput.GetText()
if len(text) == 0 {
return
} else if text[0] == '/' {
args := strings.SplitN(text, " ", 2)
command := strings.ToLower(args[0])
args = args[1:]
ui.HandleCommand(room, command, args)
} else {
ui.matrix.SendMessage(room, text)
}
ui.mainViewInput.SetText("")
}
})
2018-03-15 17:21:14 +01:00
ui.mainView.AddItem(ui.mainViewInput, 1, 2, 1, 1, 0, 0, true)
2018-03-13 20:58:43 +01:00
ui.debug.Print(ui.mainViewInput.SetInputCapture(ui.MainUIKeyHandler))
ui.mainViewRooms = make(map[string]*RoomView)
return ui.mainView
}
func (ui *GomuksUI) HandleCommand(room, command string, args []string) {
2018-03-14 23:14:39 +01:00
ui.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":
ui.gmx.Stop()
case "/clearcache":
ui.config.Session.Rooms = make(map[string]*gomatrix.Room)
ui.config.Session.NextBatch = ""
ui.config.Session.FilterID = ""
ui.config.Session.Save()
ui.gmx.Stop()
case "/part":
case "/leave":
2018-03-13 20:58:43 +01:00
ui.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 {
ui.Append(room, "*", "Usage: /join <room>")
}
mxid := args[0]
server := mxid[strings.Index(mxid, ":")+1:]
ui.matrix.client.JoinRoom(mxid, server, nil)
}
}
func (ui *GomuksUI) MainUIKeyHandler(key *tcell.EventKey) *tcell.EventKey {
if key.Modifiers() == tcell.ModCtrl {
if key.Key() == tcell.KeyDown {
ui.SwitchRoom(ui.currentRoomIndex + 1)
ui.mainViewRoomList.SetCurrentItem(ui.currentRoomIndex)
} else if key.Key() == tcell.KeyUp {
ui.SwitchRoom(ui.currentRoomIndex - 1)
ui.mainViewRoomList.SetCurrentItem(ui.currentRoomIndex)
2018-03-14 21:19:26 +01:00
} else {
return key
2018-03-13 20:58:43 +01:00
}
} else if key.Key() == tcell.KeyPgUp || key.Key() == tcell.KeyPgDn {
2018-03-14 21:19:26 +01:00
ui.mainViewRooms[ui.currentRoom()].InputHandler()(key, nil)
2018-03-13 20:58:43 +01:00
} else {
return key
}
return nil
}
func (ui *GomuksUI) SetRoomList(rooms []string) {
ui.roomList = rooms
ui.mainViewRoomList.Clear()
for index, room := range rooms {
localRoomIndex := index
2018-03-14 23:14:39 +01:00
ui.matrix.UpdateRoomInfo(room)
roomStore := ui.matrix.config.Session.LoadRoom(room)
name := room
topic := ""
if roomStore != nil {
nameEvt := roomStore.GetStateEvent("m.room.title", "")
if nameEvt != nil {
name, _ = nameEvt.Content["title"].(string)
} else {
nameEvt = roomStore.GetStateEvent("m.room.canonical_alias", "")
if nameEvt != nil {
name, _ = nameEvt.Content["alias"].(string)
}
}
topicEvt := roomStore.GetStateEvent("m.room.topic", "")
if topicEvt != nil {
topic, _ = topicEvt.Content["topic"].(string)
2018-03-15 17:21:14 +01:00
topic = strings.Replace(topic, "\n", " ", -1)
2018-03-14 23:14:39 +01:00
}
}
ui.mainViewRoomList.AddItem(name, "", 0, func() {
2018-03-13 20:58:43 +01:00
ui.SwitchRoom(localRoomIndex)
})
if !ui.mainViewRoomView.HasPage(room) {
2018-03-14 23:14:39 +01:00
roomView := NewRoomView(name, topic)
2018-03-13 20:58:43 +01:00
ui.mainViewRooms[room] = roomView
ui.mainViewRoomView.AddPage(room, roomView, true, false)
}
}
ui.SwitchRoom(0)
}
func (ui *GomuksUI) currentRoom() string {
if len(ui.roomList) == 0 {
return ""
}
return ui.roomList[ui.currentRoomIndex]
}
func (ui *GomuksUI) SwitchRoom(roomIndex int) {
if roomIndex < 0 {
roomIndex = len(ui.roomList) - 1
}
ui.currentRoomIndex = roomIndex % len(ui.roomList)
2018-03-15 17:21:14 +01:00
ui.mainViewRoomView.SwitchToPage(ui.currentRoom())
2018-03-13 20:58:43 +01:00
}
2018-03-15 17:21:14 +01:00
func (ui *GomuksUI) SetTyping(room string, users ...string) {
2018-03-14 23:14:39 +01:00
roomView, ok := ui.mainViewRooms[room]
if ok {
2018-03-15 17:21:14 +01:00
if len(users) > 0 {
roomView.status.SetText("Typing: " + strings.Join(users, ", "))
} else {
roomView.status.SetText("")
}
ui.Render()
2018-03-14 23:14:39 +01:00
}
}
2018-03-13 20:58:43 +01:00
func (ui *GomuksUI) Append(room, sender, message string) {
roomView, ok := ui.mainViewRooms[room]
if ok {
2018-03-14 23:14:39 +01:00
fmt.Fprintf(roomView.content, "<%s> %s\n", sender, message)
2018-03-13 20:58:43 +01:00
ui.Render()
}
}