From c9896e20c6f73a3133490d4bce4032ad6bad1696 Mon Sep 17 00:00:00 2001 From: Ayooluwa Isaiah Date: Fri, 10 Apr 2020 14:25:51 +0100 Subject: [PATCH 1/3] Add basic support for table scrolling --- v3/widgets/table.go | 78 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 66 insertions(+), 12 deletions(-) diff --git a/v3/widgets/table.go b/v3/widgets/table.go index 05391ad5..00823771 100644 --- a/v3/widgets/table.go +++ b/v3/widgets/table.go @@ -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,21 @@ 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) +} From cc62c7257cd41fd71c1997e9aed7d70fd4dca3fa Mon Sep 17 00:00:00 2001 From: Ayooluwa Isaiah Date: Tue, 25 Aug 2020 13:48:16 +0100 Subject: [PATCH 2/3] Add support for scrolling to top and buttom --- v3/widgets/table.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/v3/widgets/table.go b/v3/widgets/table.go index 00823771..355996e8 100644 --- a/v3/widgets/table.go +++ b/v3/widgets/table.go @@ -188,3 +188,11 @@ func (self *Table) ScrollUp() { func (self *Table) ScrollDown() { self.ScrollAmount(1) } + +func (self *Table) ScrollFirst() { + self.SelectedRow = 1 +} + +func (self *Table) ScrollLast() { + self.SelectedRow = len(self.Rows) - 1 +} From 06a0332374fb07446b341923ec7b293737338f00 Mon Sep 17 00:00:00 2001 From: Ayooluwa Isaiah Date: Tue, 25 Aug 2020 20:02:01 +0100 Subject: [PATCH 3/3] More table scrolling options --- v3/widgets/table.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/v3/widgets/table.go b/v3/widgets/table.go index 355996e8..f5fde856 100644 --- a/v3/widgets/table.go +++ b/v3/widgets/table.go @@ -189,10 +189,30 @@ func (self *Table) ScrollDown() { self.ScrollAmount(1) } -func (self *Table) ScrollFirst() { +func (self *Table) ScrollTop() { self.SelectedRow = 1 } -func (self *Table) ScrollLast() { +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))) +}