forked from goodrain/rainbond
-
Notifications
You must be signed in to change notification settings - Fork 2
/
row.go
47 lines (40 loc) · 1.36 KB
/
row.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright 2012 Apcera Inc. All rights reserved.
package termtables
import "strings"
// A Row represents one row of a Table, consisting of some number of Cell
// items.
type Row struct {
cells []*Cell
}
// CreateRow returns a Row where the cells are created as needed to hold each
// item given; each item can be a Cell or content to go into a Cell created
// to hold it.
func CreateRow(items []interface{}) *Row {
row := &Row{cells: []*Cell{}}
for _, item := range items {
row.AddCell(item)
}
return row
}
// AddCell adds one item to a row as a new cell, where the item is either a
// Cell or content to be put into a cell.
func (r *Row) AddCell(item interface{}) {
if c, ok := item.(*Cell); ok {
c.column = len(r.cells)
r.cells = append(r.cells, c)
} else {
r.cells = append(r.cells, createCell(len(r.cells), item, nil))
}
}
// Render returns a string representing the content of one row of a table, where
// the Row contains Cells (not Separators) and the representation includes any
// vertical borders needed.
func (r *Row) Render(style *renderStyle) string {
// pre-render and shove into an array... helps with cleanly adding borders
renderedCells := []string{}
for _, c := range r.cells {
renderedCells = append(renderedCells, c.Render(style))
}
// format final output
return style.BorderY + strings.Join(renderedCells, style.BorderY) + style.BorderY
}