package table
import (
)
const HeaderRow int = -1
type StyleFunc func(row, col int) lipgloss.Style
func (, int) lipgloss.Style {
return lipgloss.NewStyle()
}
type Table struct {
baseStyle lipgloss.Style
styleFunc StyleFunc
border lipgloss.Border
borderTop bool
borderBottom bool
borderLeft bool
borderRight bool
borderHeader bool
borderColumn bool
borderRow bool
borderStyle lipgloss.Style
headers []string
data Data
width int
fitContent bool
height int
useManualHeight bool
yOffset int
wrap bool
widths []int
heights []int
firstVisibleRowIndex int
lastVisibleRowIndex int
overflowHeight int
}
func () *Table {
return &Table{
styleFunc: DefaultStyles,
border: lipgloss.NormalBorder(),
borderBottom: true,
borderColumn: true,
borderHeader: true,
borderLeft: true,
borderRight: true,
borderTop: true,
wrap: true,
data: NewStringData(),
}
}
func ( *Table) () *Table {
.data = NewStringData()
return
}
func ( *Table) ( lipgloss.Style) *Table {
.baseStyle =
.borderStyle = .borderStyle.Inherit()
return
}
func ( *Table) ( StyleFunc) *Table {
.styleFunc =
return
}
func ( *Table) (, int) lipgloss.Style {
if .styleFunc == nil {
return .baseStyle
}
return .styleFunc(, ).Inherit(.baseStyle)
}
func ( *Table) ( Data) *Table {
.data =
return
}
func ( *Table) () Data {
return .data
}
func ( *Table) ( ...[]string) *Table {
for , := range {
switch .data.(type) {
case *StringData:
.data.(*StringData).Append()
}
}
return
}
func ( *Table) ( ...string) *Table {
switch .data.(type) {
case *StringData:
.data.(*StringData).Append()
}
return
}
func ( *Table) ( ...string) *Table {
.headers =
return
}
func ( *Table) () []string {
return .headers
}
func ( *Table) ( lipgloss.Border) *Table {
.border =
return
}
func ( *Table) ( bool) *Table {
.borderTop =
return
}
func ( *Table) ( bool) *Table {
.borderBottom =
return
}
func ( *Table) ( bool) *Table {
.borderLeft =
return
}
func ( *Table) ( bool) *Table {
.borderRight =
return
}
func ( *Table) ( bool) *Table {
.borderHeader =
return
}
func ( *Table) ( bool) *Table {
.borderColumn =
return
}
func ( *Table) ( bool) *Table {
.borderRow =
return
}
func ( *Table) ( lipgloss.Style) *Table {
.borderStyle = .Inherit(.baseStyle)
return
}
func ( *Table) () bool {
return .borderTop
}
func ( *Table) () bool {
return .borderBottom
}
func ( *Table) () bool {
return .borderLeft
}
func ( *Table) () bool {
return .borderRight
}
func ( *Table) () bool {
return .borderHeader
}
func ( *Table) () bool {
return .borderColumn
}
func ( *Table) () bool {
return .borderRow
}
func ( *Table) ( int) *Table {
.width =
return
}
func ( *Table) () *Table {
.fitContent = true
return
}
func ( *Table) ( int) *Table {
.height =
.useManualHeight = true
return
}
func ( *Table) () int {
return .height
}
func ( *Table) ( int) *Table {
if < 0 {
= 0
}
.yOffset =
return
}
func ( *Table) () int {
return .yOffset
}
func ( *Table) () int {
return .firstVisibleRowIndex
}
func ( *Table) () int {
return .lastVisibleRowIndex
}
func ( *Table) () int {
if .lastVisibleRowIndex == -2 {
return .data.Rows() - .firstVisibleRowIndex
}
return .lastVisibleRowIndex - .firstVisibleRowIndex + 1
}
func ( *Table) ( bool) *Table {
.wrap =
return
}
func ( *Table) () string {
:= len(.headers) > 0
:= .data != nil && .data.Rows() > 0
if ! && ! {
return ""
}
if {
for := len(.headers); < .data.Columns(); ++ {
.headers = append(.headers, "")
}
}
.resize()
var strings.Builder
if .borderTop {
.WriteString(.constructTopBorder())
.WriteString("\n")
}
if {
.WriteString(.constructHeaders())
}
var string
if .borderBottom {
= .constructBottomBorder()
}
if .data.Rows() > 0 {
for := .firstVisibleRowIndex; < .data.Rows(); ++ {
if .lastVisibleRowIndex != -2 && > .lastVisibleRowIndex {
break
}
.WriteString(.constructRow(, false))
}
if .lastVisibleRowIndex != -2 {
.WriteString(.constructRow(.lastVisibleRowIndex+1, true))
}
}
.WriteString()
return lipgloss.NewStyle().
MaxHeight(min(.height, .computeHeight())).
MaxWidth(.width).
Render(strings.TrimSuffix(.String(), "\n"))
}
func ( *Table) () int {
:= len(.headers) > 0
return sum(.heights) - 1 + btoi() +
btoi(.borderTop) + btoi(.borderBottom) +
btoi(.borderHeader) + .data.Rows()*btoi(.borderRow)
}
func ( *Table) () string {
return .String()
}
func ( *Table) () string {
var strings.Builder
if .borderLeft {
.WriteString(.borderStyle.Render(.border.TopLeft))
}
for := range .widths {
.WriteString(.borderStyle.Render(strings.Repeat(.border.Top, .widths[])))
if < len(.widths)-1 && .borderColumn {
.WriteString(.borderStyle.Render(.border.MiddleTop))
}
}
if .borderRight {
.WriteString(.borderStyle.Render(.border.TopRight))
}
return .String()
}
func ( *Table) () string {
var strings.Builder
if .borderLeft {
.WriteString(.borderStyle.Render(.border.BottomLeft))
}
for := range .widths {
.WriteString(.borderStyle.Render(strings.Repeat(.border.Bottom, .widths[])))
if < len(.widths)-1 && .borderColumn {
.WriteString(.borderStyle.Render(.border.MiddleBottom))
}
}
if .borderRight {
.WriteString(.borderStyle.Render(.border.BottomRight))
}
return .String()
}
func ( *Table) () string {
var strings.Builder
:= make([]string, 0, len(.headers)*2+1)
:= .heights[0]
:= strings.Repeat(.borderStyle.Render(.border.Left)+"\n", )
if .borderLeft {
= append(, )
}
for , := range .headers {
:= .style(HeaderRow, )
= .truncateCell(, HeaderRow, )
= append(,
.
Height(-.GetVerticalMargins()).
Width(.widths[]-.GetHorizontalMargins()).
Render(),
)
if < len(.headers)-1 && .borderColumn {
= append(, )
}
}
if .borderRight {
:= strings.Repeat(.borderStyle.Render(.border.Right)+"\n", )
= append(, )
}
for , := range {
[] = strings.TrimRight(, "\n")
}
.WriteString(lipgloss.JoinHorizontal(lipgloss.Top, ...) + "\n")
if .borderHeader {
if .borderLeft {
.WriteString(.borderStyle.Render(.border.MiddleLeft))
}
for := range .headers {
.WriteString(.borderStyle.Render(strings.Repeat(.border.Top, .widths[])))
if < len(.headers)-1 && .borderColumn {
.WriteString(.borderStyle.Render(.border.Middle))
}
}
if .borderRight {
.WriteString(.borderStyle.Render(.border.MiddleRight))
}
.WriteString("\n")
}
return .String()
}
func ( *Table) ( int, bool) string {
var strings.Builder
:= make([]string, 0, .data.Columns()*2+1)
:= len(.headers) > 0
var int
if ! {
= .heights[+btoi()]
} else {
= .overflowHeight
}
:= strings.Repeat(.borderStyle.Render(.border.Left)+"\n", )
if .borderLeft {
= append(, )
}
for := range .data.Columns() {
:= "…"
if ! {
= .data.At(, )
}
:= .style(, )
if !.wrap {
= .truncateCell(, , )
}
= append(, .
Height(-.GetVerticalMargins()).
MaxHeight().
Width(.widths[]-.GetHorizontalMargins()).
MaxWidth(.widths[]).
Render())
if < .data.Columns()-1 && .borderColumn {
= append(, )
}
}
if .borderRight {
:= strings.Repeat(.borderStyle.Render(.border.Right)+"\n", )
= append(, )
}
for , := range {
[] = strings.TrimRight(, "\n")
}
.WriteString(lipgloss.JoinHorizontal(lipgloss.Top, ...) + "\n")
if .borderRow && ! && < .data.Rows()-1 {
if .borderLeft {
.WriteString(.borderStyle.Render(.border.MiddleLeft))
}
for := range .widths {
.WriteString(.borderStyle.Render(strings.Repeat(.border.Bottom, .widths[])))
if < len(.widths)-1 && .borderColumn {
.WriteString(.borderStyle.Render(.border.Middle))
}
}
if .borderRight {
.WriteString(.borderStyle.Render(.border.MiddleRight))
}
.WriteString("\n")
}
return .String()
}
func ( *Table) ( string, , int) string {
:= len(.headers) > 0
:= .heights[+btoi()]
:= .widths[]
:= .style(, )
if == HeaderRow {
= 1
}
:= ( * ) - .GetHorizontalPadding() - .GetHorizontalMargins()
return ansi.Truncate(, , "…")
}