termui/widgets/list.go

137 lines
3.3 KiB
Go
Raw Normal View History

2019-01-23 21:12:10 -07:00
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
// Use of this source code is governed by a MIT license that can
// be found in the LICENSE file.
package widgets
import (
"image"
rw "github.com/mattn/go-runewidth"
2019-02-22 13:44:48 -07:00
2019-03-07 03:50:20 -07:00
. "github.com/gizak/termui/v3"
2019-01-23 21:12:10 -07:00
)
type List struct {
Block
2019-01-26 23:32:08 -07:00
Rows []string
WrapText bool
TextStyle Style
2019-02-23 18:15:42 -07:00
SelectedRow int
topRow int
2019-01-26 23:32:08 -07:00
SelectedRowStyle Style
2019-01-23 21:12:10 -07:00
}
func NewList() *List {
return &List{
2019-01-26 23:32:08 -07:00
Block: *NewBlock(),
TextStyle: Theme.List.Text,
SelectedRowStyle: Theme.List.Text,
2019-01-23 21:12:10 -07:00
}
}
func (self *List) Draw(buf *Buffer) {
self.Block.Draw(buf)
point := self.Inner.Min
2019-03-01 14:28:09 -07:00
// adjusts view into widget
if self.SelectedRow >= self.Inner.Dy()+self.topRow {
self.topRow = self.SelectedRow - self.Inner.Dy() + 1
} else if self.SelectedRow < self.topRow {
self.topRow = self.SelectedRow
}
2019-03-01 15:00:31 -07:00
// draw rows
2019-02-23 18:15:42 -07:00
for row := self.topRow; row < len(self.Rows) && point.Y < self.Inner.Max.Y; row++ {
2019-02-23 17:54:20 -07:00
cells := ParseStyles(self.Rows[row], self.TextStyle)
2019-01-26 23:32:08 -07:00
if self.WrapText {
2019-01-23 21:12:10 -07:00
cells = WrapCells(cells, uint(self.Inner.Dx()))
}
for j := 0; j < len(cells) && point.Y < self.Inner.Max.Y; j++ {
2019-01-26 23:32:08 -07:00
style := cells[j].Style
if row == self.SelectedRow {
style = self.SelectedRowStyle
}
2019-01-23 21:12:10 -07:00
if cells[j].Rune == '\n' {
point = image.Pt(self.Inner.Min.X, point.Y+1)
} else {
if point.X+1 == self.Inner.Max.X+1 && len(cells) > self.Inner.Dx() {
2019-01-26 23:32:08 -07:00
buf.SetCell(NewCell(ELLIPSES, style), point.Add(image.Pt(-1, 0)))
2019-01-23 21:12:10 -07:00
break
} else {
2019-01-26 23:32:08 -07:00
buf.SetCell(NewCell(cells[j].Rune, style), point)
point = point.Add(image.Pt(rw.RuneWidth(cells[j].Rune), 0))
2019-01-23 21:12:10 -07:00
}
}
}
point = image.Pt(self.Inner.Min.X, point.Y+1)
}
2019-01-26 23:32:08 -07:00
2019-03-01 15:00:31 -07:00
// draw UP_ARROW if needed
2019-01-26 23:32:08 -07:00
if self.topRow > 0 {
buf.SetCell(
NewCell(UP_ARROW, NewStyle(ColorWhite)),
image.Pt(self.Inner.Max.X-1, self.Inner.Min.Y),
)
}
2019-03-01 15:00:31 -07:00
// draw DOWN_ARROW if needed
2019-01-26 23:32:08 -07:00
if len(self.Rows) > int(self.topRow)+self.Inner.Dy() {
buf.SetCell(
NewCell(DOWN_ARROW, NewStyle(ColorWhite)),
image.Pt(self.Inner.Max.X-1, self.Inner.Max.Y-1),
)
}
}
// ScrollAmount scrolls by amount given. If amount is < 0, then scroll up.
// There is no need to set self.topRow, as this will be set automatically when drawn,
// since if the selected item is off screen then the topRow variable will change accordingly.
func (self *List) ScrollAmount(amount int) {
if len(self.Rows)-int(self.SelectedRow) <= amount {
2019-02-23 18:15:42 -07:00
self.SelectedRow = len(self.Rows) - 1
} else if int(self.SelectedRow)+amount < 0 {
self.SelectedRow = 0
} else {
2019-02-23 18:15:42 -07:00
self.SelectedRow += amount
}
}
func (self *List) ScrollUp() {
self.ScrollAmount(-1)
}
func (self *List) ScrollDown() {
self.ScrollAmount(1)
}
2019-03-01 15:00:31 -07:00
func (self *List) ScrollPageUp() {
// If an item is selected below top row, then go to the top row.
if self.SelectedRow > self.topRow {
self.SelectedRow = self.topRow
2019-01-31 20:15:11 -07:00
} else {
self.ScrollAmount(-self.Inner.Dy())
}
}
2019-03-01 15:00:31 -07:00
func (self *List) ScrollPageDown() {
self.ScrollAmount(self.Inner.Dy())
}
2019-03-01 15:00:31 -07:00
func (self *List) ScrollHalfPageUp() {
self.ScrollAmount(-int(FloorFloat64(float64(self.Inner.Dy()) / 2)))
}
2019-03-01 15:00:31 -07:00
func (self *List) ScrollHalfPageDown() {
self.ScrollAmount(int(FloorFloat64(float64(self.Inner.Dy()) / 2)))
}
func (self *List) ScrollTop() {
self.SelectedRow = 0
}
func (self *List) ScrollBottom() {
2019-02-23 18:15:42 -07:00
self.SelectedRow = len(self.Rows) - 1
}