Auto-refresh match listings

This commit is contained in:
Trevor Slocum 2023-09-10 14:59:01 -07:00
parent cb535c536b
commit 7df3279bc4
4 changed files with 41 additions and 19 deletions

41
app.go
View file

@ -18,17 +18,18 @@ const (
var colorYellow = "#FFFF00"
var (
app *cview.Application
uiGrid *cview.Grid
gameList *cview.List
gameListGrid *cview.Grid
confirmExit *cview.TextView
board *GameBoard
boardGrid *cview.Grid
gameBuffer *cview.TextView
statusBuffer *cview.TextView
inputField *cview.InputField
loginForm *cview.Form
app *cview.Application
uiGrid *cview.Grid
gameList *cview.List
gameListFooter *cview.TextView
gameListGrid *cview.Grid
confirmExit *cview.TextView
board *GameBoard
boardGrid *cview.Grid
gameBuffer *cview.TextView
statusBuffer *cview.TextView
inputField *cview.InputField
loginForm *cview.Form
createGameForm *cview.Form
createGameGrid *cview.Grid
@ -41,6 +42,7 @@ var (
screenWidth int
allGames []bgammon.GameListing
autoRefresh = true
showCreateGameDialog bool
gameInProgress bool
@ -154,6 +156,12 @@ func updateFocus() {
}
func buildLayout() {
autoRefreshStatus := "enabled"
if !autoRefresh {
autoRefreshStatus = "disabled"
}
gameListFooter.SetText("[" + colorYellow + "][\"btncreate\"][ Create match ][\"\"] [\"btnrefresh\"][ Refresh ][\"\"][-:-:-][\"dummy\"] [" + colorYellow + "][\"btnautorefresh\"][ Auto-refresh " + autoRefreshStatus + " ][\"\"][-:-:-][\"dummy\"] [\"\"]")
uiGrid.Clear()
var currentScreen cview.Primitive
@ -449,20 +457,22 @@ func RunApp(c *Client, b *GameBoard) error {
gameListHeader.SetText("[" + colorYellow + "]Status Name[-:-:-]")
gameListHeader.SetDynamicColors(true)
gameListFooter := cview.NewTextView()
gameListFooter.SetText("[" + colorYellow + "][\"btncreate\"][ Create match ][\"\"] [\"btnrefresh\"][ Refresh ][\"\"][-:-:-][\"dummy\"] [\"\"]")
gameListFooter = cview.NewTextView()
gameListFooter.SetDynamicColors(true)
gameListFooter.SetRegions(true)
gameListFooter.SetHighlightedFunc(func(added, removed, remaining []string) {
defer gameListFooter.Highlight()
if len(added) > 0 {
switch added[0] {
case "btnrefresh":
board.client.Out <- []byte("list")
case "btncreate":
showCreateGameDialog = true
resetCreateGameDialog()
buildLayout()
case "btnrefresh":
board.client.Out <- []byte("list")
case "btnautorefresh":
autoRefresh = !autoRefresh
buildLayout()
}
}
})
@ -502,7 +512,6 @@ func RunApp(c *Client, b *GameBoard) error {
buildLayout()
})
// TODO refactor
buildLayout()
defer func() {
if c.Username != "" {

2
go.mod
View file

@ -3,7 +3,7 @@ module code.rocket9labs.com/tslocum/bgammon-cli
go 1.16
require (
code.rocket9labs.com/tslocum/bgammon v0.0.0-20230909070101-edeb5806d93c
code.rocket9labs.com/tslocum/bgammon v0.0.0-20230910220109-d735899304ee
code.rocketnine.space/tslocum/cview v1.5.8
github.com/gdamore/tcell/v2 v2.6.0
github.com/mattn/go-runewidth v0.0.15 // indirect

4
go.sum
View file

@ -1,5 +1,5 @@
code.rocket9labs.com/tslocum/bgammon v0.0.0-20230909070101-edeb5806d93c h1:5rkwJD7xX8wDz8xT8sZkzEkekBWx7Vy0W5PFhkMMRMI=
code.rocket9labs.com/tslocum/bgammon v0.0.0-20230909070101-edeb5806d93c/go.mod h1:zacnxtEUuMgpvs0DOJRU5TGWSB9Tpp2LdYgiQdfVNYc=
code.rocket9labs.com/tslocum/bgammon v0.0.0-20230910220109-d735899304ee h1:NCURJ36ztPbyYD1+85XmL1O3mbOo/8mNuKxBNFZTj/s=
code.rocket9labs.com/tslocum/bgammon v0.0.0-20230910220109-d735899304ee/go.mod h1:zacnxtEUuMgpvs0DOJRU5TGWSB9Tpp2LdYgiQdfVNYc=
code.rocketnine.space/tslocum/cbind v0.1.5 h1:i6NkeLLNPNMS4NWNi3302Ay3zSU6MrqOT+yJskiodxE=
code.rocketnine.space/tslocum/cbind v0.1.5/go.mod h1:LtfqJTzM7qhg88nAvNhx+VnTjZ0SXBJtxBObbfBWo/M=
code.rocketnine.space/tslocum/cview v1.5.8 h1:G90dP78WmZ8obrLNggx9z4kPXgJDT/MhsKpEAoqqto8=

13
main.go
View file

@ -50,6 +50,17 @@ func lg(s string) {
log.Print(m)
}
func handleAutoRefresh() {
t := time.NewTicker(10 * time.Second) // TODO configurable
for range t.C {
if !autoRefresh || gameInProgress {
continue
}
board.client.Out <- []byte("list")
}
}
func main() {
var (
username string
@ -73,6 +84,8 @@ func main() {
board = NewGameBoard(c)
go handleAutoRefresh()
err := RunApp(c, board)
if err != nil {
log.Fatalf("%+v", err)