Clean up TermUI before Quitting

This commit is contained in:
Brian Buller 2017-12-19 08:03:06 -06:00
parent 32a724feb6
commit c520216ddd

View File

@ -22,9 +22,15 @@ var message string
var ticks int
var nodes []byte
var width, height int
var printMap bool
func main() {
// Grab the window size
if len(os.Args) > 1 {
if os.Args[1] == "-print" {
printMap = true
}
}
if err := termbox.Init(); err != nil {
panic(err)
}
@ -40,30 +46,39 @@ func main() {
width, height = termbox.Size()
network = StdinTo2DBytes()
PrintMap(0, 0)
if printMap {
ClearScreen()
PrintMap(0, 0)
PrintStatus()
}
posX, posY, dir := FindEntry(), 0, S
fmt.Println(posX, posY, dir)
for dir != ERR {
posX, posY, dir = Move(posX, posY, dir)
ticks++
switch network[posY][posX] {
case '|', '-', '+':
case ' ':
fmt.Println("Invalid Move")
os.Exit(1)
dir = ERR
break
default:
if len(nodes) == 0 || nodes[len(nodes)-1] != network[posY][posX] {
message = fmt.Sprint("Found node ", string(network[posY][posX]), "!")
nodes = append(nodes, network[posY][posX])
}
}
ticks++
PrintMap(posX, posY)
time.Sleep(time.Second / 10)
ClearScreen()
if printMap {
PrintMap(posX, posY)
time.Sleep(time.Second / 30)
}
PrintStatus()
}
fmt.Println("Ctrl+C to Quit")
for {
}
fmt.Println("")
}
func GetNodesAsString(nodes []byte) string {
@ -131,8 +146,11 @@ func FindEntry() int {
return -1
}
func PrintMap(posX, posY int) {
func ClearScreen() {
fmt.Print("\033[H\033[2J")
}
func PrintMap(posX, posY int) {
stX, stY := 0, 0
useHeight, useWidth := height-2, width
if posY > useHeight/2 {
@ -163,6 +181,9 @@ func PrintMap(posX, posY int) {
}
fmt.Println("")
}
}
func PrintStatus() {
fmt.Print(ticks, " Ticks ", GetNodesAsString(nodes), "\n")
fmt.Println(message)
}