Skip to content

Commit

Permalink
Init commit for add color for cell
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan Nobuhara committed Apr 19, 2019
1 parent 7e037d1 commit a2f290e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
13 changes: 11 additions & 2 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type Table struct {
borders Border
colSize int
headerParams []string
cellParams [][]string
columnsParams []string
footerParams []string
columnsAlign []int
Expand Down Expand Up @@ -661,7 +662,11 @@ func (t *Table) printRow(columns [][]string, rowIdx int) {

// Embedding escape sequence with column value
if is_esc_seq {
str = format(str, t.columnsParams[y])
if t.cellParams[x][y] != "" {
str = format(str, t.cellParams[x][y])
} else {
str = format(str, t.columnsParams[y])
}
}

// This would print alignment
Expand Down Expand Up @@ -761,7 +766,11 @@ func (t *Table) printRowMergeCells(writer io.Writer, columns [][]string, rowIdx

// Embedding escape sequence with column value
if is_esc_seq {
str = format(str, t.columnsParams[y])
if t.cellParams[x][y] != "" {
str = format(str, t.cellParams[x][y])
} else {
str = format(str, t.columnsParams[y])
}
}

if t.autoMergeCells {
Expand Down
10 changes: 10 additions & 0 deletions table_with_color.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ 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)))
}
t.cellParams[x][y] = makeSequence(color)
}

func Color(colors ...int) []int {
return colors
}

0 comments on commit a2f290e

Please sign in to comment.