Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions table.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package markdownTable

import "strings"
import (
"strings"
"unicode/utf8"
)

func CreateMarkdownTable(head []string, body [][]string) string {
separator := "|"
columnWidths := make([]int, len(head))

//Calculate maximum column width based on headers
for i, val := range head {
columnWidths[i] = len(val)
columnWidths[i] = utf8.RuneCountInString(val)
}

//Calculate maximum column widths based on body data
for _, row := range body {
for i, val := range row {
if i < len(head) && len(val) > columnWidths[i] {
columnWidths[i] = len(val)
if i < len(head) && utf8.RuneCountInString(val) > columnWidths[i] {
columnWidths[i] = utf8.RuneCountInString(val)
}
}
}
Expand Down Expand Up @@ -52,5 +55,5 @@ func CreateMarkdownTable(head []string, body [][]string) string {

// Helper function to pad a string to the required length
func padString(val string, length int) string {
return val + strings.Repeat(" ", length-len(val))
return val + strings.Repeat(" ", length-utf8.RuneCountInString(val))
}