Move widget back to root
This commit is contained in:
parent
196d9aae34
commit
e0dec9dbb9
@ -7,7 +7,6 @@
|
||||
package main
|
||||
|
||||
import "github.com/gizak/termui"
|
||||
import "github.com/gizak/termui/widget"
|
||||
|
||||
func main() {
|
||||
err := termui.Init()
|
||||
@ -16,54 +15,57 @@ func main() {
|
||||
}
|
||||
defer termui.Close()
|
||||
|
||||
termui.UseTheme("helloworld")
|
||||
//termui.UseTheme("helloworld")
|
||||
|
||||
g0 := widget.NewGauge()
|
||||
g0 := termui.NewGauge()
|
||||
g0.Percent = 40
|
||||
g0.Width = 50
|
||||
g0.Height = 3
|
||||
g0.Border.Label = "Slim Gauge"
|
||||
g0.BorderLabel = "Slim Gauge"
|
||||
g0.BarColor = termui.ColorRed
|
||||
g0.Border.Fg = termui.ColorWhite
|
||||
g0.Border.LabelFgClr = termui.ColorCyan
|
||||
g0.BorderFg = termui.ColorWhite
|
||||
g0.BorderLabelFg = termui.ColorCyan
|
||||
|
||||
gg := termui.NewBlock()
|
||||
gg.Width = 50
|
||||
gg.Height = 5
|
||||
gg.Y = 12
|
||||
gg.Border.Label = "TEST"
|
||||
gg.BorderLabel = "TEST"
|
||||
gg.Align()
|
||||
|
||||
g2 := widget.NewGauge()
|
||||
g2 := termui.NewGauge()
|
||||
g2.Percent = 60
|
||||
g2.Width = 50
|
||||
g2.Height = 3
|
||||
g2.PercentColor = termui.ColorBlue
|
||||
g2.Y = 3
|
||||
g2.Border.Label = "Slim Gauge"
|
||||
g2.BorderLabel = "Slim Gauge"
|
||||
g2.BarColor = termui.ColorYellow
|
||||
g2.Border.Fg = termui.ColorWhite
|
||||
g2.BorderFg = termui.ColorWhite
|
||||
|
||||
g1 := widget.NewGauge()
|
||||
g1 := termui.NewGauge()
|
||||
g1.Percent = 30
|
||||
g1.Width = 50
|
||||
g1.Height = 5
|
||||
g1.Y = 6
|
||||
g1.Border.Label = "Big Gauge"
|
||||
g1.BorderLabel = "Big Gauge"
|
||||
g1.PercentColor = termui.ColorYellow
|
||||
g1.BarColor = termui.ColorGreen
|
||||
g1.Border.Fg = termui.ColorWhite
|
||||
g1.Border.LabelFgClr = termui.ColorMagenta
|
||||
g1.BorderFg = termui.ColorWhite
|
||||
g1.BorderLabelFg = termui.ColorMagenta
|
||||
|
||||
g3 := termui.NewGauge()
|
||||
g3.Percent = 50
|
||||
g3.Width = 50
|
||||
g3.Height = 3
|
||||
g3.Y = 11
|
||||
g3.Border.Label = "Gauge with custom label"
|
||||
g3.BorderLabel = "Gauge with custom label"
|
||||
g3.Label = "{{percent}}% (100MBs free)"
|
||||
g3.LabelAlign = termui.AlignRight
|
||||
|
||||
termui.Render(g0, g1, g2, g3)
|
||||
<-termui.EventCh()
|
||||
termui.Handle("/sys/kbd/q", func(termui.Event) {
|
||||
termui.StopLoop()
|
||||
})
|
||||
termui.Loop()
|
||||
}
|
||||
|
@ -41,16 +41,16 @@ type BarChart struct {
|
||||
// NewBarChart returns a new *BarChart with current theme.
|
||||
func NewBarChart() *BarChart {
|
||||
bc := &BarChart{Block: *NewBlock()}
|
||||
bc.BarColor = theme.BarChartBar
|
||||
bc.NumColor = theme.BarChartNum
|
||||
bc.TextColor = theme.BarChartText
|
||||
bc.BarColor = ThemeAttr("barchart.bar.bg")
|
||||
bc.NumColor = ThemeAttr("barchart.num.fg")
|
||||
bc.TextColor = ThemeAttr("barchart.text.fg")
|
||||
bc.BarGap = 1
|
||||
bc.BarWidth = 3
|
||||
return bc
|
||||
}
|
||||
|
||||
func (bc *BarChart) layout() {
|
||||
bc.numBar = bc.innerWidth / (bc.BarGap + bc.BarWidth)
|
||||
bc.numBar = bc.innerArea.Dx() / (bc.BarGap + bc.BarWidth)
|
||||
bc.labels = make([][]rune, bc.numBar)
|
||||
bc.dataNum = make([][]rune, len(bc.Data))
|
||||
|
||||
@ -71,7 +71,7 @@ func (bc *BarChart) layout() {
|
||||
bc.max = bc.Data[i]
|
||||
}
|
||||
}
|
||||
bc.scale = float64(bc.max) / float64(bc.innerHeight-1)
|
||||
bc.scale = float64(bc.max) / float64(bc.innerArea.Dy()-1)
|
||||
}
|
||||
|
||||
func (bc *BarChart) SetMax(max int) {
|
||||
@ -82,8 +82,8 @@ func (bc *BarChart) SetMax(max int) {
|
||||
}
|
||||
|
||||
// Buffer implements Bufferer interface.
|
||||
func (bc *BarChart) Buffer() []Point {
|
||||
ps := bc.Block.Buffer()
|
||||
func (bc *BarChart) Buffer() Buffer {
|
||||
buf := bc.Block.Buffer()
|
||||
bc.layout()
|
||||
|
||||
for i := 0; i < bc.numBar && i < len(bc.Data) && i < len(bc.DataLabels); i++ {
|
||||
@ -92,46 +92,48 @@ func (bc *BarChart) Buffer() []Point {
|
||||
// plot bar
|
||||
for j := 0; j < bc.BarWidth; j++ {
|
||||
for k := 0; k < h; k++ {
|
||||
p := Point{}
|
||||
p.Ch = ' '
|
||||
p.Bg = bc.BarColor
|
||||
if bc.BarColor == ColorDefault { // when color is default, space char treated as transparent!
|
||||
p.Bg |= AttrReverse
|
||||
c := Cell{
|
||||
Ch: ' ',
|
||||
Bg: bc.BarColor,
|
||||
}
|
||||
p.X = bc.innerX + i*(bc.BarWidth+bc.BarGap) + j
|
||||
p.Y = bc.innerY + bc.innerHeight - 2 - k
|
||||
ps = append(ps, p)
|
||||
if bc.BarColor == ColorDefault { // when color is default, space char treated as transparent!
|
||||
c.Bg |= AttrReverse
|
||||
}
|
||||
x := bc.innerArea.Min.X + i*(bc.BarWidth+bc.BarGap) + j
|
||||
y := bc.innerArea.Min.Y + bc.innerArea.Dy() - 2 - k
|
||||
buf.Set(x, y, c)
|
||||
}
|
||||
}
|
||||
// plot text
|
||||
for j, k := 0, 0; j < len(bc.labels[i]); j++ {
|
||||
w := charWidth(bc.labels[i][j])
|
||||
p := Point{}
|
||||
c := Cell{}
|
||||
p.Ch = bc.labels[i][j]
|
||||
p.Bg = bc.BgColor
|
||||
p.Fg = bc.TextColor
|
||||
p.Y = bc.innerY + bc.innerHeight - 1
|
||||
p.X = bc.innerX + oftX + k
|
||||
p.Y = bc.innerArea.Min.Y + bc.innerArea.Dy() - 1
|
||||
p.X = bc.innerArea.Min.X + oftX + k
|
||||
ps = append(ps, p)
|
||||
k += w
|
||||
}
|
||||
// plot num
|
||||
for j := 0; j < len(bc.dataNum[i]); j++ {
|
||||
p := Point{}
|
||||
p.Ch = bc.dataNum[i][j]
|
||||
p.Fg = bc.NumColor
|
||||
p.Bg = bc.BarColor
|
||||
c := Cell{
|
||||
Ch: bc.dataNum[i][j],
|
||||
Fg: bc.NumColor,
|
||||
Bg: bc.BarColor,
|
||||
}
|
||||
if bc.BarColor == ColorDefault { // the same as above
|
||||
p.Bg |= AttrReverse
|
||||
c.Bg |= AttrReverse
|
||||
}
|
||||
if h == 0 {
|
||||
p.Bg = bc.BgColor
|
||||
c.Bg = bc.BgColor
|
||||
}
|
||||
p.X = bc.innerX + oftX + (bc.BarWidth-len(bc.dataNum[i]))/2 + j
|
||||
p.Y = bc.innerY + bc.innerHeight - 2
|
||||
ps = append(ps, p)
|
||||
x := bc.innerArea.Min.X + oftX + (bc.BarWidth-len(bc.dataNum[i]))/2 + j
|
||||
y := bc.innerArea.Min.Y + bc.innerArea.Dy() - 2
|
||||
buf.Set(x, y, c)
|
||||
}
|
||||
}
|
||||
|
||||
return bc.Block.chopOverflow(ps)
|
||||
return buf
|
||||
}
|
6
block.go
6
block.go
@ -119,6 +119,7 @@ type Block struct {
|
||||
PaddingBottom int
|
||||
PaddingLeft int
|
||||
PaddingRight int
|
||||
id string
|
||||
}
|
||||
|
||||
// NewBlock returns a *Block which inherits styles from current theme.
|
||||
@ -137,9 +138,14 @@ func NewBlock() *Block {
|
||||
b.Bg = ThemeAttr("block.bg")
|
||||
b.Width = 2
|
||||
b.Height = 2
|
||||
b.id = GenId()
|
||||
return &b
|
||||
}
|
||||
|
||||
func (b Block) Id() string {
|
||||
return b.id
|
||||
}
|
||||
|
||||
// Align computes box model
|
||||
func (b *Block) Align() {
|
||||
b.area.Min.X = b.X
|
||||
|
27
events.go
27
events.go
@ -152,6 +152,7 @@ type EvtStream struct {
|
||||
wg sync.WaitGroup
|
||||
sigStopLoop chan Event
|
||||
Handlers map[string]func(Event)
|
||||
hook func(Event)
|
||||
}
|
||||
|
||||
func NewEvtStream() *EvtStream {
|
||||
@ -209,10 +210,10 @@ func (es *EvtStream) Handle(path string, handler func(Event)) {
|
||||
es.Handlers[cleanPath(path)] = handler
|
||||
}
|
||||
|
||||
func (es *EvtStream) match(path string) string {
|
||||
func findMatch(mux map[string]func(Event), path string) string {
|
||||
n := -1
|
||||
pattern := ""
|
||||
for m := range es.Handlers {
|
||||
for m := range mux {
|
||||
if !isPathMatch(m, path) {
|
||||
continue
|
||||
}
|
||||
@ -222,11 +223,28 @@ func (es *EvtStream) match(path string) string {
|
||||
}
|
||||
}
|
||||
return pattern
|
||||
|
||||
}
|
||||
|
||||
func (es *EvtStream) match(path string) string {
|
||||
return findMatch(es.Handlers, path)
|
||||
}
|
||||
|
||||
/*
|
||||
var internalHandlers = make(map[string]func(Event))
|
||||
|
||||
func initInternalHandling() {
|
||||
|
||||
}
|
||||
*/
|
||||
func (es *EvtStream) Hook(f func(Event)) {
|
||||
es.hook = f
|
||||
}
|
||||
|
||||
func (es *EvtStream) Loop() {
|
||||
for e := range es.stream {
|
||||
if e.Path == "/sig/stoploop" {
|
||||
switch e.Path {
|
||||
case "/sig/stoploop":
|
||||
return
|
||||
}
|
||||
go func(a Event) {
|
||||
@ -236,6 +254,9 @@ func (es *EvtStream) Loop() {
|
||||
es.Handlers[pattern](a)
|
||||
}
|
||||
}(e)
|
||||
if es.hook != nil {
|
||||
es.hook(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -44,8 +44,8 @@ type Gauge struct {
|
||||
func NewGauge() *Gauge {
|
||||
g := &Gauge{
|
||||
Block: *NewBlock(),
|
||||
PercentColor: theme.GaugePercent,
|
||||
BarColor: theme.GaugeBar,
|
||||
PercentColor: ThemeAttr("gauge.percent.fg"),
|
||||
BarColor: ThemeAttr("gauge.bar.bg"),
|
||||
Label: "{{percent}}%",
|
||||
LabelAlign: AlignCenter,
|
||||
}
|
||||
@ -56,28 +56,26 @@ func NewGauge() *Gauge {
|
||||
}
|
||||
|
||||
// Buffer implements Bufferer interface.
|
||||
func (g *Gauge) Buffer() []Point {
|
||||
ps := g.Block.Buffer()
|
||||
func (g *Gauge) Buffer() Buffer {
|
||||
buf := g.Block.Buffer()
|
||||
|
||||
// plot bar
|
||||
w := g.Percent * g.innerWidth / 100
|
||||
for i := 0; i < g.innerHeight; i++ {
|
||||
w := g.Percent * g.innerArea.Dx() / 100
|
||||
for i := 0; i < g.innerArea.Dy(); i++ {
|
||||
for j := 0; j < w; j++ {
|
||||
p := Point{}
|
||||
p.X = g.innerX + j
|
||||
p.Y = g.innerY + i
|
||||
p.Ch = ' '
|
||||
p.Bg = g.BarColor
|
||||
if p.Bg == ColorDefault {
|
||||
p.Bg |= AttrReverse
|
||||
c := Cell{}
|
||||
c.Ch = ' '
|
||||
c.Bg = g.BarColor
|
||||
if c.Bg == ColorDefault {
|
||||
c.Bg |= AttrReverse
|
||||
}
|
||||
ps = append(ps, p)
|
||||
buf.Set(g.innerArea.Min.X+j, g.innerArea.Min.Y+i, c)
|
||||
}
|
||||
}
|
||||
|
||||
// plot percentage
|
||||
s := strings.Replace(g.Label, "{{percent}}", strconv.Itoa(g.Percent), -1)
|
||||
pry := g.innerY + g.innerHeight/2
|
||||
pry := g.innerArea.Min.Y + g.innerArea.Dy()/2
|
||||
rs := str2runes(s)
|
||||
var pos int
|
||||
switch g.LabelAlign {
|
||||
@ -85,29 +83,29 @@ func (g *Gauge) Buffer() []Point {
|
||||
pos = 0
|
||||
|
||||
case AlignCenter:
|
||||
pos = (g.innerWidth - strWidth(s)) / 2
|
||||
pos = (g.innerArea.Dx() - strWidth(s)) / 2
|
||||
|
||||
case AlignRight:
|
||||
pos = g.innerWidth - strWidth(s)
|
||||
pos = g.innerArea.Dx() - strWidth(s)
|
||||
}
|
||||
|
||||
for i, v := range rs {
|
||||
p := Point{}
|
||||
p.X = 1 + pos + i
|
||||
p.Y = pry
|
||||
p.Ch = v
|
||||
p.Fg = g.PercentColor
|
||||
if w+g.innerX > pos+i {
|
||||
p.Bg = g.BarColor
|
||||
if p.Bg == ColorDefault {
|
||||
p.Bg |= AttrReverse
|
||||
c := Cell{
|
||||
Ch: v,
|
||||
Fg: g.PercentColor,
|
||||
}
|
||||
|
||||
if w+g.innerArea.Min.X > pos+i {
|
||||
c.Bg = g.BarColor
|
||||
if c.Bg == ColorDefault {
|
||||
c.Bg |= AttrReverse
|
||||
}
|
||||
|
||||
} else {
|
||||
p.Bg = g.Block.BgColor
|
||||
c.Bg = g.Block.Bg
|
||||
}
|
||||
|
||||
ps = append(ps, p)
|
||||
buf.Set(1+pos+i, pry, c)
|
||||
}
|
||||
return g.Block.chopOverflow(ps)
|
||||
return buf
|
||||
}
|
2
grid.go
2
grid.go
@ -275,3 +275,5 @@ func (g Grid) Buffer() Buffer {
|
||||
}
|
||||
return buf
|
||||
}
|
||||
|
||||
var Body = NewGrid()
|
||||
|
@ -89,8 +89,8 @@ func NewLineChart() *LineChart {
|
||||
|
||||
// one cell contains two data points
|
||||
// so the capicity is 2x as dot-mode
|
||||
func (lc *LineChart) renderBraille() []Point {
|
||||
ps := []Point{}
|
||||
func (lc *LineChart) renderBraille() Buffer {
|
||||
buf := NewBuffer()
|
||||
|
||||
// return: b -> which cell should the point be in
|
||||
// m -> in the cell, divided into 4 equal height levels, which subcell?
|
||||
@ -106,44 +106,48 @@ func (lc *LineChart) renderBraille() []Point {
|
||||
b1, m1 := getPos(lc.Data[2*i+1])
|
||||
|
||||
if b0 == b1 {
|
||||
p := Point{}
|
||||
p.Ch = braillePatterns[[2]int{m0, m1}]
|
||||
p.Bg = lc.BgColor
|
||||
p.Fg = lc.LineColor
|
||||
p.Y = lc.innerY + lc.innerHeight - 3 - b0
|
||||
p.X = lc.innerX + lc.labelYSpace + 1 + i
|
||||
ps = append(ps, p)
|
||||
c := Cell{
|
||||
Ch: braillePatterns[[2]int{m0, m1}],
|
||||
Bg: lc.BgColor,
|
||||
Fg: lc.LineColor,
|
||||
}
|
||||
y := lc.innerArea.Min.Y + lc.innerArea.Dy() - 3 - b0
|
||||
x := lc.innerArea.Min.X + lc.labelYSpace + 1 + i
|
||||
buf.Set(x, y, c)
|
||||
} else {
|
||||
p0 := newPointWithAttrs(lSingleBraille[m0],
|
||||
lc.innerX+lc.labelYSpace+1+i,
|
||||
lc.innerY+lc.innerHeight-3-b0,
|
||||
lc.LineColor,
|
||||
lc.BgColor)
|
||||
p1 := newPointWithAttrs(rSingleBraille[m1],
|
||||
lc.innerX+lc.labelYSpace+1+i,
|
||||
lc.innerY+lc.innerHeight-3-b1,
|
||||
lc.LineColor,
|
||||
lc.BgColor)
|
||||
ps = append(ps, p0, p1)
|
||||
c0 := Cell{Ch: lSingleBraille[m0],
|
||||
Fg: lc.LineColor,
|
||||
Bg: lc.BgColor}
|
||||
x0 := lc.innerArea.Min.X + lc.labelYSpace + 1 + i
|
||||
y0 := lc.innerArea.Min.Y + lc.innerArea.Dy() - 3 - b0
|
||||
buf.Set(x0, y0, c0)
|
||||
|
||||
c1 := Cell{Ch: rSingleBraille[m1],
|
||||
Fg: lc.LineColor,
|
||||
Bg: lc.Bg}
|
||||
x1 := lc.innerArea.Min.X + lc.labelYSpace + 1 + i
|
||||
y1 := lc.innerArea.Min.Y + lc.innerArea.Dy() - 3 - b1
|
||||
buf.Set(x1, y1, c1)
|
||||
}
|
||||
|
||||
}
|
||||
return ps
|
||||
return buf
|
||||
}
|
||||
|
||||
func (lc *LineChart) renderDot() []Point {
|
||||
ps := []Point{}
|
||||
func (lc *LineChart) renderDot() Buffer {
|
||||
buf := NewBuffer()
|
||||
for i := 0; i < len(lc.Data) && i < lc.axisXWidth; i++ {
|
||||
p := Point{}
|
||||
p.Ch = lc.DotStyle
|
||||
p.Fg = lc.LineColor
|
||||
p.Bg = lc.BgColor
|
||||
p.X = lc.innerX + lc.labelYSpace + 1 + i
|
||||
p.Y = lc.innerY + lc.innerHeight - 3 - int((lc.Data[i]-lc.bottomValue)/lc.scale+0.5)
|
||||
ps = append(ps, p)
|
||||
c := Cell{
|
||||
Ch: lc.DotStyle,
|
||||
Fg: lc.LineColor,
|
||||
Bg: lc.BgColor,
|
||||
}
|
||||
x := lc.innerArea.Min.X + lc.labelYSpace + 1 + i
|
||||
y := lc.innerArea.Min.Y + lc.innerArea.Dy() - 3 - int((lc.Data[i]-lc.bottomValue)/lc.scale+0.5)
|
||||
buf.Set(x, y, c)
|
||||
}
|
||||
|
||||
return ps
|
||||
return buf
|
||||
}
|
||||
|
||||
func (lc *LineChart) calcLabelX() {
|
||||
@ -222,9 +226,9 @@ func (lc *LineChart) calcLayout() {
|
||||
lc.maxY = lc.Data[0]
|
||||
|
||||
// valid visible range
|
||||
vrange := lc.innerWidth
|
||||
vrange := lc.innerArea.Dx()
|
||||
if lc.Mode == "braille" {
|
||||
vrange = 2 * lc.innerWidth
|
||||
vrange = 2 * lc.innerArea.Dx()
|
||||
}
|
||||
if vrange > len(lc.Data) {
|
||||
vrange = len(lc.Data)
|
||||
@ -249,40 +253,30 @@ func (lc *LineChart) calcLayout() {
|
||||
lc.topValue = lc.maxY + 0.2*span
|
||||
}
|
||||
|
||||
lc.axisYHeight = lc.innerHeight - 2
|
||||
lc.axisYHeight = lc.innerArea.Dy() - 2
|
||||
lc.calcLabelY()
|
||||
|
||||
lc.axisXWidth = lc.innerWidth - 1 - lc.labelYSpace
|
||||
lc.axisXWidth = lc.innerArea.Dx() - 1 - lc.labelYSpace
|
||||
lc.calcLabelX()
|
||||
|
||||
lc.drawingX = lc.innerX + 1 + lc.labelYSpace
|
||||
lc.drawingY = lc.innerY
|
||||
lc.drawingX = lc.innerArea.Min.X + 1 + lc.labelYSpace
|
||||
lc.drawingY = lc.innerArea.Min.Y
|
||||
}
|
||||
|
||||
func (lc *LineChart) plotAxes() []Point {
|
||||
origY := lc.innerY + lc.innerHeight - 2
|
||||
origX := lc.innerX + lc.labelYSpace
|
||||
func (lc *LineChart) plotAxes() Buffer {
|
||||
buf := NewBuffer()
|
||||
|
||||
ps := []Point{newPointWithAttrs(ORIGIN, origX, origY, lc.AxesColor, lc.BgColor)}
|
||||
origY := lc.innerArea.Min.Y + lc.innerArea.Dy() - 2
|
||||
origX := lc.innerArea.Min.X + lc.labelYSpace
|
||||
|
||||
buf.Set(origX, origY, Cell{Ch: ORIGIN, Fg: lc.AxesColor, Bg: lc.Bg})
|
||||
|
||||
for x := origX + 1; x < origX+lc.axisXWidth; x++ {
|
||||
p := Point{}
|
||||
p.X = x
|
||||
p.Y = origY
|
||||
p.Bg = lc.BgColor
|
||||
p.Fg = lc.AxesColor
|
||||
p.Ch = HDASH
|
||||
ps = append(ps, p)
|
||||
buf.Set(x, origY, Cell{Ch: HDASH, Fg: lc.AxesColor, Bg: lc.Bg})
|
||||
}
|
||||
|
||||
for dy := 1; dy <= lc.axisYHeight; dy++ {
|
||||
p := Point{}
|
||||
p.X = origX
|
||||
p.Y = origY - dy
|
||||
p.Bg = lc.BgColor
|
||||
p.Fg = lc.AxesColor
|
||||
p.Ch = VDASH
|
||||
ps = append(ps, p)
|
||||
buf.Set(origX, origY-dy, Cell{Ch: VDASH, Fg: lc.AxesColor, Bg: lc.Bg})
|
||||
}
|
||||
|
||||
// x label
|
||||
@ -292,13 +286,14 @@ func (lc *LineChart) plotAxes() []Point {
|
||||
break
|
||||
}
|
||||
for j, r := range rs {
|
||||
p := Point{}
|
||||
p.Ch = r
|
||||
p.Fg = lc.AxesColor
|
||||
p.Bg = lc.BgColor
|
||||
p.X = origX + oft + j
|
||||
p.Y = lc.innerY + lc.innerHeight - 1
|
||||
ps = append(ps, p)
|
||||
c := Cell{
|
||||
Ch: r,
|
||||
Fg: lc.AxesColor,
|
||||
Bg: lc.BgColor,
|
||||
}
|
||||
x := origX + oft + j
|
||||
y := lc.innerArea.Min.Y + lc.innerArea.Dy() - 1
|
||||
buf.Set(x, y, c)
|
||||
}
|
||||
oft += len(rs) + lc.axisXLebelGap
|
||||
}
|
||||
@ -306,33 +301,31 @@ func (lc *LineChart) plotAxes() []Point {
|
||||
// y labels
|
||||
for i, rs := range lc.labelY {
|
||||
for j, r := range rs {
|
||||
p := Point{}
|
||||
p.Ch = r
|
||||
p.Fg = lc.AxesColor
|
||||
p.Bg = lc.BgColor
|
||||
p.X = lc.innerX + j
|
||||
p.Y = origY - i*(lc.axisYLebelGap+1)
|
||||
ps = append(ps, p)
|
||||
buf.Set(
|
||||
lc.innerArea.Min.X+j,
|
||||
origY-i*(lc.axisYLebelGap+1),
|
||||
Cell{Ch: r, Fg: lc.AxesColor, Bg: lc.Bg})
|
||||
}
|
||||
}
|
||||
|
||||
return ps
|
||||
return buf
|
||||
}
|
||||
|
||||
// Buffer implements Bufferer interface.
|
||||
func (lc *LineChart) Buffer() []Point {
|
||||
ps := lc.Block.Buffer()
|
||||
buf := lc.Block.Buffer()
|
||||
|
||||
if lc.Data == nil || len(lc.Data) == 0 {
|
||||
return ps
|
||||
}
|
||||
lc.calcLayout()
|
||||
ps = append(ps, lc.plotAxes()...)
|
||||
buf.Merge(lc.plotAxes())
|
||||
|
||||
if lc.Mode == "dot" {
|
||||
ps = append(ps, lc.renderDot()...)
|
||||
buf.Merge(lc.renderDot())
|
||||
} else {
|
||||
ps = append(ps, lc.renderBraille()...)
|
||||
buf.Merge(ps, lc.renderBraille())
|
||||
}
|
||||
|
||||
return lc.Block.chopOverflow(ps)
|
||||
return buf
|
||||
}
|
@ -53,7 +53,7 @@ func (l *List) Buffer() []Point {
|
||||
buffer := l.Block.Buffer()
|
||||
|
||||
breakLoop := func(y int) bool {
|
||||
return y+1 > l.innerHeight
|
||||
return y+1 > l.innerArea.Dy()
|
||||
}
|
||||
y := 0
|
||||
|
||||
@ -65,9 +65,9 @@ MainLoop:
|
||||
sequence := renderer.Render(bg, fg)
|
||||
|
||||
for n := range []rune(sequence.NormalizedText) {
|
||||
point, width := sequence.PointAt(n, x+l.innerX, y+l.innerY)
|
||||
point, width := sequence.PointAt(n, x+l.innerArea.Min.X, y+l.innerArea.Min.Y)
|
||||
|
||||
if width+x <= l.innerWidth {
|
||||
if width+x <= l.innerArea.Dx() {
|
||||
buffer = append(buffer, point)
|
||||
x += width
|
||||
} else {
|
||||
@ -79,8 +79,8 @@ MainLoop:
|
||||
x = 0
|
||||
} else {
|
||||
dotR := []rune(dot)[0]
|
||||
dotX := l.innerWidth + l.innerX - charWidth(dotR)
|
||||
p := newPointWithAttrs(dotR, dotX, y+l.innerY, bg, fg)
|
||||
dotX := l.innerArea.Dx() + l.innerArea.Min.X - charWidth(dotR)
|
||||
p := newPointWithAttrs(dotR, dotX, y+l.innerArea.Min.Y, bg, fg)
|
||||
buffer = append(buffer, p)
|
||||
break
|
||||
}
|
@ -48,16 +48,16 @@ type MBarChart struct {
|
||||
// NewBarChart returns a new *BarChart with current theme.
|
||||
func NewMBarChart() *MBarChart {
|
||||
bc := &MBarChart{Block: *NewBlock()}
|
||||
bc.BarColor[0] = theme.MBarChartBar
|
||||
bc.NumColor[0] = theme.MBarChartNum
|
||||
bc.TextColor = theme.MBarChartText
|
||||
bc.BarColor[0] = ThemeAttr("mbarchart.bar.bg")
|
||||
bc.NumColor[0] = ThemeAttr("mbarchart.num.fg")
|
||||
bc.TextColor = ThemeAttr("mbarchart.text.fg")
|
||||
bc.BarGap = 1
|
||||
bc.BarWidth = 3
|
||||
return bc
|
||||
}
|
||||
|
||||
func (bc *MBarChart) layout() {
|
||||
bc.numBar = bc.innerWidth / (bc.BarGap + bc.BarWidth)
|
||||
bc.numBar = bc.innerArea.Dx() / (bc.BarGap + bc.BarWidth)
|
||||
bc.labels = make([][]rune, bc.numBar)
|
||||
DataLen := 0
|
||||
LabelLen := len(bc.DataLabels)
|
||||
@ -129,9 +129,9 @@ func (bc *MBarChart) layout() {
|
||||
if bc.ShowScale {
|
||||
s := fmt.Sprintf("%d", bc.max)
|
||||
bc.maxScale = trimStr2Runes(s, len(s))
|
||||
bc.scale = float64(bc.max) / float64(bc.innerHeight-2)
|
||||
bc.scale = float64(bc.max) / float64(bc.innerArea.Dy()-2)
|
||||
} else {
|
||||
bc.scale = float64(bc.max) / float64(bc.innerHeight-1)
|
||||
bc.scale = float64(bc.max) / float64(bc.innerArea.Dy()-1)
|
||||
}
|
||||
|
||||
}
|
||||
@ -144,8 +144,8 @@ func (bc *MBarChart) SetMax(max int) {
|
||||
}
|
||||
|
||||
// Buffer implements Bufferer interface.
|
||||
func (bc *MBarChart) Buffer() []Point {
|
||||
ps := bc.Block.Buffer()
|
||||
func (bc *MBarChart) Buffer() Buffer {
|
||||
buf := bc.Block.Buffer()
|
||||
bc.layout()
|
||||
var oftX int
|
||||
|
||||
@ -157,15 +157,17 @@ func (bc *MBarChart) Buffer() []Point {
|
||||
// plot bars
|
||||
for j := 0; j < bc.BarWidth; j++ {
|
||||
for k := 0; k < h; k++ {
|
||||
p := Point{}
|
||||
p.Ch = ' '
|
||||
p.Bg = bc.BarColor[i1]
|
||||
if bc.BarColor[i1] == ColorDefault { // when color is default, space char treated as transparent!
|
||||
p.Bg |= AttrReverse
|
||||
c := Cell{
|
||||
Ch: ' ',
|
||||
Bg: bc.BarColor[i1],
|
||||
}
|
||||
p.X = bc.innerX + i*(bc.BarWidth+bc.BarGap) + j
|
||||
p.Y = bc.innerY + bc.innerHeight - 2 - k - ph
|
||||
ps = append(ps, p)
|
||||
if bc.BarColor[i1] == ColorDefault { // when color is default, space char treated as transparent!
|
||||
c.Bg |= AttrReverse
|
||||
}
|
||||
x := bc.innerArea.Min.X + i*(bc.BarWidth+bc.BarGap) + j
|
||||
y := bc.innerArea.Min.Y + bc.innerArea.Dy() - 2 - k - ph
|
||||
buf.Set(x, y, c)
|
||||
|
||||
}
|
||||
}
|
||||
ph += h
|
||||
@ -173,13 +175,14 @@ func (bc *MBarChart) Buffer() []Point {
|
||||
// plot text
|
||||
for j, k := 0, 0; j < len(bc.labels[i]); j++ {
|
||||
w := charWidth(bc.labels[i][j])
|
||||
p := Point{}
|
||||
p.Ch = bc.labels[i][j]
|
||||
p.Bg = bc.BgColor
|
||||
p.Fg = bc.TextColor
|
||||
p.Y = bc.innerY + bc.innerHeight - 1
|
||||
p.X = bc.innerX + oftX + ((bc.BarWidth - len(bc.labels[i])) / 2) + k
|
||||
ps = append(ps, p)
|
||||
c := Cell{
|
||||
Ch: bc.labels[i][j],
|
||||
Bg: bc.Bg,
|
||||
Fg: bc.TextColor,
|
||||
}
|
||||
y := bc.innerArea.Min.Y + bc.innerArea.Dy() - 1
|
||||
x := bc.innerArea.Max.X + oftX + ((bc.BarWidth - len(bc.labels[i])) / 2) + k
|
||||
buf.Set(x, y, c)
|
||||
k += w
|
||||
}
|
||||
// plot num
|
||||
@ -187,19 +190,20 @@ func (bc *MBarChart) Buffer() []Point {
|
||||
for i1 := 0; i1 < bc.numStack; i1++ {
|
||||
h := int(float64(bc.Data[i1][i]) / bc.scale)
|
||||
for j := 0; j < len(bc.dataNum[i1][i]) && h > 0; j++ {
|
||||
p := Point{}
|
||||
p.Ch = bc.dataNum[i1][i][j]
|
||||
p.Fg = bc.NumColor[i1]
|
||||
p.Bg = bc.BarColor[i1]
|
||||
c := Cell{
|
||||
Ch: bc.dataNum[i1][i][j],
|
||||
Fg: bc.NumColor[i1],
|
||||
Bg: bc.BarColor[i1],
|
||||
}
|
||||
if bc.BarColor[i1] == ColorDefault { // the same as above
|
||||
p.Bg |= AttrReverse
|
||||
c.Bg |= AttrReverse
|
||||
}
|
||||
if h == 0 {
|
||||
p.Bg = bc.BgColor
|
||||
c.Bg = bc.Bg
|
||||
}
|
||||
p.X = bc.innerX + oftX + (bc.BarWidth-len(bc.dataNum[i1][i]))/2 + j
|
||||
p.Y = bc.innerY + bc.innerHeight - 2 - ph
|
||||
ps = append(ps, p)
|
||||
x := bc.innerArea.Min.X + oftX + (bc.BarWidth-len(bc.dataNum[i1][i]))/2 + j
|
||||
y := bc.innerArea.Min.Y + bc.innerArea.Dy() - 2 - ph
|
||||
buf.Set(x, y, c)
|
||||
}
|
||||
ph += h
|
||||
}
|
||||
@ -208,26 +212,31 @@ func (bc *MBarChart) Buffer() []Point {
|
||||
if bc.ShowScale {
|
||||
//Currently bar graph only supprts data range from 0 to MAX
|
||||
//Plot 0
|
||||
p := Point{}
|
||||
p.Ch = '0'
|
||||
p.Bg = bc.BgColor
|
||||
p.Fg = bc.TextColor
|
||||
p.Y = bc.innerY + bc.innerHeight - 2
|
||||
p.X = bc.X
|
||||
ps = append(ps, p)
|
||||
c := Cell{
|
||||
Ch: '0',
|
||||
Bg: bc.Bg,
|
||||
Fg: bc.TextColor,
|
||||
}
|
||||
|
||||
y := bc.innerArea.Min.Y + bc.innerArea.Dy() - 2
|
||||
x := bc.X
|
||||
buf.Set(x, y, c)
|
||||
|
||||
//Plot the maximum sacle value
|
||||
for i := 0; i < len(bc.maxScale); i++ {
|
||||
p := Point{}
|
||||
p.Ch = bc.maxScale[i]
|
||||
p.Bg = bc.BgColor
|
||||
p.Fg = bc.TextColor
|
||||
p.Y = bc.innerY
|
||||
p.X = bc.X + i
|
||||
ps = append(ps, p)
|
||||
c := Cell{
|
||||
Ch: bc.maxScale[i],
|
||||
Bg: bc.Bg,
|
||||
Fg: bc.TextColor,
|
||||
}
|
||||
|
||||
y := bc.innerArea.Min.Y
|
||||
x := bc.X + i
|
||||
|
||||
buf.Set(x, y, c)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return bc.Block.chopOverflow(ps)
|
||||
return buf
|
||||
}
|
@ -41,20 +41,20 @@ func (p *Par) Buffer() []Point {
|
||||
runes := []rune(sequence.NormalizedText)
|
||||
|
||||
y, x, n := 0, 0, 0
|
||||
for y < p.innerHeight && n < len(runes) {
|
||||
point, width := sequence.PointAt(n, x+p.innerX, y+p.innerY)
|
||||
for y < p.innerArea.Dy() && n < len(runes) {
|
||||
point, width := sequence.PointAt(n, x+p.innerArea.Min.X, y+p.innerArea.Min.Y)
|
||||
|
||||
if runes[n] == '\n' || x+width > p.innerWidth {
|
||||
if runes[n] == '\n' || x+width > p.innerArea.Dx() {
|
||||
y++
|
||||
x = 0 // set x = 0
|
||||
if runes[n] == '\n' {
|
||||
n++
|
||||
}
|
||||
|
||||
if y >= p.innerHeight {
|
||||
if y >= p.innerArea.Dy() {
|
||||
ps = append(ps, newPointWithAttrs('…',
|
||||
p.innerX+p.innerWidth-1,
|
||||
p.innerY+p.innerHeight-1,
|
||||
p.innerArea.Min.X+p.innerArea.Dx()-1,
|
||||
p.innerArea.Min.Y+p.innerArea.Dy()-1,
|
||||
p.TextFgColor, p.TextBgColor))
|
||||
break
|
||||
}
|
@ -35,7 +35,13 @@ func Init() error {
|
||||
DefaultEvtStream.Merge("termbox", NewSysEvtCh())
|
||||
DefaultEvtStream.Merge("timer", NewTimerCh(time.Second))
|
||||
DefaultEvtStream.Handle("/", DefualtHandler)
|
||||
DefaultEvtStream.Handle("/sys/wnd/resize", func(e Event) {
|
||||
w := e.Data.(EvtWnd)
|
||||
Body.Width = w.Width
|
||||
})
|
||||
|
||||
DefaultWgtMgr = NewWgtMgr()
|
||||
DefaultEvtStream.Hook(DefaultWgtMgr.WgtHandlersHook())
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -69,13 +69,13 @@ func (sl *Sparklines) update() {
|
||||
sl.Lines[i].displayHeight = v.Height + 1
|
||||
}
|
||||
}
|
||||
sl.displayWidth = sl.innerWidth
|
||||
sl.displayWidth = sl.innerArea.Dx()
|
||||
|
||||
// get how many lines gotta display
|
||||
h := 0
|
||||
sl.displayLines = 0
|
||||
for _, v := range sl.Lines {
|
||||
if h+v.displayHeight <= sl.innerHeight {
|
||||
if h+v.displayHeight <= sl.innerArea.Dy() {
|
||||
sl.displayLines++
|
||||
} else {
|
||||
break
|
||||
@ -107,21 +107,21 @@ func (sl *Sparklines) Buffer() []Point {
|
||||
l := sl.Lines[i]
|
||||
data := l.Data
|
||||
|
||||
if len(data) > sl.innerWidth {
|
||||
data = data[len(data)-sl.innerWidth:]
|
||||
if len(data) > sl.innerArea.Dx() {
|
||||
data = data[len(data)-sl.innerArea.Dx():]
|
||||
}
|
||||
|
||||
if l.Title != "" {
|
||||
rs := trimStr2Runes(l.Title, sl.innerWidth)
|
||||
rs := trimStr2Runes(l.Title, sl.innerArea.Dx())
|
||||
oftX := 0
|
||||
for _, v := range rs {
|
||||
w := charWidth(v)
|
||||
p := Point{}
|
||||
c := Cell{}
|
||||
p.Ch = v
|
||||
p.Fg = l.TitleColor
|
||||
p.Bg = sl.BgColor
|
||||
p.X = sl.innerX + oftX
|
||||
p.Y = sl.innerY + oftY
|
||||
p.X = sl.innerArea.Min.X + oftX
|
||||
p.Y = sl.innerArea.Min.Y + oftY
|
||||
ps = append(ps, p)
|
||||
oftX += w
|
||||
}
|
||||
@ -132,18 +132,18 @@ func (sl *Sparklines) Buffer() []Point {
|
||||
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
|
||||
c := Cell{}
|
||||
p.X = sl.innerArea.Min.X + j
|
||||
p.Y = sl.innerArea.Min.Y + oftY + l.Height - jj
|
||||
p.Ch = ' ' // => sparks[7]
|
||||
p.Bg = l.LineColor
|
||||
//p.Bg = sl.BgColor
|
||||
ps = append(ps, p)
|
||||
}
|
||||
if barMod != 0 {
|
||||
p := Point{}
|
||||
p.X = sl.innerX + j
|
||||
p.Y = sl.innerY + oftY + l.Height - barCnt
|
||||
c := Cell{}
|
||||
p.X = sl.innerArea.Min.X + j
|
||||
p.Y = sl.innerArea.Min.Y + oftY + l.Height - barCnt
|
||||
p.Ch = sparks[barMod-1]
|
||||
p.Fg = l.LineColor
|
||||
p.Bg = sl.BgColor
|
82
widget.go
Normal file
82
widget.go
Normal file
@ -0,0 +1,82 @@
|
||||
package termui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// event mixins
|
||||
type WgtMgr map[string]WgtInfo
|
||||
|
||||
type WgtInfo struct {
|
||||
Handlers map[string]func(Event)
|
||||
WgtRef Widget
|
||||
Id string
|
||||
}
|
||||
|
||||
type Widget interface {
|
||||
Id() string
|
||||
}
|
||||
|
||||
func NewWgtInfo(wgt Widget) WgtInfo {
|
||||
return WgtInfo{
|
||||
Handlers: make(map[string]func(Event)),
|
||||
WgtRef: wgt,
|
||||
Id: wgt.Id(),
|
||||
}
|
||||
}
|
||||
|
||||
func NewWgtMgr() WgtMgr {
|
||||
wm := WgtMgr(make(map[string]WgtInfo))
|
||||
return wm
|
||||
|
||||
}
|
||||
|
||||
func (wm WgtMgr) AddWgt(wgt Widget) {
|
||||
wm[wgt.Id()] = NewWgtInfo(wgt)
|
||||
}
|
||||
|
||||
func (wm WgtMgr) RmWgt(wgt Widget) {
|
||||
wm.RmWgtById(wgt.Id())
|
||||
}
|
||||
|
||||
func (wm WgtMgr) RmWgtById(id string) {
|
||||
delete(wm, id)
|
||||
}
|
||||
|
||||
func (wm WgtMgr) AddWgtHandler(id, path string, h func(Event)) {
|
||||
if w, ok := wm[id]; ok {
|
||||
w.Handlers[path] = h
|
||||
}
|
||||
}
|
||||
|
||||
func (wm WgtMgr) RmWgtHandler(id, path string) {
|
||||
if w, ok := wm[id]; ok {
|
||||
delete(w.Handlers, path)
|
||||
}
|
||||
}
|
||||
|
||||
var counter struct {
|
||||
sync.RWMutex
|
||||
count int
|
||||
}
|
||||
|
||||
func GenId() string {
|
||||
counter.Lock()
|
||||
defer counter.Unlock()
|
||||
|
||||
counter.count += 1
|
||||
return fmt.Sprintf("%d", counter.count)
|
||||
}
|
||||
|
||||
func (wm WgtMgr) WgtHandlersHook() func(Event) {
|
||||
return func(e Event) {
|
||||
for _, v := range wm {
|
||||
if k := findMatch(v.Handlers, e.Path); k != "" {
|
||||
v.Handlers[k](e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var DefaultWgtMgr WgtMgr
|
Loading…
Reference in New Issue
Block a user