Add tui.go
This commit is contained in:
parent
475c560f33
commit
4c1216d293
|
@ -0,0 +1,143 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jroimartin/gocui"
|
||||
"log"
|
||||
)
|
||||
|
||||
func ui() {
|
||||
g, err := gocui.NewGui(gocui.Output256)
|
||||
if err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
defer g.Close()
|
||||
g.SetManagerFunc(layout)
|
||||
g.Mouse = false
|
||||
//g.Cursor = true
|
||||
|
||||
initKeybindings(g)
|
||||
|
||||
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
|
||||
log.Panicln(err)
|
||||
}
|
||||
}
|
||||
|
||||
func quit(*gocui.Gui, *gocui.View) error {
|
||||
return gocui.ErrQuit
|
||||
}
|
||||
|
||||
// func sendmsg(g *gocui.Gui, v *gocui.View) error {
|
||||
// chatbox, err := g.View("chatbox")
|
||||
// textarea, err := g.View("textarea")
|
||||
// if err != nil {
|
||||
// log.Panicln(err)
|
||||
// }
|
||||
// message := textarea.Buffer()
|
||||
// msg := string(message)
|
||||
// if msg == "" {
|
||||
// return nil
|
||||
// }
|
||||
//
|
||||
// fmt.Fprint(chatbox, msg)
|
||||
// textarea.Clear()
|
||||
// textarea.SetCursor(0, 0)
|
||||
// return nil
|
||||
// }
|
||||
|
||||
func initKeybindings(g *gocui.Gui) error {
|
||||
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
// if err := g.SetKeybinding("button", gocui.MouseLeft, gocui.ModNone, sendmsg); err != nil {
|
||||
// log.Panicln(err)
|
||||
// }
|
||||
//
|
||||
if err := g.SetKeybinding("songSelection", gocui.KeyArrowDown, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error {
|
||||
scrollSongs(v, +1)
|
||||
return nil
|
||||
}); err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
if err := g.SetKeybinding("songSelection", gocui.KeyArrowUp, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error {
|
||||
scrollSongs(v, -1)
|
||||
return nil
|
||||
}); err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
if err := g.SetKeybinding("songSelection", gocui.KeyEnter, gocui.ModNone, playSong); err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func scrollSongs(v *gocui.View, dy int) error {
|
||||
_, currentY := v.Cursor()
|
||||
songs := v.BufferLines()
|
||||
if currentY == len(songs)-2 && dy == +1 {
|
||||
v.SetCursor(0, 0)
|
||||
} else if currentY == 0 && dy == -1 {
|
||||
v.SetCursor(0, len(songs)-2)
|
||||
} else {
|
||||
v.SetCursor(0, currentY+dy)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func playSong(g *gocui.Gui, v *gocui.View) error {
|
||||
currentlyPlaying, err := g.View("currentlyPlaying")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
currentlyPlaying.Clear()
|
||||
_, currentlySelected := v.Cursor()
|
||||
|
||||
songs := v.BufferLines()
|
||||
fmt.Fprintln(currentlyPlaying, fmt.Sprintf("Playing song number %v (%v)", currentlySelected, songs[currentlySelected]))
|
||||
// fmt.Fprintln(currentlyPlaying, len(songs))
|
||||
// fmt.Fprintln(currentlyPlaying, currentlySelected)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func layout(g *gocui.Gui) error {
|
||||
maxX, maxY := g.Size()
|
||||
|
||||
if progressViewer, err := g.SetView("progressViewer", 2, 1, maxX-2, 4); err != nil {
|
||||
if err != gocui.ErrUnknownView {
|
||||
return err
|
||||
}
|
||||
progressViewer.Title = "Progress viewer"
|
||||
}
|
||||
|
||||
if songSelection, err := g.SetView("songSelection", 2, 5, maxX/2+2, maxY-2); err != nil {
|
||||
if err != gocui.ErrUnknownView {
|
||||
return err
|
||||
}
|
||||
if _, err := g.SetCurrentView("songSelection"); err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
for i := 0; i < 5; i++ {
|
||||
songName := fmt.Sprintf("SongNumber%v", i)
|
||||
fmt.Fprintln(songSelection, songName)
|
||||
}
|
||||
songSelection.Title = "Song selection:"
|
||||
songSelection.Wrap = true
|
||||
|
||||
songSelection.Editable = false
|
||||
songSelection.Highlight = true
|
||||
songSelection.SelFgColor = gocui.ColorCyan
|
||||
}
|
||||
|
||||
if currentlyPlaying, err := g.SetView("currentlyPlaying", maxX/2+4, 5, maxX-2, maxY-2); err != nil {
|
||||
if err != gocui.ErrUnknownView {
|
||||
return err
|
||||
}
|
||||
currentlyPlaying.Title = "Currently playing:"
|
||||
currentlyPlaying.Wrap = true
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue