// Package table provides a styled table renderer for terminals.
package table import ( ) // HeaderRow denotes the header's row index used when rendering headers. Use // this value when looking to customize header styles in StyleFunc. const HeaderRow int = -1 // StyleFunc is the style function that determines the style of a Cell. // // It takes the row and column of the cell as an input and determines the // lipgloss Style to use for that cell position. // // Example: // // t := table.New(). // Headers("Name", "Age"). // Row("Kini", 4). // Row("Eli", 1). // Row("Iris", 102). // StyleFunc(func(row, col int) lipgloss.Style { // switch { // case row == 0: // return HeaderStyle // case row%2 == 0: // return EvenRowStyle // default: // return OddRowStyle // } // }) type StyleFunc func(row, col int) lipgloss.Style // DefaultStyles is a TableStyleFunc that returns a new Style with no attributes. func (, int) lipgloss.Style { return lipgloss.NewStyle() } // Table is a type for rendering tables. 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 } // New returns a new Table that can be modified through different // attributes. // // By default, a table has normal border, no styling, and no rows. 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(), } } // ClearRows clears the table rows. func ( *Table) () *Table { .data = NewStringData() return } // BaseStyle sets the base style for the whole table. If you need to set a // background color for the whole table, use this. func ( *Table) ( lipgloss.Style) *Table { .baseStyle = .borderStyle = .borderStyle.Inherit() return } // StyleFunc sets the style for a cell based on it's position (row, column). func ( *Table) ( StyleFunc) *Table { .styleFunc = return } // style returns the style for a cell based on it's position (row, column). func ( *Table) (, int) lipgloss.Style { if .styleFunc == nil { return .baseStyle } return .styleFunc(, ).Inherit(.baseStyle) } // Data sets the table data. func ( *Table) ( Data) *Table { .data = return } // GetData returns the table data. func ( *Table) () Data { return .data } // Rows appends rows to the table data. func ( *Table) ( ...[]string) *Table { for , := range { switch .data.(type) { case *StringData: .data.(*StringData).Append() } } return } // Row appends a row to the table data. func ( *Table) ( ...string) *Table { switch .data.(type) { case *StringData: .data.(*StringData).Append() } return } // Headers sets the table headers. func ( *Table) ( ...string) *Table { .headers = return } // GetHeaders returns the table headers. func ( *Table) () []string { return .headers } // Border sets the table border. func ( *Table) ( lipgloss.Border) *Table { .border = return } // BorderTop sets the top border. func ( *Table) ( bool) *Table { .borderTop = return } // BorderBottom sets the bottom border. func ( *Table) ( bool) *Table { .borderBottom = return } // BorderLeft sets the left border. func ( *Table) ( bool) *Table { .borderLeft = return } // BorderRight sets the right border. func ( *Table) ( bool) *Table { .borderRight = return } // BorderHeader sets the header separator border. func ( *Table) ( bool) *Table { .borderHeader = return } // BorderColumn sets the column border separator. func ( *Table) ( bool) *Table { .borderColumn = return } // BorderRow sets the row border separator. func ( *Table) ( bool) *Table { .borderRow = return } // BorderStyle sets the style for the table border. func ( *Table) ( lipgloss.Style) *Table { .borderStyle = .Inherit(.baseStyle) return } // GetBorderTop gets the top border. func ( *Table) () bool { return .borderTop } // GetBorderBottom gets the bottom border. func ( *Table) () bool { return .borderBottom } // GetBorderLeft gets the left border. func ( *Table) () bool { return .borderLeft } // GetBorderRight gets the right border. func ( *Table) () bool { return .borderRight } // GetBorderHeader gets the header separator border. func ( *Table) () bool { return .borderHeader } // GetBorderColumn gets the column border separator. func ( *Table) () bool { return .borderColumn } // GetBorderRow gets the row border separator. func ( *Table) () bool { return .borderRow } // Width sets the table width, this auto-sizes the columns to fit the width by // either expanding or contracting the widths of each column as a best effort // approach. func ( *Table) ( int) *Table { .width = return } // FitContent configures the table to render at its content width—the width // needed to fully fit its content, including cell padding and borders—rather // than expanding to fill the width set via [Table.Width]. // // When combined with [Table.Width], the configured width acts as a maximum: the // table renders at its content width unless that exceeds the maximum, in which // case it's constrained to the maximum and its content is resized to fit. func ( *Table) () *Table { .fitContent = true return } // Height sets the table height. func ( *Table) ( int) *Table { .height = .useManualHeight = true return } // GetHeight returns the height of the table. func ( *Table) () int { return .height } // YOffset sets the table rendering offset. func ( *Table) ( int) *Table { if < 0 { = 0 } .yOffset = return } // GetYOffset returns the table rendering offset. func ( *Table) () int { return .yOffset } // FirstVisibleRowIndex returns the index of the first visible row in the table. func ( *Table) () int { return .firstVisibleRowIndex } // LastVisibleRowIndex returns the index of the last visible row in the table. func ( *Table) () int { return .lastVisibleRowIndex } // VisibleRows returns the number of visible rows in the table. func ( *Table) () int { if .lastVisibleRowIndex == -2 { return .data.Rows() - .firstVisibleRowIndex } return .lastVisibleRowIndex - .firstVisibleRowIndex + 1 } // Wrap dictates whether or not the table content should wrap. // // This only applies to data cells. Headers are never wrapped. func ( *Table) ( bool) *Table { .wrap = return } // String returns the table as a string. func ( *Table) () string { := len(.headers) > 0 := .data != nil && .data.Rows() > 0 if ! && ! { return "" } // Add empty cells to the headers, until it's the same length as the longest // row (only if there are at headers in the first place). if { for := len(.headers); < .data.Columns(); ++ { .headers = append(.headers, "") } } // Do all the sizing calculations for width and height. .resize() var strings.Builder if .borderTop { .WriteString(.constructTopBorder()) .WriteString("\n") } if { .WriteString(.constructHeaders()) } var string if .borderBottom { = .constructBottomBorder() } // If there are no data rows render nothing. if .data.Rows() > 0 { for := .firstVisibleRowIndex; < .data.Rows(); ++ { if .lastVisibleRowIndex != -2 && > .lastVisibleRowIndex { break } .WriteString(.constructRow(, false)) } // Add an overflow row to show that there are more rows not being rendered. if .lastVisibleRowIndex != -2 { .WriteString(.constructRow(.lastVisibleRowIndex+1, true)) } } .WriteString() return lipgloss.NewStyle(). MaxHeight(min(.height, .computeHeight())). MaxWidth(.width). Render(strings.TrimSuffix(.String(), "\n")) } // computeHeight computes the height of the table in its current configuration. func ( *Table) () int { := len(.headers) > 0 return sum(.heights) - 1 + btoi() + btoi(.borderTop) + btoi(.borderBottom) + btoi(.borderHeader) + .data.Rows()*btoi(.borderRow) } // Render returns the table as a string. func ( *Table) () string { return .String() } // constructTopBorder constructs the top border for the table given it's current // border configuration and data. 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() } // constructBottomBorder constructs the bottom border for the table given it's current // border configuration and data. 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() } // constructHeaders constructs the headers for the table given it's current // header configuration and data. 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, ) // NOTE(@andreynering): We always truncate headers. = .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() } // constructRow constructs the row for the table given an index and row data // based on the current configuration. If isOverflow is true, the row is // rendered as an overflow row (using ellipsis). 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(, . // Account for the margins in the cell sizing. 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(, ) // NOTE(@andreynering): We always truncate headers to 1 line. if == HeaderRow { = 1 } := ( * ) - .GetHorizontalPadding() - .GetHorizontalMargins() return ansi.Truncate(, , "…") }