/* A multiplayer rogue-like in the spirit of TOAG, but implemented in termbox-go */ func main() { //flag.Parse() /* start network handling and channels */ max_dimensions := 80 * 24 screen := make([]nr.Sprite, max_dimensions) world := loadWorld() /* initialize termbox interface */ err := tb.Init() check(err) tb.SetInputMode(tb.InputAlt) w, h := tb.Size() tb.Clear(tb.ColorBlack, tb.ColorBlack) tb.Flush() go draw(w, h) for run := true; run == true; { switch ev := tb.PollEvent(); ev.Type { case tb.EventKey: //key := ev.Ch if ev.Key == tb.KeyCtrlQ { tb.Flush() run = false } default: } } }
func main() { err := tb.Init() if err != nil { panic(err) } defer tb.Close() tb.SetInputMode(tb.InputEsc) tb.Clear(tb.ColorCyan, tb.ColorBlue) tb.Flush() loop: for i := 0; ; i++ { ev := tb.PollEvent() switch ev.Type { case tb.EventKey: if ev.Key == tb.KeyCtrlC { break loop } tb.SetCell(i, i, ev.Ch, tb.ColorGreen, tb.ColorBlack) tb.Flush() case tb.EventError: panic(ev.Err) } } }
/* draws the screen (continual loop) */ func draw(w, h int) { defer tb.Flush() for { tb.Flush() time.Sleep(20 * time.Millisecond) } }
// over draws game-over screen. func (g *game) over() { for i := 0; i < g.board.w/2+1; i++ { g.draw() drawRect(g.board.x+i, g.board.y+i, g.board.w*2-i*2, g.board.h-i*2) termbox.Flush() time.Sleep(100 * time.Millisecond) } msg := "Game Over!" fill(g.board.x+g.board.w/2, g.board.y+g.board.h/2, len(msg)+6, 1, ' ') tbprint(msg, g.board.x+g.board.w/2+3, g.board.y+g.board.h/2, fgcolor|termbox.AttrBold, bgcolor) termbox.Flush() time.Sleep(3 * time.Second) }
func draw() bool { termbox.Clear(termbox.ColorDefault, termbox.ColorDefault) dots := NewCellsBox([]termbox.Cell{dot, dot, dot, dot, dot, dot, dot, dot, dot, dot, dot, dot, dot, dot, dot, dot, dot, dot, dot, dot, }, 5) greenPlus := NewCellsBox([]termbox.Cell{greenPlus, greenPlus, greenPlus, greenPlus}, 2) if err := greenPlus.DrawInto(dots, 1, 2); err != nil { termbox.Close() fmt.Fprintf(os.Stderr, "%v1\r\n", err) return false } if err := dots.DrawInto(&TermBox{}, 0, 0); err != nil { termbox.Close() fmt.Fprintf(os.Stderr, "%v2\r\n", err) return false } termbox.SetCell(0, 0, '+', termbox.ColorRed, termbox.ColorDefault) termbox.SetCell(1, 0, '+', termbox.ColorRed, termbox.ColorDefault) termbox.SetCell(7, 0, '+', termbox.ColorRed, termbox.ColorDefault) termbox.Flush() return true }
func (v *TermView) Render() { v.Clear() // quit instructions quitMsg := "(Ctrl+C to quit)" v.Printf(v.w-len(quitMsg), 0, quitMsg) // new version message newVersion := v.state.GetNewVersion() if newVersion != "" { newVersionMsg := fmt.Sprintf("new version available at http://ngrok.com") v.APrintf(termbox.ColorYellow, 30, 0, newVersionMsg) } v.APrintf(termbox.ColorBlue|termbox.AttrBold, 0, 0, "ngrok") status := v.state.GetStatus() v.APrintf(colorForConn(status), 0, 2, "%-30s%s", "Tunnel Status", status) v.Printf(0, 3, "%-30s%s/%s", "Version", v.state.GetClientVersion(), v.state.GetServerVersion()) v.Printf(0, 4, "%-30s%s", "Protocol", v.state.GetProtocol().GetName()) v.Printf(0, 5, "%-30s%s -> %s", "Forwarding", v.state.GetPublicUrl(), v.state.GetLocalAddr()) webAddr := fmt.Sprintf("http://localhost:%d", v.state.GetWebPort()) v.Printf(0, 6, "%-30s%s", "Web Interface", webAddr) connMeter, connTimer := v.state.GetConnectionMetrics() v.Printf(0, 7, "%-30s%d", "# Conn", connMeter.Count()) msec := float64(time.Millisecond) v.Printf(0, 8, "%-30s%.2fms", "Avg Conn Time", connTimer.Mean()/msec) termbox.Flush() }
func (s *Screen) Loop() { for { e := termbox.PollEvent() // handle error if e.Type == termbox.EventError { panic(e.Err) } w := curWindow // TODO rename w to curWindow // handle resize if w.autoResize && e.Type == termbox.EventResize { err := w.Redraw() if err != nil { panic(err) // TODO dont panic here } termbox.Flush() continue } if w.CatchEvent != nil { w.CatchEvent(e) } } }
func draw() bool { termbox.Clear(termbox.ColorDefault, termbox.ColorDefault) labelField.DrawInto(&box.TermBox{}, 0, 0) termbox.Flush() return true }
func print_tb(x, y int, msg string, fg, bg termbox.Attribute) { for _, c := range []rune(msg) { termbox.SetCell(x, y, c, fg, bg) x += runewidth.RuneWidth(c) } termbox.Flush() }
func (b *board) highlightPos(x, y int) { if !b.in(x, y) { return } b.curPosVert = !b.curPosVert fg := fgcolor | termbox.AttrBold bg := bgcolor | termbox.AttrBold defch := '·' var xch, ych rune if b.curPosVert { xch = defch ych = '⇣' } else { xch = '⇢' ych = defch } // draw position indicators for i := b.x; i < x; i += 2 { termbox.SetCell(i, y, xch, fg, bg) } for i := b.y; i <= y; i++ { if (x-b.x)%2 == 1 { termbox.SetCell(x-1, i, ych, fg, bg) } else { termbox.SetCell(x, i, ych, fg, bg) } } termbox.Flush() }
func (v *TermView) run() { defer close(v.shutdown) defer termbox.Close() redraw := v.redraw.Reg() defer v.redraw.UnReg(redraw) v.draw() for { v.Debug("Waiting for update") select { case <-v.flush: termbox.Flush() case <-v.updates: v.draw() case <-redraw: v.draw() case <-v.shutdown: return } } }
func (f *Form) Run() error { draw := func() bool { termbox.Clear(termbox.ColorDefault, termbox.ColorDefault) f.DrawInto(&box.TermBox{}, 0, 0) termbox.Flush() return true } err := termbox.Init() defer termbox.Close() if err != nil { return err } if draw() { f.running = true for f.running { ev := termbox.PollEvent() if !f.HandleEvent(&ev) { f.running = false } draw() } } else { return errors.New("fail sadness") } return nil }
func (s *Session) Run() error { s.mode = &ModeEdit{} data, err := ioutil.ReadFile(s.File) if err != nil { return err } s.Buf = util.NewBuffer(data) s.W, s.H = termbox.Size() s.H-- s.View.SetBuf(s.Buf) s.View.SetSize(s.W, s.H) s.View.SetTabwidth(s.Tabwidth) for { s.Draw() termbox.Flush() termbox.Clear(0, 0) ev := termbox.PollEvent() switch ev.Type { case termbox.EventKey: s.mode, err = s.mode.HandleKey(s, ev) if err != nil { return err } case termbox.EventResize: s.W, s.H = ev.Width, ev.Height-1 s.View.SetSize(s.W, s.H) case termbox.EventMouse: case termbox.EventError: return ev.Err } } }
func flush() error { mutex.Lock() err := termbox.Flush() mutex.Unlock() updateRootBuffer() return err }
func printBoard(board TheirBoard, xStart, yStart int) { print_tb(xStart-1, 9, "/ - - - - - - - - - -\\", termbox.ColorBlack, termbox.ColorWhite) // X then Y // y := yStart for y := 0; y < 10; y++ { for x := 0; x < 10; x++ { termbox.SetCell(xStart-1, yStart+y, '|', termbox.ColorBlack, termbox.ColorWhite) switch board[x][y] { case 1: termbox.SetCell(xStart+(x*2), yStart+y, 'X', termbox.ColorRed, termbox.ColorBlack) case 2: termbox.SetCell(xStart+(x*2), yStart+y, ' ', 8, termbox.ColorBlack) default: termbox.SetCell(xStart+(x*2), yStart+y, '~', termbox.ColorGreen, termbox.ColorCyan) termbox.SetCell(xStart+(x*2)-1, yStart+y, ' ', termbox.ColorGreen, termbox.ColorCyan) } } termbox.SetCell(xStart+(10*2), yStart+y, '|', termbox.ColorBlack, termbox.ColorWhite) } termbox.Flush() print_tb(xStart-1, yStart+10, "\\ - - - - - - - - - -/", termbox.ColorBlack, termbox.ColorWhite) }
func print_tb(x, y int, fg, bg tb.Attribute, msg string) { for _, c := range msg { tb.SetCell(x, y, c, fg, bg) x++ } tb.Flush() }
func (v *TermView) Render() { v.Clear() // quit instructions quitMsg := "(Ctrl+C to quit)" v.Printf(v.w-len(quitMsg), 0, quitMsg) // new version message updateStatus := v.state.GetUpdate() var updateMsg string switch updateStatus { case ui.UpdateNone: updateMsg = "" case ui.UpdateInstalling: updateMsg = "ngrok is updating" case ui.UpdateReady: updateMsg = "ngrok has updated: restart ngrok for the new version" case ui.UpdateError: updateMsg = "new version available at https://ngrok.com" default: pct := float64(updateStatus) / 100.0 const barLength = 25 full := int(barLength * pct) bar := make([]byte, barLength+2) bar[0] = '[' bar[barLength+1] = ']' for i := 0; i < 25; i++ { if i <= full { bar[i+1] = '#' } else { bar[i+1] = ' ' } } updateMsg = "Downloading update: " + string(bar) } if updateMsg != "" { v.APrintf(termbox.ColorYellow, 30, 0, updateMsg) } v.APrintf(termbox.ColorBlue|termbox.AttrBold, 0, 0, "ngrok") status := v.state.GetStatus() v.APrintf(colorForConn(status), 0, 2, "%-30s%s", "Tunnel Status", status) v.Printf(0, 3, "%-30s%s/%s", "Version", v.state.GetClientVersion(), v.state.GetServerVersion()) v.Printf(0, 4, "%-30s%s", "Protocol", v.state.GetProtocol().GetName()) v.Printf(0, 5, "%-30s%s -> %s", "Forwarding", v.state.GetPublicUrl(), v.state.GetLocalAddr()) webAddr := fmt.Sprintf("http://localhost:%d", v.state.GetWebPort()) v.Printf(0, 6, "%-30s%s", "Web Interface", webAddr) connMeter, connTimer := v.state.GetConnectionMetrics() v.Printf(0, 7, "%-30s%d", "# Conn", connMeter.Count()) msec := float64(time.Millisecond) v.Printf(0, 8, "%-30s%.2fms", "Avg Conn Time", connTimer.Mean()/msec) termbox.Flush() }
func debug_tb(x, y int, fg, bg tb.Attribute, msg string) { for _, c := range msg { tb.SetCell(x, y, c, fg, bg) x++ } tb.Flush() _ = tb.PollEvent().Ch }
// Repaints everything on the screen func RefreshScreen() { term.Clear(ColorWhite, ColorBlack) for _, wnd := range comp.windows { wnd.Draw() } term.Flush() }
// Blur removes focus from the input box. func (i *Input) Blur() { i.focus = false i.text = i.text[:0] i.cursorPos = 0 dst := tulib.TermboxBuffer() dst.Fill(i.rect, i.proto) dst.DrawLabel(i.rect, &i.promptParams, i.prompt) term.Flush() }
// Render renders all Bufferer in the given order from left to right, // right could overlap on left ones. func Render(rs ...Bufferer) { tm.Clear(tm.ColorDefault, toTmAttr(theme.BodyBg)) for _, r := range rs { buf := r.Buffer() for _, v := range buf { tm.SetCell(v.X, v.Y, v.Ch, toTmAttr(v.Fg), toTmAttr(v.Bg)) } } tm.Flush() }
func main() { termbox.Init() defer termbox.Close() m := panel.MainScreen() for _, s := range strings.Split(example, "\n") { helper.ScrollWriteUpwards(m, s) termbox.Flush() time.Sleep(time.Millisecond * 500) } for _, s := range strings.Split(example, "\n") { helper.ScrollWrite(m, s) termbox.Flush() time.Sleep(time.Millisecond * 500) } helper.ScrollWrite(m, "And that's\neverything\nfor now.") termbox.Flush() time.Sleep(time.Millisecond * 2000) }
func main() { rand.Seed(time.Now().UnixNano()) dim := dimensions{10, 10} ships := canonicalBattleship() attackField := makeScatteredField(dim, ships, adversary) attackOrigin := coord{0, 0} defendField := makeScatteredField(dim, ships, human) defendOrigin := coord{30, 0} printOrigin := coord{0, 30} termbox.Init() defer termbox.Close() termbox.HideCursor() termbox.Clear(termbox.ColorBlack, termbox.ColorBlack) buildLabels(attackField, attackOrigin) buildLabels(defendField, defendOrigin) // game loop var msg_us, msg_them string for attackField.shipsLeft() && defendField.shipsLeft() { buildInnerField(attackField, attackOrigin) buildInnerField(defendField, defendOrigin) termbox.Flush() msg_us = move(&attackField) termbox.Flush() msg_them = counterMove(&defendField) tbprint(printOrigin, msg_us, termbox.ColorWhite, termbox.ColorBlack) tbprint(printOrigin.down(1), msg_them, termbox.ColorWhite, termbox.ColorBlack) termbox.Flush() } if defendField.shipsLeft() { fmt.Println("You've won! Congratulations.") } else if attackField.shipsLeft() { fmt.Println("You've lost!") } }
func drawDrumMachine() { tb.Clear(tb.ColorDefault, tb.ColorDefault) term.ofs.seq = drawHeader() if term.mode == newModePickSample || term.mode == newModeEnterName { term.ofs.trk = drawSamples() } else { term.ofs.trk = drawSequences() drawSections() } tb.Flush() }
func main() { termbox.Init() termbox.HideCursor() var p Player p = *NewPlayer() p.Draw() termbox.Flush() <-time.After(2000 * time.Millisecond) termbox.Close() os.Exit(0) }
func draw() bool { termbox.Clear(termbox.ColorDefault, termbox.ColorDefault) termbox.SetCell(0, 0, 'H', 0, 0) termbox.SetCell(1, 0, 'e', 0, 0) termbox.SetCell(2, 0, 'y', 0, 0) textField.DrawInto(&box.TermBox{}, 4, 0) termbox.Flush() return true }
func (v *HttpView) Render() { v.Clear() v.Printf(0, 0, "HTTP Requests") v.Printf(0, 1, "-------------") for i, obj := range v.HttpRequests.Slice() { txn := obj.(*proto.HttpTxn) v.Printf(0, 3+i, "%s %v", txn.Req.Method, txn.Req.URL.Path) if txn.Resp != nil { v.APrintf(colorFor(txn.Resp.Status), 30, 3+i, "%s", txn.Resp.Status) } } termbox.Flush() }
func calibratePoint(et gaze.EyeTracker, x, y float64) { termbox.Clear(0, 0) sizeX, sizeY := termbox.Size() dx, dy := int(float64(sizeX)*x), int(float64(sizeY)*y) termbox.CellBuffer()[sizeX*dy+dx] = termbox.Cell{'#', marker, termbox.ColorDefault} termbox.Flush() //log.Printf("Termbox Size: (%d, %d)", sizeX, sizeY) log.Printf("Calibrating (%.3f, %.3f)", x, y) done := make(chan struct{}) time.Sleep(1 * time.Second) //Minimum time. Gives user time to react. et.AddPointToCalibration(gaze.NewPoint2D(x, y), func(err error) { close(done) //Synchronizing async call. }) <-done }
// draw redraws the screen contents. func (c *CLI) draw() { dst := tulib.TermboxBuffer() if len(dst.Cells) == 0 { return } c.traceLog.Draw(&dst) c.watchers.Draw(&dst) c.sourceLog.Draw(&dst) c.breakpoints.Draw(&dst) c.status.Draw(&dst) c.input.Draw(&dst) term.Flush() }
func main() { termbox.Init() defer termbox.Close() termbox.SetInputMode(termbox.InputEsc) termbox.Flush() for i := 0; i < 7; i++ { switch ev := termbox.PollEvent(); ev.Type { case termbox.EventKey: fmt.Println(ev) } } }