|
6 | 6 | "io"
|
7 | 7 | "net/url"
|
8 | 8 | "slices"
|
| 9 | + "strconv" |
| 10 | + "strings" |
9 | 11 |
|
10 | 12 | xansi "github.com/charmbracelet/x/ansi"
|
11 | 13 | "github.com/charmbracelet/x/exp/slice"
|
@@ -37,15 +39,15 @@ func (e *TableElement) printTableLinks(ctx RenderContext) {
|
37 | 39 | w := ctx.blockStack.Current().Block
|
38 | 40 | termWidth := int(ctx.blockStack.Width(ctx)) //nolint: gosec
|
39 | 41 |
|
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) |
42 | 44 | style := ctx.options.Styles.LinkText
|
43 | 45 |
|
44 | 46 | switch link.linkType {
|
45 | 47 | case linkTypeAuto, linkTypeRegular:
|
46 |
| - token = fmt.Sprintf("[%d]: %s", position, link.content) |
| 48 | + token += fmt.Sprintf("[%d]: %s", position, link.content) |
47 | 49 | case linkTypeImage:
|
48 |
| - token = link.content |
| 50 | + token += link.content |
49 | 51 | style = ctx.options.Styles.ImageText
|
50 | 52 | style.Prefix = fmt.Sprintf("[%d]: %s", position, style.Prefix)
|
51 | 53 | }
|
@@ -76,25 +78,34 @@ func (e *TableElement) printTableLinks(ctx RenderContext) {
|
76 | 78 | renderText(w, ctx.options.ColorProfile, ctx.blockStack.Current().Style.StylePrimitive, str)
|
77 | 79 | }
|
78 | 80 |
|
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) |
81 | 86 | }
|
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 | + } |
87 | 98 | }
|
88 | 99 |
|
89 |
| - if len(ctx.table.tableImages) > 0 { |
| 100 | + if len(ctx.table.tableLinks) > 0 { |
90 | 101 | renderString("\n")
|
91 | 102 | }
|
92 |
| - for i, image := range ctx.table.tableImages { |
| 103 | + renderList(ctx.table.tableLinks) |
| 104 | + |
| 105 | + if len(ctx.table.tableImages) > 0 { |
93 | 106 | renderString("\n")
|
94 |
| - linkText := renderLinkText(image, i+1) |
95 |
| - renderString(" ") |
96 |
| - renderLinkHref(image, linkText) |
97 | 107 | }
|
| 108 | + renderList(ctx.table.tableImages) |
98 | 109 | }
|
99 | 110 |
|
100 | 111 | func (e *TableElement) shouldPrintTableLinks(ctx RenderContext) bool {
|
|
0 commit comments