package platform import ( "strings" ) // fmtMDTable renders a markdown table with column widths padded so the table // is readable as plain text without a markdown renderer. // // headers contains the column header strings. // rows contains data rows; each row must have the same number of cells as headers. // Cells with fewer entries than headers are treated as empty. func fmtMDTable(headers []string, rows [][]string) string { ncols := len(headers) if ncols == 0 { return "" } // Compute max width per column. widths := make([]int, ncols) for i, h := range headers { if len(h) > widths[i] { widths[i] = len(h) } } for _, row := range rows { for i := 0; i < ncols; i++ { cell := "" if i < len(row) { cell = row[i] } if len(cell) > widths[i] { widths[i] = len(cell) } } } var b strings.Builder // Header row. b.WriteByte('|') for i, h := range headers { b.WriteByte(' ') b.WriteString(h) b.WriteString(strings.Repeat(" ", widths[i]-len(h))) b.WriteString(" |") } b.WriteByte('\n') // Separator row. b.WriteByte('|') for i := range headers { b.WriteString(strings.Repeat("-", widths[i]+2)) b.WriteByte('|') } b.WriteByte('\n') // Data rows. for _, row := range rows { b.WriteByte('|') for i := 0; i < ncols; i++ { cell := "" if i < len(row) { cell = row[i] } b.WriteByte(' ') b.WriteString(cell) b.WriteString(strings.Repeat(" ", widths[i]-len(cell))) b.WriteString(" |") } b.WriteByte('\n') } return b.String() }