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) } } }
func Init() error { err := termbox.Init() if err != nil { return fmt.Errorf("Could not init terminal: %v", err) } err = clearWithDefaultColors() if err != nil { return fmt.Errorf("Could not clear terminal: %v", err) } termbox.SetInputMode(termbox.InputAlt) hideCursor() internalEventProxying() StartEventPolling() fpsCounter = NewFpsCounter(time.Second) go func() { for fps := range fpsCounter.Fps { log.Debug.Printf("FPS: %v (%v frames skipped)", fps, fpsSkipped) fpsSkipped = 0 } }() go func() { for _ = range paintChan { paintThrottled() } }() return nil }
func realMain() error { flag.Parse() if err := termbox.Init(); err != nil { return err } defer termbox.Close() termbox.SetInputMode(termbox.InputEsc | termbox.InputMouse) opts := &gameOpts{} if *p1 == "" { opts.p1type = computer opts.p1name = "8086" } else { opts.p1type = human opts.p1name = *p1 } if *p2 == "" { opts.p2type = computer opts.p2name = "hal" } else { opts.p2type = human opts.p2name = *p2 } game := newGame(opts) game.draw() game.loop() return nil }
/* 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 initCanvas() bool { err := term.Init() if err != nil { return false } term.SetInputMode(term.InputEsc | term.InputMouse) canvas = new(Canvas) Reset() return true }
func debug(args ...interface{}) { term.Close() for _, a := range args { fmt.Println(a) } reader := bufio.NewReader(os.Stdin) reader.ReadString('\n') err := term.Init() if err != nil { panic(err) } term.SetInputMode(term.InputAlt) }
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) } } }
func termboxExample() { err := termbox.Init() if err != nil { panic(err) } defer termbox.Close() termbox.SetInputMode(termbox.InputEsc | termbox.InputMouse) termbox.Clear(termbox.ColorDefault, termbox.ColorDefault) termbox.Flush() inputmode := 0 ctrlxpressed := false loop: for { switch ev := termbox.PollEvent(); ev.Type { case termbox.EventKey: // コンソールが消えるっぽい if ev.Key == termbox.KeyCtrlS && ctrlxpressed { termbox.Sync() break } // Ctrl+Xモード時のCtrl+Qでループを抜ける if ev.Key == termbox.KeyCtrlQ && ctrlxpressed { break loop } if ev.Key == termbox.KeyCtrlC && ctrlxpressed { chmap := []termbox.InputMode{ termbox.InputEsc | termbox.InputMouse, termbox.InputAlt | termbox.InputMouse, termbox.InputEsc, termbox.InputAlt, } inputmode++ if inputmode >= len(chmap) { inputmode = 0 } termbox.SetInputMode(chmap[inputmode]) } // Ctrl+X 入モード切り替え if ev.Key == termbox.KeyCtrlX { ctrlxpressed = true } else { ctrlxpressed = false } termbox.Clear(termbox.ColorDefault, termbox.ColorDefault) dispatch_press(&ev) pretty_print_press(&ev) termbox.Flush() case termbox.EventResize: // ターミナルのサイズ変更で呼ばれる termbox.Clear(termbox.ColorDefault, termbox.ColorDefault) pretty_print_resize(&ev) termbox.Flush() case termbox.EventMouse: // ターミナルにタッチすると呼ばれる termbox.Clear(termbox.ColorDefault, termbox.ColorDefault) pretty_print_mouse(&ev) termbox.Flush() case termbox.EventError: panic(ev.Err) } } }
func (tb termboxImpl) SetInputMode() { termbox.SetInputMode(termbox.InputEsc | termbox.InputMouse) }
func main() { var newFlag bool flag.BoolVar(&newFlag, "new", false, "let tor to edit a new file.") flag.Parse() args := flag.Args() if len(args) == 0 { fmt.Println("please, set text file") os.Exit(1) } farg := args[len(args)-1] f, initL, initO, err := parseFileArg(farg) if err != nil { fmt.Println("file arg is invalid: ", err) os.Exit(1) } exist := true if _, err := os.Stat(f); os.IsNotExist(err) { exist = false } if !exist && !newFlag { fmt.Println("file not exist. please retry with -new flag.") os.Exit(1) } else if exist && newFlag { fmt.Println("file already exist.") os.Exit(1) } var text *Text if exist { text, err = open(f) if err != nil { fmt.Println(err) os.Exit(1) } } else { lines := make([]Line, 0) lines = append(lines, Line{""}) text = &Text{lines: lines, tabToSpace: false, tabWidth: 4, edited: false} } err = term.Init() if err != nil { panic(err) } defer term.Close() term.SetInputMode(term.InputAlt) // term.SetOutputMode(term.Output256) term.Clear(term.ColorDefault, term.ColorDefault) term.Flush() termw, termh := term.Size() mainarea := NewArea(Point{0, 0}, Point{termh - 1, termw}) win := NewWindow(mainarea.Size()) cursor := &Cursor{} selection := &Selection{} history := newHistory() mode := &ModeSelector{} mode.normal = &NormalMode{ text: text, cursor: cursor, selection: selection, history: history, f: f, mode: mode, copied: loadConfig("copy"), } mode.find = &FindMode{ mode: mode, str: loadConfig("find"), } mode.replace = &ReplaceMode{ mode: mode, str: loadConfig("replace"), } mode.gotoline = &GotoLineMode{ cursor: cursor, mode: mode, } mode.exit = &ExitMode{ f: f, cursor: cursor, mode: mode, } mode.current = mode.normal // will start tor as normal mode. selection.m = mode.normal // Set cursor. cursor.m = mode.normal if initL != -1 { l := initL // to internal line number if l != 0 { l-- } cursor.GotoLine(l) if initO != -1 { cursor.SetO(initO) } } else { l, b := loadLastPosition(f) cursor.GotoLine(l) cursor.SetCloseToB(b) } events := make(chan term.Event, 20) go func() { for { events <- term.PollEvent() } }() for { win.Follow(cursor, 3) clearScreen(mainarea) drawScreen(mainarea, win, mode.normal.text, selection, cursor) if mode.current.Error() != "" { printErrorStatus(mode.current.Error()) } else { printStatus(mode.current.Status()) } if mode.current == mode.normal { winP := cursor.Position().Sub(win.min) term.SetCursor(mainarea.min.o+winP.o, mainarea.min.l+winP.l) } else { term.SetCursor(vlen(mode.current.Status(), mode.normal.text.tabWidth), termh) } term.Flush() // wait for keyboard input select { case ev := <-events: switch ev.Type { case term.EventKey: mode.current.Handle(ev) case term.EventResize: term.Clear(term.ColorDefault, term.ColorDefault) termw, termh = term.Size() resizeScreen(mainarea, win, termw, termh) } } } }
func main() { err := termbox.Init() if err != nil { panic(err) } defer termbox.Close() termbox.SetInputMode(termbox.InputEsc | termbox.InputMouse) x := 0 y := 0 termbox.SetCursor(x, y) termbox.Flush() loop: for { switch ev := termbox.PollEvent(); ev.Type { case termbox.EventKey: if ev.Key == termbox.KeyCtrlS { // Syncでコンソールが消えるっぽい x = 0 y = 0 termbox.Sync() termbox.SetCursor(x, y) termbox.Flush() break } // Ctrl+Xモード時のCtrl+Qでループを抜ける制御 if ev.Key == termbox.KeyCtrlQ { break loop } if ev.Key == termbox.KeyEnter { x = 0 y++ termbox.SetCursor(x, y) termbox.Flush() break } // タイプした文字を出力 msg := string(ev.Ch) for _, c := range msg { termbox.SetCell(x, y, c, termbox.ColorWhite, termbox.ColorBlack) x++ } // 画面端まで行ったら行を更新 xx, _ := termbox.Size() if x == xx { x = 0 y++ } debugPrint(fmt.Sprintf("x = %d xx = %d", x, xx)) termbox.SetCursor(x, y) termbox.Flush() case termbox.EventResize: // ターミナルのサイズ変更で呼ばれる termbox.Flush() case termbox.EventMouse: // ターミナルにタッチすると呼ばれる termbox.Flush() case termbox.EventError: panic(ev.Err) } } }