func termSync() { renderLock.Lock() tm.Sync() termWidth, termHeight = tm.Size() renderLock.Unlock() }
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 Sync() error { mutex.Lock() err := termbox.Sync() mutex.Unlock() return err }
func main() { flag.Parse() var tzs []*time.Location if len(*tzFlag) == 0 { tzs = []*time.Location{time.Local} } else { tzNames := strings.Split(*tzFlag, ",") for _, tzn := range tzNames { tz, err := time.LoadLocation(tzn) if err != nil { fmt.Fprintf(os.Stderr, "Couldn't load timezone %q termui: %v\n", tzn, err) os.Exit(1) } tzs = append(tzs, tz) } } if err := ui.Init(); err != nil { fmt.Fprintln(os.Stderr, "Couldn't initialize termui:", err) os.Exit(1) } defer ui.Close() timesWidget := ui.NewList() timesWidget.Items = make([]string, len(tzs)) timesWidget.Border = false // RuneCountInString is technically incorrect because it doesn't account for // combining characters, but it should be fine most of the time. timesWidget.Width = utf8.RuneCountInString(*timeFmtFlag) + 1 timesWidget.Height = len(tzs) timesWidget.X = 0 timesWidget.Y = 0 resize := func() { if *tmuxFlag { tmuxCmd := exec.Command( "tmux", "resize", "-t", os.Getenv("TMUX_PANE"), "-y", strconv.Itoa(len(tzs)), ) tmuxCmd.Stdin = nil tmuxCmd.Stdout = nil tmuxCmd.Stderr = nil if err := tmuxCmd.Start(); err != nil { // Not much to do in error case. Maybe we should have an error display area? return } _ = tmuxCmd.Wait() // Termbox can lose track of what it needs to redraw, force refresh. termbox.Sync() } } resize() draw := func() { now := time.Now() for i, tz := range tzs { timesWidget.Items[i] = now.In(tz).Format(*timeFmtFlag) } ui.Render(timesWidget) } draw() ui.Merge("timer", ui.NewTimerCh(1*time.Second)) ui.Handle("/timer", func(ui.Event) { draw() }) ui.Handle("/sys/wnd/resize", func(ui.Event) { resize() }) ui.Handle("/sys/kbd/q", func(ui.Event) { ui.StopLoop() }) ui.Loop() }
// TermHeight returns the current terminal's height. func TermHeight() int { tm.Sync() _, h := tm.Size() return h }
// TermWidth returns the current terminal's width. func TermWidth() int { tm.Sync() w, _ := tm.Size() return w }
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) } } }