Create a gist now

Instantly share code, notes, and snippets.

@juanfgs /hacktime Secret
Created May 30, 2015

Embed
What would you like to do?
Progressbar test (hacking time joke from kung fury)
package main
import (
"github.com/nsf/termbox-go"
"time"
"fmt"
"os"
"strconv"
)
var output_mode = termbox.OutputNormal
var fg = termbox.ColorGreen
var bg = termbox.ColorBlack
func main() {
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
DrawInterface()
loop:
for {
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey:
switch ev.Key {
case termbox.KeyEsc:
break loop
case termbox.KeyArrowLeft:
DrawChoice(true, 4,7)
termbox.Sync()
case termbox.KeyArrowRight:
DrawChoice(false, 4,7)
termbox.Sync()
case termbox.KeyEnter:
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
x,y := termbox.Size()
tbprint((x /2) - 5 , y /2 , fg, bg, "Hacking time")
destination,_ :=strconv.Atoi(os.Args[1])
timediff := time.Now().Year() - destination
go DrawProgressBar(timediff, x/2 - (timediff/2) , y/2 +2)
termbox.Sync()
}
case termbox.EventResize:
DrawInterface()
}
}
}
func tbprint(x, y int, fg, bg termbox.Attribute, msg string) {
for _, c := range msg {
termbox.SetCell(x, y, c, fg, bg)
x++
}
}
func DrawChoice(selected bool, x int, y int) {
var bg,fg termbox.Attribute
if selected {
bg = termbox.ColorBlack
fg = termbox.ColorGreen
} else {
fg = termbox.ColorBlack
bg = termbox.ColorGreen
}
tbprint(x, y, bg, fg, "YES")
tbprint(x + 5, y, fg, bg, "NO")
}
func DrawInterface() {
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
tbprint(2, 5, fg, bg, "Are you sure you want to hack time?")
tbprint(2, 7, fg, bg, ">")
DrawChoice(true, 4, 7)
termbox.Sync()
}
func DrawProgressBar( max int, posx,posy int){
odd := true
for i:= 0; i <= max; i++ {
termbox.SetCell(posx + i, posy, '█', fg, bg)
time.Sleep(1000 * time.Millisecond)
tbprint(posx + 6, posy + 2, termbox.ColorWhite, termbox.ColorDefault, fmt.Sprintf("Years hacked: %d", i))
if max -i <= 8 {
if odd {
tbprint(posx + 6, posy + 4, termbox.ColorBlack, termbox.ColorRed, "TOO MUCH TIME HACKED")
odd = false
} else {
tbprint(posx + 6, posy +4, termbox.ColorRed, termbox.ColorBlack, "TOO MUCH TIME HACKED")
odd = true
}
}
termbox.Sync()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment