Practicing UI for "tune" project

This commit is contained in:
raul 2024-05-16 10:48:34 +02:00
parent c98136caa8
commit 39727a66ae
3 changed files with 146 additions and 0 deletions

11
tui-test-2/go.mod Normal file
View File

@ -0,0 +1,11 @@
module tui-test
go 1.22.2
require github.com/jroimartin/gocui v0.5.0
require (
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/nsf/termbox-go v1.1.1 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
)

10
tui-test-2/go.sum Normal file
View File

@ -0,0 +1,10 @@
github.com/jroimartin/gocui v0.5.0 h1:DCZc97zY9dMnHXJSJLLmx9VqiEnAj0yh0eTNpuEtG/4=
github.com/jroimartin/gocui v0.5.0/go.mod h1:l7Hz8DoYoL6NoYnlnaX6XCNR62G7J5FfSW5jEogzaxE=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/nsf/termbox-go v1.1.1 h1:nksUPLCb73Q++DwbYUBEglYBRPZyoXJdrj5L+TkjyZY=
github.com/nsf/termbox-go v1.1.1/go.mod h1:T0cTdVuOwf7pHQNtfhnEbzHbcNyCEcVU4YPpouCbVxo=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=

125
tui-test-2/main.go Normal file
View File

@ -0,0 +1,125 @@
package main
import (
//"fmt"
"fmt"
"log"
"github.com/jroimartin/gocui"
)
func main() {
ui()
}
func ui() {
g, err := gocui.NewGui(gocui.Output256)
if err != nil {
log.Panicln(err)
}
defer g.Close()
g.SetManagerFunc(layout)
g.Mouse = true
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 {
selectSongs(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 {
selectSongs(v, -1)
return nil
}); err != nil {
log.Panicln(err)
}
return nil
}
func selectSongs(v *gocui.View, dy int) error {
//songSelection, err := g.View("songSelection")
// if err != nil {
// return err
// }
_, currentY := v.Cursor()
v.SetCursor(0, currentY+dy)
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
}
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
}