Skip to content

Commit

Permalink
feat: BorderColumn(false) unsets column separators
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Sep 19, 2023
1 parent 487f7c8 commit 176e89e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 9 deletions.
26 changes: 17 additions & 9 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Table struct {
borderLeft bool
borderRight bool
borderHeader bool
borderColumn bool

borderStyle lipgloss.Style
headers []any
Expand All @@ -48,11 +49,12 @@ func New() *Table {
return &Table{
styleFunc: NoTableStyle,
border: lipgloss.HiddenBorder(),
borderTop: true,
borderHeader: true,
borderBottom: true,
borderRight: true,
borderColumn: true,
borderHeader: true,
borderLeft: true,
borderRight: true,
borderTop: true,
}
}

Expand Down Expand Up @@ -124,12 +126,18 @@ func (t *Table) BorderRight(v bool) *Table {
return t
}

// BorderHeader sets the right border.
// BorderHeader sets the header separator border.
func (t *Table) BorderHeader(v bool) *Table {
t.borderHeader = v
return t
}

// BorderColumn sets the column border.
func (t *Table) BorderColumn(v bool) *Table {
t.borderColumn = v
return t
}

// BorderStyle sets the style for the table border.
func (t *Table) BorderStyle(style lipgloss.Style) *Table {
t.borderStyle = style
Expand Down Expand Up @@ -192,7 +200,7 @@ func (t *Table) String() string {
s.WriteString(t.borderStyle.Render(t.border.TopLeft))
for i := 0; i < len(longestRow); i++ {
s.WriteString(t.borderStyle.Render(strings.Repeat(t.border.Top, t.widths[i])))
if i < len(longestRow)-1 {
if i < len(longestRow)-1 && t.borderColumn {
s.WriteString(t.borderStyle.Render(t.border.MiddleTop))
}
}
Expand All @@ -209,7 +217,7 @@ func (t *Table) String() string {
Width(t.widths[i]).
MaxWidth(t.widths[i]).
Render(fmt.Sprint(header)))
if i < len(t.headers)-1 {
if i < len(t.headers)-1 && t.borderColumn {
s.WriteString(t.borderStyle.Render(t.border.Left))
}
}
Expand All @@ -225,7 +233,7 @@ func (t *Table) String() string {
if t.borderHeader {
for i := 0; i < len(t.headers); i++ {
s.WriteString(t.borderStyle.Render(strings.Repeat(t.border.Top, t.widths[i])))
if i < len(t.headers)-1 {
if i < len(t.headers)-1 && t.borderColumn {
s.WriteString(t.borderStyle.Render(t.border.Middle))
}
}
Expand Down Expand Up @@ -267,7 +275,7 @@ func (t *Table) String() string {
MaxWidth(t.widths[c]).
Render(fmt.Sprint(cell)))

if c < len(row)-1 {
if c < len(row)-1 && t.borderColumn {
cells = append(cells, left)
}
}
Expand All @@ -288,7 +296,7 @@ func (t *Table) String() string {
s.WriteString(t.borderStyle.Render(t.border.BottomLeft))
for i := 0; i < len(longestRow); i++ {
s.WriteString(t.borderStyle.Render(strings.Repeat(t.border.Bottom, t.widths[i])))
if i < len(longestRow)-1 {
if i < len(longestRow)-1 && t.borderColumn {
s.WriteString(t.borderStyle.Render(t.border.MiddleBottom))
}
}
Expand Down
55 changes: 55 additions & 0 deletions table/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,61 @@ func TestTableNoHeaders(t *testing.T) {
}
}

func TestTableNoColumnSeparators(t *testing.T) {
table := New().
Border(lipgloss.NormalBorder()).
BorderColumn(false).
StyleFunc(TableStyle).
Row("Chinese", "Nǐn hǎo", "Nǐ hǎo").
Row("French", "Bonjour", "Salut").
Row("Japanese", "こんにけは", "やあ").
Row("Russian", "Zdravstvuyte", "Privet").
Row("Spanish", "Hola", "ΒΏQuΓ© tal?")

expected := strings.TrimSpace(`
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Chinese Nǐn hǎo Nǐ hǎo β”‚
β”‚ French Bonjour Salut β”‚
β”‚ Japanese こんにけは やあ β”‚
β”‚ Russian Zdravstvuyte Privet β”‚
β”‚ Spanish Hola ΒΏQuΓ© tal? β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
`)

if table.String() != expected {
t.Fatalf("expected:\n\n%s\n\ngot:\n\n%s", expected, table.String())
}
}

func TestTableNoColumnSeparatorsWithHeaders(t *testing.T) {
table := New().
Border(lipgloss.NormalBorder()).
BorderColumn(false).
StyleFunc(TableStyle).
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?")

expected := strings.TrimSpace(`
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ LANGUAGE FORMAL INFORMAL β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Chinese Nǐn hǎo Nǐ hǎo β”‚
β”‚ French Bonjour Salut β”‚
β”‚ Japanese こんにけは やあ β”‚
β”‚ Russian Zdravstvuyte Privet β”‚
β”‚ Spanish Hola ΒΏQuΓ© tal? β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
`)

if table.String() != expected {
t.Fatalf("expected:\n\n%s\n\ngot:\n\n%s", expected, table.String())
}
}

func TestNew(t *testing.T) {
table := New()
expected := ""
Expand Down

0 comments on commit 176e89e

Please sign in to comment.