addressed consistency

This commit is contained in:
natercio committed 2026-10-05 15:08:19 +01:00
1 parent 6703481dbb
commit e167627ac3
8 files changed
+104 -88

No files matched your search

+21 -16
View File
@@ -1,3 +1,6 @@
// Package table renders an internal.AggregatorWriter as the human-readable
// Anexo J report table, for the terminal (Render) and for embedding in HTML
// documents (RenderHTML).
package table
import (
@@ -10,23 +13,22 @@ import (
"github.com/nmoniz/any2anexoj/internal"
)
// PrettyPrinter writes a simple, human readable, table row to the provided io.Writer for each
// translator is the contract the table printer relies on for human-readable
// column labels. *Localizer satisfies it.
type translator interface {
Translate(key string, count int, values map[string]any) string
}
// Printer writes a simple, human readable, table row to the provided io.Writer for each
// ReportItem received. Render and RenderHTML each build a fresh table.Writer so that
// invoking them (in any order or repeatedly) on the same instance never duplicates rows.
type PrettyPrinter struct {
type Printer struct {
output io.Writer
translator Translator
}
<<<<<<<< HEAD:cmd/any2anexoj-cli/pretty_printer.go
type Translator interface {
Translate(key string, count int, values map[string]any) string
}
func NewPrettyPrinter(w io.Writer, tr Translator) *PrettyPrinter {
========
func NewPrettyPrinter(w io.Writer, tr translator) *PrettyPrinter {
return &PrettyPrinter{
func NewPrinter(w io.Writer, tr translator) *Printer {
return &Printer{
output: w,
translator: tr,
}
@@ -37,8 +39,7 @@ func NewPrettyPrinter(w io.Writer, tr translator) *PrettyPrinter {
// multiple times without duplicating rows. The returned table is not wired to
// pp.output via an output mirror; callers that want the rendered output
// written to pp.output must set the mirror themselves before calling .Render().
func (pp *PrettyPrinter) buildTable(aw *internal.AggregatorWriter) table.Writer {
>>>>>>>> 6ba3d78 (writers in writing package):internal/writing/pretty/printer.go
func (pp *Printer) buildTable(aw *internal.AggregatorWriter) table.Writer {
tw := table.NewWriter()
tw.SetAutoIndex(true)
tw.SetStyle(table.StyleLight)
@@ -83,12 +84,16 @@ func (pp *PrettyPrinter) buildTable(aw *internal.AggregatorWriter) table.Writer
}
sumTxt := pp.translator.Translate("sum", 1, nil)
tw.AppendFooter(table.Row{sumTxt, sumTxt, sumTxt, sumTxt, sumTxt, aw.TotalEarned(), "", "", "", aw.TotalSpent(), aw.TotalFees(), aw.TotalTaxes()}, table.RowConfig{AutoMerge: true, AutoMergeAlign: text.AlignRight})
// The footer lists one cell per column (13) so it never relies on
// go-pretty's implicit short-row padding; the trailing empty cell is the
// counter-country column, which has no total. Totals are formatted with
// StringFixed(2) so they match the two-decimal data rows.
tw.AppendFooter(table.Row{sumTxt, sumTxt, sumTxt, sumTxt, sumTxt, aw.TotalEarned().StringFixed(2), "", "", "", aw.TotalSpent().StringFixed(2), aw.TotalFees().StringFixed(2), aw.TotalTaxes().StringFixed(2), ""}, table.RowConfig{AutoMerge: true, AutoMergeAlign: text.AlignRight})
return tw
}
func (pp *PrettyPrinter) Render(aw *internal.AggregatorWriter) {
func (pp *Printer) Render(aw *internal.AggregatorWriter) {
t := pp.buildTable(aw)
t.SetOutputMirror(pp.output)
t.Render()
@@ -96,7 +101,7 @@ func (pp *PrettyPrinter) Render(aw *internal.AggregatorWriter) {
// RenderHTML builds a fresh table from aw and renders it as an HTML string
// using go-pretty's HTMLOptions.
func (pp *PrettyPrinter) RenderHTML(aw *internal.AggregatorWriter) string {
func (pp *Printer) RenderHTML(aw *internal.AggregatorWriter) string {
t := pp.buildTable(aw)
t.Style().HTML = table.HTMLOptions{
CSSClass: "table table-striped table-hover table-bordered",
+5 -5
View File
@@ -14,7 +14,7 @@ import (
"github.com/shopspring/decimal"
)
func TestPrettyPrinter_Render(t *testing.T) {
func TestPrinter_Render(t *testing.T) {
// Create test data
aw := internal.NewAggregatorWriter()
ctx := t.Context()
@@ -64,7 +64,7 @@ func TestPrettyPrinter_Render(t *testing.T) {
// Create writing printer with buffer
var buf bytes.Buffer
pp := table.NewPrettyPrinter(&buf, localizer)
pp := table.NewPrinter(&buf, localizer)
// Render the table
pp.Render(aw)
@@ -81,13 +81,13 @@ func TestPrettyPrinter_Render(t *testing.T) {
│ 1 │ 840 - United States │ G01 │ 2023 │ 6 │ 20 │ 150.75 € │ 2023 │ 1 │ 15 │ 100.50 € │ 2.50 € │ 5.00 € │ 826 - United Kingdom │
│ 2 │ 840 - United States │ G20 │ 2023 │ 9 │ 5 │ 225.50 € │ 2023 │ 3 │ 10 │ 200.00 € │ 3.00 € │ 7.50 € │ 826 - United Kingdom │
├───┼─────────────────────┴──────┴──────┴───────┴─────┼──────────────┼──────┴───────┴─────┼──────────────┼─────────────────┼─────────────────┼──────────────────────┤
│ │ SUM │ 376.25 € │ │ 300.5 € │ 5.5 € │ 12.5 € │ │
│ │ SUM │ 376.25 € │ │ 300.50 € │ 5.50 € │ 12.50 € │ │
└───┴─────────────────────────────────────────────────┴──────────────┴────────────────────┴──────────────┴─────────────────┴─────────────────┴──────────────────────┘
`
// Compare output
if got != want {
t.Errorf("PrettyPrinter.Render() output doesn't match expected.\n\nGot:\n%s\n\nWant:\n%s", got, want)
t.Errorf("Printer.Render() output doesn't match expected.\n\nGot:\n%s\n\nWant:\n%s", got, want)
}
}
@@ -121,7 +121,7 @@ func TestRenderHTML(t *testing.T) {
// Output buffer used only to satisfy the constructor contract; RenderHTML
// must not write to it.
var buf bytes.Buffer
pp := table.NewPrettyPrinter(&buf, localizer)
pp := table.NewPrinter(&buf, localizer)
html := pp.RenderHTML(aw)