Adding a tui app that allows chat for an imported group

This commit is contained in:
Sarah Jamie Lewis 2018-06-10 14:26:48 -07:00
parent ce33f23a2e
commit 0ef06428ed
1 changed files with 89 additions and 0 deletions

89
app/tui/main.go Normal file
View File

@ -0,0 +1,89 @@
package main
import (
"fmt"
"log"
"time"
"cwtch.im/cwtch/peer"
"github.com/marcusolsson/tui-go"
"io/ioutil"
"os"
"strings"
)
func main() {
log.SetOutput(ioutil.Discard)
peer := peer.NewCwtchPeer("cwtchbot")
groupID, err := peer.ImportGroup(os.Args[1])
peer.AcceptInvite(groupID)
group := peer.Profile.GetGroupByGroupID(groupID)
peer.JoinServer(group.GroupServer)
history := tui.NewVBox()
historyScroll := tui.NewScrollArea(history)
historyScroll.SetAutoscrollToBottom(true)
historyBox := tui.NewVBox(historyScroll)
historyBox.SetBorder(true)
historyBox.SetTitle("cwtch chat:" + group.GroupID + "@" + group.GroupServer)
input := tui.NewEntry()
input.SetFocused(true)
input.SetSizePolicy(tui.Expanding, tui.Maximum)
inputBox := tui.NewHBox(input)
inputBox.SetBorder(true)
inputBox.SetSizePolicy(tui.Expanding, tui.Maximum)
chat := tui.NewVBox(historyBox, inputBox)
chat.SetSizePolicy(tui.Expanding, tui.Expanding)
input.OnSubmit(func(e *tui.Entry) {
if strings.Trim(e.Text(), " ") != "" {
go peer.SendMessageToGroup(groupID, e.Text())
e.SetText("")
}
})
seen := make(map[string]bool)
root := tui.NewHBox(chat)
ui, err := tui.New(root)
if err != nil {
log.Fatal(err)
}
ui.SetKeybinding("Esc", func() { ui.Quit() })
go func() {
for {
group = peer.Profile.GetGroupByGroupID(groupID)
if group == nil {
log.Fatalf("group does not exist")
}
timeline := group.GetTimeline()
for _, m := range timeline {
old, _ := seen[string(m.Signature)]
if !old {
ui.Update(func() {
history.Append(tui.NewHBox(
tui.NewLabel(m.Timestamp.Format("15:04")),
tui.NewPadder(1, 0, tui.NewLabel(fmt.Sprintf("<%s>", m.PeerID))),
tui.NewLabel(m.Message),
tui.NewSpacer(),
))
})
seen[string(m.Signature)] = true
}
}
time.Sleep(time.Second * 5)
}
}()
if err := ui.Run(); err != nil {
log.Fatal(err)
}
}