From 0ef06428ed87c91fbf3f5478fce79cd7a2e90901 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Sun, 10 Jun 2018 14:26:48 -0700 Subject: [PATCH] Adding a tui app that allows chat for an imported group --- app/tui/main.go | 89 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 app/tui/main.go diff --git a/app/tui/main.go b/app/tui/main.go new file mode 100644 index 0000000..a395f22 --- /dev/null +++ b/app/tui/main.go @@ -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) + } +}