示例#1
0
文件: hello.go 项目: ghigt/termutil
func main() {

	var err error

	err = termutil.Init(time.Second)
	if err != nil {
		log.Fatal(err)
	}

	termutil.Screen.EventFunc = func(ev termbox.Event) {
		switch ev.Type {
		case termbox.EventKey:
			switch ev.Key {
			case termbox.KeyEsc:
				termutil.Quit()
			}
		}
	}

	win := termutil.NewWindow()

	win.UpdateFunc = func() []termutil.Row {
		return termutil.StringsToRows([]string{"hello"}, termbox.ColorCyan, termbox.ColorMagenta)
	}

	err = termutil.Run()
	termutil.End()

	if err != nil {
		log.Fatal(err)
		return
	}
}
示例#2
0
文件: table.go 项目: ghigt/termutil
func main() {

	var err error

	err = termutil.Init(time.Second)
	if err != nil {
		log.Fatal(err)
	}

	termutil.Screen.EventFunc = func(ev termbox.Event) {
		switch ev.Type {
		case termbox.EventKey:
			switch ev.Key {
			case termbox.KeyEsc:
				termutil.Quit()
			}
		}
	}

	head := termutil.NewWindow()
	head.SizeY = 1
	head.UpdateFunc = func() []termutil.Row {
		return termutil.StringsToRows([]string{
			fmt.Sprintf("WidgetTable: %v", time.Now().Format("02/01/2006 15:04:05"))}, 0, 0)
	}

	win := termutil.NewWindow()
	win.Y = 1

	wg := &termutil.WidgetTable{
		Header: &termutil.Header{
			Titles: []termutil.HeaderTitle{
				{"name", 20},
				{"id", 10},
				{"hobby", 0},
				{"height", 10},
			},
			Fg:       termbox.ColorBlack,
			Bg:       termbox.ColorGreen,
			FgActive: termbox.ColorBlack,
			BgActive: termbox.ColorBlue,
		},
		Body: &termutil.Body{
			Active:   0,
			FgActive: termbox.ColorDefault,
			BgActive: termbox.AttrReverse,
		},
	}
	win.UpdateFunc = func() []termutil.Row {
		wg.Body.Data = bodys[time.Now().Nanosecond()%3]
		return wg.Update(win)
	}
	win.EventFunc = wg.EventFunc

	err = termutil.Run()
	termutil.End()

	if err != nil {
		log.Fatal(err)
		return
	}
}
示例#3
0
文件: box.go 项目: ghigt/termutil
func main() {

	var err error

	err = termutil.Init(time.Second)
	if err != nil {
		log.Fatal(err)
	}

	termutil.Screen.EventFunc = func(ev termbox.Event) {
		switch ev.Type {
		case termbox.EventKey:
			switch ev.Key {
			case termbox.KeyEsc:
				termutil.Quit()
			}
		}
	}

	win1 := termutil.NewWindow()
	win1.X = 3
	win1.Y = 2

	win1.UpdateFunc = func() []string {
		return []string{
			"Put the text in the box:",
			"+---+",
			"|   |",
			"|   |",
			"|   |",
			"+---+",
		}
	}
	swin1 := win1.NewSubWindow()
	swin1.X = 1
	swin1.Y = 2
	swin1.UpdateFunc = func() []string {
		return []string{
			"+-+",
			"| |",
			"+-+",
		}
	}
	sswin := swin1.NewSubWindow()
	sswin.X = 1
	sswin.Y = 1
	sswin.UpdateFunc = func() []string {
		return []string{
			"*",
		}
	}

	win2 := termutil.NewWindow()
	win2.X = 15
	win2.Y = 15
	win2.SizeX = 3
	win2.SizeY = 3

	win2.UpdateFunc = func() []string {
		if win2.X == 4 && win2.Y == 4 {
			return []string{"you", "won", "!!!"}
		}
		return []string{"aaa", "bbb", "ccc"}
	}

	win2.EventFunc = func(ev termbox.Event) {
		switch ev.Type {
		case termbox.EventKey:
			switch ev.Key {
			case termbox.KeyArrowDown:
				win2.Y += 1
			case termbox.KeyArrowUp:
				win2.Y -= 1
			case termbox.KeyArrowLeft:
				win2.X -= 1
			case termbox.KeyArrowRight:
				win2.X += 1
			}
		}
	}

	err = termutil.Run()
	termutil.End()

	if err != nil {
		log.Fatal(err)
		return
	}
}