mirror of https://github.com/rivo/tview.git
Expanded the TableCell definition in preparation of adding individual cell background colors.
This commit is contained in:
parent
4c0e74ada0
commit
b423641ec2
|
@ -21,11 +21,10 @@ func main() {
|
||||||
if c < 1 || r < 1 {
|
if c < 1 || r < 1 {
|
||||||
color = tcell.ColorYellow
|
color = tcell.ColorYellow
|
||||||
}
|
}
|
||||||
table.SetCell(r, c, &tview.TableCell{
|
table.SetCell(r, c,
|
||||||
Text: lorem[word],
|
tview.NewTableCell(lorem[word]).
|
||||||
Color: color,
|
SetTextColor(color).
|
||||||
Align: tview.AlignCenter,
|
SetAlign(tview.AlignCenter))
|
||||||
})
|
|
||||||
word = (word + 1) % len(lorem)
|
word = (word + 1) % len(lorem)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,8 +36,7 @@ func main() {
|
||||||
table.SetSelectable(true, true)
|
table.SetSelectable(true, true)
|
||||||
}
|
}
|
||||||
}).SetSelectedFunc(func(row int, column int) {
|
}).SetSelectedFunc(func(row int, column int) {
|
||||||
cell := table.GetCell(row, column)
|
table.GetCell(row, column).SetTextColor(tcell.ColorRed)
|
||||||
cell.Color = tcell.ColorRed
|
|
||||||
table.SetSelectable(false, false)
|
table.SetSelectable(false, false)
|
||||||
})
|
})
|
||||||
if err := app.SetRoot(table, true).SetFocus(table).Run(); err != nil {
|
if err := app.SetRoot(table, true).SetFocus(table).Run(); err != nil {
|
||||||
|
|
62
table.go
62
table.go
|
@ -5,7 +5,9 @@ import (
|
||||||
runewidth "github.com/mattn/go-runewidth"
|
runewidth "github.com/mattn/go-runewidth"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TableCell represents one cell inside a Table.
|
// TableCell represents one cell inside a Table. You can instantiate this type
|
||||||
|
// directly but all colors (background and text) will be set to their default
|
||||||
|
// which is black.
|
||||||
type TableCell struct {
|
type TableCell struct {
|
||||||
// The text to be displayed in the table cell.
|
// The text to be displayed in the table cell.
|
||||||
Text string
|
Text string
|
||||||
|
@ -22,6 +24,9 @@ type TableCell struct {
|
||||||
// The color of the cell text.
|
// The color of the cell text.
|
||||||
Color tcell.Color
|
Color tcell.Color
|
||||||
|
|
||||||
|
// The background color of the cell.
|
||||||
|
BackgroundColor tcell.Color
|
||||||
|
|
||||||
// If set to true, this cell cannot be selected.
|
// If set to true, this cell cannot be selected.
|
||||||
NotSelectable bool
|
NotSelectable bool
|
||||||
|
|
||||||
|
@ -29,13 +34,66 @@ type TableCell struct {
|
||||||
x, y, width int
|
x, y, width int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewTableCell returns a new table cell with sensible defaults. That is, left
|
||||||
|
// aligned text with the primary text color (see Styles) and a transparent
|
||||||
|
// background (using the background of the Table).
|
||||||
|
func NewTableCell(text string) *TableCell {
|
||||||
|
return &TableCell{
|
||||||
|
Text: text,
|
||||||
|
Align: AlignLeft,
|
||||||
|
Color: Styles.PrimaryTextColor,
|
||||||
|
BackgroundColor: tcell.ColorDefault,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetText sets the cell's text.
|
||||||
|
func (c *TableCell) SetText(text string) *TableCell {
|
||||||
|
c.Text = text
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetAlign sets the cell's text alignment, one of AlignLeft, AlignCenter, or
|
||||||
|
// AlignRight.
|
||||||
|
func (c *TableCell) SetAlign(align int) *TableCell {
|
||||||
|
c.Align = align
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetMaxWidth sets maximum width of the cell in screen space. This is used to
|
||||||
|
// give a column a maximum width. Any cell text whose screen width exceeds this
|
||||||
|
// width is cut off. Set to 0 if there is no maximum width.
|
||||||
|
func (c *TableCell) SetMaxWidth(maxWidth int) *TableCell {
|
||||||
|
c.MaxWidth = maxWidth
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetTextColor sets the cell's text color.
|
||||||
|
func (c *TableCell) SetTextColor(color tcell.Color) *TableCell {
|
||||||
|
c.Color = color
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetBackgroundColor sets the cell's background color. Set to
|
||||||
|
// tcell.ColorDefault to use the table's background color.
|
||||||
|
func (c *TableCell) SetBackgroundColor(color tcell.Color) *TableCell {
|
||||||
|
c.BackgroundColor = color
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetSelectable sets whether or not this cell can be selected by the user.
|
||||||
|
func (c *TableCell) SetSelectable(selectable bool) *TableCell {
|
||||||
|
c.NotSelectable = !selectable
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
// GetLastPosition returns the position of the table cell the last time it was
|
// GetLastPosition returns the position of the table cell the last time it was
|
||||||
// drawn on screen. If the cell is not on screen, the return values are
|
// drawn on screen. If the cell is not on screen, the return values are
|
||||||
// undefined.
|
// undefined.
|
||||||
//
|
//
|
||||||
// Because the Table class will attempt to keep selected cells on screen, this
|
// Because the Table class will attempt to keep selected cells on screen, this
|
||||||
// function is most useful in response to a "selected" event (see
|
// function is most useful in response to a "selected" event (see
|
||||||
// SetSelectedFunc()).
|
// SetSelectedFunc()) or a "selectionChanged" event (see
|
||||||
|
// SetSelectionChangedFunc()).
|
||||||
func (c *TableCell) GetLastPosition() (x, y, width int) {
|
func (c *TableCell) GetLastPosition() (x, y, width int) {
|
||||||
return c.x, c.y, c.width
|
return c.x, c.y, c.width
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue