|
|
|
@ -4,10 +4,15 @@ import (
@@ -4,10 +4,15 @@ import (
|
|
|
|
|
"fmt" |
|
|
|
|
"github.com/jroimartin/gocui" |
|
|
|
|
"log" |
|
|
|
|
"strconv" |
|
|
|
|
"samhofi.us/x/keybase" |
|
|
|
|
"strings" |
|
|
|
|
"time" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
var k = keybase.NewKeybase() |
|
|
|
|
var teamOrUser = "home" |
|
|
|
|
var channel = "" |
|
|
|
|
|
|
|
|
|
func main() { |
|
|
|
|
kbtui, err := gocui.NewGui(gocui.OutputNormal) |
|
|
|
|
if err != nil { |
|
|
|
@ -16,25 +21,46 @@ func main() {
@@ -16,25 +21,46 @@ func main() {
|
|
|
|
|
defer kbtui.Close() |
|
|
|
|
|
|
|
|
|
kbtui.SetManagerFunc(layout) |
|
|
|
|
go loginGreeter(kbtui) |
|
|
|
|
go populateList(kbtui) |
|
|
|
|
|
|
|
|
|
if err := initKeybindings(kbtui); err != nil { |
|
|
|
|
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 { |
|
|
|
|
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) { |
|
|
|
|
for i := 0; i < 50; i++ { |
|
|
|
|
printToView(kbtui, "Chat", "Message #" + strconv.Itoa(i) + "\n") |
|
|
|
|
time.Sleep(1 * time.Second) |
|
|
|
|
func sendToTeam(msg string) { |
|
|
|
|
chat := k.NewChat(keybase.Channel{ |
|
|
|
|
Name: teamOrUser, |
|
|
|
|
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") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
time.Sleep(5 * time.Second) |
|
|
|
|
} |
|
|
|
|
clearView(kbtui, "Chat") |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func clearView(kbtui *gocui.Gui, viewName string) { |
|
|
|
@ -70,14 +96,20 @@ func layout(g *gocui.Gui) error {
@@ -70,14 +96,20 @@ func layout(g *gocui.Gui) error {
|
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
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 err2 != gocui.ErrUnknownView { |
|
|
|
|
return err2 |
|
|
|
|
} |
|
|
|
|
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 err3 != gocui.ErrUnknownView { |
|
|
|
@ -87,12 +119,14 @@ func layout(g *gocui.Gui) error {
@@ -87,12 +119,14 @@ func layout(g *gocui.Gui) error {
|
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
inputView.Editable = true |
|
|
|
|
inputView.Wrap = true |
|
|
|
|
} |
|
|
|
|
if listView, err4 := g.SetView("List", 0, 0, 10, maxY-1); err4 != nil { |
|
|
|
|
if err4 != gocui.ErrUnknownView { |
|
|
|
|
return err4 |
|
|
|
|
} |
|
|
|
|
fmt.Fprintf(listView, "Lists\nWindow") |
|
|
|
|
listView.Wrap = true |
|
|
|
|
fmt.Fprintf(listView, "Lists\nWindow\nTo view\n activity") |
|
|
|
|
} |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
@ -109,19 +143,42 @@ func initKeybindings(g *gocui.Gui) error {
@@ -109,19 +143,42 @@ func initKeybindings(g *gocui.Gui) error {
|
|
|
|
|
} |
|
|
|
|
if err := g.SetKeybinding("Input", gocui.KeyEnter, gocui.ModNone, |
|
|
|
|
func(g *gocui.Gui, v *gocui.View) error { |
|
|
|
|
handleInput(g) |
|
|
|
|
return nil |
|
|
|
|
return handleInput(g) |
|
|
|
|
}); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
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) |
|
|
|
|
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") |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
func quit(g *gocui.Gui, v *gocui.View) error { |
|
|
|
|
return gocui.ErrQuit |
|
|
|
|