1
0
mirror of https://github.com/gizak/termui.git synced 2025-04-24 13:48:50 +08:00

Merge 06a0332374fb07446b341923ec7b293737338f00 into 4cca61d83fa2cc0f485c478ff768b0108f6591d6

This commit is contained in:
Ayooluwa 2020-08-25 19:03:05 +00:00 committed by GitHub
commit aaf15789f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,6 +6,7 @@ package widgets
import (
"image"
"math"
. "github.com/gizak/termui/v3"
)
@ -21,13 +22,16 @@ import (
*/
type Table struct {
Block
Rows [][]string
ColumnWidths []int
TextStyle Style
RowSeparator bool
TextAlignment Alignment
RowStyles map[int]Style
FillRow bool
Rows [][]string
ColumnWidths []int
TextStyle Style
RowSeparator bool
TextAlignment Alignment
RowStyles map[int]Style
FillRow bool
SelectedRow int
topRow int
SelectedRowStyle Style
// ColumnResizer is called on each Draw. Can be used for custom column sizing.
ColumnResizer func()
@ -35,11 +39,14 @@ type Table struct {
func NewTable() *Table {
return &Table{
Block: *NewBlock(),
TextStyle: Theme.Table.Text,
RowSeparator: true,
RowStyles: make(map[int]Style),
ColumnResizer: func() {},
Block: *NewBlock(),
TextStyle: Theme.Table.Text,
RowSeparator: true,
RowStyles: make(map[int]Style),
SelectedRow: 1,
topRow: 1,
SelectedRowStyle: NewStyle(ColorBlack, ColorBlue),
ColumnResizer: func() {},
}
}
@ -59,8 +66,33 @@ func (self *Table) Draw(buf *Buffer) {
yCoordinate := self.Inner.Min.Y
if self.RowSeparator {
viewHeight := int(math.Floor(float64(self.Inner.Dy() / 2)))
if self.Inner.Dy()%2 == 0 {
viewHeight -= 1
}
if self.SelectedRow >= viewHeight+self.topRow {
self.topRow = (self.SelectedRow - viewHeight) + 1
} else if self.SelectedRow < self.topRow {
self.topRow = self.SelectedRow
}
} else {
viewHeight := self.Inner.Dy()
if self.SelectedRow >= viewHeight+self.topRow-1 {
self.topRow = (self.SelectedRow - viewHeight) + 2
} else if self.SelectedRow < self.topRow {
self.topRow = self.SelectedRow
}
}
// draw rows
for i := 0; i < len(self.Rows) && yCoordinate < self.Inner.Max.Y; i++ {
if i != 0 && i < self.topRow {
continue
}
row := self.Rows[i]
colXCoordinate := self.Inner.Min.X
@ -70,6 +102,10 @@ func (self *Table) Draw(buf *Buffer) {
rowStyle = style
}
if i == self.SelectedRow {
rowStyle = self.SelectedRowStyle
}
if self.FillRow {
blankCell := NewCell(' ', rowStyle)
buf.Fill(blankCell, image.Rect(self.Inner.Min.X, yCoordinate, self.Inner.Max.X, yCoordinate+1))
@ -134,3 +170,49 @@ func (self *Table) Draw(buf *Buffer) {
}
}
}
func (self *Table) ScrollAmount(amount int) {
if len(self.Rows)-int(self.SelectedRow) <= amount {
self.SelectedRow = len(self.Rows) - 1
} else if int(self.SelectedRow)+amount < 1 {
self.SelectedRow = 1
} else {
self.SelectedRow += amount
}
}
func (self *Table) ScrollUp() {
self.ScrollAmount(-1)
}
func (self *Table) ScrollDown() {
self.ScrollAmount(1)
}
func (self *Table) ScrollTop() {
self.SelectedRow = 1
}
func (self *Table) ScrollBottom() {
self.SelectedRow = len(self.Rows) - 1
}
func (self *Table) ScrollPageUp() {
if self.SelectedRow > self.topRow {
self.SelectedRow = self.topRow
} else {
self.ScrollAmount(-self.Inner.Dy())
}
}
func (self *Table) ScrollPageDown() {
self.ScrollAmount(self.Inner.Dy())
}
func (self *Table) ScrollHalfPageUp() {
self.ScrollAmount(-int(FloorFloat64(float64(self.Inner.Dy()) / 2)))
}
func (self *Table) ScrollHalfPageDown() {
self.ScrollAmount(int(FloorFloat64(float64(self.Inner.Dy()) / 2)))
}