File: row.go

package info (click to toggle)
golang-github-scylladb-termtables 0.0~git20191203.c4c0b6d-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 184 kB
  • sloc: makefile: 2
file content (47 lines) | stat: -rw-r--r-- 1,393 bytes parent folder | download | duplicates (2)
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
}