Browse Source

Wrapping and chatting but no reading

pull/1/head
Gregory 'Rudi' Rudolph 5 years ago
parent
commit
4c18c64bc1
  1. 95
      main.go

95
main.go

@ -4,10 +4,15 @@ import (
"fmt" "fmt"
"github.com/jroimartin/gocui" "github.com/jroimartin/gocui"
"log" "log"
"strconv" "samhofi.us/x/keybase"
"strings"
"time" "time"
) )
var k = keybase.NewKeybase()
var teamOrUser = "home"
var channel = ""
func main() { func main() {
kbtui, err := gocui.NewGui(gocui.OutputNormal) kbtui, err := gocui.NewGui(gocui.OutputNormal)
if err != nil { if err != nil {
@ -16,25 +21,46 @@ func main() {
defer kbtui.Close() defer kbtui.Close()
kbtui.SetManagerFunc(layout) kbtui.SetManagerFunc(layout)
go loginGreeter(kbtui)
go populateList(kbtui)
if err := initKeybindings(kbtui); err != nil { if err := initKeybindings(kbtui); err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
go testAsync(kbtui)
if err := kbtui.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
log.Panicln(err)
}
if err := kbtui.MainLoop(); err != nil && err != gocui.ErrQuit { if err := kbtui.MainLoop(); err != nil && err != gocui.ErrQuit {
log.Panicln(err) log.Panicln(err)
} }
} }
func loginGreeter(g *gocui.Gui) {
messg := "Welcome " + k.Username + "!"
printToView(g, "Chat", messg)
}
func sendToUser(msg string) {
chat := k.NewChat(keybase.Channel{Name:teamOrUser})
chat.Send(msg)
}
func testAsync(kbtui *gocui.Gui) { func sendToTeam(msg string) {
for i := 0; i < 50; i++ { chat := k.NewChat(keybase.Channel{
printToView(kbtui, "Chat", "Message #" + strconv.Itoa(i) + "\n") Name: teamOrUser,
time.Sleep(1 * time.Second) MembersType: "team",
TopicName: channel,
})
chat.Send(msg)
}
func populateList(g *gocui.Gui) {
for {
if testVar, err := k.ChatList(); err != nil {
log.Fatalln(err)
} else {
clearView(g, "List")
for _, s := range testVar.Result.Conversations {
printToView(g, "List", s.Channel.Name + "\n")
}
} }
clearView(kbtui, "Chat") time.Sleep(5 * time.Second)
}
} }
func clearView(kbtui *gocui.Gui, viewName string) { func clearView(kbtui *gocui.Gui, viewName string) {
@ -70,14 +96,20 @@ func layout(g *gocui.Gui) error {
return err return err
} }
feedView.Autoscroll = true feedView.Autoscroll = true
fmt.Fprintln(feedView, "Feed Window") feedView.Wrap = true
fmt.Fprintln(feedView, "Feed Window - If you are mentioned or receive a PM it will show here")
} }
if chatView, err2 := g.SetView("Chat", 12, maxY/5+1, maxX-1, maxY-5); err2 != nil { if chatView, err2 := g.SetView("Chat", 12, maxY/5+1, maxX-1, maxY-5); err2 != nil {
if err2 != gocui.ErrUnknownView { if err2 != gocui.ErrUnknownView {
return err2 return err2
} }
chatView.Autoscroll = true chatView.Autoscroll = true
fmt.Fprintln(chatView, "Chat Window") chatView.Wrap = true
fmt.Fprintf(chatView, "Your chats will appear here.\nSupported commands are as follows:\n")
fmt.Fprintln(chatView, "/j $username - Open your chat with $username")
fmt.Fprintln(chatView, "/j $team $channel - Open $channel from $team")
fmt.Fprintln(chatView, " Please note: small teams only have #general")
fmt.Fprintln(chatView, "/q - Exit")
} }
if inputView, err3 := g.SetView("Input", 12, maxY-4, maxX-1, maxY-1); err3 != nil { if inputView, err3 := g.SetView("Input", 12, maxY-4, maxX-1, maxY-1); err3 != nil {
if err3 != gocui.ErrUnknownView { if err3 != gocui.ErrUnknownView {
@ -87,12 +119,14 @@ func layout(g *gocui.Gui) error {
return err return err
} }
inputView.Editable = true inputView.Editable = true
inputView.Wrap = true
} }
if listView, err4 := g.SetView("List", 0, 0, 10, maxY-1); err4 != nil { if listView, err4 := g.SetView("List", 0, 0, 10, maxY-1); err4 != nil {
if err4 != gocui.ErrUnknownView { if err4 != gocui.ErrUnknownView {
return err4 return err4
} }
fmt.Fprintf(listView, "Lists\nWindow") listView.Wrap = true
fmt.Fprintf(listView, "Lists\nWindow\nTo view\n activity")
} }
return nil return nil
} }
@ -109,19 +143,42 @@ func initKeybindings(g *gocui.Gui) error {
} }
if err := g.SetKeybinding("Input", gocui.KeyEnter, gocui.ModNone, if err := g.SetKeybinding("Input", gocui.KeyEnter, gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error { func(g *gocui.Gui, v *gocui.View) error {
handleInput(g) return handleInput(g)
return nil
}); err != nil { }); err != nil {
return err return err
} }
return nil return nil
} }
func updateChatWindow() {
func handleInput(g *gocui.Gui) { }
printToView(g, "Chat", "Enter was hit!\n") func handleInput(g *gocui.Gui) error {
inputString, _ := getInputString(g) inputString, _ := getInputString(g)
printToView(g, "Chat", inputString+"\n") command := inputString[:2]
if "/q" == command {
return gocui.ErrQuit
} else if "/j" == command {
clearView(g, "Chat")
arr := strings.Split(inputString, " ")
if len(arr) > 2 {
teamOrUser = arr[1]
channel = arr[2]
printToView(g, "Feed", "You have joined: @" + teamOrUser + "#" + channel + "\n")
} else {
teamOrUser = arr[1]
channel = ""
printToView(g, "Feed", "You have joined: @" + teamOrUser + "\n")
}
updateChatWindow()
} else {
if channel == "" {
sendToUser(inputString)
} else {
sendToTeam(inputString)
}
}
clearView(g, "Input") clearView(g, "Input")
return nil
} }
func quit(g *gocui.Gui, v *gocui.View) error { func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit return gocui.ErrQuit

Loading…
Cancel
Save