diff --git a/examples/table/languages/main.go b/examples/table/languages/main.go index 7e2f07f7..601cdc84 100644 --- a/examples/table/languages/main.go +++ b/examples/table/languages/main.go @@ -36,7 +36,6 @@ func main() { {"Arabic", "أهلين", "أهلا"}, {"Russian", "Здравствуйте", "Привет"}, {"Spanish", "Hola", "¿Qué tal?"}, - {"English", "You look absolutely fabulous.", "How's it going?"}, } t := table.New(). @@ -60,7 +59,7 @@ func main() { } // Arabic is a right-to-left language, so right align the text. - if rows[row-1][0] == "Arabic" && col != 0 { + if row < len(rows) && rows[row-1][0] == "Arabic" && col != 0 { style = style.Copy().Align(lipgloss.Right) } @@ -69,5 +68,7 @@ func main() { Headers("LANGUAGE", "FORMAL", "INFORMAL"). Rows(rows...) + t.Row("English", "You look absolutely fabulous.", "How's it going?") + fmt.Println(t) } diff --git a/table/table_test.go b/table/table_test.go index 4a86f1be..4e5ccad0 100644 --- a/table/table_test.go +++ b/table/table_test.go @@ -46,6 +46,54 @@ func TestTable(t *testing.T) { } } +func TestTableExample(t *testing.T) { + HeaderStyle := lipgloss.NewStyle().Padding(0, 1).Align(lipgloss.Center) + EvenRowStyle := lipgloss.NewStyle().Padding(0, 1) + OddRowStyle := lipgloss.NewStyle().Padding(0, 1) + + rows := [][]string{ + {"Chinese", "您好", "你好"}, + {"Japanese", "こんにちは", "やあ"}, + {"Russian", "Здравствуйте", "Привет"}, + {"Spanish", "Hola", "¿Qué tal?"}, + } + + table := New(). + Border(lipgloss.NormalBorder()). + BorderStyle(lipgloss.NewStyle().Foreground(lipgloss.Color("99"))). + StyleFunc(func(row, col int) lipgloss.Style { + switch { + case row == 0: + return HeaderStyle + case row%2 == 0: + return EvenRowStyle + default: + return OddRowStyle + } + }). + Headers("LANGUAGE", "FORMAL", "INFORMAL"). + Rows(rows...) + + // You can also add tables row-by-row + table.Row("English", "You look absolutely fabulous.", "How's it going?") + + expected := strings.TrimSpace(` +┌──────────┬───────────────────────────────┬─────────────────┐ +│ LANGUAGE │ FORMAL │ INFORMAL │ +├──────────┼───────────────────────────────┼─────────────────┤ +│ Chinese │ 您好 │ 你好 │ +│ Japanese │ こんにちは │ やあ │ +│ Russian │ Здравствуйте │ Привет │ +│ Spanish │ Hola │ ¿Qué tal? │ +│ English │ You look absolutely fabulous. │ How's it going? │ +└──────────┴───────────────────────────────┴─────────────────┘ +`) + + if table.String() != expected { + t.Fatalf("expected:\n\n%s\n\ngot:\n\n%s", expected, table.String()) + } +} + func TestTableEmpty(t *testing.T) { table := New(). Border(lipgloss.NormalBorder()).