printRow is responsible for the laying-out, transforming and printing of the string in a single row
(result io.Writer, transformer rowTransformer, rowIndex int, row []string)
| 177 | // printRow is responsible for the laying-out, transforming and |
| 178 | // printing of the string in a single row |
| 179 | func (t *Table) printRow(result io.Writer, transformer rowTransformer, rowIndex int, row []string) error { |
| 180 | |
| 181 | height := t.rowHeight[rowIndex] |
| 182 | |
| 183 | // Compute the index of the last column as the min number of |
| 184 | // cells in the header and cells in the current row. |
| 185 | // Note: math.Min seems to be for float only :( |
| 186 | last := len(t.headers) - 1 |
| 187 | lastr := len(row) - 1 |
| 188 | if lastr < last { |
| 189 | last = lastr |
| 190 | } |
| 191 | |
| 192 | // Note how we always print into a line buffer before placing |
| 193 | // the assembled line into the result. This allows us to trim |
| 194 | // superfluous trailing whitespace from the line before making |
| 195 | // it final. |
| 196 | |
| 197 | if height <= 1 { |
| 198 | // Easy case, all cells in the row are single-line |
| 199 | line := &bytes.Buffer{} |
| 200 | |
| 201 | for columnIndex := range row { |
| 202 | // Truncate long row, ignore the additional fields. |
| 203 | if columnIndex >= len(t.headers) { |
| 204 | break |
| 205 | } |
| 206 | |
| 207 | err := t.printCellValue(line, transformer, columnIndex, last, row[columnIndex]) |
| 208 | if err != nil { |
| 209 | return err |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | fmt.Fprintf(result, "%s\n", trim(string(line.Bytes()))) |
| 214 | return nil |
| 215 | } |
| 216 | |
| 217 | // We have at least one multi-line cell in this row. |
| 218 | // Treat it a bit like a mini-table. |
| 219 | |
| 220 | // Step I. Fill the mini-table. Note how it is stored |
| 221 | // column-major, not row-major. |
| 222 | |
| 223 | // [column][row]string |
| 224 | sub := make([][]string, len(t.headers)) |
| 225 | for columnIndex := range row { |
| 226 | // Truncate long row, ignore the additional fields. |
| 227 | if columnIndex >= len(t.headers) { |
| 228 | break |
| 229 | } |
| 230 | sub[columnIndex] = strings.Split(row[columnIndex], "\n") |
| 231 | // (*) Extend the column to the full height. |
| 232 | for len(sub[columnIndex]) < height { |
| 233 | sub[columnIndex] = append(sub[columnIndex], "") |
| 234 | } |
| 235 | } |
| 236 |
no test coverage detected