gotop/termui/block.go

99 lines
2.7 KiB
Go
Raw Normal View History

2018-02-19 15:25:02 +08:00
package termui
2018-02-19 15:51:55 +08:00
import (
"image"
)
2018-02-19 15:25:02 +08:00
2018-02-23 16:42:39 +08:00
// Block is a base struct for all other upper level widgets.
2018-02-19 15:25:02 +08:00
type Block struct {
Grid image.Rectangle
2018-02-23 16:42:39 +08:00
X int // largest X value in the inner square
Y int // largest Y value in the inner square
XOffset int // the X position of the widget on the terminal
YOffset int // the Y position of the widget on the terminal
2018-02-19 15:25:02 +08:00
Label string
2018-02-21 16:46:11 +08:00
BorderFg Color
BorderBg Color
LabelFg Color
LabelBg Color
Fg Color
2018-02-21 18:24:36 +08:00
Bg Color
2018-02-19 15:25:02 +08:00
}
2018-02-23 16:42:39 +08:00
// NewBlock returns a *Block which inherits styles from the current theme.
2018-02-19 15:25:02 +08:00
func NewBlock() *Block {
return &Block{
Fg: Theme.Fg,
Bg: Theme.Bg,
BorderFg: Theme.BorderFg,
BorderBg: Theme.BorderBg,
LabelFg: Theme.LabelFg,
LabelBg: Theme.LabelBg,
}
}
func (b *Block) drawBorder(buf *Buffer) {
x := b.X + 1
y := b.Y + 1
// draw lines
buf.Merge(NewFilledBuffer(0, 0, x, 1, Cell{HORIZONTAL_LINE, b.BorderFg, b.BorderBg}))
buf.Merge(NewFilledBuffer(0, y, x, y+1, Cell{HORIZONTAL_LINE, b.BorderFg, b.BorderBg}))
buf.Merge(NewFilledBuffer(0, 0, 1, y+1, Cell{VERTICAL_LINE, b.BorderFg, b.BorderBg}))
buf.Merge(NewFilledBuffer(x, 0, x+1, y+1, Cell{VERTICAL_LINE, b.BorderFg, b.BorderBg}))
// draw corners
buf.SetCell(0, 0, Cell{TOP_LEFT, b.BorderFg, b.BorderBg})
buf.SetCell(x, 0, Cell{TOP_RIGHT, b.BorderFg, b.BorderBg})
buf.SetCell(0, y, Cell{BOTTOM_LEFT, b.BorderFg, b.BorderBg})
buf.SetCell(x, y, Cell{BOTTOM_RIGHT, b.BorderFg, b.BorderBg})
}
func (b *Block) drawLabel(buf *Buffer) {
r := MaxString(b.Label, (b.X-3)-1)
buf.SetString(3, 0, r, b.LabelFg, b.LabelBg)
if b.Label == "" {
return
}
c := Cell{' ', b.Fg, b.Bg}
buf.SetCell(2, 0, c)
if len(b.Label)+3 < b.X {
buf.SetCell(len(b.Label)+3, 0, c)
} else {
buf.SetCell(b.X-1, 0, c)
}
}
2018-02-23 16:42:39 +08:00
// Resize computes Height, Width, XOffset, and YOffset given terminal dimensions.
2018-02-19 15:25:02 +08:00
func (b *Block) Resize(termWidth, termHeight, termCols, termRows int) {
b.X = int((float64(b.Grid.Dx())/float64(termCols))*float64(termWidth)) - 2
b.Y = int((float64(b.Grid.Dy())/float64(termRows))*float64(termHeight)) - 2
b.XOffset = int(((float64(b.Grid.Min.X) / float64(termCols)) * float64(termWidth)))
b.YOffset = int(((float64(b.Grid.Min.Y) / float64(termRows)) * float64(termHeight)))
}
2018-02-23 16:42:39 +08:00
// SetGrid create a rectangle representing the block's dimensions in the grid.
2018-02-19 15:25:02 +08:00
func (b *Block) SetGrid(c0, r0, c1, r1 int) {
b.Grid = image.Rect(c0, r0, c1, r1)
}
func (b *Block) GetXOffset() int {
return b.XOffset
}
func (b *Block) GetYOffset() int {
return b.YOffset
}
2018-02-23 16:42:39 +08:00
// Buffer implements Bufferer interface and draws background, border, and borderlabel.
2018-02-19 15:25:02 +08:00
func (b *Block) Buffer() *Buffer {
buf := NewBuffer()
buf.SetAreaXY(b.X+2, b.Y+2)
buf.Fill(Cell{' ', ColorDefault, b.Bg})
b.drawBorder(buf)
b.drawLabel(buf)
return buf
}