Skip to content

Commit

Permalink
test: add table tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Aug 26, 2023
1 parent ac8231e commit bc8e4c0
Show file tree
Hide file tree
Showing 6 changed files with 647 additions and 50 deletions.
126 changes: 78 additions & 48 deletions borders.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ import (
// Border contains a series of values which comprise the various parts of a
// border.
type Border struct {
Top string
Bottom string
Left string
Right string
TopLeft string
TopRight string
BottomRight string
BottomLeft string
Bottom string
BottomLeft string
BottomRight string
Left string
Middle string
MiddleBottom string
MiddleLeft string
MiddleRight string
Right string
Top string
TopLeft string
TopMiddle string
TopRight string
}

// GetTopSize returns the width of the top border. If borders contain runes of
Expand Down Expand Up @@ -63,25 +68,35 @@ var (
noBorder = Border{}

normalBorder = Border{
Top: "─",
Bottom: "─",
Left: "│",
Right: "│",
TopLeft: "┌",
TopRight: "┐",
BottomLeft: "└",
BottomRight: "┘",
Top: "─",
Bottom: "─",
Left: "│",
Right: "│",
TopLeft: "┌",
TopRight: "┐",
BottomLeft: "└",
BottomRight: "┘",
MiddleLeft: "├",
MiddleRight: "┤",
Middle: "┼",
TopMiddle: "┬",
MiddleBottom: "┴",
}

roundedBorder = Border{
Top: "─",
Bottom: "─",
Left: "│",
Right: "│",
TopLeft: "╭",
TopRight: "╮",
BottomLeft: "╰",
BottomRight: "╯",
Top: "─",
Bottom: "─",
Left: "│",
Right: "│",
TopLeft: "╭",
TopRight: "╮",
BottomLeft: "╰",
BottomRight: "╯",
MiddleLeft: "├",
MiddleRight: "┤",
Middle: "┼",
TopMiddle: "┬",
MiddleBottom: "┴",
}

blockBorder = Border{
Expand Down Expand Up @@ -118,36 +133,51 @@ var (
}

thickBorder = Border{
Top: "━",
Bottom: "━",
Left: "┃",
Right: "┃",
TopLeft: "┏",
TopRight: "┓",
BottomLeft: "┗",
BottomRight: "┛",
Top: "━",
Bottom: "━",
Left: "┃",
Right: "┃",
TopLeft: "┏",
TopRight: "┓",
BottomLeft: "┗",
BottomRight: "┛",
MiddleLeft: "┣",
MiddleRight: "┫",
Middle: "╋",
TopMiddle: "┳",
MiddleBottom: "┻",
}

doubleBorder = Border{
Top: "═",
Bottom: "═",
Left: "║",
Right: "║",
TopLeft: "╔",
TopRight: "╗",
BottomLeft: "╚",
BottomRight: "╝",
Top: "═",
Bottom: "═",
Left: "║",
Right: "║",
TopLeft: "╔",
TopRight: "╗",
BottomLeft: "╚",
BottomRight: "╝",
MiddleLeft: "╠",
MiddleRight: "╣",
Middle: "╬",
TopMiddle: "╦",
MiddleBottom: "╩",
}

hiddenBorder = Border{
Top: " ",
Bottom: " ",
Left: " ",
Right: " ",
TopLeft: " ",
TopRight: " ",
BottomLeft: " ",
BottomRight: " ",
Top: " ",
Bottom: " ",
Left: " ",
Right: " ",
TopLeft: " ",
TopRight: " ",
BottomLeft: " ",
BottomRight: " ",
MiddleLeft: " ",
MiddleRight: " ",
Middle: " ",
TopMiddle: " ",
MiddleBottom: " ",
}
)

Expand Down
2 changes: 1 addition & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module examples

go 1.17
go 1.18

replace github.com/charmbracelet/lipgloss => ../

Expand Down
48 changes: 48 additions & 0 deletions examples/table/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"fmt"

"github.com/charmbracelet/lipgloss"
)

const (
purple = lipgloss.Color("99")
gray = lipgloss.Color("245")
lightGray = lipgloss.Color("241")
)

var (
// HeaderStyle is the lipgloss style used for the table headers.
HeaderStyle = lipgloss.NewStyle().Foreground(purple).Bold(true).Align(lipgloss.Center)
// RowStyle is the base lipgloss style used for the table rows.
RowStyle = lipgloss.NewStyle().Padding(1)
// OddRowStyle is the lipgloss style used for odd-numbered table rows.
OddRowStyle = RowStyle.Copy().Foreground(gray)
// EvenRowStyle is the lipgloss style used for even-numbered table rows.
EvenRowStyle = RowStyle.Copy().Foreground(lightGray)
)

func main() {
t := lipgloss.NewTable().
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").
Row("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Row("French", "Bonjour", "Salut").
Row("Japanese", "こんにちは", "やあ").
Row("Russian", "Zdravstvuyte", "Privet").
Row("Spanish", "Hola", "¿Qué tal?")

fmt.Println(t)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/charmbracelet/lipgloss

retract v0.7.0 // v0.7.0 introduces a bug that causes some apps to freeze.

go 1.17
go 1.18

require (
github.com/mattn/go-runewidth v0.0.14
Expand Down
Loading

0 comments on commit bc8e4c0

Please sign in to comment.