Skip to content

Commit

Permalink
table: colorize title borders; fixes #234 (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t committed Nov 6, 2022
1 parent 58a875d commit 711c5ea
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 124 deletions.
18 changes: 9 additions & 9 deletions table/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,15 +364,16 @@ func (t *Table) renderRowsHeader(out *strings.Builder) {

func (t *Table) renderTitle(out *strings.Builder) {
if t.title != "" {
colors := t.style.Title.Colors
rowLength := t.maxRowLength
if t.allowedRowLength != 0 && t.allowedRowLength < rowLength {
rowLength = t.allowedRowLength
}
if t.style.Options.DrawBorder {
lenBorder := rowLength - text.RuneWidthWithoutEscSequences(t.style.Box.TopLeft+t.style.Box.TopRight)
out.WriteString(t.style.Box.TopLeft)
out.WriteString(text.RepeatAndTrim(t.style.Box.MiddleHorizontal, lenBorder))
out.WriteString(t.style.Box.TopRight)
out.WriteString(colors.Sprint(t.style.Box.TopLeft))
out.WriteString(colors.Sprint(text.RepeatAndTrim(t.style.Box.MiddleHorizontal, lenBorder)))
out.WriteString(colors.Sprint(t.style.Box.TopRight))
}

lenText := rowLength - text.RuneWidthWithoutEscSequences(t.style.Box.PaddingLeft+t.style.Box.PaddingRight)
Expand All @@ -381,26 +382,25 @@ func (t *Table) renderTitle(out *strings.Builder) {
}
titleText := text.WrapText(t.title, lenText)
for _, titleLine := range strings.Split(titleText, "\n") {
t.renderTitleLine(out, lenText, titleLine)
t.renderTitleLine(out, lenText, titleLine, colors)
}
}
}

func (t *Table) renderTitleLine(out *strings.Builder, lenText int, titleLine string) {
func (t *Table) renderTitleLine(out *strings.Builder, lenText int, titleLine string, colors text.Colors) {
titleLine = strings.TrimSpace(titleLine)
titleLine = t.style.Title.Format.Apply(titleLine)
titleLine = t.style.Title.Align.Apply(titleLine, lenText)
titleLine = t.style.Box.PaddingLeft + titleLine + t.style.Box.PaddingRight
titleLine = t.style.Title.Colors.Sprint(titleLine)

if out.Len() > 0 {
out.WriteRune('\n')
}
if t.style.Options.DrawBorder {
out.WriteString(t.style.Box.Left)
out.WriteString(colors.Sprint(t.style.Box.Left))
}
out.WriteString(titleLine)
out.WriteString(colors.Sprint(titleLine))
if t.style.Options.DrawBorder {
out.WriteString(t.style.Box.Right)
out.WriteString(colors.Sprint(t.style.Box.Right))
}
}

0 comments on commit 711c5ea

Please sign in to comment.