Browse Source

Cleaning things up

pull/1/head
Gregory 'Rudi' Rudolph 5 years ago
parent
commit
576cda8751
  1. 28
      main.go

28
main.go

@ -26,6 +26,7 @@ func main() {
log.Panicln(err) log.Panicln(err)
} }
} }
func testAsync(kbtui *gocui.Gui) { func testAsync(kbtui *gocui.Gui) {
for i := 0; i < 50; i++ { for i := 0; i < 50; i++ {
printToView(kbtui, "Chat", "Message #" + strconv.Itoa(i) + "\n") printToView(kbtui, "Chat", "Message #" + strconv.Itoa(i) + "\n")
@ -33,6 +34,7 @@ func testAsync(kbtui *gocui.Gui) {
} }
clearView(kbtui, "Chat") clearView(kbtui, "Chat")
} }
func clearView(kbtui *gocui.Gui, viewName string) { func clearView(kbtui *gocui.Gui, viewName string) {
kbtui.Update(func(g *gocui.Gui) error { kbtui.Update(func(g *gocui.Gui) error {
inputView, err := kbtui.View(viewName) inputView, err := kbtui.View(viewName)
@ -51,7 +53,7 @@ func printToView(kbtui *gocui.Gui, viewName string, message string) {
if err != nil { if err != nil {
return err return err
} else { } else {
_, _ = fmt.Fprintf(updatingView, message) fmt.Fprintf(updatingView, message)
} }
return nil return nil
}) })
@ -64,44 +66,34 @@ func layout(g *gocui.Gui) error {
return err return err
} }
feedView.Autoscroll = true feedView.Autoscroll = true
_, _ = fmt.Fprintln(feedView, "Feed Window") fmt.Fprintln(feedView, "Feed Window")
} }
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") fmt.Fprintln(chatView, "Chat Window")
} }
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 {
return err3 return err3
} }
if _, err := g.SetCurrentView("Input"); err != nil {
return err
}
inputView.Editable = true inputView.Editable = true
_, _ = fmt.Fprintln(inputView, "Input Window") fmt.Fprintln(inputView, "Input Window")
} }
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") fmt.Fprintf(listView, "Lists\nWindow")
} }
return nil return nil
} }
func Edit(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) {
cx, _ := v.Cursor()
ox, _ := v.Origin()
limit := ox+cx+1 > 255
switch {
case ch != 0 && mod == 0 && !limit:
v.EditWrite(ch)
case key == gocui.KeySpace && !limit:
v.EditWrite(' ')
case key == gocui.KeyBackspace || key == gocui.KeyBackspace2:
v.EditDelete(true)
}
}
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