Srender renders the TablePrinter as a string.
()
| 164 | |
| 165 | // Srender renders the TablePrinter as a string. |
| 166 | func (p TablePrinter) Srender() (string, error) { |
| 167 | if p.Style == nil { |
| 168 | p.Style = NewStyle() |
| 169 | } |
| 170 | if p.SeparatorStyle == nil { |
| 171 | p.SeparatorStyle = NewStyle() |
| 172 | } |
| 173 | if p.HeaderStyle == nil { |
| 174 | p.HeaderStyle = NewStyle() |
| 175 | } |
| 176 | if p.HeaderRowSeparatorStyle == nil { |
| 177 | p.HeaderRowSeparatorStyle = NewStyle() |
| 178 | } |
| 179 | if p.RowSeparatorStyle == nil { |
| 180 | p.RowSeparatorStyle = NewStyle() |
| 181 | } |
| 182 | |
| 183 | var t table |
| 184 | |
| 185 | // convert data to table and calculate values |
| 186 | for _, rRaw := range p.Data { |
| 187 | var r row |
| 188 | for _, cRaw := range rRaw { |
| 189 | var c cell |
| 190 | c.lines = strings.Split(cRaw, "\n") |
| 191 | c.height = len(c.lines) |
| 192 | for _, l := range c.lines { |
| 193 | if maxWidth := internal.GetStringMaxWidth(l); maxWidth > c.width { |
| 194 | c.width = maxWidth |
| 195 | } |
| 196 | } |
| 197 | r.cells = append(r.cells, c) |
| 198 | if c.height > r.height { |
| 199 | r.height = c.height |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // set max column widths of table |
| 204 | for i, c := range r.cells { |
| 205 | if len(t.maxColumnWidths) <= i { |
| 206 | t.maxColumnWidths = append(t.maxColumnWidths, c.width) |
| 207 | } else if c.width > t.maxColumnWidths[i] { |
| 208 | t.maxColumnWidths[i] = c.width |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | t.rows = append(t.rows, r) |
| 213 | } |
| 214 | |
| 215 | var maxRowWidth int |
| 216 | for _, r := range t.rows { |
| 217 | rowWidth := internal.GetStringMaxWidth(p.renderRow(t, r)) |
| 218 | if rowWidth > maxRowWidth { |
| 219 | maxRowWidth = rowWidth |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // render table |