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