row style

This commit is contained in:
2025-12-28 19:02:00 +03:00
parent b13d113be6
commit 0be8937fea
6 changed files with 50 additions and 9 deletions

View File

@@ -10,12 +10,6 @@ import (
"golang.org/x/image/math/fixed"
)
type TableBlock struct {
Title string
Header []string
Rows []any
}
type Document struct {
Blocks []TableBlock
}

View File

@@ -181,10 +181,19 @@ func DrawTableWarm(header []string, rows []any) image.Image {
y := headerHeight
for rowIndex, row := range rows {
bg := rowBg1
if rowIndex%2 == 1 {
bg = rowBg2
}
sCells := row
switch rt := row.(type) {
case ITableRow:
bg = rt.GetBackgroundColor()
sCells = rt.GetCells()
}
// фон строки
for xx := 0; xx < imgWidth; xx++ {
for yy := 0; yy < rowHeight; yy++ {
@@ -197,7 +206,7 @@ func DrawTableWarm(header []string, rows []any) image.Image {
img.Set(xx, y, dividerCol)
}
cells := rowToStrings(row)
cells := rowToStrings(sCells)
x = 0
for i := 0; i < colCount && i < len(cells); i++ {

View File

@@ -1,6 +1,7 @@
package image_table
import (
"image/color"
"image/png"
"log"
"os"
@@ -44,7 +45,10 @@ func TestDocument(t *testing.T) {
Header: []string{"ID", "Name", "Age"},
Rows: []any{
[]any{1, "Иван", 30},
[]any{2, "Пётр", 25},
&TableBlockStyle{
Cells: []any{2, "Пётр", 25},
BackgroundColor: color.RGBA{R: 225, G: 255, B: 225, A: 255},
},
},
},
{
@@ -52,7 +56,10 @@ func TestDocument(t *testing.T) {
Header: []string{"Метрика", "Значение"},
Rows: []any{
[]any{"Requests", 12000},
[]any{"Errors", 37},
&TableBlockStyle{
Cells: []any{"Errors", 37},
BackgroundColor: color.RGBA{R: 255, G: 225, B: 225, A: 255},
},
},
},
},

8
itablerow.go Normal file
View File

@@ -0,0 +1,8 @@
package image_table
import "image/color"
type ITableRow interface {
GetCells() []any
GetBackgroundColor() color.RGBA
}

7
table_block.go Normal file
View File

@@ -0,0 +1,7 @@
package image_table
type TableBlock struct {
Title string
Header []string
Rows []any
}

16
tablle_block_style.go Normal file
View File

@@ -0,0 +1,16 @@
package image_table
import "image/color"
type TableBlockStyle struct {
Cells []any
BackgroundColor color.RGBA
}
func (s *TableBlockStyle) GetCells() []any {
return s.Cells
}
func (s *TableBlockStyle) GetBackgroundColor() color.RGBA {
return s.BackgroundColor
}