Skip to content

Commit 9d87373

Browse files
committed
feat(table): pad position on table link list
1 parent a11e9a0 commit 9d87373

File tree

3 files changed

+45
-34
lines changed

3 files changed

+45
-34
lines changed

ansi/table_links.go

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"io"
77
"net/url"
88
"slices"
9+
"strconv"
10+
"strings"
911

1012
xansi "github.com/charmbracelet/x/ansi"
1113
"github.com/charmbracelet/x/exp/slice"
@@ -37,15 +39,15 @@ func (e *TableElement) printTableLinks(ctx RenderContext) {
3739
w := ctx.blockStack.Current().Block
3840
termWidth := int(ctx.blockStack.Width(ctx)) //nolint: gosec
3941

40-
renderLinkText := func(link tableLink, position int) string {
41-
var token string
42+
renderLinkText := func(link tableLink, position, padding int) string {
43+
token := strings.Repeat(" ", padding)
4244
style := ctx.options.Styles.LinkText
4345

4446
switch link.linkType {
4547
case linkTypeAuto, linkTypeRegular:
46-
token = fmt.Sprintf("[%d]: %s", position, link.content)
48+
token += fmt.Sprintf("[%d]: %s", position, link.content)
4749
case linkTypeImage:
48-
token = link.content
50+
token += link.content
4951
style = ctx.options.Styles.ImageText
5052
style.Prefix = fmt.Sprintf("[%d]: %s", position, style.Prefix)
5153
}
@@ -76,25 +78,34 @@ func (e *TableElement) printTableLinks(ctx RenderContext) {
7678
renderText(w, ctx.options.ColorProfile, ctx.blockStack.Current().Style.StylePrimitive, str)
7779
}
7880

79-
if len(ctx.table.tableLinks) > 0 {
80-
renderString("\n")
81+
paddingFor := func(total, position int) int {
82+
totalSize := len(strconv.Itoa(total))
83+
positionSize := len(strconv.Itoa(position))
84+
85+
return max(totalSize-positionSize, 0)
8186
}
82-
for i, link := range ctx.table.tableLinks {
83-
renderString("\n")
84-
linkText := renderLinkText(link, i+1)
85-
renderString(" ")
86-
renderLinkHref(link, linkText)
87+
88+
renderList := func(list []tableLink) {
89+
for i, item := range list {
90+
position := i + 1
91+
padding := paddingFor(len(list), position)
92+
93+
renderString("\n")
94+
linkText := renderLinkText(item, position, padding)
95+
renderString(" ")
96+
renderLinkHref(item, linkText)
97+
}
8798
}
8899

89-
if len(ctx.table.tableImages) > 0 {
100+
if len(ctx.table.tableLinks) > 0 {
90101
renderString("\n")
91102
}
92-
for i, image := range ctx.table.tableImages {
103+
renderList(ctx.table.tableLinks)
104+
105+
if len(ctx.table.tableImages) > 0 {
93106
renderString("\n")
94-
linkText := renderLinkText(image, i+1)
95-
renderString(" ")
96-
renderLinkHref(image, linkText)
97107
}
108+
renderList(ctx.table.tableImages)
98109
}
99110

100111
func (e *TableElement) shouldPrintTableLinks(ctx RenderContext) bool {

ansi/testdata/TestRenderer/table_with_footer_links_complex.golden

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ansi/testdata/TestRendererIssues/60.golden

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)