Skip to content

Commit

Permalink
Add SetCellColor to allow for colorizing individual cells in conjunct…
Browse files Browse the repository at this point in the history
…ion with column colors
  • Loading branch information
Bryan Nobuhara committed Apr 23, 2019
1 parent a2f290e commit 0709194
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
15 changes: 8 additions & 7 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type Table struct {
borders Border
colSize int
headerParams []string
cellParams [][]string
cellParams []CellColor
columnsParams []string
footerParams []string
columnsAlign []int
Expand Down Expand Up @@ -113,6 +113,7 @@ func NewWriter(writer io.Writer) *Table {
borders: Border{Left: true, Right: true, Bottom: true, Top: true},
colSize: -1,
headerParams: []string{},
cellParams: []CellColor{},
columnsParams: []string{},
footerParams: []string{},
columnsAlign: []int{}}
Expand Down Expand Up @@ -637,7 +638,7 @@ func (t *Table) printRow(columns [][]string, rowIdx int) {

// Checking for ANSI escape sequences for columns
is_esc_seq := false
if len(t.columnsParams) > 0 {
if len(t.columnsParams) > 0 || len(t.cellParams) > 0 {
is_esc_seq = true
}
t.fillAlignment(total)
Expand All @@ -662,8 +663,8 @@ func (t *Table) printRow(columns [][]string, rowIdx int) {

// Embedding escape sequence with column value
if is_esc_seq {
if t.cellParams[x][y] != "" {
str = format(str, t.cellParams[x][y])
if val := t.getColorForCell(y, x+rowIdx); val != "" {
str = format(str, val)
} else {
str = format(str, t.columnsParams[y])
}
Expand Down Expand Up @@ -740,7 +741,7 @@ func (t *Table) printRowMergeCells(writer io.Writer, columns [][]string, rowIdx

// Checking for ANSI escape sequences for columns
is_esc_seq := false
if len(t.columnsParams) > 0 {
if len(t.columnsParams) > 0 || len(t.cellParams) > 0 {
is_esc_seq = true
}
for i, line := range columns {
Expand All @@ -766,8 +767,8 @@ func (t *Table) printRowMergeCells(writer io.Writer, columns [][]string, rowIdx

// Embedding escape sequence with column value
if is_esc_seq {
if t.cellParams[x][y] != "" {
str = format(str, t.cellParams[x][y])
if val := t.getColorForCell(y, x+rowIdx); val != "" {
str = format(str, val)
} else {
str = format(str, t.columnsParams[y])
}
Expand Down
38 changes: 32 additions & 6 deletions table_with_color.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ const (

type Colors []int

// CellColor Color information (ie FgHiBlackColor, FgHiRedColor, etc...)
// For the column and row position in your table
type CellColor struct {
column int
row int
color string
}

func startFormat(seq string) string {
return fmt.Sprintf("%s[%sm", ESC, seq)
}
Expand Down Expand Up @@ -130,13 +138,31 @@ func (t *Table) SetFooterColor(colors ...Colors) {
}

// Adding color for cell (ANSI codes) {
func (t *Table) SetCellColor(x, y int, color Colors) {
if x < 0 || x > t.colSize {
panic(fmt.Sprintf("X Value: %d is outside column size %d", x, t.colSize))
} else if y < 0 || y > len(t.rows) {
panic(fmt.Sprintf("Y Value: %d is outside row size %d", y, len(t.rows)))
// Column and Rows start at 0
// This does NOT do anything for header/footers
func (t *Table) SetCellColor(column, row int, color Colors) {
if column < 0 || column > t.colSize-1 {
panic(fmt.Sprintf("Column Value: %d is outside column size %d", column, t.colSize-1))
} else if row < 0 || row > len(t.lines)-1 {
panic(fmt.Sprintf("Row Value: %d is outside row size %d", row, len(t.lines)-1))
}
t.cellParams[x][y] = makeSequence(color)

t.cellParams = append(t.cellParams, CellColor{
column: column,
row: row,
color: makeSequence(color),
})
}

// Returns makeSequence version of color by cell indices
func (t *Table) getColorForCell(column, row int) string {
for _, colorInfo := range t.cellParams {
if column == colorInfo.column && row == colorInfo.row {
return colorInfo.color
}
}

return ""
}

func Color(colors ...int) []int {
Expand Down

0 comments on commit 0709194

Please sign in to comment.