feat: some kind of sample code for curses
This commit is contained in:
parent
1ef0757412
commit
a83b6a66ae
5 changed files with 86 additions and 4 deletions
BIN
ai_game
Executable file
BIN
ai_game
Executable file
Binary file not shown.
|
@ -50,6 +50,7 @@
|
||||||
tealdeer
|
tealdeer
|
||||||
gopls
|
gopls
|
||||||
air
|
air
|
||||||
|
ncurses
|
||||||
];
|
];
|
||||||
|
|
||||||
languages = {
|
languages = {
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -1,3 +1,5 @@
|
||||||
module gitlab.com/stvnliu/ai_game
|
module gitlab.com/stvnliu/ai_game
|
||||||
|
|
||||||
go 1.23.2
|
go 1.23.2
|
||||||
|
|
||||||
|
require github.com/gbin/goncurses v0.0.0-20240517145248-be6a464272ae
|
||||||
|
|
2
go.sum
Normal file
2
go.sum
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
github.com/gbin/goncurses v0.0.0-20240517145248-be6a464272ae h1:WeLSOuEYiwcuwg39YirhW0DibOkTztefXCTau5sSbyc=
|
||||||
|
github.com/gbin/goncurses v0.0.0-20240517145248-be6a464272ae/go.mod h1:dmRjyC3ZOQQ4EXWMOIAQi0TLaJPcg61LFsJ9mvhSGRE=
|
85
main.go
85
main.go
|
@ -1,10 +1,87 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
. "github.com/gbin/goncurses"
|
||||||
"gitlab.com/stvnliu/ai_game/tests"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
HEIGHT = 10
|
||||||
|
WIDTH = 30
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("What's up bro")
|
var active int
|
||||||
fmt.Println(tests.DoThis("Yo"))
|
menu := []string{"Choice 1", "Choice 2", "Choice 3", "Choice 4", "Exit"}
|
||||||
|
|
||||||
|
stdscr, err := Init()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer End()
|
||||||
|
|
||||||
|
Raw(true)
|
||||||
|
Echo(false)
|
||||||
|
Cursor(0)
|
||||||
|
stdscr.Clear()
|
||||||
|
stdscr.Keypad(true)
|
||||||
|
|
||||||
|
my, mx := stdscr.MaxYX()
|
||||||
|
y, x := 2, (mx/2)-(WIDTH/2)
|
||||||
|
|
||||||
|
win, _ := NewWindow(HEIGHT, WIDTH, y, x)
|
||||||
|
win.Keypad(true)
|
||||||
|
|
||||||
|
stdscr.Print("Use arrow keys to go up and down, Press enter to select")
|
||||||
|
stdscr.Refresh()
|
||||||
|
|
||||||
|
printmenu(win, menu, active)
|
||||||
|
|
||||||
|
for {
|
||||||
|
ch := stdscr.GetChar()
|
||||||
|
switch Key(ch) {
|
||||||
|
case 'q':
|
||||||
|
return
|
||||||
|
case KEY_UP:
|
||||||
|
if active == 0 {
|
||||||
|
active = len(menu) - 1
|
||||||
|
} else {
|
||||||
|
active -= 1
|
||||||
|
}
|
||||||
|
case KEY_DOWN:
|
||||||
|
if active == len(menu)-1 {
|
||||||
|
active = 0
|
||||||
|
} else {
|
||||||
|
active += 1
|
||||||
|
}
|
||||||
|
case KEY_RETURN, KEY_ENTER, Key('\r'):
|
||||||
|
stdscr.MovePrintf(my-2, 0, "Choice #%d: %s selected",
|
||||||
|
active,
|
||||||
|
menu[active])
|
||||||
|
stdscr.ClearToEOL()
|
||||||
|
stdscr.Refresh()
|
||||||
|
default:
|
||||||
|
stdscr.MovePrintf(my-2, 0, "Character pressed = %3d/%c",
|
||||||
|
ch, ch)
|
||||||
|
stdscr.ClearToEOL()
|
||||||
|
stdscr.Refresh()
|
||||||
|
}
|
||||||
|
|
||||||
|
printmenu(win, menu, active)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func printmenu(w *Window, menu []string, active int) {
|
||||||
|
y, x := 2, 2
|
||||||
|
w.Box(0, 0)
|
||||||
|
for i, s := range menu {
|
||||||
|
if i == active {
|
||||||
|
w.AttrOn(A_REVERSE)
|
||||||
|
w.MovePrint(y+i, x, s)
|
||||||
|
w.AttrOff(A_REVERSE)
|
||||||
|
} else {
|
||||||
|
w.MovePrint(y+i, x, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
w.Refresh()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue