Add example
This commit is contained in:
		
							parent
							
								
									821cc1f44b
								
							
						
					
					
						commit
						1a79160e56
					
				
							
								
								
									
										69
									
								
								example/dashboard.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										69
									
								
								example/dashboard.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,69 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import ui "github.com/gizak/termui"
 | 
			
		||||
import tm "github.com/nsf/termbox-go"
 | 
			
		||||
import "time"
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	err := ui.Init()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		panic(err)
 | 
			
		||||
	}
 | 
			
		||||
	defer ui.Close()
 | 
			
		||||
 | 
			
		||||
	p := ui.NewP(":PRESS q TO QUIT DEMO\nThis is an example of termui package rendering.")
 | 
			
		||||
	p.Height = 4
 | 
			
		||||
	p.Width = 59
 | 
			
		||||
	p.TextFgColor = ui.ColorWhite
 | 
			
		||||
	p.Border.Label = "Text"
 | 
			
		||||
	p.Border.FgColor = ui.ColorCyan
 | 
			
		||||
 | 
			
		||||
	strs := []string{"[0] gizak/termui", "[1] editbox.go", "[2] iterrupt.go", "[3] keyboard.go", "[4] output.go", "[5] random_out.go", "[6] dashboard.go", "[7] nsf/termbox-go"}
 | 
			
		||||
	list := ui.NewList()
 | 
			
		||||
	list.Items = strs
 | 
			
		||||
	list.ItemFgColor = ui.ColorYellow
 | 
			
		||||
	list.Border.Label = "List"
 | 
			
		||||
	list.Height = 7
 | 
			
		||||
	list.Width = 25
 | 
			
		||||
	list.Y = 4
 | 
			
		||||
 | 
			
		||||
	g := ui.NewGauge()
 | 
			
		||||
	g.Percent = 50
 | 
			
		||||
	g.Width = 52
 | 
			
		||||
	g.Height = 3
 | 
			
		||||
	g.Y = 11
 | 
			
		||||
	g.Border.Label = "Gauge"
 | 
			
		||||
	g.BarColor = ui.ColorRed
 | 
			
		||||
	g.Border.FgColor = ui.ColorWhite
 | 
			
		||||
	g.Border.LabelFgColor = ui.ColorCyan
 | 
			
		||||
 | 
			
		||||
	draw := func(t int) {
 | 
			
		||||
		g.Percent = t % 101
 | 
			
		||||
		list.Items = strs[t%9:]
 | 
			
		||||
		ui.Render(p, list, g)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	evt := make(chan tm.Event)
 | 
			
		||||
	go func() {
 | 
			
		||||
		for {
 | 
			
		||||
			evt <- tm.PollEvent()
 | 
			
		||||
		}
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	i := 0
 | 
			
		||||
	for {
 | 
			
		||||
		select {
 | 
			
		||||
		case e := <-evt:
 | 
			
		||||
			if e.Type == tm.EventKey && e.Ch == 'q' {
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
		default:
 | 
			
		||||
			draw(i)
 | 
			
		||||
			i++
 | 
			
		||||
			if i == 102 {
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
			time.Sleep(time.Second / 2)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										123
									
								
								sparkline.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										123
									
								
								sparkline.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,123 @@
 | 
			
		||||
package termui
 | 
			
		||||
 | 
			
		||||
import "math"
 | 
			
		||||
 | 
			
		||||
type Sparkline struct {
 | 
			
		||||
	Data          []int
 | 
			
		||||
	Height        int
 | 
			
		||||
	Title         string
 | 
			
		||||
	TitleColor    Attribute
 | 
			
		||||
	LineColor     Attribute
 | 
			
		||||
	displayHeight int
 | 
			
		||||
	scale         float32
 | 
			
		||||
	max           int
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Sparklines struct {
 | 
			
		||||
	Block
 | 
			
		||||
	Lines        []Sparkline
 | 
			
		||||
	displayLines int
 | 
			
		||||
	displayWidth int
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var sparks = []rune{'▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'}
 | 
			
		||||
 | 
			
		||||
func (s *Sparklines) Add(sl Sparkline) {
 | 
			
		||||
	s.Lines = append(s.Lines, sl)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewSparklines(ss ...Sparkline) *Sparklines {
 | 
			
		||||
	s := &Sparklines{Block: *NewBlock(), Lines: ss}
 | 
			
		||||
	return s
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (sl *Sparklines) update() {
 | 
			
		||||
	for i, v := range sl.Lines {
 | 
			
		||||
		if v.Title == "" {
 | 
			
		||||
			sl.Lines[i].displayHeight = v.Height
 | 
			
		||||
		} else {
 | 
			
		||||
			sl.Lines[i].displayHeight = v.Height + 1
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	sl.displayWidth = sl.innerWidth
 | 
			
		||||
 | 
			
		||||
	// get how many lines gotta display
 | 
			
		||||
	h := 0
 | 
			
		||||
	for _, v := range sl.Lines {
 | 
			
		||||
		if h+v.displayHeight <= sl.innerHeight {
 | 
			
		||||
			sl.displayLines++
 | 
			
		||||
		} else {
 | 
			
		||||
			break
 | 
			
		||||
		}
 | 
			
		||||
		h += v.displayHeight
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for i := 0; i < sl.displayLines; i++ {
 | 
			
		||||
		data := sl.Lines[i].Data
 | 
			
		||||
 | 
			
		||||
		max := math.MinInt32
 | 
			
		||||
		for _, v := range data {
 | 
			
		||||
			if max < v {
 | 
			
		||||
				max = v
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		sl.Lines[i].max = max
 | 
			
		||||
		sl.Lines[i].scale = float32(8*sl.Lines[i].Height) / float32(max)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (sl *Sparklines) Buffer() []Point {
 | 
			
		||||
	ps := sl.Block.Buffer()
 | 
			
		||||
	sl.update()
 | 
			
		||||
 | 
			
		||||
	oftY := 0
 | 
			
		||||
	for i := 0; i < sl.displayLines; i++ {
 | 
			
		||||
		l := sl.Lines[i]
 | 
			
		||||
		data := l.Data
 | 
			
		||||
 | 
			
		||||
		if len(data) > sl.innerWidth {
 | 
			
		||||
			data = data[:sl.innerWidth]
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if l.Title != "" {
 | 
			
		||||
			rs := trimStr2Runes(l.Title, sl.innerWidth)
 | 
			
		||||
			for oftX, v := range rs {
 | 
			
		||||
				p := Point{}
 | 
			
		||||
				p.Code.Ch = v
 | 
			
		||||
				p.Code.Fg = toTmAttr(l.TitleColor)
 | 
			
		||||
				p.Code.Bg = toTmAttr(sl.BgColor)
 | 
			
		||||
				p.X = oftX
 | 
			
		||||
				p.Y = oftY
 | 
			
		||||
				ps = append(ps, p)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		for j, v := range data {
 | 
			
		||||
			h := int(float32(v) * l.scale)
 | 
			
		||||
			barCnt := h / 8
 | 
			
		||||
			barMod := h % 8
 | 
			
		||||
			for jj := 0; jj < barCnt; jj++ {
 | 
			
		||||
				p := Point{}
 | 
			
		||||
				p.X = sl.innerX + j
 | 
			
		||||
				p.Y = sl.innerY + oftY + l.Height - jj
 | 
			
		||||
				p.Code.Ch = sparks[7]
 | 
			
		||||
				p.Code.Fg = toTmAttr(l.LineColor)
 | 
			
		||||
				p.Code.Bg = toTmAttr(sl.BgColor)
 | 
			
		||||
				ps = append(ps, p)
 | 
			
		||||
			}
 | 
			
		||||
			if barMod != 0 {
 | 
			
		||||
				p := Point{}
 | 
			
		||||
				p.X = sl.innerX + j
 | 
			
		||||
				p.Y = sl.innerY + oftY + l.Height - barCnt
 | 
			
		||||
				p.Code.Ch = sparks[barMod-1]
 | 
			
		||||
				p.Code.Fg = toTmAttr(l.LineColor)
 | 
			
		||||
				p.Code.Bg = toTmAttr(sl.BgColor)
 | 
			
		||||
				ps = append(ps, p)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		oftY += l.displayHeight
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return ps
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user