diff --git a/go.mod b/go.mod index 87b0be43a9..e409c13251 100644 --- a/go.mod +++ b/go.mod @@ -1,13 +1,15 @@ module github.com/CortexFoundation/CortexTheseus -go 1.23.4 +go 1.24.0 + +toolchain go1.24.3 require ( github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.6.1 github.com/CortexFoundation/inference v1.0.2-0.20230307032835-9197d586a4e8 github.com/CortexFoundation/statik v0.0.0-20210315012922-8bb8a7b5dc66 github.com/CortexFoundation/torrentfs v1.0.69-0.20250515103356-dcc757b78082 - github.com/VictoriaMetrics/fastcache v1.12.2 + github.com/VictoriaMetrics/fastcache v1.12.4 github.com/arsham/figurine v1.3.0 github.com/aws/aws-sdk-go-v2 v1.36.3 github.com/aws/aws-sdk-go-v2/config v1.29.14 @@ -50,7 +52,7 @@ require ( github.com/muesli/reflow v0.3.0 github.com/muesli/termenv v0.16.0 github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416 - github.com/olekukonko/tablewriter v1.0.6 + github.com/olekukonko/tablewriter v1.0.7 github.com/peterh/liner v1.2.2 github.com/pion/stun/v2 v2.0.0 github.com/rs/cors v1.11.1 @@ -153,7 +155,7 @@ require ( github.com/getsentry/sentry-go v0.33.0 // indirect github.com/go-llsqlite/adapter v0.2.0 // indirect github.com/go-llsqlite/crawshaw v0.5.5 // indirect - github.com/go-logr/logr v1.4.2 // indirect + github.com/go-logr/logr v1.4.3 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/go-ole/go-ole v1.3.0 // indirect github.com/go-resty/resty/v2 v2.16.5 // indirect @@ -196,14 +198,14 @@ require ( github.com/pion/dtls/v2 v2.2.12 // indirect github.com/pion/dtls/v3 v3.0.6 // indirect github.com/pion/ice/v4 v4.0.10 // indirect - github.com/pion/interceptor v0.1.37 // indirect + github.com/pion/interceptor v0.1.38 // indirect github.com/pion/logging v0.2.3 // indirect github.com/pion/mdns/v2 v2.0.7 // indirect github.com/pion/randutil v0.1.0 // indirect github.com/pion/rtcp v1.2.15 // indirect - github.com/pion/rtp v1.8.15 // indirect + github.com/pion/rtp v1.8.16 // indirect github.com/pion/sctp v1.8.39 // indirect - github.com/pion/sdp/v3 v3.0.12 // indirect + github.com/pion/sdp/v3 v3.0.13 // indirect github.com/pion/srtp/v3 v3.0.4 // indirect github.com/pion/stun/v3 v3.0.0 // indirect github.com/pion/transport/v2 v2.2.10 // indirect @@ -246,17 +248,17 @@ require ( go.opentelemetry.io/otel v1.36.0 // indirect go.opentelemetry.io/otel/metric v1.36.0 // indirect go.opentelemetry.io/otel/trace v1.36.0 // indirect - golang.org/x/exp v0.0.0-20250506013437-ce4c2cf36ca6 // indirect + golang.org/x/exp v0.0.0-20250530174510-65e920069ea6 // indirect golang.org/x/mod v0.24.0 // indirect golang.org/x/net v0.40.0 // indirect golang.org/x/term v0.32.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect lukechampine.com/blake3 v1.4.1 // indirect - modernc.org/libc v1.65.7 // indirect + modernc.org/libc v1.65.8 // indirect modernc.org/mathutil v1.7.1 // indirect modernc.org/memory v1.11.0 // indirect modernc.org/sqlite v1.37.1 // indirect rsc.io/tmplfunc v0.0.3 // indirect - zombiezen.com/go/sqlite v1.4.0 // indirect + zombiezen.com/go/sqlite v1.4.2 // indirect ) diff --git a/go.sum b/go.sum index f0dc267196..08917aaaec 100644 --- a/go.sum +++ b/go.sum @@ -96,8 +96,8 @@ github.com/Shopify/sarama v1.26.1/go.mod h1:NbSGBSSndYaIhRcBtY9V0U7AyH+x71bG668A github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI= github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg= github.com/VictoriaMetrics/fastcache v1.5.8-0.20200305212624-8835719dc76c/go.mod h1:ptDBkNMQI4RtmVo8VS/XwRY6RoTu1dAWCbrk+6WsEM8= -github.com/VictoriaMetrics/fastcache v1.12.2 h1:N0y9ASrJ0F6h0QaC3o6uJb3NIZ9VKLjCM7NQbSmF7WI= -github.com/VictoriaMetrics/fastcache v1.12.2/go.mod h1:AmC+Nzz1+3G2eCPapF6UcsnkThDcMsQicp4xDukwJYI= +github.com/VictoriaMetrics/fastcache v1.12.4 h1:2xvmwZBW+9QtHsXggfzAZRs1FZWCsBs8QDg22bMidf0= +github.com/VictoriaMetrics/fastcache v1.12.4/go.mod h1:K+JGPBn0sueFlLjZ8rcVM0cKkWKNElKyQXmw57QOoYI= github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g= github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII= github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c= @@ -334,7 +334,6 @@ github.com/cespare/cp v1.1.1/go.mod h1:SOGHArjBr4JWaSDEVpWpo/hNg6RoKrls6Oh40hiwW github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= -github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/charmbracelet/bubbletea v1.3.5 h1:JAMNLTbqMOhSwoELIr0qyP4VidFq72/6E9j7HHmRKQc= @@ -521,8 +520,8 @@ github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9 github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= -github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= -github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI= +github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM= @@ -907,8 +906,8 @@ github.com/olekukonko/ll v0.0.8/go.mod h1:En+sEW0JNETl26+K8eZ6/W4UQ7CYSrrgg/EdIY github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo= github.com/olekukonko/tablewriter v0.0.4/go.mod h1:zq6QwlOf5SlnkVbMSr5EoBv3636FWnp+qbPhuoO21uA= github.com/olekukonko/tablewriter v0.0.5-0.20200416053754-163badb3bac6/go.mod h1:zq6QwlOf5SlnkVbMSr5EoBv3636FWnp+qbPhuoO21uA= -github.com/olekukonko/tablewriter v1.0.6 h1:/T45mIHc5hcEvibgzBzvMy7ruT+RjgoQRvkHbnl6OWA= -github.com/olekukonko/tablewriter v1.0.6/go.mod h1:SJ0MV1aHb/89fLcsBMXMp30Xg3g5eGoOUu0RptEk4AU= +github.com/olekukonko/tablewriter v1.0.7 h1:HCC2e3MM+2g72M81ZcJU11uciw6z/p82aEnm4/ySDGw= +github.com/olekukonko/tablewriter v1.0.7/go.mod h1:H428M+HzoUXC6JU2Abj9IT9ooRmdq9CxuDmKMtrOCMs= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= @@ -975,8 +974,8 @@ github.com/pion/ice v0.7.13/go.mod h1:U3ERMkJgkPMlBjzMe2XxIQPl6ZrfRHyENwGCBoFrWW github.com/pion/ice v0.7.14/go.mod h1:/Lz6jAUhsvXed7kNJImXtvVSgjtcdGKoZAZIYb9WEm0= github.com/pion/ice/v4 v4.0.10 h1:P59w1iauC/wPk9PdY8Vjl4fOFL5B+USq1+xbDcN6gT4= github.com/pion/ice/v4 v4.0.10/go.mod h1:y3M18aPhIxLlcO/4dn9X8LzLLSma84cx6emMSu14FGw= -github.com/pion/interceptor v0.1.37 h1:aRA8Zpab/wE7/c0O3fh1PqY0AJI3fCSEM5lRWJVorwI= -github.com/pion/interceptor v0.1.37/go.mod h1:JzxbJ4umVTlZAf+/utHzNesY8tmRkM2lVmkS82TTj8Y= +github.com/pion/interceptor v0.1.38 h1:Mgt3XIIq47uR5vcLLahfRucE6tFPjxHak+z5ZZFEzLU= +github.com/pion/interceptor v0.1.38/go.mod h1:HS9X+Ue5LDE6q2C2tuvOuO83XkBdJFgn6MBDtfoJX4Q= github.com/pion/logging v0.2.2/go.mod h1:k0/tDVsRCX2Mb2ZEmTqNa7CWsQPc+YYCB7Q+5pahoms= github.com/pion/logging v0.2.3 h1:gHuf0zpoh1GW67Nr6Gj4cv5Z9ZscU7g/EaoC/Ke/igI= github.com/pion/logging v0.2.3/go.mod h1:z8YfknkquMe1csOrxK5kc+5/ZPAzMxbKLX5aXpbpC90= @@ -991,14 +990,14 @@ github.com/pion/rtcp v1.2.15 h1:LZQi2JbdipLOj4eBjK4wlVoQWfrZbh3Q6eHtWtJBZBo= github.com/pion/rtcp v1.2.15/go.mod h1:jlGuAjHMEXwMUHK78RgX0UmEJFV4zUKOFHR7OP+D3D0= github.com/pion/rtp v1.3.2/go.mod h1:q9wPnA96pu2urCcW/sK/RiDn597bhGoAQQ+y2fDwHuY= github.com/pion/rtp v1.4.0/go.mod h1:/l4cvcKd0D3u9JLs2xSVI95YkfXW87a3br3nqmVtSlE= -github.com/pion/rtp v1.8.15 h1:MuhuGn1cxpVCPLNY1lI7F1tQ8Spntpgf12ob+pOYT8s= -github.com/pion/rtp v1.8.15/go.mod h1:bAu2UFKScgzyFqvUKmbvzSdPr+NGbZtv6UB2hesqXBk= +github.com/pion/rtp v1.8.16 h1:0mpfguLyN9HCpPIXcoOho4BkMsz5eB1Yjvf+obI5cEQ= +github.com/pion/rtp v1.8.16/go.mod h1:bAu2UFKScgzyFqvUKmbvzSdPr+NGbZtv6UB2hesqXBk= github.com/pion/sctp v1.7.6/go.mod h1:ichkYQ5tlgCQwEwvgfdcAolqx1nHbYCxo4D7zK/K0X8= github.com/pion/sctp v1.8.39 h1:PJma40vRHa3UTO3C4MyeJDQ+KIobVYRZQZ0Nt7SjQnE= github.com/pion/sctp v1.8.39/go.mod h1:cNiLdchXra8fHQwmIoqw0MbLLMs+f7uQ+dGMG2gWebE= github.com/pion/sdp/v2 v2.3.7/go.mod h1:+ZZf35r1+zbaWYiZLfPutWfx58DAWcGb2QsS3D/s9M8= -github.com/pion/sdp/v3 v3.0.12 h1:pwUpHQ4W8LhQR37kRVC9YvWa5/GSPqfgxr8ejnwyaL0= -github.com/pion/sdp/v3 v3.0.12/go.mod h1:88GMahN5xnScv1hIMTqLdu/cOcUkj6a9ytbncwMCq2E= +github.com/pion/sdp/v3 v3.0.13 h1:uN3SS2b+QDZnWXgdr69SM8KB4EbcnPnPf2Laxhty/l4= +github.com/pion/sdp/v3 v3.0.13/go.mod h1:88GMahN5xnScv1hIMTqLdu/cOcUkj6a9ytbncwMCq2E= github.com/pion/srtp v1.3.1/go.mod h1:nxEytDDGTN+eNKJ1l5gzOCWQFuksgijorsSlgEjc40Y= github.com/pion/srtp v1.3.2/go.mod h1:snPrfN+gVpRBpmats49oxLWfcFB01eH1N9F+N7+dxKI= github.com/pion/srtp/v3 v3.0.4 h1:2Z6vDVxzrX3UHEgrUyIGM4rRouoC7v+NiF1IHtp9B5M= @@ -1373,8 +1372,8 @@ golang.org/x/exp v0.0.0-20191030013958-a1ab85dbe136/go.mod h1:JXzH8nQsPlswgeRAPE golang.org/x/exp v0.0.0-20191129062945-2f5052295587/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= golang.org/x/exp v0.0.0-20220428152302-39d4317da171/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE= -golang.org/x/exp v0.0.0-20250506013437-ce4c2cf36ca6 h1:y5zboxd6LQAqYIhHnB48p0ByQ/GnQx2BE33L8BOHQkI= -golang.org/x/exp v0.0.0-20250506013437-ce4c2cf36ca6/go.mod h1:U6Lno4MTRCDY+Ba7aCcauB9T60gsv5s4ralQzP72ZoQ= +golang.org/x/exp v0.0.0-20250530174510-65e920069ea6 h1:gllJVKwONftmCc4KlNbN8o/LvmbxotqQy6zzi6yDQOQ= +golang.org/x/exp v0.0.0-20250530174510-65e920069ea6/go.mod h1:U6Lno4MTRCDY+Ba7aCcauB9T60gsv5s4ralQzP72ZoQ= golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= @@ -1560,7 +1559,6 @@ golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw= golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= @@ -1776,8 +1774,8 @@ modernc.org/fileutil v1.3.1 h1:8vq5fe7jdtEvoCf3Zf9Nm0Q05sH6kGx0Op2CPx1wTC8= modernc.org/fileutil v1.3.1/go.mod h1:HxmghZSZVAz/LXcMNwZPA/DRrQZEVP9VX0V4LQGQFOc= modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI= modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito= -modernc.org/libc v1.65.7 h1:Ia9Z4yzZtWNtUIuiPuQ7Qf7kxYrxP1/jeHZzG8bFu00= -modernc.org/libc v1.65.7/go.mod h1:011EQibzzio/VX3ygj1qGFt5kMjP0lHb0qCW5/D/pQU= +modernc.org/libc v1.65.8 h1:7PXRJai0TXZ8uNA3srsmYzmTyrLoHImV5QxHeni108Q= +modernc.org/libc v1.65.8/go.mod h1:011EQibzzio/VX3ygj1qGFt5kMjP0lHb0qCW5/D/pQU= modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU= modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg= modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI= @@ -1802,5 +1800,5 @@ sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o= sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0/go.mod h1:hI742Nqp5OhwiqlzhgfbWU4mW4yO10fP+LoT9WOswdU= sourcegraph.com/sourcegraph/go-diff v0.5.0/go.mod h1:kuch7UrkMzY0X+p9CRK03kfuPQ2zzQcaEFbx8wA8rck= sourcegraph.com/sqs/pbtypes v0.0.0-20180604144634-d3ebe8f20ae4/go.mod h1:ketZ/q3QxT9HOBeFhu6RdvsftgpsbFHBF5Cas6cDKZ0= -zombiezen.com/go/sqlite v1.4.0 h1:N1s3RIljwtp4541Y8rM880qgGIgq3fTD2yks1xftnKU= -zombiezen.com/go/sqlite v1.4.0/go.mod h1:0w9F1DN9IZj9AcLS9YDKMboubCACkwYCGkzoy3eG5ik= +zombiezen.com/go/sqlite v1.4.2 h1:KZXLrBuJ7tKNEm+VJcApLMeQbhmAUOKA5VWS93DfFRo= +zombiezen.com/go/sqlite v1.4.2/go.mod h1:5Kd4taTAD4MkBzT25mQ9uaAlLjyR0rFhsR6iINO70jc= diff --git a/vendor/github.com/VictoriaMetrics/fastcache/file.go b/vendor/github.com/VictoriaMetrics/fastcache/file.go index dfbc0701d9..01f1d36e3a 100644 --- a/vendor/github.com/VictoriaMetrics/fastcache/file.go +++ b/vendor/github.com/VictoriaMetrics/fastcache/file.go @@ -79,8 +79,8 @@ func (c *Cache) SaveToFileConcurrent(filePath string, concurrency int) error { // LoadFromFile loads cache data from the given filePath. // // See SaveToFile* for saving cache data to file. -func LoadFromFile(filePath string) (*Cache, error) { - return load(filePath, 0) +func LoadFromFile(filePath string, maxBytes int) (*Cache, error) { + return load(filePath, maxBytes) } // LoadFromFileOrNew tries loading cache data from the given filePath. @@ -141,7 +141,7 @@ func load(filePath string, maxBytes int) (*Cache, error) { // Read bucket files from filePath dir. d, err := os.Open(filePath) if err != nil { - return nil, fmt.Errorf("cannot open %q: %s", filePath, err) + return nil, fmt.Errorf("cannot open %q: %w", filePath, err) } defer func() { _ = d.Close() @@ -206,7 +206,7 @@ func loadMetadata(dir string) (uint64, error) { metadataPath := dir + "/metadata.bin" metadataFile, err := os.Open(metadataPath) if err != nil { - return 0, fmt.Errorf("cannot open %q: %s", metadataPath, err) + return 0, fmt.Errorf("cannot open %q: %w", metadataPath, err) } defer func() { _ = metadataFile.Close() diff --git a/vendor/github.com/go-logr/logr/.golangci.yaml b/vendor/github.com/go-logr/logr/.golangci.yaml index 0cffafa7bf..0ed62c1a18 100644 --- a/vendor/github.com/go-logr/logr/.golangci.yaml +++ b/vendor/github.com/go-logr/logr/.golangci.yaml @@ -1,26 +1,28 @@ +version: "2" + run: timeout: 1m tests: true linters: - disable-all: true - enable: + default: none + enable: # please keep this alphabetized + - asasalint - asciicheck + - copyloopvar + - dupl - errcheck - forcetypeassert + - goconst - gocritic - - gofmt - - goimports - - gosimple - govet - ineffassign - misspell + - musttag - revive - staticcheck - - typecheck - unused issues: - exclude-use-default: false max-issues-per-linter: 0 max-same-issues: 10 diff --git a/vendor/github.com/go-logr/logr/funcr/funcr.go b/vendor/github.com/go-logr/logr/funcr/funcr.go index 30568e768d..b22c57d713 100644 --- a/vendor/github.com/go-logr/logr/funcr/funcr.go +++ b/vendor/github.com/go-logr/logr/funcr/funcr.go @@ -77,7 +77,7 @@ func newSink(fn func(prefix, args string), formatter Formatter) logr.LogSink { write: fn, } // For skipping fnlogger.Info and fnlogger.Error. - l.Formatter.AddCallDepth(1) + l.AddCallDepth(1) // via Formatter return l } @@ -164,17 +164,17 @@ type fnlogger struct { } func (l fnlogger) WithName(name string) logr.LogSink { - l.Formatter.AddName(name) + l.AddName(name) // via Formatter return &l } func (l fnlogger) WithValues(kvList ...any) logr.LogSink { - l.Formatter.AddValues(kvList) + l.AddValues(kvList) // via Formatter return &l } func (l fnlogger) WithCallDepth(depth int) logr.LogSink { - l.Formatter.AddCallDepth(depth) + l.AddCallDepth(depth) // via Formatter return &l } diff --git a/vendor/github.com/olekukonko/tablewriter/MIGRATION.md b/vendor/github.com/olekukonko/tablewriter/MIGRATION.md new file mode 100644 index 0000000000..6500cd32d9 --- /dev/null +++ b/vendor/github.com/olekukonko/tablewriter/MIGRATION.md @@ -0,0 +1,3042 @@ +# Migration Guide: tablewriter v0.0.5 to v1.0.x +> **NOTE:** This document is work in progress, use with `caution`. This document is a comprehensive guide. For specific issues or advanced scenarios, please refer to the source code or open an issue. + +The `tablewriter` library has undergone a significant redesign between versions **v0.0.5** and **v1.0.x**, transitioning from a primarily method-driven API to a more robust, modular, and configuration-driven framework. This guide provides a detailed roadmap for migrating your v0.0.5 codebase to v1.0.x. It includes mappings of old methods to new approaches, practical examples, and explanations of new features. + +We believe these changes significantly improve the library's flexibility, maintainability, and power, enabling new features and making complex table configurations more manageable. + +## Why Migrate to v1.0.x? + +The v1.0.x redesign enhances `tablewriter`’s flexibility, maintainability, and feature set: +- **Extensibility**: Decoupled rendering supports diverse output formats (e.g., HTML, Markdown, CSV). +- **Robust Configuration**: Centralized `Config` struct and fluent builders ensure atomic, predictable setups. +- **Streaming Capability**: Dedicated API for row-by-row rendering, ideal for large or real-time datasets. +- **Type Safety**: Specific types (e.g., `tw.State`, `tw.Align`) reduce errors and improve clarity. +- **Consistent API**: Unified interface for intuitive usage across simple and complex use cases. +- **New Features**: Hierarchical merging, granular padding, table captions, and fixed column widths. + +These improvements make v1.0.x more powerful, but they require updating code to leverage the new configuration-driven framework and take advantage of advanced functionalities. + +## Key New Features in v1.0.x + +- **Fluent Configuration Builders**: `NewConfigBuilder()` enables chained, readable setups (`config.go:NewConfigBuilder`). +- **Centralized Configuration**: `Config` struct governs table behavior and data processing (`config.go:Config`). +- **Decoupled Renderer**: `tw.Renderer` interface with `tw.Rendition` for visual styling, allowing custom renderers (`tw/renderer.go`). +- **True Streaming Support**: `Start()`, `Append()`, `Close()` for incremental rendering (`stream.go`). +- **Hierarchical Cell Merging**: `tw.MergeHierarchical` for complex data structures (`tw/tw.go:MergeMode` constant, logic in `zoo.go`). +- **Granular Padding Control**: Per-side (`Top`, `Bottom`, `Left`, `Right`) and per-column padding (`tw/cell.go:CellPadding`, `tw/types.go:Padding`). +- **Enhanced Type System**: `tw.State`, `tw.Align`, `tw.Spot`, and others for clarity and safety (`tw/state.go`, `tw/types.go`). +- **Comprehensive Error Handling**: Methods like `Render()` and `Append()` return errors (`tablewriter.go`, `stream.go`). +- **Fixed Column Width System**: `Config.Widths` for precise column sizing, especially in streaming (`config.go:Config`, `tw/cell.go:CellWidth`). +- **Table Captioning**: Flexible placement and styling with `tw.Caption` (`tw/types.go:Caption`). +- **Advanced Data Processing**: Support for `tw.Formatter`, per-column filters, and stringer caching (`tw/cell.go:CellFilter`, `tablewriter.go:WithStringer`). + +## Core Philosophy Changes in v1.0.x + +Understanding these shifts is essential for a successful migration: + +1. **Configuration-Driven Approach**: + - **Old**: Relied on `table.SetXxx()` methods for incremental, stateful modifications to table properties. + - **New**: Table behavior is defined by a `tablewriter.Config` struct (`config.go:Config`), while visual styling is managed by a `tw.Rendition` struct (`tw/renderer.go:Rendition`). These are typically set at table creation using `NewTable()` with `Option` functions or via a fluent `ConfigBuilder`, ensuring atomic and predictable configuration changes. + +2. **Decoupled Rendering Engine**: + - **Old**: Rendering logic was tightly integrated into the `Table` struct, limiting output flexibility. + - **New**: The `tw.Renderer` interface (`tw/renderer.go:Renderer`) defines rendering logic, with `renderer.NewBlueprint()` as the default text-based renderer. The renderer’s appearance (e.g., borders, symbols) is controlled by `tw.Rendition`, enabling support for alternative formats like HTML or Markdown. + +3. **Unified Section Configuration**: + - **Old**: Headers, rows, and footers had separate, inconsistent configuration methods. + - **New**: `tw.CellConfig` (`tw/cell.go:CellConfig`) standardizes configuration across headers, rows, and footers, encompassing formatting (`tw.CellFormatting`), padding (`tw.CellPadding`), column widths (`tw.CellWidth`), alignments (`tw.CellAlignment`), and filters (`tw.CellFilter`). + +4. **Fluent Configuration Builders**: + - **Old**: Configuration was done via individual setters, often requiring multiple method calls. + - **New**: `tablewriter.NewConfigBuilder()` (`config.go:NewConfigBuilder`) provides a chained, fluent API for constructing `Config` objects, with nested builders for `Header()`, `Row()`, `Footer()`, `Alignment()`, `Behavior()`, and `ForColumn()` to simplify complex setups. + +5. **Explicit Streaming Mode**: + - **Old**: No dedicated streaming support; tables were rendered in batch mode. + - **New**: Streaming for row-by-row rendering is enabled via `Config.Stream.Enable` or `WithStreaming(tw.StreamConfig{Enable: true})` and managed with `Table.Start()`, `Table.Append()` (or `Table.Header()`, `Table.Footer()`), and `Table.Close()` (`stream.go`). This is ideal for large datasets or continuous output. + +6. **Enhanced Error Handling**: + - **Old**: Methods like `Render()` did not return errors, making error detection difficult. + - **New**: Key methods (`Render()`, `Start()`, `Close()`, `Append()`, `Bulk()`) return errors to promote robust error handling and improve application reliability (`tablewriter.go`, `stream.go`). + +7. **Richer Type System & `tw` Package**: + - **Old**: Used integer constants (e.g., `ALIGN_CENTER`) and booleans, leading to potential errors. + - **New**: The `tw` sub-package introduces type-safe constructs like `tw.State` (`tw/state.go`), `tw.Align` (`tw/types.go`), `tw.Position` (`tw/types.go`), and `tw.CellConfig` (`tw/cell.go`), replacing magic constants and enhancing code clarity. + +## Configuration Methods in v1.0.x + +v1.0.x offers four flexible methods to configure tables, catering to different use cases and complexity levels. Each method can be used independently or combined, providing versatility for both simple and advanced setups. + +1. **Using `WithConfig` Option**: + - **Description**: Pass a fully populated `tablewriter.Config` struct during `NewTable` initialization using the `WithConfig` option. + - **Use Case**: Ideal for predefined, reusable configurations that can be serialized or shared across multiple tables. + - **Pros**: Explicit, portable, and suitable for complex setups; allows complete control over all configuration aspects. + - **Cons**: Verbose for simple changes, requiring manual struct population. + - **Example**: + +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/tw" + "os" +) + +func main() { + cfg := tablewriter.Config{ + Header: tw.CellConfig{ + Alignment: tw.CellAlignment{Global: tw.AlignCenter}, + Formatting: tw.CellFormatting{AutoFormat: tw.On}, + }, + Row: tw.CellConfig{ + Alignment: tw.CellAlignment{Global: tw.AlignLeft}, + }, + MaxWidth: 80, + Behavior: tw.Behavior{TrimSpace: tw.On}, + } + table := tablewriter.NewTable(os.Stdout, tablewriter.WithConfig(cfg)) + table.Header("Name", "Status") + table.Append("Node1", "Ready") + table.Render() +} +``` +**Output**: + ``` +┌───────┬────────┐ +│ NAME │ STATUS │ +├───────┼────────┤ +│ Node1 │ Ready │ +└───────┴────────┘ + ``` + +2. **Using `Table.Configure` method**: + - **Description**: After creating a `Table` instance, use the `Configure` method with a function that modifies the table's `Config` struct. + - **Use Case**: Suitable for quick, ad-hoc tweaks post-initialization, especially for simple or dynamic adjustments. + - **Pros**: Straightforward for minor changes; no need for additional structs or builders if you already have a `Table` instance. + - **Cons**: Less readable for complex configurations compared to a builder; modifications are applied to an existing instance. + - **Example**: +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/tw" + "os" +) + +func main() { + table := tablewriter.NewTable(os.Stdout) + table.Configure(func(cfg *tablewriter.Config) { + cfg.Header.Alignment.Global = tw.AlignCenter + cfg.Row.Alignment.Global = tw.AlignLeft + }) + + table.Header("Name", "Status") + table.Append("Node1", "Ready") + table.Render() +} +``` +**Output**: Same as above. + +3. **Standalone `Option` Functions**: + - **Description**: Use `WithXxx` functions (e.g., `WithHeaderAlignment`, `WithDebug`) during `NewTable` initialization or via `table.Options()` to apply targeted settings. + - **Use Case**: Best for simple, specific configuration changes without needing a full `Config` struct. + - **Pros**: Concise, readable, and intuitive for common settings; ideal for minimal setups. + - **Cons**: Limited for complex, multi-faceted configurations; requires multiple options for extensive changes. + - **Example**: +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/tw" + "os" +) + +func main() { + table := tablewriter.NewTable(os.Stdout, + tablewriter.WithHeaderAlignment(tw.AlignCenter), + tablewriter.WithRowAlignment(tw.AlignLeft), + tablewriter.WithDebug(true), + ) + table.Header("Name", "Status") + table.Append("Node1", "Ready") + table.Render() +} +``` + **Output**: Same as above. + +4. **Fluent `ConfigBuilder`**: + - **Description**: Use `tablewriter.NewConfigBuilder()` to construct a `Config` struct through a chained, fluent API, then apply it with `WithConfig(builder.Build())`. + - **Use Case**: Optimal for complex, dynamic, or programmatically generated configurations requiring fine-grained control. + - **Pros**: Highly readable, maintainable, and expressive; supports nested builders for sections and columns. + - **Cons**: Slightly verbose; requires understanding builder methods and `Build()` call. + - **Example**: +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/tw" + "os" +) + +func main() { + cnfBuilder := tablewriter.NewConfigBuilder() + cnfBuilder.Header().Alignment().WithGlobal(tw.AlignCenter) + // Example of configuring a specific column (less emphasis on this for now) + // cnfBuilder.ForColumn(0).WithAlignment(tw.AlignLeft).Build() // Call Build() to return to ConfigBuilder + + table := tablewriter.NewTable(os.Stdout, tablewriter.WithConfig(cnfBuilder.Build())) + table.Header("Name", "Status") + table.Append("Node1", "Ready") + table.Render() +} +``` +**Output**: Same as above. + +**Best Practices**: +- Use `WithConfig` or `ConfigBuilder` for complex setups or reusable configurations. +- Opt for `Option` functions for simple, targeted changes. +- Use `Table.Configure` for direct modifications after table creation, but avoid changes during rendering. +- Combine methods as needed (e.g., `ConfigBuilder` for initial setup, `Option` functions for overrides). + +## Default Parameters in v1.0.x + +The `defaultConfig()` function (`config.go:defaultConfig`) establishes baseline settings for new tables, ensuring predictable behavior unless overridden. Below is a detailed table of default parameters, organized by configuration section, to help you understand the starting point for table behavior and appearance. + +| Section | Parameter | Default Value | Description | +|---------------|-------------------------------|-----------------------------------|-----------------------------------------------------------------------------| +| **Header** | `Alignment.Global` | `tw.AlignCenter` | Centers header text globally unless overridden by `PerColumn`. | +| Header | `Alignment.PerColumn` | `[]tw.Align{}` | Empty; falls back to `Global` unless specified. | +| Header | `Formatting.AutoFormat` | `tw.On` | Applies title case (e.g., "col_one" → "COL ONE") to header content. | +| Header | `Formatting.AutoWrap` | `tw.WrapTruncate` | Truncates long header text with "…" based on width constraints. | +| Header | `Formatting.MergeMode` | `tw.MergeNone` | Disables cell merging in headers by default. | +| Header | `Padding.Global` | `tw.PaddingDefault` (`" "`) | Adds one space on left and right of header cells. | +| Header | `Padding.PerColumn` | `[]tw.Padding{}` | Empty; falls back to `Global` unless specified. | +| Header | `ColMaxWidths.Global` | `0` (unlimited) | No maximum content width for header cells unless set. | +| Header | `ColMaxWidths.PerColumn` | `tw.NewMapper[int, int]()` | Empty map; no per-column content width limits unless specified. | +| Header | `Filter.Global` | `nil` | No global content transformation for header cells. | +| Header | `Filter.PerColumn` | `[]func(string) string{}` | No per-column content transformations unless specified. | +| **Row** | `Alignment.Global` | `tw.AlignLeft` | Left-aligns row text globally unless overridden by `PerColumn`. | +| Row | `Alignment.PerColumn` | `[]tw.Align{}` | Empty; falls back to `Global`. | +| Row | `Formatting.AutoFormat` | `tw.Off` | Disables auto-formatting (e.g., title case) for row content. | +| Row | `Formatting.AutoWrap` | `tw.WrapNormal` | Wraps long row text naturally at word boundaries based on width constraints.| +| Row | `Formatting.MergeMode` | `tw.MergeNone` | Disables cell merging in rows by default. | +| Row | `Padding.Global` | `tw.PaddingDefault` (`" "`) | Adds one space on left and right of row cells. | +| Row | `Padding.PerColumn` | `[]tw.Padding{}` | Empty; falls back to `Global`. | +| Row | `ColMaxWidths.Global` | `0` (unlimited) | No maximum content width for row cells. | +| Row | `ColMaxWidths.PerColumn` | `tw.NewMapper[int, int]()` | Empty map; no per-column content width limits. | +| Row | `Filter.Global` | `nil` | No global content transformation for row cells. | +| Row | `Filter.PerColumn` | `[]func(string) string{}` | No per-column content transformations. | +| **Footer** | `Alignment.Global` | `tw.AlignRight` | Right-aligns footer text globally unless overridden by `PerColumn`. | +| Footer | `Alignment.PerColumn` | `[]tw.Align{}` | Empty; falls back to `Global`. | +| Footer | `Formatting.AutoFormat` | `tw.Off` | Disables auto-formatting for footer content. | +| Footer | `Formatting.AutoWrap` | `tw.WrapNormal` | Wraps long footer text naturally. | +| Footer | `Formatting.MergeMode` | `tw.MergeNone` | Disables cell merging in footers. | +| Footer | `Padding.Global` | `tw.PaddingDefault` (`" "`) | Adds one space on left and right of footer cells. | +| Footer | `Padding.PerColumn` | `[]tw.Padding{}` | Empty; falls back to `Global`. | +| Footer | `ColMaxWidths.Global` | `0` (unlimited) | No maximum content width for footer cells. | +| Footer | `ColMaxWidths.PerColumn` | `tw.NewMapper[int, int]()` | Empty map; no per-column content width limits. | +| Footer | `Filter.Global` | `nil` | No global content transformation for footer cells. | +| Footer | `Filter.PerColumn` | `[]func(string) string{}` | No per-column content transformations. | +| **Global** | `MaxWidth` | `0` (unlimited) | No overall table width limit. | +| Global | `Behavior.AutoHide` | `tw.Off` | Displays empty columns (ignored in streaming). | +| Global | `Behavior.TrimSpace` | `tw.On` | Trims leading/trailing spaces from cell content. | +| Global | `Behavior.Header` | `tw.Control{Hide: tw.Off}` | Shows header if content is provided. | +| Global | `Behavior.Footer` | `tw.Control{Hide: tw.Off}` | Shows footer if content is provided. | +| Global | `Behavior.Compact` | `tw.Compact{Merge: tw.Off}` | No compact width optimization for merged cells. | +| Global | `Debug` | `false` | Disables debug logging. | +| Global | `Stream.Enable` | `false` | Disables streaming mode by default. | +| Global | `Widths.Global` | `0` (unlimited) | No fixed column width unless specified. | +| Global | `Widths.PerColumn` | `tw.NewMapper[int, int]()` | Empty map; no per-column fixed widths unless specified. | + +**Notes**: +- Defaults can be overridden using any configuration method. +- `tw.PaddingDefault` is `{Left: " ", Right: " "}` (`tw/preset.go`). +- Alignment within `tw.CellFormatting` is deprecated; `tw.CellAlignment` is preferred. `tw.AlignDefault` falls back to `Global` or `tw.AlignLeft` (`tw/types.go`). +- Streaming mode uses `Widths` for fixed column sizing (`stream.go`). + +## Renderer Types and Customization + +v1.0.x introduces a flexible rendering system via the `tw.Renderer` interface (`tw/renderer.go:Renderer`), allowing for both default text-based rendering and custom output formats. This decouples rendering logic from table data processing, enabling support for diverse formats like HTML, CSV, or JSON. + +### Default Renderer: `renderer.NewBlueprint` +- **Description**: `renderer.NewBlueprint()` creates a text-based renderer. Its visual styles are configured using `tw.Rendition`. +- **Use Case**: Standard terminal or text output with configurable borders, symbols, and separators. +- **Configuration**: Styled via `tw.Rendition`, which controls: + - `Borders`: Outer table borders (`tw.Border`) with `tw.State` for each side (`tw/renderer.go`). + - `Settings`: Internal lines (`tw.Lines`) and separators (`tw.Separators`) (`tw/renderer.go`). + - `Symbols`: Characters for drawing table lines and junctions (`tw.Symbols`) (`tw/symbols.go`). + - **Example**: +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/renderer" // Import the renderer package + "github.com/olekukonko/tablewriter/tw" + "os" +) + +func main() { + table := tablewriter.NewTable(os.Stdout, + tablewriter.WithRenderer(renderer.NewBlueprint()), // Default Blueprint renderer + tablewriter.WithRendition(tw.Rendition{ // Apply custom rendition + Symbols: tw.NewSymbols(tw.StyleRounded), + Borders: tw.Border{Top: tw.On, Bottom: tw.On, Left: tw.On, Right: tw.On}, + Settings: tw.Settings{ + Separators: tw.Separators{BetweenRows: tw.On}, + Lines: tw.Lines{ShowHeaderLine: tw.On}, + }, + }), + ) + table.Header("Name", "Status") + table.Append("Node1", "Ready") + table.Render() +} +``` +**Output**: + ``` + ╭───────┬────────╮ + │ Name │ Status │ + ├───────┼────────┤ + │ Node1 │ Ready │ + ╰───────┴────────╯ + ``` + +### Custom Renderer Implementation +- **Description**: Implement the `tw.Renderer` interface to create custom output formats (e.g., HTML, CSV). +- **Use Case**: Non-text outputs, specialized formatting, or integration with other systems. +- **Interface Methods**: + - `Start(w io.Writer) error`: Initializes rendering. + - `Header(headers [][]string, ctx tw.Formatting)`: Renders header rows. + - `Row(row []string, ctx tw.Formatting)`: Renders a data row. + - `Footer(footers [][]string, ctx tw.Formatting)`: Renders footer rows. + - `Line(ctx tw.Formatting)`: Renders separator lines. + - `Close() error`: Finalizes rendering. + - `Config() tw.Rendition`: Returns renderer's current rendition configuration. + - `Logger(logger *ll.Logger)`: Sets logger for debugging. + - **Example (HTML Renderer)**: + +```go +package main + +import ( + "fmt" + "github.com/olekukonko/ll" // For logger type + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/tw" + "io" + "os" +) + +// BasicHTMLRenderer implements tw.Renderer +type BasicHTMLRenderer struct { + writer io.Writer + config tw.Rendition // Store the rendition + logger *ll.Logger + err error +} + +func (r *BasicHTMLRenderer) Start(w io.Writer) error { + r.writer = w + _, r.err = r.writer.Write([]byte("\n")) + return r.err +} + +// Header expects [][]string for potentially multi-line headers +func (r *BasicHTMLRenderer) Header(headers [][]string, ctx tw.Formatting) { + if r.err != nil { return } + _, r.err = r.writer.Write([]byte(" \n")) + if r.err != nil { return } + // Iterate over cells from the context for the current line + for _, cellCtx := range ctx.Row.Current { + content := fmt.Sprintf(" \n", cellCtx.Data) + _, r.err = r.writer.Write([]byte(content)) + if r.err != nil { return } + } + _, r.err = r.writer.Write([]byte(" \n")) +} + +// Row expects []string for a single line row, but uses ctx for actual data +func (r *BasicHTMLRenderer) Row(row []string, ctx tw.Formatting) { // row param is less relevant here, ctx.Row.Current is key + if r.err != nil { return } + _, r.err = r.writer.Write([]byte(" \n")) + if r.err != nil { return } + for _, cellCtx := range ctx.Row.Current { + content := fmt.Sprintf(" \n", cellCtx.Data) + _, r.err = r.writer.Write([]byte(content)) + if r.err != nil { return } + } + _, r.err = r.writer.Write([]byte(" \n")) +} + +func (r *BasicHTMLRenderer) Footer(footers [][]string, ctx tw.Formatting) { + if r.err != nil { return } + // Similar to Header/Row, using ctx.Row.Current for the footer line data + // The footers [][]string param might be used if the renderer needs multi-line footer logic + r.Row(nil, ctx) // Reusing Row logic, passing nil for the direct row []string as ctx contains the data +} + +func (r *BasicHTMLRenderer) Line(ctx tw.Formatting) { /* No lines in basic HTML */ } + +func (r *BasicHTMLRenderer) Close() error { + if r.err != nil { + return r.err + } + _, r.err = r.writer.Write([]byte("
%s
%s
\n")) + return r.err +} + +func (r *BasicHTMLRenderer) Config() tw.Rendition { return r.config } +func (r *BasicHTMLRenderer) Logger(logger *ll.Logger) { r.logger = logger } + +func main() { + table := tablewriter.NewTable(os.Stdout, tablewriter.WithRenderer(&BasicHTMLRenderer{ + config: tw.Rendition{Symbols: tw.NewSymbols(tw.StyleASCII)}, // Provide a default Rendition + })) + table.Header("Name", "Status") + table.Append("Node1", "Ready") + table.Render() +} +``` +**Output**: +```html + + + + + + + + + +
NAMESTATUS
Node1Ready
+``` + +**Notes**: +- The `renderer.NewBlueprint()` is sufficient for most text-based use cases. +- Custom renderers require implementing all interface methods to handle table structure correctly. `tw.Formatting` (which includes `tw.RowContext`) provides cell content and metadata. + +## Function Mapping Table (v0.0.5 → v1.0.x) + +The following table maps v0.0.5 methods to their v1.0.x equivalents, ensuring a quick reference for migration. All deprecated methods are retained for compatibility but should be replaced with new APIs. + +| v0.0.5 Method | v1.0.x Equivalent(s) | Notes | +|-----------------------------------|--------------------------------------------------------------------------------------|----------------------------------------------------------------------| +| `NewWriter(w)` | `NewTable(w, opts...)` | `NewWriter` deprecated; wraps `NewTable` (`tablewriter.go`). | +| `SetHeader([]string)` | `Header(...any)` | Variadic or slice; supports any type (`tablewriter.go`). | +| `Append([]string)` | `Append(...any)` | Variadic, slice, or struct (`tablewriter.go`). | +| `AppendBulk([][]string)` | `Bulk([]any)` | Slice of rows; supports any type (`tablewriter.go`). | +| `SetFooter([]string)` | `Footer(...any)` | Variadic or slice; supports any type (`tablewriter.go`). | +| `Render()` | `Render()` (returns `error`) | Batch mode; streaming uses `Start/Close` (`tablewriter.go`). | +| `SetBorder(bool)` | `WithRendition(tw.Rendition{Borders: ...})` or `WithBorders(tw.Border)` (deprecated) | Use `tw.Border` (`deprecated.go`, `tw/renderer.go`). | +| `SetRowLine(bool)` | `WithRendition(tw.Rendition{Settings: {Separators: {BetweenRows: tw.On}}})` | `tw.Separators` (`tw/renderer.go`). | +| `SetHeaderLine(bool)` | `WithRendition(tw.Rendition{Settings: {Lines: {ShowHeaderLine: tw.On}}})` | `tw.Lines` (`tw/renderer.go`). | +| `SetColumnSeparator(string)` | `WithRendition(tw.Rendition{Symbols: ...})` or `WithSymbols(tw.Symbols)` (deprecated)| `tw.NewSymbols` or custom `tw.Symbols` (`tw/symbols.go`). | +| `SetCenterSeparator(string)` | `WithRendition(tw.Rendition{Symbols: ...})` or `WithSymbols(tw.Symbols)` (deprecated)| `tw.Symbols` (`tw/symbols.go`). | +| `SetRowSeparator(string)` | `WithRendition(tw.Rendition{Symbols: ...})` or `WithSymbols(tw.Symbols)` (deprecated)| `tw.Symbols` (`tw/symbols.go`). | +| `SetAlignment(int)` | `WithRowAlignment(tw.Align)` or `Config.Row.Alignment.Global` | `tw.Align` type (`config.go`). | +| `SetHeaderAlignment(int)` | `WithHeaderAlignment(tw.Align)` or `Config.Header.Alignment.Global` | `tw.Align` type (`config.go`). | +| `SetAutoFormatHeaders(bool)` | `WithHeaderAutoFormat(tw.State)` or `Config.Header.Formatting.AutoFormat` | `tw.State` (`config.go`). | +| `SetAutoWrapText(bool)` | `WithRowAutoWrap(int)` or `Config.Row.Formatting.AutoWrap` (uses `tw.Wrap...` const) | `tw.WrapNormal`, `tw.WrapTruncate`, etc. (`config.go`). | +| `SetAutoMergeCells(bool)` | `WithRowMergeMode(int)` or `Config.Row.Formatting.MergeMode` (uses `tw.Merge...` const) | Supports `Vertical`, `Hierarchical` (`config.go`). | +| `SetColMinWidth(col, w)` | `WithColumnWidths(tw.NewMapper[int,int]().Set(col, w))` or `Config.Widths.PerColumn` | `Config.Widths` for fixed widths (`config.go`). | +| `SetTablePadding(string)` | Use `Config.
.Padding.Global` with `tw.Padding` | No direct equivalent; manage via cell padding (`tw/cell.go`). | +| `SetDebug(bool)` | `WithDebug(bool)` or `Config.Debug` | Logs via `table.Debug()` (`config.go`). | +| `Clear()` | `Reset()` | Clears data and state (`tablewriter.go`). | +| `SetNoWhiteSpace(bool)` | `WithTrimSpace(tw.Off)` (if `true`) or `WithPadding(tw.PaddingNone)` | Manage via `Config.Behavior.TrimSpace` or padding (`config.go`). | +| `SetColumnColor(Colors)` | Embed ANSI codes in cell data, use `tw.Formatter`, or `Config.
.Filter` | Colors via data or filters (`tw/cell.go`). | +| `SetHeaderColor(Colors)` | Embed ANSI codes in cell data, use `tw.Formatter`, or `Config.Header.Filter` | Colors via data or filters (`tw/cell.go`). | +| `SetFooterColor(Colors)` | Embed ANSI codes in cell data, use `tw.Formatter`, or `Config.Footer.Filter` | Colors via data or filters (`tw/cell.go`). | + +**Notes**: +- Deprecated methods are in `deprecated.go` but should be replaced. +- `tw` package types (e.g., `tw.Align`, `tw.State`) are required for new APIs. +- Examples for each mapping are provided in the migration steps below. + +## Detailed Migration Steps: Initialization, Data Input, Rendering + +This section provides step-by-step guidance for migrating core table operations, with code examples and explanations to ensure clarity. Each step maps v0.0.5 methods to v1.0.x equivalents, highlighting changes, best practices, and potential pitfalls. + +### 1. Table Initialization +Initialization has shifted from `NewWriter` to `NewTable`, which supports flexible configuration via `Option` functions, `Config` structs, or `ConfigBuilder`. + +**Old (v0.0.5):** +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "os" +) + +func main() { + table := tablewriter.NewWriter(os.Stdout) + // ... use table + _ = table // Avoid "declared but not used" +} +``` + +**New (v1.0.x):** +```go +package main + +import ( + "fmt" // Added for FormattableEntry example + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/renderer" // Import renderer + "github.com/olekukonko/tablewriter/tw" + "os" + "strings" // Added for Formatter example +) + +func main() { + // Minimal Setup (Default Configuration) + tableMinimal := tablewriter.NewTable(os.Stdout) + _ = tableMinimal // Avoid "declared but not used" + + // Using Option Functions for Targeted Configuration + tableWithOptions := tablewriter.NewTable(os.Stdout, + tablewriter.WithHeaderAlignment(tw.AlignCenter), // Center header text + tablewriter.WithRowAlignment(tw.AlignLeft), // Left-align row text + tablewriter.WithDebug(true), // Enable debug logging + ) + _ = tableWithOptions // Avoid "declared but not used" + + // Using a Full Config Struct for Comprehensive Control + cfg := tablewriter.Config{ + Header: tw.CellConfig{ + Alignment: tw.CellAlignment{ + Global: tw.AlignCenter, + PerColumn: []tw.Align{tw.AlignLeft, tw.AlignRight}, // Column-specific alignment + }, + Formatting: tw.CellFormatting{AutoFormat: tw.On}, + }, + Row: tw.CellConfig{ + Alignment: tw.CellAlignment{Global: tw.AlignLeft}, + Formatting: tw.CellFormatting{AutoWrap: tw.WrapNormal}, + }, + Footer: tw.CellConfig{ + Alignment: tw.CellAlignment{Global: tw.AlignRight}, + }, + MaxWidth: 80, // Constrain total table width + Behavior: tw.Behavior{ + AutoHide: tw.Off, // Show empty columns + TrimSpace: tw.On, // Trim cell spaces + }, + Widths: tw.CellWidth{ + Global: 20, // Default fixed column width + PerColumn: tw.NewMapper[int, int]().Set(0, 15), // Column 0 fixed at 15 + }, + } + tableWithConfig := tablewriter.NewTable(os.Stdout, tablewriter.WithConfig(cfg)) + _ = tableWithConfig // Avoid "declared but not used" + + // Using ConfigBuilder for Fluent, Complex Configuration + builder := tablewriter.NewConfigBuilder(). + WithMaxWidth(80). + WithAutoHide(tw.Off). + WithTrimSpace(tw.On). + WithDebug(true). // Enable debug logging + Header(). + Alignment(). + WithGlobal(tw.AlignCenter). + Build(). // Returns *ConfigBuilder + Header(). + Formatting(). + WithAutoFormat(tw.On). + WithAutoWrap(tw.WrapTruncate). // Test truncation + WithMergeMode(tw.MergeNone). // Explicit merge mode + Build(). // Returns *HeaderConfigBuilder + Padding(). + WithGlobal(tw.Padding{Left: "[", Right: "]", Overwrite: true}). + Build(). // Returns *HeaderConfigBuilder + Build(). // Returns *ConfigBuilder + Row(). + Formatting(). + WithAutoFormat(tw.On). // Uppercase rows + Build(). // Returns *RowConfigBuilder + Build(). // Returns *ConfigBuilder + Row(). + Alignment(). + WithGlobal(tw.AlignLeft). + Build(). // Returns *ConfigBuilder + Build() // Finalize Config + + tableWithFluent := tablewriter.NewTable(os.Stdout, tablewriter.WithConfig(builder)) + _ = tableWithFluent // Avoid "declared but not used" +} +``` + +**Key Changes**: +- **Deprecation**: `NewWriter` is deprecated but retained for compatibility, internally calling `NewTable` (`tablewriter.go:NewWriter`). Transition to `NewTable` for new code. +- **Flexibility**: `NewTable` accepts an `io.Writer` and variadic `Option` functions (`tablewriter.go:NewTable`), enabling configuration via: + - `WithConfig(Config)`: Applies a complete `Config` struct (`config.go:WithConfig`). + - `WithRenderer(tw.Renderer)`: Sets a custom renderer, defaulting to `renderer.NewBlueprint()` (`tablewriter.go:WithRenderer`). + - `WithRendition(tw.Rendition)`: Configures visual styles for the renderer (`tablewriter.go:WithRendition`). + - `WithStreaming(tw.StreamConfig)`: Enables streaming mode (`tablewriter.go:WithStreaming`). + - Other `WithXxx` functions for specific settings (e.g., `WithHeaderAlignment`, `WithDebug`). +- **ConfigBuilder**: Provides a fluent API for complex setups; `Build()` finalizes the `Config` (`config.go:ConfigBuilder`). +- **Method Chaining in Builder**: Remember to call `Build()` on nested builders to return to the parent builder (e.g., `builder.Header().Alignment().WithGlobal(...).Build().Formatting()...`). + +**Migration Tips**: +- Replace `NewWriter` with `NewTable` and specify configurations explicitly. +- Use `ConfigBuilder` for complex setups or when readability is paramount. +- Apply `Option` functions for quick, targeted changes. +- Ensure `tw` package is imported for types like `tw.Align` and `tw.CellConfig`. +- Verify renderer settings if custom styling is needed, as `renderer.NewBlueprint()` is the default. + +**Potential Pitfalls**: +- **Unintended Defaults**: Without explicit configuration, `defaultConfig()` applies (see Default Parameters), which may differ from v0.0.5 behavior (e.g., `Header.Formatting.AutoFormat = tw.On`). +- **Renderer Absence**: If no renderer is set, `NewTable` defaults to `renderer.NewBlueprint()`; explicitly set for custom formats. +- **ConfigBuilder Errors**: Always call `Build()` at the end of a builder chain and on nested builders; omitting it can lead to incomplete configurations or runtime errors. +- **Concurrent Modification**: Avoid modifying `Table.config` (if using the `Configure` method or direct access) in concurrent scenarios or during rendering to prevent race conditions. + +### 2. Data Input +Data input methods in v1.0.x are more flexible, accepting `any` type for headers, rows, and footers, with robust conversion logic to handle strings, structs, and custom types. + +#### 2.1. Setting Headers +Headers define the table’s column labels and are typically the first data added. + +**Old (v0.0.5):** +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "os" +) + +func main() { + table := tablewriter.NewWriter(os.Stdout) + table.SetHeader([]string{"Name", "Sign", "Rating"}) + // ... +} +``` + +**New (v1.0.x):** +```go +package main + +import ( + "fmt" + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/tw" // For tw.Formatter + "os" + "strings" +) + +// Struct with Formatter +type HeaderData struct { // Renamed to avoid conflict + Label string +} + +func (h HeaderData) Format() string { return strings.ToUpper(h.Label) } // Implements tw.Formatter + +func main() { + table := tablewriter.NewTable(os.Stdout) + + // Variadic Arguments (Preferred for Simplicity) + table.Header("Name", "Sign", "Rating") + + // Slice of Strings + // table.Header([]string{"Name", "Sign", "Rating"}) // Example, comment out if using variadic + + // Slice of Any Type (Flexible) + // table.Header([]any{"Name", "Sign", 123}) // Numbers converted to strings + + // Using Formatter + // table.Header(HeaderData{"name"}, HeaderData{"sign"}, HeaderData{"rating"}) // Outputs "NAME", "SIGN", "RATING" + + table.Append("Example", "Row", "Data") // Add some data to render + table.Render() +} +``` + +**Key Changes**: +- **Method**: `SetHeader([]string)` replaced by `Header(...any)` (`tablewriter.go:Header`). +- **Flexibility**: Accepts variadic arguments or a single slice; supports any type, not just strings. +- **Conversion**: Elements are processed by `processVariadic` (`zoo.go:processVariadic`) and converted to strings via `convertCellsToStrings` (`zoo.go:convertCellsToStrings`), supporting: + - Basic types (e.g., `string`, `int`, `float64`). + - `fmt.Stringer` implementations. + - `tw.Formatter` implementations (custom string conversion). + - Structs (exported fields as cells or single cell if `Stringer`/`Formatter`). +- **Streaming**: In streaming mode, `Header()` renders immediately via `streamRenderHeader` (`stream.go:streamRenderHeader`). +- **Formatting**: Headers are formatted per `Config.Header` settings (e.g., `AutoFormat`, `Alignment`) during rendering (`zoo.go:prepareContent`). + +**Migration Tips**: +- Replace `SetHeader` with `Header`, using variadic arguments for simplicity. +- Use slices for dynamic header lists or when passing from a variable. +- Implement `tw.Formatter` for custom header formatting (e.g., uppercase). +- Ensure header count matches expected columns to avoid width mismatches. +- In streaming mode, call `Header()` before rows, as it fixes column widths (`stream.go`). + +**Potential Pitfalls**: +- **Type Mismatch**: Non-string types are converted to strings; ensure desired formatting (e.g., use `tw.Formatter` for custom types). +- **Streaming Widths**: Headers influence column widths in streaming; set `Config.Widths` explicitly if specific widths are needed (`stream.go:streamCalculateWidths`). +- **Empty Headers**: Missing headers may cause rendering issues; provide placeholders (e.g., `""`) if needed. +- **AutoFormat**: Default `Header.Formatting.AutoFormat = tw.On` applies title case; disable with `WithHeaderAutoFormat(tw.Off)` if unwanted (`config.go`). + +#### 2.2. Appending Rows +Rows represent the table’s data entries, and v1.0.x enhances flexibility with support for varied input types. + +**Old (v0.0.5):** +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "os" +) + +func main() { + table := tablewriter.NewWriter(os.Stdout) + table.SetHeader([]string{"ColA", "ColB", "ColC"}) // Add header for context + table.Append([]string{"A", "The Good", "500"}) + table.Append([]string{"B", "The Very Bad", "288"}) + data := [][]string{ + {"C", "The Ugly", "120"}, + {"D", "The Gopher", "800"}, + } + table.AppendBulk(data) + table.Render() +} +``` + +**New (v1.0.x):** +```go +package main + +import ( + "fmt" + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/tw" // For tw.Formatter + "log" + "os" +) + +// Struct for examples +type Entry struct { + ID string + Label string + Score int +} + +// Struct with Formatter +type FormattableEntry struct { + ID string + Label string + Score int +} + +func (e FormattableEntry) Format() string { return fmt.Sprintf("%s (%d)", e.Label, e.Score) } // Implements tw.Formatter + +func main() { + table := tablewriter.NewTable(os.Stdout) + table.Header("ID", "Description", "Value") // Header for context + + // Variadic Arguments (Single Row) + table.Append("A", "The Good", 500) // Numbers converted to strings + + // Slice of Any Type (Single Row) + table.Append([]any{"B", "The Very Bad", "288"}) + + // Struct with Fields + table.Append(Entry{ID: "C", Label: "The Ugly", Score: 120}) // Fields as cells + + // Struct with Formatter (will produce a single cell for this row based on Format()) + // To make it fit 3 columns, the formatter would need to produce a string that looks like 3 cells, or the table config would need to adapt. + // For this example, let's assume it's meant to be one wide cell, or adjust the header. + // For now, let's simplify and append it to a table with one header. + tableOneCol := tablewriter.NewTable(os.Stdout) + tableOneCol.Header("Formatted Entry") + tableOneCol.Append(FormattableEntry{ID: "D", Label: "The Gopher", Score: 800}) // Single cell "The Gopher (800)" + tableOneCol.Render() + fmt.Println("---") + + + // Re-initialize main table for Bulk example + table = tablewriter.NewTable(os.Stdout) + table.Header("ID", "Description", "Value") + + // Multiple Rows with Bulk + data := []any{ + []any{"E", "The Fast", 300}, + Entry{ID: "F", Label: "The Swift", Score: 400}, // Struct instance + // FormattableEntry{ID: "G", Label: "The Bold", Score: 500}, // Would also be one cell + } + if err := table.Bulk(data); err != nil { + log.Fatalf("Bulk append failed: %v", err) + } + table.Render() +} +``` + +**Key Changes**: +- **Method**: `Append([]string)` replaced by `Append(...any)`; `AppendBulk([][]string)` replaced by `Bulk([]any)` (`tablewriter.go`). +- **Input Flexibility**: `Append` accepts: + - Multiple arguments as cells of a single row. + - A single slice (e.g., `[]string`, `[]any`) as cells of a single row. + - A single struct, processed by `convertItemToCells` (`zoo.go`): + - Uses `tw.Formatter` or `fmt.Stringer` for single-cell output. + - Extracts exported fields as multiple cells otherwise. +- **Bulk Input**: `Bulk` accepts a slice where each element is a row (e.g., `[][]any`, `[]Entry`), processed by `appendSingle` (`zoo.go:appendSingle`). +- **Streaming**: Rows render immediately in streaming mode via `streamAppendRow` (`stream.go:streamAppendRow`). +- **Error Handling**: `Append` and `Bulk` return errors for invalid conversions or streaming issues (`tablewriter.go`). + +**Migration Tips**: +- Replace `Append([]string)` with `Append(...any)` for variadic input. +- Use `Bulk` for multiple rows, ensuring each element is a valid row representation. +- Implement `tw.Formatter` for custom struct formatting to control cell output. +- Match row cell count to headers to avoid alignment issues. +- In streaming mode, append rows after `Start()` and before `Close()` (`stream.go`). + +**Potential Pitfalls**: +- **Cell Count Mismatch**: Uneven row lengths may cause rendering errors; pad with empty strings (e.g., `""`) if needed. +- **Struct Conversion**: Unexported fields are ignored; ensure fields are public or use `Formatter`/`Stringer` (`zoo.go`). +- **Streaming Order**: Rows must be appended after headers in streaming to maintain width consistency (`stream.go`). +- **Error Ignored**: Always check `Bulk` errors, as invalid data can cause failures. + +#### 2.3. Setting Footers +Footers provide summary or closing data for the table, often aligned differently from rows. + +**Old (v0.0.5):** +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "os" +) + +func main() { + table := tablewriter.NewWriter(os.Stdout) + table.SetHeader([]string{"ColA", "ColB", "ColC", "ColD"}) // Add header for context + table.SetFooter([]string{"", "", "Total", "1408"}) + table.Render() +} +``` + +**New (v1.0.x):** +```go +package main + +import ( + "fmt" + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/tw" // For tw.Formatter + "os" +) + +// Using Formatter +type FooterSummary struct { // Renamed to avoid conflict + Label string + Value float64 +} + +func (s FooterSummary) Format() string { return fmt.Sprintf("%s: %.2f", s.Label, s.Value) } // Implements tw.Formatter + +func main() { + table := tablewriter.NewTable(os.Stdout) + table.Header("ColA", "ColB", "ColC", "ColD") // Header for context + + // Variadic Arguments + table.Footer("", "", "Total", 1408) + + // Slice of Any Type + // table.Footer([]any{"", "", "Total", 1408.50}) + + table.Render() // Render this table + + fmt.Println("--- Another Table with Formatter Footer ---") + table2 := tablewriter.NewTable(os.Stdout) + table2.Header("Summary Info") // Single column header + // Using Formatter for a single cell footer + table2.Footer(FooterSummary{Label: "Grand Total", Value: 1408.50}) // Single cell: "Grand Total: 1408.50" + table2.Render() +} +``` + +**Key Changes**: +- **Method**: `SetFooter([]string)` replaced by `Footer(...any)` (`tablewriter.go:Footer`). +- **Input Flexibility**: Like `Header`, accepts variadic arguments, slices, or structs with `Formatter`/`Stringer` support (`zoo.go:processVariadic`). +- **Streaming**: Footers are buffered via `streamStoreFooter` and rendered during `Close()` (`stream.go:streamStoreFooter`, `stream.go:streamRenderFooter`). +- **Formatting**: Controlled by `Config.Footer` settings (e.g., `Alignment`, `AutoWrap`) (`zoo.go:prepareTableSection`). + +**Migration Tips**: +- Replace `SetFooter` with `Footer`, preferring variadic input for simplicity. +- Use `tw.Formatter` for custom footer formatting (e.g., formatted numbers). +- Ensure footer cell count matches headers/rows to maintain alignment. +- In streaming mode, call `Footer()` before `Close()` to include it in rendering. + +**Potential Pitfalls**: +- **Alignment Differences**: Default `Footer.Alignment.Global = tw.AlignRight` differs from rows (`tw.AlignLeft`); adjust if needed (`config.go`). +- **Streaming Timing**: Footers not rendered until `Close()`; ensure `Close()` is called (`stream.go`). +- **Empty Footers**: Missing footers may affect table appearance; use placeholders if needed. + +### 3. Rendering the Table +Rendering has been overhauled to support both batch and streaming modes, with mandatory error handling for robustness. + +**Old (v0.0.5):** +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "os" +) + +func main() { + table := tablewriter.NewWriter(os.Stdout) + table.SetHeader([]string{"Data"}) + table.Append([]string{"Example"}) + table.Render() +} +``` + +**New (v1.0.x) - Batch Mode:** +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "log" + "os" +) + +func main() { + table := tablewriter.NewTable(os.Stdout) + table.Header("Data") + table.Append("Example") + if err := table.Render(); err != nil { + log.Fatalf("Table rendering failed: %v", err) + } +} +``` + +**New (v1.0.x) - Streaming Mode:** +```go +package main + +import ( + "fmt" + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/tw" + "log" + "os" +) + +func main() { + tableStream := tablewriter.NewTable(os.Stdout, + tablewriter.WithConfig(tablewriter.Config{ + Stream: tw.StreamConfig{Enable: true}, + Widths: tw.CellWidth{ + Global: 12, // Fixed column width for streaming + PerColumn: tw.NewMapper[int, int]().Set(0, 15), // Column 0 at 15 + }, + }), + ) + // Alternative: Using WithStreaming Option + // tableStream := tablewriter.NewTable(os.Stdout, + // tablewriter.WithStreaming(tw.StreamConfig{Enable: true}), + // tablewriter.WithColumnMax(12), // Sets Config.Widths.Global + // ) + + if err := tableStream.Start(); err != nil { + log.Fatalf("Failed to start table stream: %v", err) + } + tableStream.Header("Column 1", "Column 2") + for i := 0; i < 3; i++ { + if err := tableStream.Append(fmt.Sprintf("Data %d-1", i), fmt.Sprintf("Data %d-2", i)); err != nil { + log.Printf("Failed to append row %d: %v", i, err) // Log and continue or handle differently + } + } + tableStream.Footer("End", "Summary") + if err := tableStream.Close(); err != nil { + log.Fatalf("Failed to close table stream: %v", err) + } +} +``` + +**Key Changes**: +- **Batch Mode**: + - `Render()` processes all data (headers, rows, footers) in one go (`tablewriter.go:render`). + - Calculates column widths dynamically based on content, `Config.Widths`, `ColMaxWidths`, and `MaxWidth` (`zoo.go:calculateAndNormalizeWidths`). + - Invokes renderer methods: `Start()`, `Header()`, `Row()`, `Footer()`, `Line()`, `Close()` (`tw/renderer.go`). + - Returns errors for invalid configurations or I/O issues. +- **Streaming Mode**: + - Enabled via `Config.Stream.Enable` or `WithStreaming(tw.StreamConfig{Enable: true})` (`tablewriter.go:WithStreaming`). + - `Start()` initializes the stream, fixing column widths based on `Config.Widths` or first data (header/row) (`stream.go:streamCalculateWidths`). + - `Header()`, `Append()` render immediately (`stream.go:streamRenderHeader`, `stream.go:streamAppendRow`). + - `Footer()` buffers data, rendered by `Close()` (`stream.go:streamStoreFooter`, `stream.go:streamRenderFooter`). + - `Close()` finalizes rendering with footer and borders (`stream.go:Close`). + - All methods return errors for robust handling. +- **Error Handling**: Mandatory error checks replace silent failures, improving reliability. + +**Migration Tips**: +- Replace `Render()` calls with error-checked versions in batch mode. +- For streaming, adopt `Start()`, `Append()`, `Close()` workflow, ensuring `Start()` precedes data input. +- Set `Config.Widths` for consistent column widths in streaming mode (`config.go`). +- Use `WithRendition` to customize visual output, as renderer settings are decoupled (`tablewriter.go`). +- Test rendering with small datasets to verify configuration before scaling. + +**Potential Pitfalls**: +- **Missing Error Checks**: Failing to check errors can miss rendering failures; always use `if err != nil`. +- **Streaming Widths**: Widths are fixed after `Start()`; inconsistent data may cause truncation (`stream.go`). +- **Renderer Misconfiguration**: Ensure `tw.Rendition` matches desired output style (`tw/renderer.go`). +- **Incomplete Streaming**: Forgetting `Close()` in streaming mode omits footer and final borders (`stream.go`). +- **Batch vs. Streaming**: Using `Render()` in streaming mode causes errors; use `Start()`/`Close()` instead (`tablewriter.go`). + + +## Styling and Appearance Configuration + +Styling in v1.0.x is split between `tablewriter.Config` for data processing (e.g., alignment, padding, wrapping) and `tw.Rendition` for visual rendering (e.g., borders, symbols, lines). This section details how to migrate v0.0.5 styling methods to v1.0.x, providing examples, best practices, and migration tips to maintain or enhance table appearance. + +### 4.1. Table Styles +Table styles define the visual structure through border and separator characters. In v0.0.5, styles were set via individual separator methods, whereas v1.0.x uses `tw.Rendition.Symbols` for a cohesive approach, offering predefined styles and custom symbol sets. + +**Available Table Styles**: +The `tw.Symbols` interface (`tw/symbols.go`) supports a variety of predefined styles, each tailored to specific use cases, as well as custom configurations for unique requirements. + +| Style Name | Use Case | Symbols Example | Recommended Context | +|----------------|-----------------------------------|-------------------------------------|-----------------------------------------| +| `StyleASCII` | Simple, terminal-friendly | `+`, `-`, `|` | Basic CLI output, minimal dependencies; ensures compatibility across all terminals, including older or non-Unicode systems. | +| `StyleLight` | Clean, lightweight borders | `┌`, `└`, `─`, `│` | Modern terminals, clean and professional aesthetics; suitable for most CLI applications requiring a modern look. | +| `StyleHeavy` | Thick, prominent borders | `┏`, `┗`, `━`, `┃` | High-visibility reports, dashboards, or logs; emphasizes table structure for critical data presentation. | +| `StyleDouble` | Double-line borders | `╔`, `╚`, `═`, `║` | Formal documents, structured data exports; visually distinct for presentations or printed outputs. | +| `StyleRounded` | Rounded corners, aesthetic | `╭`, `╰`, `─`, `│` | User-friendly CLI applications, reports; enhances visual appeal with smooth, rounded edges. | +| `StyleMarkdown`| Markdown-compatible | `|`, `-`, `:` | Documentation, GitHub READMEs, or Markdown-based platforms; ensures proper rendering in Markdown viewers. | +*Note: `StyleBold` was mentioned but not defined in the symbols code, `StyleHeavy` is similar.* + +**Old (v0.0.5):** +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "os" +) + +func main() { + table := tablewriter.NewWriter(os.Stdout) + // table.SetCenterSeparator("*") // Example, actual v0.0.5 method might vary + // table.SetColumnSeparator("!") + // table.SetRowSeparator("=") + // table.SetBorder(true) + table.SetHeader([]string{"Header1", "Header2"}) + table.Append([]string{"Cell1", "Cell2"}) + table.Render() +} +``` + +**New (v1.0.x):** +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/renderer" + "github.com/olekukonko/tablewriter/tw" + "os" +) + +func main() { + // Using a Predefined Style (Rounded Corners for Aesthetic Output) + tableStyled := tablewriter.NewTable(os.Stdout, + tablewriter.WithRenderer(renderer.NewBlueprint()), // Ensure renderer is set + tablewriter.WithRendition(tw.Rendition{ + Symbols: tw.NewSymbols(tw.StyleRounded), // Predefined rounded style + Borders: tw.Border{Top: tw.On, Bottom: tw.On, Left: tw.On, Right: tw.On}, + Settings: tw.Settings{ + Separators: tw.Separators{BetweenRows: tw.On}, // Lines between rows + }, + }), + ) + tableStyled.Header("Name", "Status") + tableStyled.Append("Node1", "Ready") + tableStyled.Render() + + // Using Fully Custom Symbols (Mimicking v0.0.5 Custom Separators) + mySymbols := tw.NewSymbolCustom("my-style"). // Fluent builder for custom symbols + WithCenter("*"). // Junction of lines + WithColumn("!"). // Vertical separator + WithRow("="). // Horizontal separator + WithTopLeft("/"). + WithTopMid("-"). + WithTopRight("\\"). + WithMidLeft("["). + WithMidRight("]"). // Corrected from duplicate WithMidRight("+") + // WithMidMid was not a method in SymbolCustom + WithBottomLeft("\\"). + WithBottomMid("_"). + WithBottomRight("/"). + WithHeaderLeft("("). + WithHeaderMid("-"). + WithHeaderRight(")") // Header-specific + tableCustomSymbols := tablewriter.NewTable(os.Stdout, + tablewriter.WithRenderer(renderer.NewBlueprint()), + tablewriter.WithRendition(tw.Rendition{ + Symbols: mySymbols, + Borders: tw.Border{Top: tw.On, Bottom: tw.On, Left: tw.On, Right: tw.On}, + }), + ) + tableCustomSymbols.Header("Name", "Status") + tableCustomSymbols.Append("Node1", "Ready") + tableCustomSymbols.Render() +} +``` + +**Output (Styled, Rounded):** +``` +╭───────┬────────╮ +│ NAME │ STATUS │ +├───────┼────────┤ +│ Node1 │ Ready │ +╰───────┴────────╯ +``` + +**Output (Custom Symbols):** +``` +/=======-========\ +! NAME ! STATUS ! +[=======*========] +! Node1 ! Ready ! +\=======_========/ +``` + +**Key Changes**: +- **Deprecated Methods**: `SetCenterSeparator`, `SetColumnSeparator`, `SetRowSeparator`, and `SetBorder` are deprecated and moved to `deprecated.go`. These are replaced by `tw.Rendition.Symbols` and `tw.Rendition.Borders` for a unified styling approach (`tablewriter.go`). +- **Symbol System**: The `tw.Symbols` interface defines all drawing characters used for table lines, junctions, and corners (`tw/symbols.go:Symbols`). + - `tw.NewSymbols(tw.BorderStyle)` provides predefined styles (e.g., `tw.StyleASCII`, `tw.StyleMarkdown`, `tw.StyleRounded`) for common use cases. (Note: `tw.Style` should be `tw.BorderStyle`) + - `tw.NewSymbolCustom(name string)` enables fully custom symbol sets via a fluent builder, allowing precise control over each character (e.g., `WithCenter`, `WithTopLeft`). +- **Renderer Dependency**: Styling requires a renderer, set via `WithRenderer`; the default is `renderer.NewBlueprint()` if not specified (`tablewriter.go:WithRenderer`). +- **Border Control**: `tw.Rendition.Borders` uses `tw.State` (`tw.On`, `tw.Off`) to enable or disable borders on each side (Top, Bottom, Left, Right) (`tw/renderer.go:Border`). +- **Extensibility**: Custom styles can be combined with custom renderers for non-text outputs, enhancing flexibility (`tw/renderer.go`). + +**Migration Tips**: +- Replace individual separator setters (`SetCenterSeparator`, etc.) with `tw.NewSymbols` for predefined styles or `tw.NewSymbolCustom` for custom designs to maintain or enhance v0.0.5 styling. +- Use `WithRendition` to apply `tw.Rendition` settings, ensuring a renderer is explicitly set to avoid default behavior (`tablewriter.go`). +- Test table styles in your target environment (e.g., terminal, Markdown viewer, or log file) to ensure compatibility with fonts and display capabilities. +- For Markdown-based outputs (e.g., GitHub READMEs), use `tw.StyleMarkdown` to ensure proper rendering in Markdown parsers (`tw/symbols.go`). +- Combine `tw.Rendition.Symbols` with `tw.Rendition.Borders` and `tw.Rendition.Settings` to replicate or improve v0.0.5’s border and line configurations (`tw/renderer.go`). +- Document custom symbol sets in code comments to aid maintenance, as they can be complex (`tw/symbols.go`). + +**Potential Pitfalls**: +- **Missing Renderer**: If `WithRenderer` is omitted, `NewTable` defaults to `renderer.NewBlueprint` with minimal styling, which may not match v0.0.5’s `SetBorder(true)` behavior; always specify for custom styles (`tablewriter.go`). +- **Incomplete Custom Symbols**: When using `tw.NewSymbolCustom`, failing to set all required symbols (e.g., `TopLeft`, `Center`, `HeaderLeft`) can cause rendering errors or inconsistent visuals; ensure all necessary characters are defined (`tw/symbols.go`). +- **Terminal Compatibility Issues**: Advanced styles like `StyleDouble` or `StyleHeavy` may not render correctly in older terminals or non-Unicode environments; use `StyleASCII` for maximum compatibility across platforms (`tw/symbols.go`). +- **Border and Symbol Mismatch**: Inconsistent `tw.Rendition.Borders` and `tw.Symbols` settings (e.g., enabling borders but using minimal symbols) can lead to visual artifacts; test with small tables to verify alignment (`tw/renderer.go`). +- **Markdown Rendering**: Non-Markdown styles (e.g., `StyleRounded`) may not render correctly in Markdown viewers; use `StyleMarkdown` for documentation or web-based outputs (`tw/symbols.go`). + +### 4.2. Borders and Separator Lines +Borders and internal lines define the table’s structural appearance, controlling the visibility of outer edges and internal divisions. In v0.0.5, these were set via specific methods, while v1.0.x uses `tw.Rendition` fields for a more integrated approach. + +**Old (v0.0.5):** +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "os" +) + +func main() { + table := tablewriter.NewWriter(os.Stdout) + // table.SetBorder(false) // Disable all outer borders + // table.SetRowLine(true) // Enable lines between data rows + // table.SetHeaderLine(true) // Enable line below header + table.SetHeader([]string{"Header1", "Header2"}) + table.Append([]string{"Cell1", "Cell2"}) + table.Render() +} +``` + +**New (v1.0.x):** +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/renderer" + "github.com/olekukonko/tablewriter/tw" + "os" +) + +func main() { + // Standard Bordered Table with Internal Lines + table := tablewriter.NewTable(os.Stdout, + tablewriter.WithRenderer(renderer.NewBlueprint()), + tablewriter.WithRendition(tw.Rendition{ + Borders: tw.Border{ // Outer table borders + Left: tw.On, + Right: tw.On, + Top: tw.On, + Bottom: tw.On, + }, + Settings: tw.Settings{ + Lines: tw.Lines{ // Major internal separator lines + ShowHeaderLine: tw.On, // Line after header + ShowFooterLine: tw.On, // Line before footer (if footer exists) + }, + Separators: tw.Separators{ // General row and column separators + BetweenRows: tw.On, // Horizontal lines between data rows + BetweenColumns: tw.On, // Vertical lines between columns + }, + }, + }), + ) + table.Header("Name", "Status") + table.Append("Node1", "Ready") + table.Render() + + // Borderless Table (kubectl-style, No Lines or Separators) + // Configure the table with a borderless, kubectl-style layout + config := tablewriter.NewConfigBuilder(). + Header(). + Padding(). + WithGlobal(tw.PaddingNone). // No header padding + Build(). + Alignment(). + WithGlobal(tw.AlignLeft). // Left-align header + Build(). + Row(). + Padding(). + WithGlobal(tw.PaddingNone). // No row padding + Build(). + Alignment(). + WithGlobal(tw.AlignLeft). // Left-align rows + Build(). + Footer(). + Padding(). + WithGlobal(tw.PaddingNone). // No footer padding + Build(). + Alignment(). + WithGlobal(tw.AlignLeft). // Left-align footer (if used) + Build(). + WithDebug(true). // Enable debug logging + Build() + + // Create borderless table + tableBorderless := tablewriter.NewTable(os.Stdout, + tablewriter.WithRenderer(renderer.NewBlueprint()), // Assumes valid renderer + tablewriter.WithRendition(tw.Rendition{ + Borders: tw.BorderNone, // No borders + Symbols: tw.NewSymbols(tw.StyleNone), // No border symbols + Settings: tw.Settings{ + Lines: tw.LinesNone, // No header/footer lines + Separators: tw.SeparatorsNone, // No row/column separators + }, + }), + tablewriter.WithConfig(config), + ) + + // Set headers and data + tableBorderless.Header("Name", "Status") + tableBorderless.Append("Node1", "Ready") + tableBorderless.Render() +} +``` + +**Output (Standard Bordered):** +``` +┌───────┬────────┐ +│ Name │ Status │ +├───────┼────────┤ +│ Node1 │ Ready │ +└───────┴────────┘ +``` + +**Output (Borderless):** +``` +NAME STATUS +Node1 Ready +``` + +**Key Changes**: +- **Deprecated Methods**: `SetBorder`, `SetRowLine`, and `SetHeaderLine` are deprecated and moved to `deprecated.go`. These are replaced by fields in `tw.Rendition` (`tw/renderer.go`): + - `Borders`: Controls outer table borders (`tw.Border`) with `tw.State` (`tw.On`, `tw.Off`) for each side (Top, Bottom, Left, Right). + - `Settings.Lines`: Manages major internal lines (`ShowHeaderLine` for header, `ShowFooterLine` for footer) (`tw.Lines`). + - `Settings.Separators`: Controls general separators between rows (`BetweenRows`) and columns (`BetweenColumns`) (`tw.Separators`). +- **Presets for Simplicity**: `tw.BorderNone`, `tw.LinesNone`, and `tw.SeparatorsNone` provide quick configurations for minimal or borderless tables (`tw/preset.go`). +- **Renderer Integration**: Border and line settings are applied via `WithRendition`, requiring a renderer to be set (`tablewriter.go:WithRendition`). +- **Granular Control**: Each border side and line type can be independently configured, offering greater flexibility than v0.0.5’s boolean toggles. +- **Dependency on Symbols**: The appearance of borders and lines depends on `tw.Rendition.Symbols`; ensure compatible symbol sets (`tw/symbols.go`). + +**Migration Tips**: +- Replace `SetBorder(false)` with `tw.Rendition.Borders = tw.BorderNone` to disable all outer borders (`tw/preset.go`). +- Use `tw.Rendition.Settings.Separators.BetweenRows = tw.On` to replicate `SetRowLine(true)`, ensuring row separators are visible (`tw/renderer.go`). +- Set `tw.Rendition.Settings.Lines.ShowHeaderLine = tw.On` to mimic `SetHeaderLine(true)` for a line below the header (`tw/renderer.go`). +- For kubectl-style borderless tables, combine `tw.BorderNone`, `tw.LinesNone`, `tw.SeparatorsNone`, and `WithPadding(tw.PaddingNone)` (applied via `ConfigBuilder` or `WithConfig`) to eliminate all lines and spacing (`tw/preset.go`, `config.go`). +- Test border and line configurations with small tables to verify visual consistency, especially when combining with custom `tw.Symbols`. +- Use `WithDebug(true)` to log rendering details if borders or lines appear incorrectly (`config.go`). + +**Potential Pitfalls**: +- **Separator Absence**: If `tw.Rendition.Settings.Separators.BetweenColumns` is `tw.Off` and borders are disabled, columns may lack visual separation; use `tw.CellPadding` or ensure content spacing (`tw/cell.go`). +- **Line and Border Conflicts**: Mismatched settings (e.g., enabling `ShowHeaderLine` but disabling `Borders.Top`) can cause uneven rendering; align `Borders`, `Lines`, and `Separators` settings (`tw/renderer.go`). +- **Renderer Dependency**: Border settings require a renderer; omitting `WithRenderer` defaults to `renderer.NewBlueprint` with minimal styling, which may not match v0.0.5 expectations (`tablewriter.go`). +- **Streaming Limitations**: In streaming mode, separator rendering is fixed after `Start()`; ensure `tw.Rendition` is set correctly before rendering begins (`stream.go`). +- **Symbol Mismatch**: Using minimal `tw.Symbols` (e.g., `StyleASCII`) with complex `Borders` settings may lead to visual artifacts; test with matching symbol sets (`tw/symbols.go`). + +### 4.3. Alignment +Alignment controls the positioning of text within table cells, now configurable per section (Header, Row, Footer) with both global and per-column options for greater precision. + +**Old (v0.0.5):** +```go +package main + +import ( + "github.com/olekukonko/tablewriter" // Assuming ALIGN_CENTER etc. were constants here + "os" +) + +const ( // Mocking v0.0.5 constants for example completeness + ALIGN_CENTER = 1 // Example values + ALIGN_LEFT = 2 +) + + +func main() { + table := tablewriter.NewWriter(os.Stdout) + // table.SetAlignment(ALIGN_CENTER) // Applied to data rows + // table.SetHeaderAlignment(ALIGN_LEFT) // Applied to header + // No specific footer alignment setter + table.SetHeader([]string{"Header1", "Header2"}) + table.Append([]string{"Cell1", "Cell2"}) + table.Render() +} +``` + +**New (v1.0.x):** +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/tw" + "os" +) + +func main() { + cfg := tablewriter.Config{ + Header: tw.CellConfig{ + Alignment: tw.CellAlignment{Global: tw.AlignLeft}, + }, + Row: tw.CellConfig{ + Alignment: tw.CellAlignment{ + Global: tw.AlignCenter, + PerColumn: []tw.Align{tw.AlignLeft, tw.AlignRight}, // Col 0 left, Col 1 right + }, + }, + Footer: tw.CellConfig{ + Alignment: tw.CellAlignment{Global: tw.AlignRight}, + }, + } + table := tablewriter.NewTable(os.Stdout, tablewriter.WithConfig(cfg)) + + table.Header("Name", "Status") + table.Append("Node1", "Ready") + table.Footer("", "Active") // Ensure footer has content to show alignment + table.Render() +} +``` + +**Output:** +``` +┌───────┬────────┐ +│ NAME │ STATUS │ +├───────┼────────┤ +│ Node1 │ Ready │ +├───────┼────────┤ +│ │ Active │ +└───────┴────────┘ +``` + +**Key Changes**: +- **Deprecated Methods**: `SetAlignment` and `SetHeaderAlignment` are replaced by `WithRowAlignment`, `WithHeaderAlignment`, `WithFooterAlignment`, or direct `Config` settings (`config.go`). These old methods are retained in `deprecated.go` for compatibility but should be migrated. +- **Alignment Structure**: Alignment is managed within `tw.CellConfig.Alignment` (`tw/cell.go:CellAlignment`), which includes: + - `Global`: A single `tw.Align` value applied to all cells in the section (`tw.AlignLeft`, `tw.AlignCenter`, `tw.AlignRight`, `tw.AlignNone`). + - `PerColumn`: A slice of `tw.Align` values for column-specific alignment, overriding `Global` for specified columns. +- **Footer Alignment**: v1.0.x introduces explicit footer alignment via `WithFooterAlignment` or `Config.Footer.Alignment`, addressing v0.0.5’s lack of footer-specific control (`config.go`). +- **Type Safety**: `tw.Align` string constants replace v0.0.5’s integer constants (e.g., `ALIGN_CENTER`), improving clarity and reducing errors (`tw/types.go`). +- **Builder Support**: `ConfigBuilder` provides `Alignment()` methods for each section. `ForColumn(idx).WithAlignment()` applies alignment to a specific column across all sections (`config.go:ConfigBuilder`, `config.go:ColumnConfigBuilder`). +- **Deprecated Fields**: `tw.CellConfig.ColumnAligns` (slice) and `tw.CellFormatting.Alignment` (single value) are supported for backward compatibility but should be migrated to `tw.CellAlignment.Global` and `tw.CellAlignment.PerColumn` (`tw/cell.go`). + +**Migration Tips**: +- Replace `SetAlignment(ALIGN_X)` with `WithRowAlignment(tw.AlignX)` or `Config.Row.Alignment.Global = tw.AlignX` to set row alignment (`config.go`). +- Use `WithHeaderAlignment(tw.AlignX)` for headers and `WithFooterAlignment(tw.AlignX)` for footers to maintain or adjust v0.0.5 behavior (`config.go`). +- Specify per-column alignments with `ConfigBuilder.Row().Alignment().WithPerColumn([]tw.Align{...})` or by setting `Config.Row.Alignment.PerColumn` for fine-grained control (`config.go`). +- Use `ConfigBuilder.ForColumn(idx).WithAlignment(tw.AlignX)` to apply consistent alignment to a specific column across all sections (Header, Row, Footer) (`config.go`). +- Verify alignment settings against defaults (`Header: tw.AlignCenter`, `Row: tw.AlignLeft`, `Footer: tw.AlignRight`) to ensure expected output (`config.go:defaultConfig`). +- Test alignment with varied cell content lengths to confirm readability, especially when combined with wrapping or padding settings (`zoo.go:prepareContent`). + +**Potential Pitfalls**: +- **Alignment Precedence**: `PerColumn` settings override `Global` within a section; ensure column-specific alignments are intentional (`tw/cell.go:CellAlignment`). +- **Deprecated Fields**: Relying on `ColumnAligns` or `tw.CellFormatting.Alignment` is temporary; migrate to `tw.CellAlignment` to avoid future issues (`tw/cell.go`). +- **Cell Count Mismatch**: Rows or footers with fewer cells than headers can cause alignment errors; pad with empty strings (`""`) to match column count (`zoo.go`). +- **Streaming Width Impact**: In streaming mode, alignment depends on fixed column widths set by `Config.Widths`; narrow widths may truncate content, misaligning text (`stream.go:streamCalculateWidths`). +- **Default Misalignment**: The default `Footer.Alignment.Global = tw.AlignRight` differs from rows (`tw.AlignLeft`); explicitly set `WithFooterAlignment` if consistency is needed (`config.go`). + +### 4.4. Auto-Formatting (Header Title Case) +Auto-formatting applies transformations like title case to cell content, primarily for headers to enhance readability, but it can be enabled for rows or footers in v1.0.x. + +**Old (v0.0.5):** +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "os" +) + +func main() { + table := tablewriter.NewWriter(os.Stdout) + // table.SetAutoFormatHeaders(true) // Default: true; applies title case to headers + table.SetHeader([]string{"col_one", "status_report"}) + table.Append([]string{"Node1", "Ready"}) + table.Render() +} +``` + +**New (v1.0.x):** +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/tw" + "os" +) + +func main() { + // Using Direct Config Struct to turn OFF default AutoFormat for Header + cfg := tablewriter.Config{ + Header: tw.CellConfig{Formatting: tw.CellFormatting{AutoFormat: tw.Off}}, // Turn OFF title case for headers + Row: tw.CellConfig{Formatting: tw.CellFormatting{AutoFormat: tw.Off}}, // No formatting for rows (default) + Footer: tw.CellConfig{Formatting: tw.CellFormatting{AutoFormat: tw.Off}}, // No formatting for footers (default) + } + tableNoAutoFormat := tablewriter.NewTable(os.Stdout, tablewriter.WithConfig(cfg)) + + tableNoAutoFormat.Header("col_one", "status_report") // Stays as "col_one", "status_report" + tableNoAutoFormat.Append("Node1", "Ready") + tableNoAutoFormat.Render() + + // Using Option Function for Headers (default is ON, this makes it explicit) + tableWithAutoFormat := tablewriter.NewTable(os.Stdout, + tablewriter.WithHeaderAutoFormat(tw.On), // Explicitly enable title case (default behavior) + ) + + tableWithAutoFormat.Header("col_one", "status_report") // Becomes "COL ONE", "STATUS REPORT" + tableWithAutoFormat.Append("Node1", "Ready") + tableWithAutoFormat.Render() +} +``` + +**Output:** +``` +┌─────────┬───────────────┐ +│ col_one │ status_report │ +├─────────┼───────────────┤ +│ Node1 │ Ready │ +└─────────┴───────────────┘ +┌─────────┬───────────────┐ +│ COL ONE │ STATUS REPORT │ +├─────────┼───────────────┤ +│ Node1 │ Ready │ +└─────────┴───────────────┘ +``` + +**Key Changes**: +- **Method**: `SetAutoFormatHeaders` is replaced by `Config.Header.Formatting.AutoFormat`, or equivalent builder methods (`config.go`). +- **Extended Scope**: Auto-formatting can now be applied to rows and footers via `Config.Row.Formatting.AutoFormat` and `Config.Footer.Formatting.AutoFormat`, unlike v0.0.5’s header-only support (`tw/cell.go`). +- **Type Safety**: `tw.State` (`tw.On`, `tw.Off`, `tw.Unknown`) controls formatting state, replacing boolean flags (`tw/state.go`). +- **Behavior**: When `tw.On`, the `tw.Title` function converts text to uppercase and replaces underscores and some periods with spaces (e.g., "col_one" → "COL ONE") (`tw/fn.go:Title`, `zoo.go:prepareContent`). +- **Defaults**: `Header.Formatting.AutoFormat = tw.On`, `Row.Formatting.AutoFormat = tw.Off`, `Footer.Formatting.AutoFormat = tw.Off` (`config.go:defaultConfig`). + +**Migration Tips**: +- To maintain v0.0.5’s default header title case behavior, no explicit action is needed as `WithHeaderAutoFormat(tw.On)` is the default. If you were using `SetAutoFormatHeaders(false)`, you'd use `WithHeaderAutoFormat(tw.Off)`. +- Explicitly set `WithRowAutoFormat(tw.Off)` or `WithFooterAutoFormat(tw.Off)` if you want to ensure rows and footers remain unformatted, as v1.0.x allows enabling formatting for these sections (`config.go`). +- Use `ConfigBuilder.Header().Formatting().WithAutoFormat(tw.State)` for precise control over formatting per section (`config.go`). +- Test header output with underscores or periods (e.g., "my_column") to verify title case transformation meets expectations. +- For custom formatting beyond title case (e.g., custom capitalization), use `tw.CellFilter` instead of `AutoFormat` (`tw/cell.go`). + +**Potential Pitfalls**: +- **Default Header Formatting**: `Header.Formatting.AutoFormat = tw.On` by default may unexpectedly alter header text (e.g., "col_one" → "COL ONE"); disable with `WithHeaderAutoFormat(tw.Off)` if raw headers are preferred (`config.go`). +- **Row/Footer Formatting**: Enabling `AutoFormat` for rows or footers (not default) applies title case, which may not suit data content; verify with `tw.Off` unless intended (`tw/cell.go`). +- **Filter Conflicts**: Combining `AutoFormat` with `tw.CellFilter` can lead to overlapping transformations; prioritize filters for complex formatting (`zoo.go:prepareContent`). +- **Performance Overhead**: Auto-formatting large datasets may add minor processing time; disable for performance-critical applications (`zoo.go`). + +### 4.5. Text Wrapping +Text wrapping determines how long cell content is handled within width constraints, offering more options in v1.0.x compared to v0.0.5’s binary toggle. + +**Old (v0.0.5):** +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "os" +) + +func main() { + table := tablewriter.NewWriter(os.Stdout) + // table.SetAutoWrapText(true) // Enable normal word wrapping + // table.SetAutoWrapText(false) // Disable wrapping + table.SetHeader([]string{"Long Header Text Example", "Status"}) + table.Append([]string{"This is some very long cell content that might wrap", "Ready"}) + table.Render() +} +``` + +**New (v1.0.x):** +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/tw" // For tw.WrapNormal etc. + "os" +) + +func main() { + // Using Option Functions for Quick Wrapping Settings on a specific section (e.g., Row) + // To actually see wrapping, a MaxWidth for the table or columns is needed. + table := tablewriter.NewTable(os.Stdout, + tablewriter.WithRowAutoWrap(tw.WrapNormal), // Set row wrapping + tablewriter.WithHeaderAutoWrap(tw.WrapTruncate), // Header truncates (default) + tablewriter.WithMaxWidth(30), // Force wrapping by setting table max width + ) + table.Header("Long Header Text", "Status") + table.Append("This is a very long cell content", "Ready") + table.Footer("Summary", "Active") + table.Render() + + // For more fine-grained control (e.g., different wrapping for header, row, footer): + cfgBuilder := tablewriter.NewConfigBuilder() + cfgBuilder.Header().Formatting().WithAutoWrap(tw.WrapTruncate) // Header: Truncate + cfgBuilder.Row().Formatting().WithAutoWrap(tw.WrapNormal) // Row: Normal wrap + cfgBuilder.Footer().Formatting().WithAutoWrap(tw.WrapBreak) // Footer: Break words + cfgBuilder.WithMaxWidth(40) // Overall table width constraint + + tableFullCfg := tablewriter.NewTable(os.Stdout, tablewriter.WithConfig(cfgBuilder.Build())) + tableFullCfg.Header("Another Very Long Header Example That Will Be Truncated", "Info") + tableFullCfg.Append("This is an example of row content that should wrap normally based on available space.", "Detail") + tableFullCfg.Footer("FinalSummaryInformationThatMightBreakAcrossLines", "End") + tableFullCfg.Render() +} +``` + +**Output (tableOpt):** +``` +┌──────────────┬────────┐ +│ LONG HEADER… │ STATUS │ +├──────────────┼────────┤ +│ This is a │ Ready │ +│ very long │ │ +│ cell content │ │ +├──────────────┼────────┤ +│ Summary │ Active │ +└──────────────┴────────┘ +``` +*(Second table output will vary based on content and width)* + +**Key Changes**: +- **Method**: `SetAutoWrapText` is replaced by `Config.
.Formatting.AutoWrap` or specific `With
AutoWrap` options (`config.go`). +- **Wrapping Modes**: `int` constants for `AutoWrap` (e.g., `tw.WrapNone`, `tw.WrapNormal`, `tw.WrapTruncate`, `tw.WrapBreak`) replace v0.0.5’s binary toggle (`tw/tw.go`). +- **Section-Specific Control**: Wrapping is configurable per section (Header, Row, Footer), unlike v0.0.5’s global setting (`tw/cell.go`). +- **Defaults**: `Header: tw.WrapTruncate`, `Row: tw.WrapNormal`, `Footer: tw.WrapNormal` (`config.go:defaultConfig`). +- **Width Dependency**: Wrapping behavior relies on width constraints set by `Config.Widths` (fixed column widths), `Config.
.ColMaxWidths` (max content width), or `Config.MaxWidth` (total table width) (`zoo.go:calculateContentMaxWidth`, `zoo.go:prepareContent`). + +**Migration Tips**: +- Replace `SetAutoWrapText(true)` with `WithRowAutoWrap(tw.WrapNormal)` to maintain v0.0.5’s default wrapping for rows (`config.go`). +- Use `WithHeaderAutoWrap(tw.WrapTruncate)` to align with v1.0.x’s default header behavior, ensuring long headers are truncated (`config.go`). +- Set `Config.Widths` or `Config.MaxWidth` explicitly to enforce wrapping, as unconstrained widths may prevent wrapping (`config.go`). +- Use `ConfigBuilder.
().Formatting().WithAutoWrap(int_wrap_mode)` for precise control over wrapping per section (`config.go`). +- Test wrapping with varied content lengths (e.g., short and long text) to ensure readability and proper width allocation. +- Consider `tw.WrapNormal` for data rows to preserve content integrity, reserving `tw.WrapTruncate` for headers or footers (`tw/tw.go`). + +**Potential Pitfalls**: +- **Missing Width Constraints**: Without `Config.Widths`, `ColMaxWidths`, or `MaxWidth`, wrapping may not occur, leading to overflow; always define width limits for wrapping (`zoo.go`). +- **Streaming Width Impact**: In streaming mode, wrapping depends on fixed widths set at `Start()`; narrow widths may truncate content excessively (`stream.go:streamCalculateWidths`). +- **Truncation Data Loss**: `tw.WrapTruncate` may obscure critical data in rows; use `tw.WrapNormal` or wider columns to retain content (`tw/tw.go`). +- **Performance Overhead**: Wrapping large datasets with `tw.WrapNormal` or `tw.WrapBreak` can add processing time; optimize widths for performance-critical applications (`zoo.go:prepareContent`). +- **Inconsistent Section Wrapping**: Default wrapping differs (`Header: tw.WrapTruncate`, `Row/Footer: tw.WrapNormal`); align settings if uniform behavior is needed (`config.go`). + +### 4.6. Padding +Padding adds spacing within cells, enhancing readability and affecting cell width calculations. v1.0.x introduces granular, per-side padding, replacing v0.0.5’s single inter-column padding control. + +**Old (v0.0.5):** +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "os" +) + +func main() { + table := tablewriter.NewWriter(os.Stdout) + // table.SetTablePadding("\t") // Set inter-column space character when borders are off + // Default: single space within cells + table.SetHeader([]string{"Header1"}) + table.Append([]string{"Cell1"}) + table.Render() +} +``` + +**New (v1.0.x):** +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/tw" + "os" +) + +func main() { + // Using Direct Config Struct + cfg := tablewriter.Config{ + Header: tw.CellConfig{ + Padding: tw.CellPadding{ + Global: tw.Padding{Left: "[", Right: "]", Top: "-", Bottom: "-"}, // Padding for all header cells + PerColumn: []tw.Padding{ // Specific padding for header column 0 + {Left: ">>", Right: "<<", Top: "=", Bottom: "="}, // Overrides Global for column 0 + }, + }, + }, + Row: tw.CellConfig{ + Padding: tw.CellPadding{ + Global: tw.PaddingDefault, // One space left/right for all row cells + }, + }, + Footer: tw.CellConfig{ + Padding: tw.CellPadding{ + Global: tw.Padding{Top: "~", Bottom: "~"}, // Top/bottom padding for all footer cells + }, + }, + } + table := tablewriter.NewTable(os.Stdout, tablewriter.WithConfig(cfg)) + + table.Header("Name", "Status") // Two columns + table.Append("Node1", "Ready") + table.Footer("End", "Active") + table.Render() +} +``` + +**Output:** +``` +┌────────┬────────┐ +│========│[------]│ +│>>NAME<<│[STATUS]│ +│========│[------]│ +├────────┼────────┤ +│ Node1 │ Ready │ +│~~~~~~~~│~~~~~~~~│ +│End │Active │ +│~~~~~~~~│~~~~~~~~│ +└────────┴────────┘ +``` + +**Key Changes**: +- **No Direct Equivalent for `SetTablePadding`**: `SetTablePadding` controlled inter-column spacing when borders were off in v0.0.5; v1.0.x has no direct equivalent for *inter-column* spacing separate from cell padding. Inter-column visual separation is now primarily handled by `tw.Rendition.Settings.Separators.BetweenColumns` and the chosen `tw.Symbols`. +- **Granular Cell Padding**: `tw.CellPadding` (`tw/cell.go:CellPadding`) supports: + - `Global`: A `tw.Padding` struct with `Left`, `Right`, `Top`, `Bottom` strings and an `Overwrite` flag (`tw/types.go:Padding`). This padding is *inside* the cell. + - `PerColumn`: A slice of `tw.Padding` for column-specific padding, overriding `Global` for specified columns. +- **Per-Side Control**: `Top` and `Bottom` padding add extra lines *within* cells, unlike v0.0.5’s left/right-only spacing (`zoo.go:prepareContent`). +- **Defaults**: `tw.PaddingDefault` is `{Left: " ", Right: " "}` for all sections (applied inside cells); `Top` and `Bottom` are empty by default (`tw/preset.go`). +- **Width Impact**: Cell padding contributes to column widths, calculated in `Config.Widths` (`zoo.go:calculateAndNormalizeWidths`). +- **Presets**: `tw.PaddingNone` (`{Left: "", Right: "", Top: "", Bottom: "", Overwrite: true}`) removes padding for tight layouts (`tw/preset.go`). + +**Migration Tips**: +- To achieve spacing similar to `SetTablePadding("\t")` when borders are off, you would set cell padding: `WithPadding(tw.Padding{Left: "\t", Right: "\t"})`. If you truly mean space *between* columns and not *inside* cells, ensure `tw.Rendition.Settings.Separators.BetweenColumns` is `tw.On` and customize `tw.Symbols.Column()` if needed. +- Use `tw.PaddingNone` (e.g., via `ConfigBuilder.
().Padding().WithGlobal(tw.PaddingNone)`) for no cell padding. +- Set `Top` and `Bottom` padding for vertical spacing *within* cells, enhancing readability for multi-line content (`tw/types.go`). +- Use `ConfigBuilder.
().Padding().WithPerColumn` for column-specific padding to differentiate sections or columns (`config.go`). +- Test padding with varied content and widths to ensure proper alignment and spacing, especially with wrapping enabled (`zoo.go`). +- Combine padding with `Config.Widths` or `ColMaxWidths` to control total cell size (`config.go`). + +**Potential Pitfalls**: +- **Inter-Column Spacing vs. Cell Padding**: Be clear whether you want space *between* columns (separators) or *inside* cells (padding). `SetTablePadding` was ambiguous; v1.0.x distinguishes these. +- **Width Inflation**: Cell padding increases column widths, potentially exceeding `Config.MaxWidth` or causing truncation in streaming; adjust `Config.Widths` accordingly (`zoo.go`). +- **Top/Bottom Padding**: Non-empty `Top` or `Bottom` padding adds vertical lines *within* cells, increasing cell height; use sparingly for dense tables (`zoo.go:prepareContent`). +- **Streaming Constraints**: Padding is fixed in streaming mode after `Start()`; ensure `Config.Widths` accommodates padding (`stream.go`). +- **Default Padding**: `tw.PaddingDefault` adds spaces *inside* cells; set `tw.PaddingNone` for no internal cell padding (`tw/preset.go`). + +### 4.7. Column Widths (Fixed Widths and Max Content Widths) +Column width control in v1.0.x is more sophisticated, offering fixed widths, maximum content widths, and overall table width constraints, replacing v0.0.5’s limited `SetColMinWidth`. + +**Old (v0.0.5):** +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "os" +) + +func main() { + table := tablewriter.NewWriter(os.Stdout) + // table.SetColMinWidth(0, 10) // Set minimum width for column 0 + table.SetHeader([]string{"Header1", "Header2"}) + table.Append([]string{"Cell1-Content", "Cell2-Content"}) + table.Render() +} +``` + +**New (v1.0.x):** +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/tw" + "os" +) + +func main() { + // Direct Config for Width Control + cfg := tablewriter.Config{ + Widths: tw.CellWidth{ // Fixed total column widths (content + padding) + Global: 20, // Default fixed width for all columns + PerColumn: tw.NewMapper[int, int]().Set(0, 15), // Column 0 fixed at 15 + }, + Header: tw.CellConfig{ + ColMaxWidths: tw.CellWidth{ // Max content width (excluding padding) for header cells + Global: 15, // Max content width for all header cells + PerColumn: tw.NewMapper[int, int]().Set(0, 10), // Header Col 0 max content at 10 + }, + // Default header padding is " " on left/right, so content 10 + padding 2 = 12. + // If Widths.PerColumn[0] is 15, there's space. + }, + Row: tw.CellConfig{ + ColMaxWidths: tw.CellWidth{Global: 18}, // Max content width for row cells (18 + default padding 2 = 20) + }, + MaxWidth: 80, // Constrain total table width (optional, columns might shrink) + } + tableWithCfg := tablewriter.NewTable(os.Stdout, tablewriter.WithConfig(cfg)) + tableWithCfg.Header("Very Long Header Name", "Status Information") + tableWithCfg.Append("Some long content for the first column", "Ready") + tableWithCfg.Render() + + // Option Functions for Width Settings + tableWithOpts := tablewriter.NewTable(os.Stdout, + tablewriter.WithColumnMax(20), // Sets Config.Widths.Global (fixed total col width) + tablewriter.WithColumnWidths(tw.NewMapper[int, int]().Set(0, 15)), // Sets Config.Widths.PerColumn for col 0 + tablewriter.WithMaxWidth(80), // Sets Config.MaxWidth + // For max content width per section, you'd use WithHeaderConfig, WithRowConfig, etc. + // e.g., tablewriter.WithRowMaxWidth(18) // Sets Config.Row.ColMaxWidths.Global + ) + tableWithOpts.Header("Long Header", "Status") + tableWithOpts.Append("Long Content", "Ready") + tableWithOpts.Render() +} +``` + +**Output (tableWithCfg - illustrative, exact wrapping depends on content and full config):** +``` +┌───────────┬──────────────────┐ +│ VERY LONG │ STATUS INFORMAT… │ +│ HEADER NA…│ │ +├───────────┼──────────────────┤ +│ Some long │ Ready │ +│ content f…│ │ +└───────────┴──────────────────┘ +``` + +**Key Changes**: +- **Enhanced Width System**: v1.0.x introduces three levels of width control, replacing `SetColMinWidth` (`config.go`): + - **Config.Widths**: Sets fixed total widths (content + padding) for columns, applied globally or per-column (`tw.CellWidth`). + - `Global`: Default fixed width for all columns. + - `PerColumn`: `tw.Mapper[int, int]` for specific column widths. + - **Config.
.ColMaxWidths**: Sets maximum content widths (excluding padding) for a section (Header, Row, Footer) (`tw.CellWidth`). + - `Global`: Max content width for all cells in the section. + - `PerColumn`: `tw.Mapper[int, int]` for specific columns in the section. + - **Config.MaxWidth**: Constrains the total table width, shrinking columns proportionally if needed (`config.go`). +- **Streaming Support**: In streaming mode, `Config.Widths` fixes column widths at `Start()`; `ColMaxWidths` is used only for wrapping/truncation (`stream.go:streamCalculateWidths`). +- **Calculation Logic**: Widths are computed by `calculateAndNormalizeWidths` in batch mode and `streamCalculateWidths` in streaming mode, considering content, padding, and constraints (`zoo.go`, `stream.go`). +- **Deprecated Approach**: `SetColMinWidth` is replaced by `Config.Widths.PerColumn` or `Config.
.ColMaxWidths.PerColumn` for more precise control (`deprecated.go`). + +**Migration Tips**: +- Replace `SetColMinWidth(col, w)` with `WithColumnWidths(tw.NewMapper[int, int]().Set(col, w))` for fixed column widths or `Config.
.ColMaxWidths.PerColumn` for content width limits (`config.go`). +- Use `Config.Widths.Global` or `WithColumnMax(w)` to set a default fixed width for all columns, ensuring consistency (`tablewriter.go`). +- Apply `Config.MaxWidth` to constrain total table width, especially for wide datasets (`config.go`). +- Use `ConfigBuilder.ForColumn(idx).WithMaxWidth(w)` to set per-column content width limits across sections (`config.go`). *(Note: This sets it for Header, Row, and Footer)* +- In streaming mode, set `Config.Widths` before `Start()` to fix widths, avoiding content-based sizing (`stream.go`). +- Test width settings with varied content to ensure wrapping and truncation behave as expected (`zoo.go`). + +**Potential Pitfalls**: +- **Width Precedence**: `Config.Widths.PerColumn` overrides `Widths.Global`; `ColMaxWidths` applies *within* those fixed total widths for content wrapping/truncation (`zoo.go`). +- **Streaming Width Fixing**: Widths are locked after `Start()` in streaming; inconsistent data may cause truncation (`stream.go`). +- **Padding Impact**: Padding adds to total width when considering `Config.Widths`; account for `tw.CellPadding` when setting fixed column widths (`zoo.go`). +- **MaxWidth Shrinkage**: `Config.MaxWidth` may shrink columns unevenly; test with `MaxWidth` to avoid cramped layouts (`zoo.go`). +- **No Width Constraints**: Without `Widths` or `MaxWidth`, columns size to content, potentially causing overflow; define limits (`zoo.go`). + +### 4.8. Colors +Colors in v0.0.5 were applied via specific color-setting methods, while v1.0.x embeds ANSI escape codes in cell content or uses data-driven formatting for greater flexibility. + +**Old (v0.0.5):** +```go +package main +// Assuming tablewriter.Colors and color constants existed in v0.0.5 +// This is a mock representation as the actual v0.0.5 definitions are not provided. +// import "github.com/olekukonko/tablewriter" +// import "os" + +// type Colors []interface{} // Mock +// const ( +// Bold = 1; FgGreenColor = 2; FgRedColor = 3 // Mock constants +// ) + +func main() { + // table := tablewriter.NewWriter(os.Stdout) + // table.SetColumnColor( + // tablewriter.Colors{tablewriter.Bold, tablewriter.FgGreenColor}, // Column 0 + // tablewriter.Colors{tablewriter.FgRedColor}, // Column 1 + // ) + // table.SetHeader([]string{"Name", "Status"}) + // table.Append([]string{"Node1", "Ready"}) + // table.Render() +} +``` + +**New (v1.0.x):** +```go +package main + +import ( + "fmt" + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/tw" // For tw.Formatter + "os" +) + +// Direct ANSI Code Embedding +const ( + Reset = "\033[0m" + Bold = "\033[1m" + FgGreen = "\033[32m" + FgRed = "\033[31m" +) + +// Using tw.Formatter for Custom Types +type Status string + +func (s Status) Format() string { // Implements tw.Formatter + color := FgGreen + if s == "Error" || s == "Inactive" { + color = FgRed + } + return color + string(s) + Reset +} + +func main() { + // Example 1: Direct ANSI embedding + tableDirectANSI := tablewriter.NewTable(os.Stdout, + tablewriter.WithHeaderAutoFormat(tw.Off), // Keep header text as is for coloring + ) + tableDirectANSI.Header(Bold+FgGreen+"Name"+Reset, Bold+FgRed+"Status"+Reset) + tableDirectANSI.Append([]any{"Node1", FgGreen + "Ready" + Reset}) + tableDirectANSI.Append([]any{"Node2", FgRed + "Error" + Reset}) + tableDirectANSI.Render() + + fmt.Println("\n--- Table with Formatter for Colors ---") + + // Example 2: Using tw.Formatter + tableFormatter := tablewriter.NewTable(os.Stdout) + tableFormatter.Header("Name", "Status") // AutoFormat will apply to "NAME", "STATUS" + tableFormatter.Append([]any{"Alice", Status("Active")}) + tableFormatter.Append([]any{"Bob", Status("Inactive")}) + tableFormatter.Render() + + fmt.Println("\n--- Table with CellFilter for Colors ---") + // Example 3: Using CellFilter + tableWithFilters := tablewriter.NewTable(os.Stdout, + tablewriter.WithConfig( + tablewriter.NewConfigBuilder(). + Row(). + Filter(). + WithPerColumn([]func(string) string{ + nil, // No filter for Item column + func(s string) string { // Status column: apply color + if s == "Ready" || s == "Active" { + return FgGreen + s + Reset + } + return FgRed + s + Reset + }, + }). + Build(). // Return to RowConfigBuilder + Build(). // Return to ConfigBuilder + Build(), // Finalize Config + ), + ) + tableWithFilters.Header("Item", "Availability") + tableWithFilters.Append("ItemA", "Ready") + tableWithFilters.Append("ItemB", "Unavailable") + tableWithFilters.Render() +} +``` + +**Output (Text Approximation, Colors Not Shown):** +``` +┌──────┬────────┐ +│ Name │ Status │ +├──────┼────────┤ +│Node1 │ Ready │ +│Node2 │ Error │ +└──────┴────────┘ + +--- Table with Formatter for Colors --- +┌───────┬──────────┐ +│ NAME │ STATUS │ +├───────┼──────────┤ +│ Alice │ Active │ +│ Bob │ Inactive │ +└───────┴──────────┘ + +--- Table with CellFilter for Colors --- +┌───────┬────────────┐ +│ ITEM │ AVAILABILI │ +│ │ TY │ +├───────┼────────────┤ +│ ItemA │ Ready │ +│ ItemB │ Unavailabl │ +│ │ e │ +└───────┴────────────┘ +``` + +**Key Changes**: +- **Removed Color Methods**: `SetColumnColor`, `SetHeaderColor`, and `SetFooterColor` are removed; colors are now applied by embedding ANSI escape codes in cell content or via data-driven mechanisms (`tablewriter.go`). +- **Flexible Coloring Options**: + - **Direct ANSI Codes**: Embed codes (e.g., `\033[32m` for green) in strings for manual control (`zoo.go:convertCellsToStrings`). + - **tw.Formatter**: Implement `Format() string` on custom types to control cell output, including colors (`tw/types.go:Formatter`). + - **tw.CellFilter**: Use `Config.
.Filter.Global` or `PerColumn` to apply transformations like coloring dynamically (`tw/cell.go:CellFilter`). +- **Width Handling**: `tw.DisplayWidth()` correctly calculates display width of ANSI-coded strings, ignoring escape sequences (`tw/fn.go:DisplayWidth`). +- **No Built-In Color Presets**: Unlike v0.0.5’s potential `tablewriter.Colors`, v1.0.x requires manual ANSI code management or external libraries for color constants. + +**Migration Tips**: +- Replace `SetColumnColor` with direct ANSI code embedding for simple cases (e.g., `FgGreen+"text"+Reset`) (`zoo.go`). +- Implement `tw.Formatter` on custom types for reusable, semantic color logic (e.g., `Status` type above) (`tw/types.go`). +- Use `ConfigBuilder.
().Filter().WithPerColumn` to apply color filters to specific columns, mimicking v0.0.5’s per-column coloring (`config.go`). +- Define ANSI constants in your codebase or use a library (e.g., `github.com/fatih/color`) to simplify color management. +- Test colored output in your target terminal to ensure ANSI codes render correctly. +- Combine filters with `AutoFormat` or wrapping for consistent styling (`zoo.go:prepareContent`). + +**Potential Pitfalls**: +- **Terminal Support**: Some terminals may not support ANSI codes, causing artifacts; test in your environment or provide a non-colored fallback. +- **Filter Overlap**: Combining `tw.CellFilter` with `AutoFormat` or other transformations can lead to unexpected results; prioritize filters for coloring (`zoo.go`). +- **Width Miscalculation**: Incorrect ANSI code handling (e.g., missing `Reset`) can skew width calculations; use `tw.DisplayWidth` (`tw/fn.go`). +- **Streaming Consistency**: In streaming mode, ensure color codes are applied consistently across rows to avoid visual discrepancies (`stream.go`). +- **Performance**: Applying filters to large datasets may add overhead; optimize filter logic for efficiency (`zoo.go`). + +## Advanced Features in v1.0.x + +v1.0.x introduces several advanced features that enhance table functionality beyond v0.0.5’s capabilities. This section covers cell merging, captions, filters, stringers, and performance optimizations, providing migration guidance and examples for leveraging these features. + +### 5. Cell Merging +Cell merging combines adjacent cells with identical content, improving readability for grouped data. v1.0.x expands merging options beyond v0.0.5’s horizontal merging. + +**Old (v0.0.5):** +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "os" +) + +func main() { + table := tablewriter.NewWriter(os.Stdout) + // table.SetAutoMergeCells(true) // Enable horizontal merging + table.SetHeader([]string{"Category", "Item", "Notes"}) + table.Append([]string{"Fruit", "Apple", "Red"}) + table.Append([]string{"Fruit", "Apple", "Green"}) // "Apple" might merge if SetAutoMergeCells was on + table.Render() +} +``` + +**New (v1.0.x):** +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/renderer" + "github.com/olekukonko/tablewriter/tw" + "os" +) + +func main() { + // Horizontal Merging (Similar to v0.0.5) + tableH := tablewriter.NewTable(os.Stdout, + tablewriter.WithConfig(tablewriter.Config{Row: tw.CellConfig{Formatting: tw.CellFormatting{MergeMode: tw.MergeHorizontal}}}), + tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{Symbols: tw.NewSymbols(tw.StyleASCII)})), // Specify renderer for symbols + ) + tableH.Header("Category", "Item", "Item", "Notes") // Note: Two "Item" headers for demo + tableH.Append("Fruit", "Apple", "Apple", "Red") // "Apple" cells merge + tableH.Render() + + // Vertical Merging + tableV := tablewriter.NewTable(os.Stdout, + tablewriter.WithConfig(tablewriter.Config{Row: tw.CellConfig{Formatting: tw.CellFormatting{MergeMode: tw.MergeVertical}}}), + tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{Symbols: tw.NewSymbols(tw.StyleASCII)})), + ) + tableV.Header("User", "Permission") + tableV.Append("Alice", "Read") + tableV.Append("Alice", "Write") // "Alice" cells merge vertically + tableV.Append("Bob", "Read") + tableV.Render() + + // Hierarchical Merging + tableHier := tablewriter.NewTable(os.Stdout, + tablewriter.WithConfig(tablewriter.Config{Row: tw.CellConfig{Formatting: tw.CellFormatting{MergeMode: tw.MergeHierarchical}}}), + tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{Symbols: tw.NewSymbols(tw.StyleASCII)})), + ) + tableHier.Header("Group", "SubGroup", "Item") + tableHier.Append("Tech", "CPU", "i7") + tableHier.Append("Tech", "CPU", "i9") // "Tech" and "CPU" merge + tableHier.Append("Tech", "RAM", "16GB") // "Tech" merges, "RAM" is new + tableHier.Append("Office", "CPU", "i5") // "Office" is new + tableHier.Render() +} +``` + +**Output (Horizontal):** +``` ++----------+-------+-------+-------+ +| CATEGORY | ITEM | ITEM | NOTES | ++----------+-------+-------+-------+ +| Fruit | Apple | Red | ++----------+---------------+-------+ +``` + +**Output (Vertical):** +``` ++-------+------------+ +| USER | PERMISSION | ++-------+------------+ +| Alice | Read | +| | Write | ++-------+------------+ +| Bob | Read | ++-------+------------+ +``` + +**Output (Hierarchical):** +``` ++---------+----------+------+ +| GROUP | SUBGROUP | ITEM | ++---------+----------+------+ +| Tech | CPU | i7 | +| | | i9 | +| +----------+------+ +| | RAM | 16GB | ++---------+----------+------+ +| Office | CPU | i5 | ++---------+----------+------+ +``` + +**Key Changes**: +- **Method**: `SetAutoMergeCells` is replaced by `WithRowMergeMode(int_merge_mode)` or `Config.Row.Formatting.MergeMode` (`config.go`). Uses `tw.Merge...` constants. +- **Merge Modes**: `tw.MergeMode` constants (`tw.MergeNone`, `tw.MergeHorizontal`, `tw.MergeVertical`, `tw.MergeHierarchical`) define behavior (`tw/tw.go`). +- **Section-Specific**: Merging can be applied to `Header`, `Row`, or `Footer` via `Config.
.Formatting.MergeMode` (`tw/cell.go`). +- **Processing**: Merging is handled during content preparation (`zoo.go:prepareWithMerges`, `zoo.go:applyVerticalMerges`, `zoo.go:applyHierarchicalMerges`). +- **Width Adjustment**: Horizontal merging adjusts column widths (`zoo.go:applyHorizontalMergeWidths`). +- **Renderer Support**: `tw.MergeState` in `tw.CellContext` ensures correct border drawing for merged cells (`tw/cell.go:CellContext`). +- **Streaming Limitation**: Streaming mode supports only simple horizontal merging due to fixed widths (`stream.go:streamAppendRow`). + +**Migration Tips**: +- Replace `SetAutoMergeCells(true)` with `WithRowMergeMode(tw.MergeHorizontal)` to maintain v0.0.5’s horizontal merging behavior (`config.go`). +- Use `tw.MergeVertical` for vertical grouping (e.g., repeated user names) or `tw.MergeHierarchical` for nested data structures (`tw/tw.go`). +- Apply merging to specific sections via `ConfigBuilder.
().Formatting().WithMergeMode(int_merge_mode)` (`config.go`). +- Test merging with sample data to verify visual output, especially for hierarchical merging with complex datasets. +- In streaming mode, ensure `Config.Widths` supports merged cell widths to avoid truncation (`stream.go`). +- Use `WithDebug(true)` to log merge processing for troubleshooting (`config.go`). + +**Potential Pitfalls**: +- **Streaming Restrictions**: Vertical and hierarchical merging are unsupported in streaming mode; use batch mode for these features (`stream.go`). +- **Width Misalignment**: Merged cells may require wider columns; adjust `Config.Widths` or `ColMaxWidths` (`zoo.go`). +- **Data Dependency**: Merging requires identical content; case or whitespace differences prevent merging (`zoo.go`). +- **Renderer Errors**: Incorrect `tw.MergeState` handling in custom renderers can break merged cell borders; test thoroughly (`tw/cell.go`). +- **Performance**: Hierarchical merging with large datasets may slow rendering; optimize data or limit merging (`zoo.go`). + +### 6. Table Captions +Captions add descriptive text above or below the table, a new feature in v1.0.x not present in v0.0.5. + +**Old (v0.0.5):** +```go +package main +// No direct caption support in v0.0.5. Users might have printed text manually. +// import "github.com/olekukonko/tablewriter" +// import "os" +// import "fmt" + +func main() { + // fmt.Println("Movie ratings.") // Manual caption + // table := tablewriter.NewWriter(os.Stdout) + // table.SetHeader([]string{"Name", "Sign", "Rating"}) + // table.Render() +} +``` + +**New (v1.0.x):** +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/tw" + "os" +) + +func main() { + table := tablewriter.NewTable(os.Stdout) + table.Caption(tw.Caption{ // tw/types.go:Caption + Text: "System Status Overview - A Very Long Caption Example To Demonstrate Wrapping Behavior", + Spot: tw.SpotTopCenter, // Placement: TopLeft, TopCenter, TopRight, BottomLeft, BottomCenter, BottomRight + Align: tw.AlignCenter, // Text alignment within caption width + Width: 30, // Fixed width for caption text wrapping; if 0, wraps to table width + }) + table.Header("Name", "Status") + table.Append("Node1", "Ready") + table.Append("SuperLongNodeNameHere", "ActiveNow") + table.Render() +} +``` + +**Output:** +``` + System Status Overview - A Very +Long Caption Example To Demonst… +┌─────────────────────┬──────────┐ +│ NAME │ STATUS │ +├─────────────────────┼──────────┤ +│ Node1 │ Ready │ +│ SuperLongNodeNameHe │ ActiveNo │ +│ re │ w │ +└─────────────────────┴──────────┘ +``` + +**Key Changes**: +- **New Feature**: `Table.Caption(tw.Caption)` introduces captions, absent in v0.0.5 (`tablewriter.go:Caption`). +- **Configuration**: `tw.Caption` (`tw/types.go:Caption`) includes: + - `Text`: Caption content. + - `Spot`: Placement (`tw.SpotTopLeft`, `tw.SpotBottomCenter`, etc.); defaults to `tw.SpotBottomCenter` if `tw.SpotNone`. + - `Align`: Text alignment (`tw.AlignLeft`, `tw.AlignCenter`, `tw.AlignRight`). + - `Width`: Optional fixed width for wrapping; defaults to table width. +- **Rendering**: Captions are rendered by `printTopBottomCaption` before or after the table based on `Spot` (`tablewriter.go:printTopBottomCaption`). +- **Streaming**: Captions are rendered during `Close()` in streaming mode if placed at the bottom (`stream.go`). + +**Migration Tips**: +- Add captions to enhance table context, especially for reports or documentation (`tw/types.go`). +- Use `tw.SpotTopCenter` for prominent placement above the table, aligning with common report formats. +- Set `Align` to match table aesthetics (e.g., `tw.AlignCenter` for balanced appearance). +- Specify `Width` for consistent wrapping, especially with long captions or narrow tables (`tablewriter.go`). +- Test caption placement and alignment with different table sizes to ensure readability. + +**Potential Pitfalls**: +- **Spot Default**: If `Spot` is `tw.SpotNone`, caption defaults to `tw.SpotBottomCenter`, which may surprise users expecting no caption (`tablewriter.go`). +- **Width Overflow**: Without `Width`, captions wrap to table width, potentially causing misalignment; set explicitly for control (`tw/types.go`). +- **Streaming Delay**: Bottom-placed captions in streaming mode appear only at `Close()`; ensure `Close()` is called (`stream.go`). +- **Alignment Confusion**: Caption `Align` is independent of table cell alignment; verify separately (`tw/cell.go`). + +### 7. Filters +Filters allow dynamic transformation of cell content during rendering, a new feature in v1.0.x for tasks like formatting, coloring, or sanitizing data. + +**Old (v0.0.5):** +```go +package main +// No direct support for cell content transformation in v0.0.5. +// Users would typically preprocess data before appending. +// import "github.com/olekukonko/tablewriter" +// import "os" +// import "strings" + +func main() { + // table := tablewriter.NewWriter(os.Stdout) + // table.SetHeader([]string{"Name", "Status"}) + // status := " Ready " + // preprocessedStatus := "Status: " + strings.TrimSpace(status) + // table.Append([]string{"Node1", preprocessedStatus}) + // table.Render() +} +``` + +**New (v1.0.x):** +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/tw" // For tw.CellConfig etc. + "os" + "strings" +) + +func main() { + // Per-Column Filter for Specific Transformations + cfgBuilder := tablewriter.NewConfigBuilder() + cfgBuilder.Row().Filter().WithPerColumn([]func(string) string{ + nil, // No filter for Name column + func(s string) string { // Status column: prefix and trim + return "Status: " + strings.TrimSpace(s) + }, + }) + + tableWithFilter := tablewriter.NewTable(os.Stdout, tablewriter.WithConfig(cfgBuilder.Build())) + tableWithFilter.Header("Name", "Status") + tableWithFilter.Append("Node1", " Ready ") // Note the extra spaces + tableWithFilter.Append("Node2", "Pending") + tableWithFilter.Render() + + // Global filter example (applied to all cells in the Row section) + cfgGlobalFilter := tablewriter.NewConfigBuilder() + cfgGlobalFilter.Row().Filter().WithGlobal(func(s string) string { + return "[" + s + "]" // Wrap all row cells in brackets + }) + tableGlobalFilter := tablewriter.NewTable(os.Stdout, tablewriter.WithConfig(cfgGlobalFilter.Build())) + tableGlobalFilter.Header("Item", "Count") + tableGlobalFilter.Append("Apple", "5") + tableGlobalFilter.Render() +} +``` + +**Output (Per-Column Filter):** +``` +┌───────┬─────────────────┐ +│ NAME │ STATUS │ +├───────┼─────────────────┤ +│ Node1 │ Status: Ready │ +│ Node2 │ Status: Pending │ +└───────┴─────────────────┘ +``` + +**Output (Global Filter):** +``` +┌───────┬─────────┐ +│ ITEM │ COUNT │ +├───────┼─────────┤ +│[Apple]│ [5] │ +└───────┴─────────┘ +``` + +**Key Changes**: +- **New Feature**: `tw.CellFilter` (`tw/cell.go:CellFilter`) introduces: + - `Global`: A `func(s []string) []string` applied to entire rows (all cells in that row) of a section. + - `PerColumn`: A slice of `func(string) string` for column-specific transformations on individual cells. +- **Configuration**: Set via `Config.
.Filter` (`Header`, `Row`, `Footer`) using `ConfigBuilder` or direct `Config` (`config.go`). +- **Processing**: Filters are applied during content preparation, after `AutoFormat` but before rendering (`zoo.go:convertCellsToStrings` calls `prepareContent` which applies some transformations, filters are applied in `convertCellsToStrings` itself). +- **Use Cases**: Formatting (e.g., uppercase, prefixes), coloring (via ANSI codes), sanitization (e.g., removing sensitive data), or data normalization. + +**Migration Tips**: +- Use filters to replace manual content preprocessing in v0.0.5 (e.g., string manipulation before `Append`). +- Apply `Global` filters for uniform transformations across all cells of rows in a section (e.g., uppercasing all row data) (`tw/cell.go`). +- Use `PerColumn` filters for column-specific formatting (e.g., adding prefixes to status columns) (`config.go`). +- Combine filters with `tw.Formatter` for complex types or ANSI coloring for visual enhancements (`tw/cell.go`). +- Test filters with diverse inputs to ensure transformations preserve data integrity (`zoo.go`). + +**Potential Pitfalls**: +- **Filter Order**: Filters apply before some other transformations like padding and alignment; combining can lead to interactions. +- **Performance**: Complex filters on large datasets may slow rendering; optimize logic (`zoo.go`). +- **Nil Filters**: Unset filters (`nil`) are ignored, but incorrect indexing in `PerColumn` can skip columns (`tw/cell.go`). +- **Streaming Consistency**: Filters must be consistent in streaming mode, as widths are fixed at `Start()` (`stream.go`). + +### 8. Stringers and Caching +Stringers allow custom string conversion for data types, with v1.0.x adding caching for performance. + +**Old (v0.0.5):** +```go +package main +// v0.0.5 primarily relied on fmt.Stringer for custom types. +// import "fmt" +// import "github.com/olekukonko/tablewriter" +// import "os" + +// type MyCustomType struct { +// Value string +// } +// func (m MyCustomType) String() string { return "Formatted: " + m.Value } + +func main() { + // table := tablewriter.NewWriter(os.Stdout) + // table.SetHeader([]string{"Custom Data"}) + // table.Append([]string{MyCustomType{"test"}.String()}) // Manual call to String() + // table.Render() +} +``` + +**New (v1.0.x):** +```go +package main + +import ( + "fmt" + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/tw" // For tw.Formatter + "os" + "strings" // For Person example +) + +// Example 1: Using WithStringer for general conversion +type CustomInt int + +func main() { + // Table with a general stringer (func(any) []string) + tableWithStringer := tablewriter.NewTable(os.Stdout, + tablewriter.WithStringer(func(v any) []string { // Must return []string + if ci, ok := v.(CustomInt); ok { + return []string{fmt.Sprintf("CustomInt Value: %d!", ci)} + } + return []string{fmt.Sprintf("Value: %v", v)} // Fallback + }), + tablewriter.WithDebug(true), // Enable caching if WithStringerCache() is also used + // tablewriter.WithStringerCache(), // Optional: enable caching + ) + tableWithStringer.Header("Data") + tableWithStringer.Append(123) + tableWithStringer.Append(CustomInt(456)) + tableWithStringer.Render() + + fmt.Println("\n--- Table with Type-Specific Stringer for Structs ---") + + // Example 2: Stringer for a specific struct type + type Person struct { + ID int + Name string + City string + } + + // Stringer for Person to produce 3 cells + personToStrings := func(p Person) []string { + return []string{ + fmt.Sprintf("ID: %d", p.ID), + p.Name, + strings.ToUpper(p.City), + } + } + + tablePersonStringer := tablewriter.NewTable(os.Stdout, + tablewriter.WithStringer(personToStrings), // Pass the type-specific function + ) + tablePersonStringer.Header("User ID", "Full Name", "Location") + tablePersonStringer.Append(Person{1, "Alice", "New York"}) + tablePersonStringer.Append(Person{2, "Bob", "London"}) + tablePersonStringer.Render() + + fmt.Println("\n--- Table with tw.Formatter ---") + // Example 3: Using tw.Formatter for types + type Product struct { + Name string + Price float64 + } + func (p Product) Format() string { // Implements tw.Formatter + return fmt.Sprintf("%s - $%.2f", p.Name, p.Price) + } + tableFormatter := tablewriter.NewTable(os.Stdout) + tableFormatter.Header("Product Details") + tableFormatter.Append(Product{"Laptop", 1200.99}) // Will use Format() + tableFormatter.Render() +} +``` + +**Output (Stringer Examples):** +``` +┌─────────────────────┐ +│ DATA │ +├─────────────────────┤ +│ Value: 123 │ +│ CustomInt Value: 456! │ +└─────────────────────┘ + +--- Table with Type-Specific Stringer for Structs --- +┌─────────┬───────────┬──────────┐ +│ USER ID │ FULL NAME │ LOCATION │ +├─────────┼───────────┼──────────┤ +│ ID: 1 │ Alice │ NEW YORK │ +│ ID: 2 │ Bob │ LONDON │ +└─────────┴───────────┴──────────┘ + +--- Table with tw.Formatter --- +┌─────────────────┐ +│ PRODUCT DETAILS │ +├─────────────────┤ +│ Laptop - $1200… │ +└─────────────────┘ +``` + +**Key Changes**: +- **Stringer Support**: `WithStringer(fn any)` sets a table-wide string conversion function. This function must have a signature like `func(SomeType) []string` or `func(any) []string`. It's used to convert an input item (e.g., a struct) into a slice of strings, where each string is a cell for the row (`tablewriter.go:WithStringer`). +- **Caching**: `WithStringerCache()` enables caching for the function provided via `WithStringer`, improving performance for repeated conversions of the same input type (`tablewriter.go:WithStringerCache`). +- **Formatter**: `tw.Formatter` interface (`Format() string`) allows types to define their own single-string representation for a cell. This is checked before `fmt.Stringer` (`tw/types.go:Formatter`). +- **Priority**: When converting an item to cell(s): + 1. `WithStringer` (if provided and compatible with the item's type). + 2. If not handled by `WithStringer`, or if `WithStringer` is not set: + * If the item is a struct that does *not* implement `tw.Formatter` or `fmt.Stringer`, its exported fields are reflected into multiple cells. + * If the item (or struct) implements `tw.Formatter` (`Format() string`), that's used for a single cell. + * Else, if it implements `fmt.Stringer` (`String() string`), that's used for a single cell. + * Else, default `fmt.Sprintf("%v", ...)` for a single cell. + (`zoo.go:convertCellsToStrings`, `zoo.go:convertItemToCells`) + +**Migration Tips**: +- For types that should produce a single cell with custom formatting, implement `tw.Formatter`. +- For types (especially structs) that should be expanded into multiple cells with custom logic, use `WithStringer` with a function like `func(MyType) []string`. +- If you have a general way to convert *any* type into a set of cells, use `WithStringer(func(any) []string)`. +- Enable `WithStringerCache` for large datasets with repetitive data types if using `WithStringer`. +- Test stringer/formatter output to ensure formatting meets expectations (`zoo.go`). + +**Potential Pitfalls**: +- **Cache Overhead**: `WithStringerCache` may increase memory usage for diverse data; disable for small tables or if not using `WithStringer` (`tablewriter.go`). +- **Stringer Signature**: The function passed to `WithStringer` *must* return `[]string`. A function returning `string` will lead to a warning and fallback behavior. +- **Formatter vs. Stringer Priority**: Be aware of the conversion priority if your types implement multiple interfaces or if you also use `WithStringer`. +- **Streaming**: Stringers/Formatters must produce consistent cell counts in streaming mode to maintain width alignment (`stream.go`). + +## Examples + +This section provides practical examples to demonstrate v1.0.x features, covering common and advanced use cases to aid migration. Each example includes code, output, and notes to illustrate functionality. + +### Example: Minimal Setup +A basic table with default settings, ideal for quick setups. + +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "os" +) + +func main() { + table := tablewriter.NewTable(os.Stdout) + table.Header("Name", "Status") + table.Append("Node1", "Ready") + table.Render() +} +``` + +**Output**: +``` +┌───────┬────────┐ +│ NAME │ STATUS │ +├───────┼────────┤ +│ Node1 │ Ready │ +└───────┴────────┘ +``` + +**Notes**: +- Uses default `renderer.NewBlueprint()` and `defaultConfig()` settings (`tablewriter.go`, `config.go`). +- `Header: tw.AlignCenter`, `Row: tw.AlignLeft` (`config.go:defaultConfig`). +- Simple replacement for v0.0.5’s `NewWriter` and `SetHeader`/`Append`. + +### Example: Streaming with Fixed Widths +Demonstrates streaming mode for real-time data output. + +```go +package main + +import ( + "fmt" + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/tw" + "log" + "os" +) + +func main() { + table := tablewriter.NewTable(os.Stdout, + tablewriter.WithStreaming(tw.StreamConfig{Enable: true}), + tablewriter.WithColumnMax(10), // Sets Config.Widths.Global + ) + if err := table.Start(); err != nil { + log.Fatalf("Start failed: %v", err) + } + table.Header("Name", "Status") + for i := 0; i < 2; i++ { + err := table.Append(fmt.Sprintf("Node%d", i+1), "Ready") + if err != nil { + log.Printf("Append failed: %v", err) + } + } + if err := table.Close(); err != nil { + log.Fatalf("Close failed: %v", err) + } +} +``` + +**Output**: +``` +┌──────────┬──────────┐ +│ NAME │ STATUS │ +├──────────┼──────────┤ +│ Node1 │ Ready │ +│ Node2 │ Ready │ +└──────────┴──────────┘ +``` + +**Notes**: +- Streaming requires `Start()` and `Close()`; `Config.Widths` (here set via `WithColumnMax`) fixes widths (`stream.go`). +- Replaces v0.0.5’s batch rendering for real-time use cases. +- Ensure error handling for `Start()`, `Append()`, and `Close()`. + +### Example: Markdown-Style Table +Creates a Markdown-compatible table for documentation. + +```go +package main + +import ( + "fmt" + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/renderer" // Import renderer + "github.com/olekukonko/tablewriter/tw" + "os" +) + +func main() { + // Example 1: Using Blueprint renderer with Markdown symbols + tableBlueprintMarkdown := tablewriter.NewTable(os.Stdout, + tablewriter.WithRenderer(renderer.NewBlueprint()), // Use Blueprint + tablewriter.WithRendition(tw.Rendition{ + Symbols: tw.NewSymbols(tw.StyleMarkdown), + Borders: tw.Border{Left: tw.On, Right: tw.On}, // Markdown needs left/right borders + }), + tablewriter.WithRowAlignment(tw.AlignLeft), // Common for Markdown + tablewriter.WithHeaderAlignment(tw.AlignCenter), // Center align headers + ) + tableBlueprintMarkdown.Header("Name", "Status") + tableBlueprintMarkdown.Append("Node1", "Ready") + tableBlueprintMarkdown.Append("Node2", "NotReady") + tableBlueprintMarkdown.Render() + + fmt.Println("\n--- Using dedicated Markdown Renderer (if one exists or is built) ---") + // Example 2: Assuming a dedicated Markdown renderer (hypothetical example) + // If a `renderer.NewMarkdown()` existed that directly outputs GitHub Flavored Markdown table syntax: + /* + tableDedicatedMarkdown := tablewriter.NewTable(os.Stdout, + tablewriter.WithRenderer(renderer.NewMarkdown()), // Hypothetical Markdown renderer + ) + tableDedicatedMarkdown.Header("Name", "Status") + tableDedicatedMarkdown.Append("Node1", "Ready") + tableDedicatedMarkdown.Append("Node2", "NotReady") + tableDedicatedMarkdown.Render() + */ + // Since `renderer.NewMarkdown()` isn't shown in the provided code, + // the first example (Blueprint with StyleMarkdown) is the current viable way. +} +``` + +**Output (Blueprint with StyleMarkdown):** +``` +| NAME | STATUS | +|--------|----------| +| Node1 | Ready | +| Node2 | NotReady | +``` + +**Notes**: +- `StyleMarkdown` ensures compatibility with Markdown parsers (`tw/symbols.go`). +- Left alignment for rows and center for headers is common for Markdown readability (`config.go`). +- Ideal for GitHub READMEs or documentation. +- A dedicated Markdown renderer (like the commented-out example) would typically handle alignment syntax (e.g., `|:---:|:---|`). With `Blueprint` and `StyleMarkdown`, alignment is visual within the text rather than Markdown syntax. + +### Example: ASCII-Style Table +Uses `StyleASCII` for maximum terminal compatibility. + +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/renderer" + "github.com/olekukonko/tablewriter/tw" + "os" +) + +func main() { + table := tablewriter.NewTable(os.Stdout, + tablewriter.WithRenderer(renderer.NewBlueprint()), + tablewriter.WithRendition(tw.Rendition{ + Symbols: tw.NewSymbols(tw.StyleASCII), + }), + tablewriter.WithRowAlignment(tw.AlignLeft), + ) + table.Header("ID", "Value") + table.Append("1", "Test") + table.Render() +} +``` + +**Output**: +``` ++----+-------+ +│ ID │ VALUE │ ++----+-------+ +│ 1 │ Test │ ++----+-------+ +``` + +**Notes**: +- `StyleASCII` is robust for all terminals (`tw/symbols.go`). +- Replaces v0.0.5’s default style with explicit configuration. + + +### Example: Kubectl-Style Output +Creates a borderless, minimal table similar to `kubectl` command output, emphasizing simplicity and readability. + +```go +package main + +import ( + "os" + "sync" + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/renderer" + "github.com/olekukonko/tablewriter/tw" +) + +var wg sync.WaitGroup + +func main() { + data := [][]any{ + {"node1.example.com", "Ready", "compute", "1.11"}, + {"node2.example.com", "Ready", "compute", "1.11"}, + {"node3.example.com", "Ready", "compute", "1.11"}, + {"node4.example.com", "NotReady", "compute", "1.11"}, + } + + table := tablewriter.NewTable(os.Stdout, + tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{ + Borders: tw.BorderNone, + Settings: tw.Settings{ + Separators: tw.SeparatorsNone, + Lines: tw.LinesNone, + }, + })), + tablewriter.WithConfig(tablewriter.Config{ + Header: tw.CellConfig{ + Formatting: tw.CellFormatting{Alignment: tw.AlignLeft}, + }, + Row: tw.CellConfig{ + Formatting: tw.CellFormatting{Alignment: tw.AlignLeft}, + Padding: tw.CellPadding{Global: tw.PaddingNone}, + }, + }), + ) + table.Header("Name", "Status", "Role", "Version") + table.Bulk(data) + table.Render() +} +``` + +**Output:** +``` +NAME STATUS ROLE VERSION +node1.example.com Ready compute 1.21.3 +node2.example.com Ready infra 1.21.3 +node3.example.com NotReady compute 1.20.7 +``` + +**Notes**: +- **Configuration**: Uses `tw.BorderNone`, `tw.LinesNone`, `tw.SeparatorsNone`, `tw.NewSymbols(tw.StyleNone)` and specific padding (`Padding{Right:" "}`) for a minimal, borderless layout. +- **Migration from v0.0.5**: Replaces `SetBorder(false)` and manual spacing with `tw.Rendition` and `Config` settings, achieving a cleaner kubectl-like output. +- **Key Features**: + - Left-aligned text for readability (`config.go`). + - `WithTrimSpace(tw.Off)` preserves spacing (`config.go`). + - `Bulk` efficiently adds multiple rows (`tablewriter.go`). + - Padding `Right: " "` is used to create space between columns as separators are off. +- **Best Practices**: Test in terminals to ensure spacing aligns with command-line aesthetics; use `WithDebug(true)` for layout issues (`config.go`). +- **Potential Issues**: Column widths are content-based. For very long content in one column and short in others, it might not look perfectly aligned like fixed-width CLI tools. `Config.Widths` could be used for more control if needed. + +### Example: Hierarchical Merging +Demonstrates hierarchical cell merging for nested data structures, a new feature in v1.0.x. + +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/renderer" + "github.com/olekukonko/tablewriter/tw" + "os" +) + +func main() { + data := [][]string{ + // Header row is separate + {"table\nwriter", "v0.0.1", "legacy"}, + {"table\nwriter", "v0.0.2", "legacy"}, + {"table\nwriter", "v0.0.2", "legacy"}, // Duplicate for testing merge + {"table\nwriter", "v0.0.2", "legacy"}, // Duplicate for testing merge + {"table\nwriter", "v0.0.5", "legacy"}, + {"table\nwriter", "v1.0.6", "latest"}, + } + + rendition := tw.Rendition{ + Symbols: tw.NewSymbols(tw.StyleLight), // Use light for clearer merge lines + Settings: tw.Settings{ + Separators: tw.Separators{BetweenRows: tw.On}, + Lines: tw.Lines{ShowHeaderLine: tw.On, ShowFooterLine: tw.On}, // Show header line + }, + Borders: tw.Border{Left:tw.On, Right:tw.On, Top:tw.On, Bottom:tw.On}, + } + + tableHier := tablewriter.NewTable(os.Stdout, + tablewriter.WithRenderer(renderer.NewBlueprint()), + tablewriter.WithRendition(rendition), + tablewriter.WithConfig(tablewriter.Config{ + Row: tw.CellConfig{ + Formatting: tw.CellFormatting{ + MergeMode: tw.MergeHierarchical, + // Alignment: tw.AlignCenter, // Default is Left, often better for hierarchical + AutoWrap: tw.WrapNormal, // Allow wrapping for "table\nwriter" + }, + }, + }), + ) + + tableHier.Header("Package", "Version", "Status") // Header + tableHier.Bulk(data) // Bulk data + tableHier.Render() + + // --- Vertical Merging Example for Contrast --- + tableVert := tablewriter.NewTable(os.Stdout, + tablewriter.WithRenderer(renderer.NewBlueprint()), + tablewriter.WithRendition(rendition), // Reuse same rendition + tablewriter.WithConfig(tablewriter.Config{ + Row: tw.CellConfig{ + Formatting: tw.CellFormatting{ + MergeMode: tw.MergeVertical, + AutoWrap: tw.WrapNormal, + }, + }, + }), + ) + tableVert.Header("Package", "Version", "Status") + tableVert.Bulk(data) + tableVert.Render() +} +``` + +**Output (Hierarchical):** +``` +┌─────────┬─────────┬────────┐ +│ PACKAGE │ VERSION │ STATUS │ +├─────────┼─────────┼────────┤ +│ table │ v0.0.1 │ legacy │ +│ writer │ │ │ +│ ├─────────┼────────┤ +│ │ v0.0.2 │ legacy │ +│ │ │ │ +│ │ │ │ +│ │ │ │ +│ │ │ │ +│ ├─────────┼────────┤ +│ │ v0.0.5 │ legacy │ +│ ├─────────┼────────┤ +│ │ v1.0.6 │ latest │ +└─────────┴─────────┴────────┘ +``` +**Output (Vertical):** +``` +┌─────────┬─────────┬────────┐ +│ PACKAGE │ VERSION │ STATUS │ +├─────────┼─────────┼────────┤ +│ table │ v0.0.1 │ legacy │ +│ writer │ │ │ +│ ├─────────┤ │ +│ │ v0.0.2 │ │ +│ │ │ │ +│ │ │ │ +│ │ │ │ +│ │ │ │ +│ ├─────────┤ │ +│ │ v0.0.5 │ │ +│ ├─────────┼────────┤ +│ │ v1.0.6 │ latest │ +└─────────┴─────────┴────────┘ +``` + +**Notes**: +- **Configuration**: Uses `tw.MergeHierarchical` to merge cells based on matching values in preceding columns (`tw/tw.go`). +- **Migration from v0.0.5**: Extends `SetAutoMergeCells(true)` (horizontal only) with hierarchical merging for complex data (`tablewriter.go`). +- **Key Features**: + - `StyleLight` for clear visuals (`tw/symbols.go`). + - `Bulk` for efficient data loading (`tablewriter.go`). +- **Best Practices**: Test merging with nested data; use batch mode, as streaming doesn’t support hierarchical merging (`stream.go`). +- **Potential Issues**: Ensure data is sorted appropriately for hierarchical merging to work as expected; mismatches prevent merging (`zoo.go`). `AutoWrap: tw.WrapNormal` helps with multi-line cell content like "table\nwriter". + +### Example: Colorized Table +Applies ANSI colors to highlight status values, replacing v0.0.5’s `SetColumnColor`. + +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/tw" // For tw.State, tw.CellConfig etc. + "os" +) + +func main() { + const ( + FgGreen = "\033[32m" + FgRed = "\033[31m" + Reset = "\033[0m" + ) + + cfgBuilder := tablewriter.NewConfigBuilder() + cfgBuilder.Row().Filter().WithPerColumn([]func(string) string{ + nil, // No filter for Name + func(s string) string { // Color Status + if s == "Ready" { + return FgGreen + s + Reset + } + return FgRed + s + Reset + }, + }) + + table := tablewriter.NewTable(os.Stdout, tablewriter.WithConfig(cfgBuilder.Build())) + table.Header("Name", "Status") + table.Append("Node1", "Ready") + table.Append("Node2", "Error") + table.Render() +} +``` + +**Output (Text Approximation, Colors Not Shown):** +``` +┌───────┬────────┐ +│ NAME │ STATUS │ +├───────┼────────┤ +│ Node1 │ Ready │ +│ Node2 │ Error │ +└───────┴────────┘ +``` + +**Notes**: +- **Configuration**: Uses `tw.CellFilter` for per-column coloring, embedding ANSI codes (`tw/cell.go`). +- **Migration from v0.0.5**: Replaces `SetColumnColor` with dynamic filters (`tablewriter.go`). +- **Key Features**: Flexible color application; `tw.DisplayWidth` handles ANSI codes correctly (`tw/fn.go`). +- **Best Practices**: Test in ANSI-compatible terminals; use constants for code clarity. +- **Potential Issues**: Non-ANSI terminals may show artifacts; provide fallbacks (`tw/fn.go`). + +### Example: Vertical Merging +Shows vertical cell merging for repeated values in a column. (This example was very similar to the hierarchical one, so I'll ensure it's distinct by using simpler data). + +```go +package main + +import ( + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/renderer" + "github.com/olekukonko/tablewriter/tw" + "os" +) + +func main() { + table := tablewriter.NewTable(os.Stdout, + tablewriter.WithRenderer(renderer.NewBlueprint()), // Default renderer + tablewriter.WithRendition(tw.Rendition{ + Symbols: tw.NewSymbols(tw.StyleLight), + Settings: tw.Settings{Separators: tw.Separators{BetweenRows: tw.On}}, + Borders: tw.Border{Left:tw.On, Right:tw.On, Top:tw.On, Bottom:tw.On}, + }), + tablewriter.WithConfig( + tablewriter.NewConfigBuilder(). + Row().Formatting().WithMergeMode(tw.MergeVertical).Build(). // Enable Vertical Merge + Build(), + ), + ) + table.Header("User", "Permission", "Target") + table.Append("Alice", "Read", "FileA") + table.Append("Alice", "Write", "FileA") // Alice and FileA will merge vertically + table.Append("Alice", "Read", "FileB") + table.Append("Bob", "Read", "FileA") + table.Append("Bob", "Read", "FileC") // Bob and Read will merge + table.Render() +} +``` + +**Output:** +``` +┌───────┬────────────┬────────┐ +│ USER │ PERMISSION │ TARGET │ +├───────┼────────────┼────────┤ +│ Alice │ Read │ FileA │ +│ │ │ │ +│ ├────────────┤ │ +│ │ Write │ │ +│ ├────────────┼────────┤ +│ │ Read │ FileB │ +├───────┼────────────┼────────┤ +│ Bob │ Read │ FileA │ +│ │ ├────────┤ +│ │ │ FileC │ +└───────┴────────────┴────────┘ +``` + +**Notes**: +- **Configuration**: `tw.MergeVertical` merges identical cells vertically (`tw/tw.go`). +- **Migration from v0.0.5**: Extends `SetAutoMergeCells` with vertical merging (`tablewriter.go`). +- **Key Features**: Enhances grouped data display; requires batch mode (`stream.go`). +- **Best Practices**: Sort data by the columns you intend to merge for best results; test with `StyleLight` for clarity (`tw/symbols.go`). +- **Potential Issues**: Streaming doesn’t support vertical merging; use batch mode (`stream.go`). + +### Example: Custom Renderer (CSV Output) +Implements a custom renderer for CSV output. + +```go +package main + +import ( + "fmt" + "github.com/olekukonko/ll" // For logger type + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/tw" + "io" + "os" + "strings" // For CSV escaping +) + +// CSVRenderer implements tw.Renderer +type CSVRenderer struct { + writer io.Writer + config tw.Rendition // Store the rendition + logger *ll.Logger + err error +} + +func (r *CSVRenderer) Start(w io.Writer) error { + r.writer = w + return nil // No initial output for CSV typically +} + +func (r *CSVRenderer) escapeCSVCell(data string) string { + // Basic CSV escaping: double quotes if it contains comma, newline, or quote + if strings.ContainsAny(data, ",\"\n") { + return `"` + strings.ReplaceAll(data, `"`, `""`) + `"` + } + return data +} + +func (r *CSVRenderer) writeLine(cells map[int]tw.CellContext, numCols int) { + if r.err != nil { return } + var lineParts []string + // Need to iterate in column order for CSV + keys := make([]int, 0, len(cells)) + for k := range cells { + keys = append(keys, k) + } + // This simple sort works for int keys 0,1,2... + // For more complex scenarios, a proper sort might be needed if keys aren't sequential. + for i := 0; i < numCols; i++ { // Assume numCols reflects the intended max columns + if cellCtx, ok := cells[i]; ok { + lineParts = append(lineParts, r.escapeCSVCell(cellCtx.Data)) + } else { + lineParts = append(lineParts, "") // Empty cell if not present + } + } + _, r.err = r.writer.Write([]byte(strings.Join(lineParts, ",") + "\n")) +} + + +func (r *CSVRenderer) Header(headers [][]string, ctx tw.Formatting) { + // For CSV, usually only the first line of headers is relevant + // The ctx.Row.Current will contain the cells for the first line of the header being processed + r.writeLine(ctx.Row.Current, len(ctx.Row.Current)) +} + +func (r *CSVRenderer) Row(row []string, ctx tw.Formatting) { + // ctx.Row.Current contains the cells for the current row line + r.writeLine(ctx.Row.Current, len(ctx.Row.Current)) +} + +func (r *CSVRenderer) Footer(footers [][]string, ctx tw.Formatting) { + // Similar to Header/Row, using ctx.Row.Current for the footer line data + r.writeLine(ctx.Row.Current, len(ctx.Row.Current)) +} + +func (r *CSVRenderer) Line(ctx tw.Formatting) { /* No separator lines in CSV */ } + +func (r *CSVRenderer) Close() error { return r.err } + +func (r *CSVRenderer) Config() tw.Rendition { return r.config } +func (r *CSVRenderer) Logger(logger *ll.Logger) { r.logger = logger } + +func main() { + table := tablewriter.NewTable(os.Stdout, tablewriter.WithRenderer(&CSVRenderer{ + // config can be minimal for CSV as symbols/borders aren't used + config: tw.Rendition{}, + })) + table.Header("Name", "Status", "Notes, with comma") + table.Append("Node1", "Ready", "All systems \"go\"!") + table.Append("Node2", "Error", "Needs\nattention") + table.Footer("Summary", "2 Nodes", "Check logs") + table.Render() +} +``` + +**Output:** +```csv +Name,Status,"Notes, with comma" +Node1,Ready,"All systems ""go""!" +Node2,Error,"Needs +attention" +Summary,2 Nodes,Check logs +``` + +**Notes**: +- **Configuration**: Custom `CSVRenderer` implements `tw.Renderer` for CSV output (`tw/renderer.go`). +- **Migration from v0.0.5**: Extends v0.0.5’s text-only output with custom formats (`tablewriter.go`). +- **Key Features**: Handles basic CSV cell escaping; supports streaming if `Append` is called multiple times. `ctx.Row.Current` (map[int]tw.CellContext) is used to access cell data. +- **Best Practices**: Test with complex data (e.g., commas, quotes, newlines); implement all renderer methods. A more robust CSV renderer would use the `encoding/csv` package. +- **Potential Issues**: Custom renderers require careful error handling and correct interpretation of `tw.Formatting` and `tw.RowContext`. This example's `writeLine` assumes columns are 0-indexed and contiguous for simplicity. + +## Troubleshooting and Common Pitfalls + +This section addresses common migration issues with detailed solutions, covering 30+ scenarios to reduce support tickets. + +| Issue | Cause/Solution | +|-------------------------------------------|-------------------------------------------------------------------------------| +| No output from `Render()` | **Cause**: Missing `Start()`/`Close()` in streaming mode or invalid `io.Writer`. **Solution**: Ensure `Start()` and `Close()` are called in streaming; verify `io.Writer` (`stream.go`). | +| Incorrect column widths | **Cause**: Missing `Config.Widths` in streaming or content-based sizing. **Solution**: Set `Config.Widths` before `Start()`; use `WithColumnMax` (`stream.go`). | +| Merging not working | **Cause**: Streaming mode or mismatched data. **Solution**: Use batch mode for vertical/hierarchical merging; ensure identical content (`zoo.go`). | +| Alignment ignored | **Cause**: `PerColumn` overrides `Global`. **Solution**: Check `Config.Section.Alignment.PerColumn` settings or `ConfigBuilder` calls (`tw/cell.go`). | +| Padding affects widths | **Cause**: Padding included in `Config.Widths`. **Solution**: Adjust `Config.Widths` to account for `tw.CellPadding` (`zoo.go`). | +| Colors not rendering | **Cause**: Non-ANSI terminal or incorrect codes. **Solution**: Test in ANSI-compatible terminal; use `tw.DisplayWidth` (`tw/fn.go`). | +| Caption missing | **Cause**: `Close()` not called in streaming or incorrect `Spot`. **Solution**: Ensure `Close()`; verify `tw.Caption.Spot` (`tablewriter.go`). | +| Filters not applied | **Cause**: Incorrect `PerColumn` indexing or nil filters. **Solution**: Set filters correctly; test with sample data (`tw/cell.go`). | +| Stringer cache overhead | **Cause**: Large datasets with diverse types. **Solution**: Disable `WithStringerCache` for small tables if not using `WithStringer` or if types vary greatly (`tablewriter.go`). | +| Deprecated methods used | **Cause**: Using `WithBorders`, old `tablewriter.Behavior` constants. **Solution**: Migrate to `WithRendition`, `tw.Behavior` struct (`tablewriter.go`, `deprecated.go`). | +| Streaming footer missing | **Cause**: `Close()` not called. **Solution**: Always call `Close()` (`stream.go`). | +| Hierarchical merging fails | **Cause**: Unsorted data or streaming mode. **Solution**: Sort data; use batch mode (`zoo.go`). | +| Custom renderer errors | **Cause**: Incomplete method implementation or misinterpreting `tw.Formatting`. **Solution**: Implement all `tw.Renderer` methods; test thoroughly (`tw/renderer.go`). | +| Width overflow | **Cause**: No `MaxWidth` or wide content. **Solution**: Set `Config.MaxWidth` (`config.go`). | +| Truncated content | **Cause**: Narrow `Config.Widths` or `tw.WrapTruncate`. **Solution**: Widen columns or use `tw.WrapNormal` (`zoo.go`). | +| Debug logs absent | **Cause**: `Debug = false`. **Solution**: Enable `WithDebug(true)` (`config.go`). | +| Alignment mismatch across sections | **Cause**: Different defaults. **Solution**: Set uniform alignment options (e.g., via `ConfigBuilder.
.Alignment()`) (`config.go`). | +| ANSI code artifacts | **Cause**: Non-ANSI terminal. **Solution**: Provide non-colored fallback (`tw/fn.go`). | +| Slow rendering | **Cause**: Complex filters or merging. **Solution**: Optimize logic; limit merging (`zoo.go`). | +| Uneven cell counts | **Cause**: Mismatched rows/headers. **Solution**: Pad with `""` (`zoo.go`). | +| Border inconsistencies | **Cause**: Mismatched `Borders`/`Symbols`. **Solution**: Align settings (`tw/renderer.go`). | +| Streaming width issues | **Cause**: No `Config.Widths`. **Solution**: Set before `Start()` (`stream.go`). | +| Formatter ignored | **Cause**: `WithStringer` might take precedence if compatible. **Solution**: Review conversion priority; `tw.Formatter` is high-priority for single-item-to-single-cell conversion (`zoo.go`). | +| Caption misalignment | **Cause**: Incorrect `Width` or `Align`. **Solution**: Set `tw.Caption.Width`/`Align` (`tablewriter.go`). | +| Per-column padding errors | **Cause**: Incorrect indexing in `Padding.PerColumn`. **Solution**: Verify indices (`tw/cell.go`). | +| Vertical merging in streaming | **Cause**: Unsupported. **Solution**: Use batch mode (`stream.go`). | +| Filter performance | **Cause**: Complex logic. **Solution**: Simplify filters (`zoo.go`). | +| Custom symbols incomplete | **Cause**: Missing characters. **Solution**: Define all symbols (`tw/symbols.go`). | +| Table too wide | **Cause**: No `MaxWidth`. **Solution**: Set `Config.MaxWidth` (`config.go`). | +| Streaming errors | **Cause**: Missing `Start()`. **Solution**: Call `Start()` before data input (`stream.go`). | + +## Additional Notes + +- **Performance Optimization**: Enable `WithStringerCache` for repetitive data types when using `WithStringer`; optimize filters and merging for large datasets (`tablewriter.go`, `zoo.go`). +- **Debugging**: Use `WithDebug(true)` and `table.Debug()` to log configuration and rendering details; invaluable for troubleshooting (`config.go`). +- **Testing Resources**: The `tests/` directory contains examples of various configurations. +- **Community Support**: For advanced use cases or issues, consult the source code or open an issue on the `tablewriter` repository. +- **Future Considerations**: Deprecated methods in `deprecated.go` (e.g., `WithBorders`) are slated for removal in future releases; migrate promptly to ensure compatibility. + +This guide aims to cover all migration scenarios comprehensively. For highly specific or advanced use cases, refer to the source files (`config.go`, `tablewriter.go`, `stream.go`, `tw/*`) or engage with the `tablewriter` community for support. \ No newline at end of file diff --git a/vendor/github.com/olekukonko/tablewriter/README.md b/vendor/github.com/olekukonko/tablewriter/README.md index 8aecd0f55b..f694340f73 100644 --- a/vendor/github.com/olekukonko/tablewriter/README.md +++ b/vendor/github.com/olekukonko/tablewriter/README.md @@ -28,14 +28,14 @@ go get github.com/olekukonko/tablewriter@v0.0.5 #### Latest Version The latest stable version ```bash -go get github.com/olekukonko/tablewriter@v1.0.6 +go get github.com/olekukonko/tablewriter@v1.0.7 ``` **Warning:** Version `v1.0.0` contains missing functionality and should not be used. > **Version Guidance** -> - Production: Use `v0.0.5` (stable) +> - Legacy: Use `v0.0.5` (stable) > - New Features: Use `@latest` (includes generics, super fast streaming APIs) > - Legacy Docs: See [README_LEGACY.md](README_LEGACY.md) @@ -62,7 +62,7 @@ func main() { data := [][]string{ {"Package", "Version", "Status"}, {"tablewriter", "v0.0.5", "legacy"}, - {"tablewriter", "v1.0.6", "latest"}, + {"tablewriter", "v1.0.7", "latest"}, } table := tablewriter.NewWriter(os.Stdout) @@ -77,7 +77,7 @@ func main() { │ PACKAGE │ VERSION │ STATUS │ ├─────────────┼─────────┼────────┤ │ tablewriter │ v0.0.5 │ legacy │ -│ tablewriter │ v1.0.6 │ latest │ +│ tablewriter │ v1.0.7 │ latest │ └─────────────┴─────────┴────────┘ ``` @@ -297,7 +297,7 @@ func main() { } table.Configure(func(config *tablewriter.Config) { - config.Row.Formatting.Alignment = tw.AlignLeft + config.Row.Alignment.Global = tw.AlignLeft }) table.Render() } @@ -368,14 +368,12 @@ func main() { tablewriter.WithRenderer(renderer.NewColorized(colorCfg)), tablewriter.WithConfig(tablewriter.Config{ Row: tw.CellConfig{ - Formatting: tw.CellFormatting{ - AutoWrap: tw.WrapNormal, // Wrap long content - Alignment: tw.AlignLeft, // Left-align rows - }, + Formatting: tw.CellFormatting{AutoWrap: tw.WrapNormal}, // Wrap long content + Alignment: tw.CellAlignment{Global: tw.AlignLeft}, // Left-align rows ColMaxWidths: tw.CellWidth{Global: 25}, }, Footer: tw.CellConfig{ - Formatting: tw.CellFormatting{Alignment: tw.AlignRight}, + Alignment: tw.CellAlignment{Global: tw.AlignRight}, }, }), ) @@ -480,19 +478,13 @@ func main() { table := tablewriter.NewTable(os.Stdout, tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{ - Settings: tw.Settings{ - Separators: tw.Separators{BetweenRows: tw.On}, - }, + Settings: tw.Settings{Separators: tw.Separators{BetweenRows: tw.On}}, })), tablewriter.WithConfig(tablewriter.Config{ - Header: tw.CellConfig{ - Formatting: tw.CellFormatting{Alignment: tw.AlignCenter}, - }, + Header: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignCenter}}, Row: tw.CellConfig{ - Formatting: tw.CellFormatting{ - MergeMode: tw.MergeHierarchical, - Alignment: tw.AlignLeft, - }, + Formatting: tw.CellFormatting{MergeMode: tw.MergeHierarchical}, + Alignment: tw.CellAlignment{Global: tw.AlignLeft}, }, }), ) @@ -546,21 +538,20 @@ func main() { table := tablewriter.NewTable(os.Stdout, tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{ - Settings: tw.Settings{ - Separators: tw.Separators{BetweenRows: tw.On}, - }, + Settings: tw.Settings{Separators: tw.Separators{BetweenRows: tw.On}}, })), tablewriter.WithConfig(tablewriter.Config{ Row: tw.CellConfig{ - Formatting: tw.CellFormatting{MergeMode: tw.MergeBoth}, - ColumnAligns: []tw.Align{tw.Skip, tw.Skip, tw.AlignRight, tw.AlignLeft}, + Formatting: tw.CellFormatting{MergeMode: tw.MergeBoth}, + Alignment: tw.CellAlignment{PerColumn: []tw.Align{tw.Skip, tw.Skip, tw.AlignRight, tw.AlignLeft}}, }, + Footer: tw.CellConfig{ Padding: tw.CellPadding{ Global: tw.Padding{Left: "*", Right: "*"}, PerColumn: []tw.Padding{{}, {}, {Bottom: "^"}, {Bottom: "^"}}, }, - ColumnAligns: []tw.Align{tw.Skip, tw.Skip, tw.AlignRight, tw.AlignLeft}, + Alignment: tw.CellAlignment{PerColumn: []tw.Align{tw.Skip, tw.Skip, tw.AlignRight, tw.AlignLeft}}, }, }), ) @@ -617,9 +608,7 @@ func main() { })), tablewriter.WithConfig(tablewriter.Config{ MaxWidth: 10, - Row: tw.CellConfig{ - Formatting: tw.CellFormatting{Alignment: tw.AlignCenter}, - }, + Row: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignCenter}}, }), ) table.Append([]string{s, s}) @@ -631,16 +620,12 @@ func main() { // Main table table := tablewriter.NewTable(os.Stdout, tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{ - Borders: tw.BorderNone, - Settings: tw.Settings{ - Separators: tw.Separators{BetweenColumns: tw.On}, - }, + Borders: tw.BorderNone, + Settings: tw.Settings{Separators: tw.Separators{BetweenColumns: tw.On}}, })), tablewriter.WithConfig(tablewriter.Config{ MaxWidth: 30, - Row: tw.CellConfig{ - Formatting: tw.CellFormatting{Alignment: tw.AlignCenter}, - }, + Row: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignCenter}}, }), ) table.Append([]string{createSubTable("A"), createSubTable("B")}) @@ -711,14 +696,11 @@ func main() { tablewriter.WithStringer(employeeStringer), tablewriter.WithConfig(tablewriter.Config{ Header: tw.CellConfig{ - Formatting: tw.CellFormatting{Alignment: tw.AlignCenter, AutoFormat: tw.On}, - }, - Row: tw.CellConfig{ - Formatting: tw.CellFormatting{Alignment: tw.AlignLeft}, - }, - Footer: tw.CellConfig{ - Formatting: tw.CellFormatting{Alignment: tw.AlignRight}, + Formatting: tw.CellFormatting{AutoFormat: tw.On}, + Alignment: tw.CellAlignment{Global: tw.AlignCenter}, }, + Row: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignLeft}}, + Footer: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignRight}}, }), ) table.Header([]string{"ID", "Name", "Age", "Department", "Salary"}) @@ -784,23 +766,17 @@ func main() { } table := tablewriter.NewTable(os.Stdout, - tablewriter.WithRenderer(renderer.NewHTML(os.Stdout, false, htmlCfg)), + tablewriter.WithRenderer(renderer.NewHTML(htmlCfg)), tablewriter.WithConfig(tablewriter.Config{ Header: tw.CellConfig{ - Formatting: tw.CellFormatting{ - Alignment: tw.AlignCenter, - MergeMode: tw.MergeHorizontal, // Merge identical header cells - }, + Formatting: tw.CellFormatting{MergeMode: tw.MergeHorizontal}, // Merge identical header cells + Alignment: tw.CellAlignment{Global: tw.AlignCenter}, }, Row: tw.CellConfig{ - Formatting: tw.CellFormatting{ - MergeMode: tw.MergeHorizontal, // Merge identical row cells - Alignment: tw.AlignLeft, - }, - }, - Footer: tw.CellConfig{ - Formatting: tw.CellFormatting{Alignment: tw.AlignRight}, + Formatting: tw.CellFormatting{MergeMode: tw.MergeHorizontal}, // Merge identical row cells + Alignment: tw.CellAlignment{Global: tw.AlignLeft}, }, + Footer: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignRight}}, }), ) diff --git a/vendor/github.com/olekukonko/tablewriter/config.go b/vendor/github.com/olekukonko/tablewriter/config.go index 64c55786c7..94094f1b00 100644 --- a/vendor/github.com/olekukonko/tablewriter/config.go +++ b/vendor/github.com/olekukonko/tablewriter/config.go @@ -1,89 +1,71 @@ -// Package tablewriter provides functionality for creating and formatting tables with customizable configurations. package tablewriter import ( - "github.com/olekukonko/ll" // Logging library for debug output - "github.com/olekukonko/tablewriter/tw" // Table writer core types and utilities - "io" // Input/output interfaces - "reflect" // Reflection for type handling + "github.com/olekukonko/tablewriter/tw" ) -// ColumnConfigBuilder is used to configure settings for a specific column across all table sections (header, row, footer). -type ColumnConfigBuilder struct { - parent *ConfigBuilder // Reference to the parent ConfigBuilder for chaining - col int // Index of the column being configured -} - -// Build returns the parent ConfigBuilder to allow method chaining. -func (c *ColumnConfigBuilder) Build() *ConfigBuilder { - return c.parent +// Config represents the table configuration +type Config struct { + MaxWidth int + Header tw.CellConfig + Row tw.CellConfig + Footer tw.CellConfig + Debug bool + Stream tw.StreamConfig + Behavior tw.Behavior + Widths tw.CellWidth } -// WithAlignment sets the text alignment for a specific column in the header section only. -// Invalid alignments are ignored, and the method returns the builder for chaining. -func (c *ColumnConfigBuilder) WithAlignment(align tw.Align) *ColumnConfigBuilder { - if align != tw.AlignLeft && align != tw.AlignRight && align != tw.AlignCenter && align != tw.AlignNone { - return c - } - // Ensure the ColumnAligns slice is large enough to accommodate the column index - if len(c.parent.config.Header.ColumnAligns) <= c.col { - newAligns := make([]tw.Align, c.col+1) - copy(newAligns, c.parent.config.Header.ColumnAligns) - c.parent.config.Header.ColumnAligns = newAligns - } - c.parent.config.Header.ColumnAligns[c.col] = align - return c +// ConfigBuilder provides a fluent interface for building Config +type ConfigBuilder struct { + config Config } -// WithMaxWidth sets the maximum width for a specific column across all sections (header, row, footer). -// Negative widths are ignored, and the method returns the builder for chaining. -func (c *ColumnConfigBuilder) WithMaxWidth(width int) *ColumnConfigBuilder { - if width < 0 { - return c - } - // Initialize PerColumn maps if they don't exist - if c.parent.config.Header.ColMaxWidths.PerColumn == nil { - c.parent.config.Header.ColMaxWidths.PerColumn = make(map[int]int) - c.parent.config.Row.ColMaxWidths.PerColumn = make(map[int]int) - c.parent.config.Footer.ColMaxWidths.PerColumn = make(map[int]int) +// NewConfigBuilder creates a new ConfigBuilder with defaults +func NewConfigBuilder() *ConfigBuilder { + return &ConfigBuilder{ + config: defaultConfig(), } - c.parent.config.Header.ColMaxWidths.PerColumn[c.col] = width - c.parent.config.Row.ColMaxWidths.PerColumn[c.col] = width - c.parent.config.Footer.ColMaxWidths.PerColumn[c.col] = width - return c } -// Config represents the overall configuration for a table, including settings for header, rows, footer, and behavior. -type Config struct { - MaxWidth int // Maximum width of the entire table (0 for unlimited) - Header tw.CellConfig // Configuration for the header section - Row tw.CellConfig // Configuration for the row section - Footer tw.CellConfig // Configuration for the footer section - Debug bool // Enables debug logging when true - Stream tw.StreamConfig // Configuration specific to streaming mode - Behavior Behavior // Behavioral settings like auto-hiding and trimming +// Build returns the built Config +func (b *ConfigBuilder) Build() Config { + return b.config } -// ConfigBuilder provides a fluent interface for building a Config struct with both direct and nested configuration methods. -type ConfigBuilder struct { - config Config // The configuration being built +// Header returns a HeaderConfigBuilder for header configuration +func (b *ConfigBuilder) Header() *HeaderConfigBuilder { + return &HeaderConfigBuilder{ + parent: b, + config: &b.config.Header, + } } -// Build finalizes and returns the Config struct after all modifications. -func (b *ConfigBuilder) Build() Config { - return b.config +// Row returns a RowConfigBuilder for row configuration +func (b *ConfigBuilder) Row() *RowConfigBuilder { + return &RowConfigBuilder{ + parent: b, + config: &b.config.Row, + } } -// Footer returns a builder for advanced configuration of the footer section. +// Footer returns a FooterConfigBuilder for footer configuration func (b *ConfigBuilder) Footer() *FooterConfigBuilder { return &FooterConfigBuilder{ - parent: b, - config: &b.config.Footer, - section: "footer", + parent: b, + config: &b.config.Footer, + } +} + +// Behavior returns a BehaviorConfigBuilder for behavior configuration +func (b *ConfigBuilder) Behavior() *BehaviorConfigBuilder { + return &BehaviorConfigBuilder{ + parent: b, + config: &b.config.Behavior, } } -// ForColumn returns a builder for configuring a specific column across all sections. +// ForColumn returns a ColumnConfigBuilder for column-specific configuration func (b *ConfigBuilder) ForColumn(col int) *ColumnConfigBuilder { return &ColumnConfigBuilder{ parent: b, @@ -91,22 +73,17 @@ func (b *ConfigBuilder) ForColumn(col int) *ColumnConfigBuilder { } } -// Header returns a builder for advanced configuration of the header section. -func (b *ConfigBuilder) Header() *HeaderConfigBuilder { - return &HeaderConfigBuilder{ - parent: b, - config: &b.config.Header, - section: "header", - } +// WithTrimSpace enables or disables automatic trimming of leading/trailing spaces. +// Ignored in streaming mode. +func (b *ConfigBuilder) WithTrimSpace(state tw.State) *ConfigBuilder { + b.config.Behavior.TrimSpace = state + return b } -// Row returns a builder for advanced configuration of the row section. -func (b *ConfigBuilder) Row() *RowConfigBuilder { - return &RowConfigBuilder{ - parent: b, - config: &b.config.Row, - section: "row", - } +// WithDebug enables/disables debug logging +func (b *ConfigBuilder) WithDebug(debug bool) *ConfigBuilder { + b.config.Debug = debug + return b } // WithAutoHide enables or disables automatic hiding of empty columns (ignored in streaming mode). @@ -115,19 +92,13 @@ func (b *ConfigBuilder) WithAutoHide(state tw.State) *ConfigBuilder { return b } -// WithDebug enables or disables debug logging for the table. -func (b *ConfigBuilder) WithDebug(debug bool) *ConfigBuilder { - b.config.Debug = debug - return b -} - // WithFooterAlignment sets the text alignment for all footer cells. // Invalid alignments are ignored. func (b *ConfigBuilder) WithFooterAlignment(align tw.Align) *ConfigBuilder { if align != tw.AlignLeft && align != tw.AlignRight && align != tw.AlignCenter && align != tw.AlignNone { return b } - b.config.Footer.Formatting.Alignment = align + b.config.Footer.Alignment.Global = align return b } @@ -179,7 +150,7 @@ func (b *ConfigBuilder) WithHeaderAlignment(align tw.Align) *ConfigBuilder { if align != tw.AlignLeft && align != tw.AlignRight && align != tw.AlignCenter && align != tw.AlignNone { return b } - b.config.Header.Formatting.Alignment = align + b.config.Header.Alignment.Global = align return b } @@ -242,7 +213,7 @@ func (b *ConfigBuilder) WithRowAlignment(align tw.Align) *ConfigBuilder { if align != tw.AlignLeft && align != tw.AlignRight && align != tw.AlignCenter && align != tw.AlignNone { return b } - b.config.Row.Formatting.Alignment = align + b.config.Row.Alignment.Global = align return b } @@ -288,864 +259,702 @@ func (b *ConfigBuilder) WithRowMergeMode(mergeMode int) *ConfigBuilder { return b } -// WithTrimSpace enables or disables automatic trimming of leading/trailing spaces. -// Ignored in streaming mode. -func (b *ConfigBuilder) WithTrimSpace(state tw.State) *ConfigBuilder { - b.config.Behavior.TrimSpace = state - return b +// HeaderConfigBuilder configures header settings +type HeaderConfigBuilder struct { + parent *ConfigBuilder + config *tw.CellConfig } -// FooterConfigBuilder provides advanced configuration options for the footer section. -type FooterConfigBuilder struct { - parent *ConfigBuilder // Reference to the parent ConfigBuilder - config *tw.CellConfig // Footer configuration being modified - section string // Section name for logging/debugging +// Build returns the parent ConfigBuilder +func (h *HeaderConfigBuilder) Build() *ConfigBuilder { + return h.parent } -// Build returns the parent ConfigBuilder for chaining. -func (f *FooterConfigBuilder) Build() *ConfigBuilder { - return f.parent +// Alignment returns an AlignmentConfigBuilder for header alignment +func (h *HeaderConfigBuilder) Alignment() *AlignmentConfigBuilder { + return &AlignmentConfigBuilder{ + parent: h.parent, + config: &h.config.Alignment, + section: "header", + } } -// Formatting returns a builder for configuring footer formatting settings. -func (f *FooterConfigBuilder) Formatting() *FooterFormattingBuilder { - return &FooterFormattingBuilder{ - parent: f, - config: &f.config.Formatting, - section: f.section, +// Formatting returns a HeaderFormattingBuilder for header formatting +func (h *HeaderConfigBuilder) Formatting() *HeaderFormattingBuilder { + return &HeaderFormattingBuilder{ + parent: h, + config: &h.config.Formatting, + section: "header", } } -// Padding returns a builder for configuring footer padding settings. -func (f *FooterConfigBuilder) Padding() *FooterPaddingBuilder { - return &FooterPaddingBuilder{ - parent: f, - config: &f.config.Padding, - section: f.section, +// Padding returns a HeaderPaddingBuilder for header padding +func (h *HeaderConfigBuilder) Padding() *HeaderPaddingBuilder { + return &HeaderPaddingBuilder{ + parent: h, + config: &h.config.Padding, + section: "header", } } -// FooterFormattingBuilder configures formatting options for the footer section. -type FooterFormattingBuilder struct { - parent *FooterConfigBuilder // Reference to the parent FooterConfigBuilder - config *tw.CellFormatting // Formatting configuration being modified - section string // Section name for logging/debugging +// Filter returns a HeaderFilterBuilder for header filtering +func (h *HeaderConfigBuilder) Filter() *HeaderFilterBuilder { + return &HeaderFilterBuilder{ + parent: h, + config: &h.config.Filter, + section: "header", + } } -// Build returns the parent FooterConfigBuilder for chaining. -func (ff *FooterFormattingBuilder) Build() *FooterConfigBuilder { - return ff.parent +// Callbacks returns a HeaderCallbacksBuilder for header callbacks +func (h *HeaderConfigBuilder) Callbacks() *HeaderCallbacksBuilder { + return &HeaderCallbacksBuilder{ + parent: h, + config: &h.config.Callbacks, + section: "header", + } } -// WithAlignment sets the text alignment for footer cells. -// Invalid alignments are ignored. -func (ff *FooterFormattingBuilder) WithAlignment(align tw.Align) *FooterFormattingBuilder { - if align != tw.AlignLeft && align != tw.AlignRight && align != tw.AlignCenter && align != tw.AlignNone { - return ff +// RowConfigBuilder configures row settings +type RowConfigBuilder struct { + parent *ConfigBuilder + config *tw.CellConfig +} + +// Build returns the parent ConfigBuilder +func (r *RowConfigBuilder) Build() *ConfigBuilder { + return r.parent +} + +// Alignment returns an AlignmentConfigBuilder for row alignment +func (r *RowConfigBuilder) Alignment() *AlignmentConfigBuilder { + return &AlignmentConfigBuilder{ + parent: r.parent, + config: &r.config.Alignment, + section: "row", } - ff.config.Alignment = align - return ff } -// WithAutoFormat enables or disables automatic formatting for footer cells. -func (ff *FooterFormattingBuilder) WithAutoFormat(autoFormat tw.State) *FooterFormattingBuilder { - ff.config.AutoFormat = autoFormat - return ff +// Formatting returns a RowFormattingBuilder for row formatting +func (r *RowConfigBuilder) Formatting() *RowFormattingBuilder { + return &RowFormattingBuilder{ + parent: r, + config: &r.config.Formatting, + section: "row", + } } -// WithAutoWrap sets the wrapping behavior for footer cells. -// Invalid wrap modes are ignored. -func (ff *FooterFormattingBuilder) WithAutoWrap(autoWrap int) *FooterFormattingBuilder { - if autoWrap < tw.WrapNone || autoWrap > tw.WrapBreak { - return ff +// Padding returns a RowPaddingBuilder for row padding +func (r *RowConfigBuilder) Padding() *RowPaddingBuilder { + return &RowPaddingBuilder{ + parent: r, + config: &r.config.Padding, + section: "row", } - ff.config.AutoWrap = autoWrap - return ff } -// WithMaxWidth sets the maximum content width for footer cells. -// Negative values are ignored. -//func (ff *FooterFormattingBuilder) WithMaxWidth(maxWidth int) *FooterFormattingBuilder { -// if maxWidth < 0 { -// return ff -// } -// ff.config.Foo = maxWidth -// return ff -//} - -// WithNewarkMode sets the merge behavior for footer cells (likely a typo, should be WithMergeMode). -// Invalid merge modes are ignored. -func (ff *FooterFormattingBuilder) WithNewarkMode(mergeMode int) *FooterFormattingBuilder { - if mergeMode < tw.MergeNone || mergeMode > tw.MergeHierarchical { - return ff +// Filter returns a RowFilterBuilder for row filtering +func (r *RowConfigBuilder) Filter() *RowFilterBuilder { + return &RowFilterBuilder{ + parent: r, + config: &r.config.Filter, + section: "row", } - ff.config.MergeMode = mergeMode - return ff } -// FooterPaddingBuilder configures padding options for the footer section. -type FooterPaddingBuilder struct { - parent *FooterConfigBuilder // Reference to the parent FooterConfigBuilder - config *tw.CellPadding // Padding configuration being modified - section string // Section name for logging/debugging +// Callbacks returns a RowCallbacksBuilder for row callbacks +func (r *RowConfigBuilder) Callbacks() *RowCallbacksBuilder { + return &RowCallbacksBuilder{ + parent: r, + config: &r.config.Callbacks, + section: "row", + } } -// AddColumnPadding adds padding for a specific column in the footer. -func (fp *FooterPaddingBuilder) AddColumnPadding(padding tw.Padding) *FooterPaddingBuilder { - fp.config.PerColumn = append(fp.config.PerColumn, padding) - return fp +// FooterConfigBuilder configures footer settings +type FooterConfigBuilder struct { + parent *ConfigBuilder + config *tw.CellConfig } -// Build returns the parent FooterConfigBuilder for chaining. -func (fp *FooterPaddingBuilder) Build() *FooterConfigBuilder { - return fp.parent +// Build returns the parent ConfigBuilder +func (f *FooterConfigBuilder) Build() *ConfigBuilder { + return f.parent } -// WithGlobal sets the global padding for all footer cells. -func (fp *FooterPaddingBuilder) WithGlobal(padding tw.Padding) *FooterPaddingBuilder { - fp.config.Global = padding - return fp +// Alignment returns an AlignmentConfigBuilder for footer alignment +func (f *FooterConfigBuilder) Alignment() *AlignmentConfigBuilder { + return &AlignmentConfigBuilder{ + parent: f.parent, + config: &f.config.Alignment, + section: "footer", + } } -// WithPerColumn sets per-column padding for the footer. -func (fp *FooterPaddingBuilder) WithPerColumn(padding []tw.Padding) *FooterPaddingBuilder { - fp.config.PerColumn = padding - return fp +// Formatting returns a FooterFormattingBuilder for footer formatting +func (f *FooterConfigBuilder) Formatting() *FooterFormattingBuilder { + return &FooterFormattingBuilder{ + parent: f, + config: &f.config.Formatting, + section: "footer", + } } -// HeaderConfigBuilder provides advanced configuration options for the header section. -type HeaderConfigBuilder struct { - parent *ConfigBuilder // Reference to the parent ConfigBuilder - config *tw.CellConfig // Header configuration being modified - section string // Section name for logging/debugging +// Padding returns a FooterPaddingBuilder for footer padding +func (f *FooterConfigBuilder) Padding() *FooterPaddingBuilder { + return &FooterPaddingBuilder{ + parent: f, + config: &f.config.Padding, + section: "footer", + } } -// Build returns the parent ConfigBuilder for chaining. -func (h *HeaderConfigBuilder) Build() *ConfigBuilder { - return h.parent +// Filter returns a FooterFilterBuilder for footer filtering +func (f *FooterConfigBuilder) Filter() *FooterFilterBuilder { + return &FooterFilterBuilder{ + parent: f, + config: &f.config.Filter, + section: "footer", + } } -// Formatting returns a builder for configuring header formatting settings. -func (h *HeaderConfigBuilder) Formatting() *HeaderFormattingBuilder { - return &HeaderFormattingBuilder{ - parent: h, - config: &h.config.Formatting, - section: h.section, +// Callbacks returns a FooterCallbacksBuilder for footer callbacks +func (f *FooterConfigBuilder) Callbacks() *FooterCallbacksBuilder { + return &FooterCallbacksBuilder{ + parent: f, + config: &f.config.Callbacks, + section: "footer", } } -// Padding returns a builder for configuring header padding settings. -func (h *HeaderConfigBuilder) Padding() *HeaderPaddingBuilder { - return &HeaderPaddingBuilder{ - parent: h, - config: &h.config.Padding, - section: h.section, +// AlignmentConfigBuilder configures alignment settings +type AlignmentConfigBuilder struct { + parent *ConfigBuilder + config *tw.CellAlignment + section string +} + +// Build returns the parent ConfigBuilder +func (a *AlignmentConfigBuilder) Build() *ConfigBuilder { + return a.parent +} + +// WithGlobal sets global alignment +func (a *AlignmentConfigBuilder) WithGlobal(align tw.Align) *AlignmentConfigBuilder { + if err := align.Validate(); err == nil { + a.config.Global = align } + return a } -// HeaderFormattingBuilder configures formatting options for the header section. +// WithPerColumn sets per-column alignments +func (a *AlignmentConfigBuilder) WithPerColumn(alignments []tw.Align) *AlignmentConfigBuilder { + if len(alignments) > 0 { + a.config.PerColumn = alignments + } + return a +} + +// HeaderFormattingBuilder configures header formatting type HeaderFormattingBuilder struct { - parent *HeaderConfigBuilder // Reference to the parent HeaderConfigBuilder - config *tw.CellFormatting // Formatting configuration being modified - section string // Section name for logging/debugging + parent *HeaderConfigBuilder + config *tw.CellFormatting + section string } -// Build returns the parent HeaderConfigBuilder for chaining. +// Build returns the parent HeaderConfigBuilder func (hf *HeaderFormattingBuilder) Build() *HeaderConfigBuilder { return hf.parent } -// WithAlignment sets the text alignment for header cells. -// Invalid alignments are ignored. -func (hf *HeaderFormattingBuilder) WithAlignment(align tw.Align) *HeaderFormattingBuilder { - if align != tw.AlignLeft && align != tw.AlignRight && align != tw.AlignCenter && align != tw.AlignNone { - return hf - } - hf.config.Alignment = align - return hf -} - -// WithAutoFormat enables or disables automatic formatting for header cells. +// WithAutoFormat enables/disables auto formatting func (hf *HeaderFormattingBuilder) WithAutoFormat(autoFormat tw.State) *HeaderFormattingBuilder { hf.config.AutoFormat = autoFormat return hf } -// WithAutoWrap sets the wrapping behavior for header cells. -// Invalid wrap modes are ignored. +// WithAutoWrap sets auto wrap mode func (hf *HeaderFormattingBuilder) WithAutoWrap(autoWrap int) *HeaderFormattingBuilder { - if autoWrap < tw.WrapNone || autoWrap > tw.WrapBreak { - return hf + if autoWrap >= tw.WrapNone && autoWrap <= tw.WrapBreak { + hf.config.AutoWrap = autoWrap } - hf.config.AutoWrap = autoWrap return hf } -// WithMaxWidth sets the maximum content width for header cells. -// Negative values are ignored. -//func (hf *HeaderFormattingBuilder) WithMaxWidth(maxWidth int) *HeaderFormattingBuilder { -// if maxWidth < 0 { -// return hf -// } -// hf.config.MaxWidth = maxWidth -// return hf -//} - -// WithMergeMode sets the merge behavior for header cells. -// Invalid merge modes are ignored. +// WithMergeMode sets merge mode func (hf *HeaderFormattingBuilder) WithMergeMode(mergeMode int) *HeaderFormattingBuilder { - if mergeMode < tw.MergeNone || mergeMode > tw.MergeHierarchical { - return hf + if mergeMode >= tw.MergeNone && mergeMode <= tw.MergeHierarchical { + hf.config.MergeMode = mergeMode } - hf.config.MergeMode = mergeMode return hf } -// HeaderPaddingBuilder configures padding options for the header section. -type HeaderPaddingBuilder struct { - parent *HeaderConfigBuilder // Reference to the parent HeaderConfigBuilder - config *tw.CellPadding // Padding configuration being modified - section string // Section name for logging/debugging +// RowFormattingBuilder configures row formatting +type RowFormattingBuilder struct { + parent *RowConfigBuilder + config *tw.CellFormatting + section string } -// AddColumnPadding adds padding for a specific column in the header. -func (hp *HeaderPaddingBuilder) AddColumnPadding(padding tw.Padding) *HeaderPaddingBuilder { - hp.config.PerColumn = append(hp.config.PerColumn, padding) - return hp +// Build returns the parent RowConfigBuilder +func (rf *RowFormattingBuilder) Build() *RowConfigBuilder { + return rf.parent } -// Build returns the parent HeaderConfigBuilder for chaining. -func (hp *HeaderPaddingBuilder) Build() *HeaderConfigBuilder { - return hp.parent +// WithAutoFormat enables/disables auto formatting +func (rf *RowFormattingBuilder) WithAutoFormat(autoFormat tw.State) *RowFormattingBuilder { + rf.config.AutoFormat = autoFormat + return rf } -// WithGlobal sets the global padding for all header cells. -func (hp *HeaderPaddingBuilder) WithGlobal(padding tw.Padding) *HeaderPaddingBuilder { - hp.config.Global = padding - return hp +// WithAutoWrap sets auto wrap mode +func (rf *RowFormattingBuilder) WithAutoWrap(autoWrap int) *RowFormattingBuilder { + if autoWrap >= tw.WrapNone && autoWrap <= tw.WrapBreak { + rf.config.AutoWrap = autoWrap + } + return rf } -// WithPerColumn sets per-column padding for the header. -func (hp *HeaderPaddingBuilder) WithPerColumn(padding []tw.Padding) *HeaderPaddingBuilder { - hp.config.PerColumn = padding - return hp +// WithMergeMode sets merge mode +func (rf *RowFormattingBuilder) WithMergeMode(mergeMode int) *RowFormattingBuilder { + if mergeMode >= tw.MergeNone && mergeMode <= tw.MergeHierarchical { + rf.config.MergeMode = mergeMode + } + return rf } -// Option defines a function type for configuring a Table instance. -type Option func(target *Table) - -// RowConfigBuilder provides advanced configuration options for the row section. -type RowConfigBuilder struct { - parent *ConfigBuilder // Reference to the parent ConfigBuilder - config *tw.CellConfig // Row configuration being modified - section string // Section name for logging/debugging +// FooterFormattingBuilder configures footer formatting +type FooterFormattingBuilder struct { + parent *FooterConfigBuilder + config *tw.CellFormatting + section string } -// Build returns the parent ConfigBuilder for chaining. -func (r *RowConfigBuilder) Build() *ConfigBuilder { - return r.parent +// Build returns the parent FooterConfigBuilder +func (ff *FooterFormattingBuilder) Build() *FooterConfigBuilder { + return ff.parent } -// Formatting returns a builder for configuring row formatting settings. -func (r *RowConfigBuilder) Formatting() *RowFormattingBuilder { - return &RowFormattingBuilder{ - parent: r, - config: &r.config.Formatting, - section: r.section, - } +// WithAutoFormat enables/disables auto formatting +func (ff *FooterFormattingBuilder) WithAutoFormat(autoFormat tw.State) *FooterFormattingBuilder { + ff.config.AutoFormat = autoFormat + return ff } -// Padding returns a builder for configuring row padding settings. -func (r *RowConfigBuilder) Padding() *RowPaddingBuilder { - return &RowPaddingBuilder{ - parent: r, - config: &r.config.Padding, - section: r.section, +// WithAutoWrap sets auto wrap mode +func (ff *FooterFormattingBuilder) WithAutoWrap(autoWrap int) *FooterFormattingBuilder { + if autoWrap >= tw.WrapNone && autoWrap <= tw.WrapBreak { + ff.config.AutoWrap = autoWrap } + return ff } -// RowFormattingBuilder configures formatting options for the row section. -type RowFormattingBuilder struct { - parent *RowConfigBuilder // Reference to the parent RowConfigBuilder - config *tw.CellFormatting // Formatting configuration being modified - section string // Section name for logging/debugging +// WithMergeMode sets merge mode +func (ff *FooterFormattingBuilder) WithMergeMode(mergeMode int) *FooterFormattingBuilder { + if mergeMode >= tw.MergeNone && mergeMode <= tw.MergeHierarchical { + ff.config.MergeMode = mergeMode + } + return ff } -// Build returns the parent RowConfigBuilder for chaining. -func (rf *RowFormattingBuilder) Build() *RowConfigBuilder { - return rf.parent +// HeaderPaddingBuilder configures header padding +type HeaderPaddingBuilder struct { + parent *HeaderConfigBuilder + config *tw.CellPadding + section string } -// WithAlignment sets the text alignment for row cells. -// Invalid alignments are ignored. -func (rf *RowFormattingBuilder) WithAlignment(align tw.Align) *RowFormattingBuilder { - if align != tw.AlignLeft && align != tw.AlignRight && align != tw.AlignCenter && align != tw.AlignNone { - return rf - } - rf.config.Alignment = align - return rf +// Build returns the parent HeaderConfigBuilder +func (hp *HeaderPaddingBuilder) Build() *HeaderConfigBuilder { + return hp.parent } -// WithAutoFormat enables or disables automatic formatting for row cells. -func (rf *RowFormattingBuilder) WithAutoFormat(autoFormat tw.State) *RowFormattingBuilder { - rf.config.AutoFormat = autoFormat - return rf +// WithGlobal sets global padding +func (hp *HeaderPaddingBuilder) WithGlobal(padding tw.Padding) *HeaderPaddingBuilder { + hp.config.Global = padding + return hp } -// WithAutoWrap sets the wrapping behavior for row cells. -// Invalid wrap modes are ignored. -func (rf *RowFormattingBuilder) WithAutoWrap(autoWrap int) *RowFormattingBuilder { - if autoWrap < tw.WrapNone || autoWrap > tw.WrapBreak { - return rf - } - rf.config.AutoWrap = autoWrap - return rf +// WithPerColumn sets per-column padding +func (hp *HeaderPaddingBuilder) WithPerColumn(padding []tw.Padding) *HeaderPaddingBuilder { + hp.config.PerColumn = padding + return hp } -// WithMergeMode sets the merge behavior for row cells. -// Invalid merge modes are ignored. -func (rf *RowFormattingBuilder) WithMergeMode(mergeMode int) *RowFormattingBuilder { - if mergeMode < tw.MergeNone || mergeMode > tw.MergeHierarchical { - return rf - } - rf.config.MergeMode = mergeMode - return rf +// AddColumnPadding adds padding for a specific column in the header +func (hp *HeaderPaddingBuilder) AddColumnPadding(padding tw.Padding) *HeaderPaddingBuilder { + hp.config.PerColumn = append(hp.config.PerColumn, padding) + return hp } -// RowPaddingBuilder configures padding options for the row section. +// RowPaddingBuilder configures row padding type RowPaddingBuilder struct { - parent *RowConfigBuilder // Reference to the parent RowConfigBuilder - config *tw.CellPadding // Padding configuration being modified - section string // Section name for logging/debugging + parent *RowConfigBuilder + config *tw.CellPadding + section string } -// AddColumnPadding adds padding for a specific column in the rows. -func (rp *RowPaddingBuilder) AddColumnPadding(padding tw.Padding) *RowPaddingBuilder { - rp.config.PerColumn = append(rp.config.PerColumn, padding) - return rp -} - -// Build returns the parent RowConfigBuilder for chaining. +// Build returns the parent RowConfigBuilder func (rp *RowPaddingBuilder) Build() *RowConfigBuilder { return rp.parent } -// WithGlobal sets the global padding for all row cells. +// WithGlobal sets global padding func (rp *RowPaddingBuilder) WithGlobal(padding tw.Padding) *RowPaddingBuilder { rp.config.Global = padding return rp } -// WithPerColumn sets per-column padding for the rows. +// WithPerColumn sets per-column padding func (rp *RowPaddingBuilder) WithPerColumn(padding []tw.Padding) *RowPaddingBuilder { rp.config.PerColumn = padding return rp } -// NewConfigBuilder creates a new ConfigBuilder initialized with default settings. -func NewConfigBuilder() *ConfigBuilder { - return &ConfigBuilder{ - config: defaultConfig(), - } +// AddColumnPadding adds padding for a specific column in the rows +func (rp *RowPaddingBuilder) AddColumnPadding(padding tw.Padding) *RowPaddingBuilder { + rp.config.PerColumn = append(rp.config.PerColumn, padding) + return rp } -// NewWriter creates a new table with default settings for backward compatibility. -// It logs the creation if debugging is enabled. -func NewWriter(w io.Writer) *Table { - t := NewTable(w) - if t.logger != nil { - t.logger.Debug("NewWriter created buffered Table") - } - return t +// FooterPaddingBuilder configures footer padding +type FooterPaddingBuilder struct { + parent *FooterConfigBuilder + config *tw.CellPadding + section string } -// WithAutoHide enables or disables automatic hiding of columns with empty data rows. -// Logs the change if debugging is enabled. -func WithAutoHide(state tw.State) Option { - return func(target *Table) { - target.config.Behavior.AutoHide = state - if target.logger != nil { - target.logger.Debugf("Option: WithAutoHide applied to Table: %v", state) - } - } +// Build returns the parent FooterConfigBuilder +func (fp *FooterPaddingBuilder) Build() *FooterConfigBuilder { + return fp.parent } -// WithColumnMax sets a global maximum column width for the table in streaming mode. -// Negative values are ignored, and the change is logged if debugging is enabled. -func WithColumnMax(width int) Option { - return func(target *Table) { - if width < 0 { - return - } - target.config.Stream.Widths.Global = width - if target.logger != nil { - target.logger.Debugf("Option: WithColumnMax applied to Table: %v", width) - } - } +// WithGlobal sets global padding +func (fp *FooterPaddingBuilder) WithGlobal(padding tw.Padding) *FooterPaddingBuilder { + fp.config.Global = padding + return fp } -// WithTableMax sets a global maximum table width for the table -// Negative values are ignored, and the change is logged if debugging is enabled. -func WithTableMax(width int) Option { - return func(target *Table) { - if width < 0 { - return - } - target.config.MaxWidth = width - if target.logger != nil { - target.logger.Debugf("Option: WithTableMax applied to Table: %v", width) - } - } +// WithPerColumn sets per-column padding +func (fp *FooterPaddingBuilder) WithPerColumn(padding []tw.Padding) *FooterPaddingBuilder { + fp.config.PerColumn = padding + return fp } -// WithColumnWidths sets per-column widths for the table in streaming mode. -// Negative widths are removed, and the change is logged if debugging is enabled. -func WithColumnWidths(widths map[int]int) Option { - return func(target *Table) { - for k, v := range widths { - if v < 0 { - delete(widths, k) - } - } - target.config.Stream.Widths.PerColumn = widths - if target.logger != nil { - target.logger.Debugf("Option: WithColumnWidths applied to Table: %v", widths) - } - } +// AddColumnPadding adds padding for a specific column in the footer +func (fp *FooterPaddingBuilder) AddColumnPadding(padding tw.Padding) *FooterPaddingBuilder { + fp.config.PerColumn = append(fp.config.PerColumn, padding) + return fp } -// WithConfig applies a custom configuration to the table by merging it with the default configuration. -func WithConfig(cfg Config) Option { - return func(target *Table) { - target.config = mergeConfig(defaultConfig(), cfg) - } +// BehaviorConfigBuilder configures behavior settings +type BehaviorConfigBuilder struct { + parent *ConfigBuilder + config *tw.Behavior } -// WithDebug enables or disables debug logging and adjusts the logger level accordingly. -// Logs the change if debugging is enabled. -func WithDebug(debug bool) Option { - return func(target *Table) { - target.config.Debug = debug - } +// Build returns the parent ConfigBuilder +func (bb *BehaviorConfigBuilder) Build() *ConfigBuilder { + return bb.parent } -// WithFooter sets the table footers by calling the Footer method. -func WithFooter(footers []string) Option { - return func(target *Table) { - target.Footer(footers) - } +// WithAutoHide enables/disables auto-hide +func (bb *BehaviorConfigBuilder) WithAutoHide(state tw.State) *BehaviorConfigBuilder { + bb.config.AutoHide = state + return bb } -// WithFooterConfig applies a full footer configuration to the table. -// Logs the change if debugging is enabled. -func WithFooterConfig(config tw.CellConfig) Option { - return func(target *Table) { - target.config.Footer = config - if target.logger != nil { - target.logger.Debug("Option: WithFooterConfig applied to Table.") - } - } +// WithTrimSpace enables/disables trim space +func (bb *BehaviorConfigBuilder) WithTrimSpace(state tw.State) *BehaviorConfigBuilder { + bb.config.TrimSpace = state + return bb } -// WithFooterMergeMode sets the merge mode for footer cells. -// Invalid merge modes are ignored, and the change is logged if debugging is enabled. -func WithFooterMergeMode(mergeMode int) Option { - return func(target *Table) { - if mergeMode < tw.MergeNone || mergeMode > tw.MergeHierarchical { - return - } - target.config.Footer.Formatting.MergeMode = mergeMode - if target.logger != nil { - target.logger.Debugf("Option: WithFooterMergeMode applied to Table: %v", mergeMode) - } - } +// WithHeaderHide enables/disables header visibility +func (bb *BehaviorConfigBuilder) WithHeaderHide(state tw.State) *BehaviorConfigBuilder { + bb.config.Header.Hide = state + return bb } -// WithHeader sets the table headers by calling the Header method. -func WithHeader(headers []string) Option { - return func(target *Table) { - target.Header(headers) - } +// WithFooterHide enables/disables footer visibility +func (bb *BehaviorConfigBuilder) WithFooterHide(state tw.State) *BehaviorConfigBuilder { + bb.config.Footer.Hide = state + return bb +} + +// WithCompactMerge enables/disables compact width optimization for merged cells +func (bb *BehaviorConfigBuilder) WithCompactMerge(state tw.State) *BehaviorConfigBuilder { + bb.config.Compact.Merge = state + return bb +} + +// ColumnConfigBuilder configures column-specific settings +type ColumnConfigBuilder struct { + parent *ConfigBuilder + col int +} + +// Build returns the parent ConfigBuilder +func (c *ColumnConfigBuilder) Build() *ConfigBuilder { + return c.parent } -// WithHeaderAlignment sets the text alignment for header cells. -// Invalid alignments are ignored, and the change is logged if debugging is enabled. -func WithHeaderAlignment(align tw.Align) Option { - return func(target *Table) { - if align != tw.AlignLeft && align != tw.AlignRight && align != tw.AlignCenter && align != tw.AlignNone { - return +// WithAlignment sets alignment for the column +func (c *ColumnConfigBuilder) WithAlignment(align tw.Align) *ColumnConfigBuilder { + if err := align.Validate(); err == nil { + // Ensure slices are large enough + if len(c.parent.config.Header.Alignment.PerColumn) <= c.col { + newAligns := make([]tw.Align, c.col+1) + copy(newAligns, c.parent.config.Header.Alignment.PerColumn) + c.parent.config.Header.Alignment.PerColumn = newAligns } - target.config.Header.Formatting.Alignment = align - if target.logger != nil { - target.logger.Debugf("Option: WithHeaderAlignment applied to Table: %v", align) + c.parent.config.Header.Alignment.PerColumn[c.col] = align + + if len(c.parent.config.Row.Alignment.PerColumn) <= c.col { + newAligns := make([]tw.Align, c.col+1) + copy(newAligns, c.parent.config.Row.Alignment.PerColumn) + c.parent.config.Row.Alignment.PerColumn = newAligns } - } -} + c.parent.config.Row.Alignment.PerColumn[c.col] = align -// WithHeaderConfig applies a full header configuration to the table. -// Logs the change if debugging is enabled. -func WithHeaderConfig(config tw.CellConfig) Option { - return func(target *Table) { - target.config.Header = config - if target.logger != nil { - target.logger.Debug("Option: WithHeaderConfig applied to Table.") + if len(c.parent.config.Footer.Alignment.PerColumn) <= c.col { + newAligns := make([]tw.Align, c.col+1) + copy(newAligns, c.parent.config.Footer.Alignment.PerColumn) + c.parent.config.Footer.Alignment.PerColumn = newAligns } + c.parent.config.Footer.Alignment.PerColumn[c.col] = align } + return c } -// WithLogger sets a custom logger for the table and updates the renderer if present. -// Logs the change if debugging is enabled. -func WithLogger(logger *ll.Logger) Option { - return func(target *Table) { - target.logger = logger - if target.logger != nil { - target.logger.Debug("Option: WithLogger applied to Table.") - if target.renderer != nil { - target.renderer.Logger(target.logger) - } +// WithMaxWidth sets max width for the column +func (c *ColumnConfigBuilder) WithMaxWidth(width int) *ColumnConfigBuilder { + if width >= 0 { + // Initialize maps if needed + if c.parent.config.Header.ColMaxWidths.PerColumn == nil { + c.parent.config.Header.ColMaxWidths.PerColumn = make(tw.Mapper[int, int]) + c.parent.config.Row.ColMaxWidths.PerColumn = make(tw.Mapper[int, int]) + c.parent.config.Footer.ColMaxWidths.PerColumn = make(tw.Mapper[int, int]) } + c.parent.config.Header.ColMaxWidths.PerColumn[c.col] = width + c.parent.config.Row.ColMaxWidths.PerColumn[c.col] = width + c.parent.config.Footer.ColMaxWidths.PerColumn[c.col] = width } + return c } -// WithRenderer sets a custom renderer for the table and attaches the logger if present. -// Logs the change if debugging is enabled. -func WithRenderer(f tw.Renderer) Option { - return func(target *Table) { - target.renderer = f - if target.logger != nil { - target.logger.Debugf("Option: WithRenderer applied to Table: %T", f) - f.Logger(target.logger) - } - } +// HeaderFilterBuilder configures header filtering +type HeaderFilterBuilder struct { + parent *HeaderConfigBuilder + config *tw.CellFilter + section string } -// WithRowConfig applies a full row configuration to the table. -// Logs the change if debugging is enabled. -func WithRowConfig(config tw.CellConfig) Option { - return func(target *Table) { - target.config.Row = config - if target.logger != nil { - target.logger.Debug("Option: WithRowConfig applied to Table.") - } - } +// Build returns the parent HeaderConfigBuilder +func (hf *HeaderFilterBuilder) Build() *HeaderConfigBuilder { + return hf.parent } -// WithRowMaxWidth sets the maximum content width for row cells. -// Negative values are ignored, and the change is logged if debugging is enabled. -func WithRowMaxWidth(maxWidth int) Option { - return func(target *Table) { - if maxWidth < 0 { - return - } - target.config.Row.ColMaxWidths.Global = maxWidth - if target.logger != nil { - target.logger.Debugf("Option: WithRowMaxWidth applied to Table: %v", maxWidth) - } +// WithGlobal sets the global filter function for the header +func (hf *HeaderFilterBuilder) WithGlobal(filter func([]string) []string) *HeaderFilterBuilder { + if filter != nil { + hf.config.Global = filter } + return hf } -// WithStreaming applies a streaming configuration to the table by merging it with the existing configuration. -// Logs the change if debugging is enabled. -func WithStreaming(c tw.StreamConfig) Option { - return func(target *Table) { - target.config.Stream = mergeStreamConfig(target.config.Stream, c) - if target.logger != nil { - target.logger.Debug("Option: WithStreaming applied to Table.") - } +// WithPerColumn sets per-column filter functions for the header +func (hf *HeaderFilterBuilder) WithPerColumn(filters []func(string) string) *HeaderFilterBuilder { + if len(filters) > 0 { + hf.config.PerColumn = filters } + return hf } -// WithStringer sets a custom stringer function for converting row data and clears the stringer cache. -// Logs the change if debugging is enabled. -func WithStringer(stringer interface{}) Option { - return func(t *Table) { - t.stringer = stringer - t.stringerCacheMu.Lock() - t.stringerCache = make(map[reflect.Type]reflect.Value) - t.stringerCacheMu.Unlock() - t.logger.Debug("Stringer updated, cache cleared") +// AddColumnFilter adds a filter function for a specific column in the header +func (hf *HeaderFilterBuilder) AddColumnFilter(filter func(string) string) *HeaderFilterBuilder { + if filter != nil { + hf.config.PerColumn = append(hf.config.PerColumn, filter) } + return hf } -// WithStringerCache enables caching for the stringer function. -func WithStringerCache() Option { - return func(t *Table) { - t.stringerCacheEnabled = true - } +// RowFilterBuilder configures row filtering +type RowFilterBuilder struct { + parent *RowConfigBuilder + config *tw.CellFilter + section string } -// WithSymbols sets the symbols used for table drawing and updates the renderer's configuration. -// Logs the change if debugging is enabled. -func WithSymbols(symbols tw.Symbols) Option { - return func(target *Table) { - if target.renderer != nil { - cfg := target.renderer.Config() - cfg.Symbols = symbols - if target.logger != nil { - target.logger.Debug("Option: WithSymbols applied to Table.") - } - } - } +// Build returns the parent RowConfigBuilder +func (rf *RowFilterBuilder) Build() *RowConfigBuilder { + return rf.parent } -// WithTrimSpace sets whether leading and trailing spaces are automatically trimmed. -// Logs the change if debugging is enabled. -func WithTrimSpace(state tw.State) Option { - return func(target *Table) { - target.config.Behavior.TrimSpace = state - if target.logger != nil { - target.logger.Debugf("Option: WithTrimSpace applied to Table: %v", state) - } +// WithGlobal sets the global filter function for the rows +func (rf *RowFilterBuilder) WithGlobal(filter func([]string) []string) *RowFilterBuilder { + if filter != nil { + rf.config.Global = filter } + return rf } -func WithHeaderAutoFormat(state tw.State) Option { - return func(target *Table) { - target.config.Header.Formatting.AutoFormat = state +// WithPerColumn sets per-column filter functions for the rows +func (rf *RowFilterBuilder) WithPerColumn(filters []func(string) string) *RowFilterBuilder { + if len(filters) > 0 { + rf.config.PerColumn = filters } + return rf } -// WithHeaderControl sets the control behavior for the table header. -// Logs the change if debugging is enabled. -func WithHeaderControl(control tw.Control) Option { - return func(target *Table) { - target.config.Behavior.Header = control - if target.logger != nil { - target.logger.Debugf("Option: WithHeaderControl applied to Table: %v", control) // Fixed 'state' to 'control' - } +// AddColumnFilter adds a filter function for a specific column in the rows +func (rf *RowFilterBuilder) AddColumnFilter(filter func(string) string) *RowFilterBuilder { + if filter != nil { + rf.config.PerColumn = append(rf.config.PerColumn, filter) } + return rf } -// WithFooterControl sets the control behavior for the table footer. -// Logs the change if debugging is enabled. -func WithFooterControl(control tw.Control) Option { - return func(target *Table) { - target.config.Behavior.Footer = control - if target.logger != nil { - target.logger.Debugf("Option: WithFooterControl applied to Table: %v", control) // Fixed log message and 'state' to 'control' - } +// FooterFilterBuilder configures footer filtering +type FooterFilterBuilder struct { + parent *FooterConfigBuilder + config *tw.CellFilter + section string +} + +// Build returns the parent FooterConfigBuilder +func (ff *FooterFilterBuilder) Build() *FooterConfigBuilder { + return ff.parent +} + +// WithGlobal sets the global filter function for the footer +func (ff *FooterFilterBuilder) WithGlobal(filter func([]string) []string) *FooterFilterBuilder { + if filter != nil { + ff.config.Global = filter } + return ff } -// WithAlignment sets the default column alignment for the header, rows, and footer. -func WithAlignment(alignment tw.Alignment) Option { - return func(target *Table) { - target.config.Header.ColumnAligns = alignment - target.config.Row.ColumnAligns = alignment - target.config.Footer.ColumnAligns = alignment +// WithPerColumn sets per-column filter functions for the footer +func (ff *FooterFilterBuilder) WithPerColumn(filters []func(string) string) *FooterFilterBuilder { + if len(filters) > 0 { + ff.config.PerColumn = filters } + return ff } -// WithPadding sets the global padding for the header, rows, and footer. -func WithPadding(padding tw.Padding) Option { - return func(target *Table) { - target.config.Header.Padding.Global = padding - target.config.Row.Padding.Global = padding - target.config.Footer.Padding.Global = padding +// AddColumnFilter adds a filter function for a specific column in the footer +func (ff *FooterFilterBuilder) AddColumnFilter(filter func(string) string) *FooterFilterBuilder { + if filter != nil { + ff.config.PerColumn = append(ff.config.PerColumn, filter) } + return ff } -// WithRendition allows updating the active renderer's rendition configuration -// by merging the provided rendition. -// If the renderer does not implement tw.Renditioning, a warning is logged. -func WithRendition(rendition tw.Rendition) Option { - return func(target *Table) { - if target.renderer == nil { - target.logger.Warn("Option: WithRendition: No renderer set on table.") - return - } +// HeaderCallbacksBuilder configures header callbacks +type HeaderCallbacksBuilder struct { + parent *HeaderConfigBuilder + config *tw.CellCallbacks + section string +} - if ru, ok := target.renderer.(tw.Renditioning); ok { - ru.Rendition(rendition) - target.logger.Debugf("Option: WithRendition: Applied to renderer via Renditioning.SetRendition(): %+v", rendition) - } else { - target.logger.Warnf("Option: WithRendition: Current renderer type %T does not implement tw.Renditioning. Rendition may not be applied as expected.", target.renderer) - } - } +// Build returns the parent HeaderConfigBuilder +func (hc *HeaderCallbacksBuilder) Build() *HeaderConfigBuilder { + return hc.parent } -// defaultConfig returns a default Config with sensible settings for headers, rows, footers, and behavior. -func defaultConfig() Config { - defaultPadding := tw.Padding{Left: tw.Space, Right: tw.Space, Top: tw.Empty, Bottom: tw.Empty} - return Config{ - MaxWidth: 0, - Header: tw.CellConfig{ - Formatting: tw.CellFormatting{ - AutoWrap: tw.WrapTruncate, - Alignment: tw.AlignCenter, - AutoFormat: tw.On, - MergeMode: tw.MergeNone, - }, - Padding: tw.CellPadding{ - Global: defaultPadding, - }, - }, - Row: tw.CellConfig{ - Formatting: tw.CellFormatting{ - AutoWrap: tw.WrapNormal, - Alignment: tw.AlignLeft, - AutoFormat: tw.Off, - MergeMode: tw.MergeNone, - }, - Padding: tw.CellPadding{ - Global: defaultPadding, - }, - }, - Footer: tw.CellConfig{ - Formatting: tw.CellFormatting{ - AutoWrap: tw.WrapNormal, - Alignment: tw.AlignRight, - AutoFormat: tw.Off, - MergeMode: tw.MergeNone, - }, - Padding: tw.CellPadding{ - Global: defaultPadding, - }, - }, - Stream: tw.StreamConfig{}, - Debug: false, - Behavior: Behavior{ - AutoHide: tw.Off, - TrimSpace: tw.On, - }, +// WithGlobal sets the global callback function for the header +func (hc *HeaderCallbacksBuilder) WithGlobal(callback func()) *HeaderCallbacksBuilder { + if callback != nil { + hc.config.Global = callback } + return hc } -// mergeCellConfig merges a source CellConfig into a destination CellConfig, prioritizing non-default source values. -// It handles deep merging for complex fields like padding and callbacks. -func mergeCellConfig(dst, src tw.CellConfig) tw.CellConfig { - if src.Formatting.Alignment != tw.Empty { - dst.Formatting.Alignment = src.Formatting.Alignment - } - if src.Formatting.AutoWrap != 0 { - dst.Formatting.AutoWrap = src.Formatting.AutoWrap +// WithPerColumn sets per-column callback functions for the header +func (hc *HeaderCallbacksBuilder) WithPerColumn(callbacks []func()) *HeaderCallbacksBuilder { + if len(callbacks) > 0 { + hc.config.PerColumn = callbacks } - if src.ColMaxWidths.Global != 0 { - dst.ColMaxWidths.Global = src.ColMaxWidths.Global - } - if src.Formatting.MergeMode != 0 { - dst.Formatting.MergeMode = src.Formatting.MergeMode + return hc +} + +// AddColumnCallback adds a callback function for a specific column in the header +func (hc *HeaderCallbacksBuilder) AddColumnCallback(callback func()) *HeaderCallbacksBuilder { + if callback != nil { + hc.config.PerColumn = append(hc.config.PerColumn, callback) } + return hc +} - dst.Formatting.AutoFormat = src.Formatting.AutoFormat +// RowCallbacksBuilder configures row callbacks +type RowCallbacksBuilder struct { + parent *RowConfigBuilder + config *tw.CellCallbacks + section string +} - if src.Padding.Global != (tw.Padding{}) { - dst.Padding.Global = src.Padding.Global - } - if len(src.Padding.PerColumn) > 0 { - if dst.Padding.PerColumn == nil { - dst.Padding.PerColumn = make([]tw.Padding, len(src.Padding.PerColumn)) - } else if len(src.Padding.PerColumn) > len(dst.Padding.PerColumn) { - dst.Padding.PerColumn = append(dst.Padding.PerColumn, make([]tw.Padding, len(src.Padding.PerColumn)-len(dst.Padding.PerColumn))...) - } - for i, pad := range src.Padding.PerColumn { - if pad != (tw.Padding{}) { - dst.Padding.PerColumn[i] = pad - } - } - } - if src.Callbacks.Global != nil { - dst.Callbacks.Global = src.Callbacks.Global - } - if len(src.Callbacks.PerColumn) > 0 { - if dst.Callbacks.PerColumn == nil { - dst.Callbacks.PerColumn = make([]func(), len(src.Callbacks.PerColumn)) - } else if len(src.Callbacks.PerColumn) > len(dst.Callbacks.PerColumn) { - dst.Callbacks.PerColumn = append(dst.Callbacks.PerColumn, make([]func(), len(src.Callbacks.PerColumn)-len(dst.Callbacks.PerColumn))...) - } - for i, cb := range src.Callbacks.PerColumn { - if cb != nil { - dst.Callbacks.PerColumn[i] = cb - } - } - } - if src.Filter.Global != nil { - dst.Filter.Global = src.Filter.Global - } - if len(src.Filter.PerColumn) > 0 { - if dst.Filter.PerColumn == nil { - dst.Filter.PerColumn = make([]func(string) string, len(src.Filter.PerColumn)) - } else if len(src.Filter.PerColumn) > len(dst.Filter.PerColumn) { - dst.Filter.PerColumn = append(dst.Filter.PerColumn, make([]func(string) string, len(src.Filter.PerColumn)-len(dst.Filter.PerColumn))...) - } - for i, filter := range src.Filter.PerColumn { - if filter != nil { - dst.Filter.PerColumn[i] = filter - } - } - } - if len(src.ColumnAligns) > 0 { - if dst.ColumnAligns == nil { - dst.ColumnAligns = make([]tw.Align, len(src.ColumnAligns)) - } else if len(src.ColumnAligns) > len(dst.ColumnAligns) { - dst.ColumnAligns = append(dst.ColumnAligns, make([]tw.Align, len(src.ColumnAligns)-len(dst.ColumnAligns))...) - } - for i, align := range src.ColumnAligns { - if align != tw.Empty && align != tw.Skip { - dst.ColumnAligns[i] = align - } - } +// Build returns the parent RowConfigBuilder +func (rc *RowCallbacksBuilder) Build() *RowConfigBuilder { + return rc.parent +} + +// WithGlobal sets the global callback function for the rows +func (rc *RowCallbacksBuilder) WithGlobal(callback func()) *RowCallbacksBuilder { + if callback != nil { + rc.config.Global = callback } - if len(src.ColMaxWidths.PerColumn) > 0 { - if dst.ColMaxWidths.PerColumn == nil { - dst.ColMaxWidths.PerColumn = make(map[int]int) - } - for k, v := range src.ColMaxWidths.PerColumn { - if v != 0 { - dst.ColMaxWidths.PerColumn[k] = v - } - } + return rc +} + +// WithPerColumn sets per-column callback functions for the rows +func (rc *RowCallbacksBuilder) WithPerColumn(callbacks []func()) *RowCallbacksBuilder { + if len(callbacks) > 0 { + rc.config.PerColumn = callbacks } - return dst + return rc } -// mergeConfig merges a source Config into a destination Config, prioritizing non-default source values. -// It performs deep merging for complex types like Header, Row, Footer, and Stream. -func mergeConfig(dst, src Config) Config { - if src.MaxWidth != 0 { - dst.MaxWidth = src.MaxWidth +// AddColumnCallback adds a callback function for a specific column in the rows +func (rc *RowCallbacksBuilder) AddColumnCallback(callback func()) *RowCallbacksBuilder { + if callback != nil { + rc.config.PerColumn = append(rc.config.PerColumn, callback) } - dst.Debug = src.Debug || dst.Debug - dst.Behavior.AutoHide = src.Behavior.AutoHide - dst.Behavior.TrimSpace = src.Behavior.TrimSpace - dst.Header = mergeCellConfig(dst.Header, src.Header) - dst.Row = mergeCellConfig(dst.Row, src.Row) - dst.Footer = mergeCellConfig(dst.Footer, src.Footer) - dst.Stream = mergeStreamConfig(dst.Stream, src.Stream) + return rc +} - return dst +// FooterCallbacksBuilder configures footer callbacks +type FooterCallbacksBuilder struct { + parent *FooterConfigBuilder + config *tw.CellCallbacks + section string } -// mergeStreamConfig merges a source StreamConfig into a destination StreamConfig, prioritizing non-default source values. -func mergeStreamConfig(dst, src tw.StreamConfig) tw.StreamConfig { - if src.Enable { - dst.Enable = true - } - if src.Widths.Global != 0 { - dst.Widths.Global = src.Widths.Global - } - if len(src.Widths.PerColumn) > 0 { - if dst.Widths.PerColumn == nil { - dst.Widths.PerColumn = make(map[int]int) - } - for k, v := range src.Widths.PerColumn { - if v != 0 { - dst.Widths.PerColumn[k] = v - } - } +// Build returns the parent FooterConfigBuilder +func (fc *FooterCallbacksBuilder) Build() *FooterConfigBuilder { + return fc.parent +} + +// WithGlobal sets the global callback function for the footer +func (fc *FooterCallbacksBuilder) WithGlobal(callback func()) *FooterCallbacksBuilder { + if callback != nil { + fc.config.Global = callback } - return dst + return fc } -// padLine pads a line to the specified column count by appending empty strings as needed. -func padLine(line []string, numCols int) []string { - if len(line) >= numCols { - return line +// WithPerColumn sets per-column callback functions for the footer +func (fc *FooterCallbacksBuilder) WithPerColumn(callbacks []func()) *FooterCallbacksBuilder { + if len(callbacks) > 0 { + fc.config.PerColumn = callbacks } - padded := make([]string, numCols) - copy(padded, line) - for i := len(line); i < numCols; i++ { - padded[i] = tw.Empty + return fc +} + +// AddColumnCallback adds a callback function for a specific column in the footer +func (fc *FooterCallbacksBuilder) AddColumnCallback(callback func()) *FooterCallbacksBuilder { + if callback != nil { + fc.config.PerColumn = append(fc.config.PerColumn, callback) } - return padded + return fc } diff --git a/vendor/github.com/olekukonko/tablewriter/deprecated.go b/vendor/github.com/olekukonko/tablewriter/deprecated.go index 3958a25b3a..b61d507ac8 100644 --- a/vendor/github.com/olekukonko/tablewriter/deprecated.go +++ b/vendor/github.com/olekukonko/tablewriter/deprecated.go @@ -2,9 +2,27 @@ package tablewriter import "github.com/olekukonko/tablewriter/tw" -// Deprecated: WithBorders is no longer used. -// Border control has been moved to the renderer, which now manages its own borders. -// This Option has no effect on the Table and may be removed in future versions. +// WithBorders configures the table's border settings by updating the renderer's border configuration. +// This function is deprecated and will be removed in a future version. +// +// Deprecated: Use [WithRendition] to configure border settings for renderers that support +// [tw.Renditioning], or update the renderer's [tw.RenderConfig] directly via its Config() method. +// This function has no effect if no renderer is set on the table. +// +// Example migration: +// +// // Old (deprecated) +// table.Options(WithBorders(tw.Border{Top: true, Bottom: true})) +// // New (recommended) +// table.Options(WithRendition(tw.Rendition{Borders: tw.Border{Top: true, Bottom: true}})) +// +// Parameters: +// - borders: The [tw.Border] configuration to apply to the renderer's borders. +// +// Returns: +// +// An [Option] that updates the renderer's border settings if a renderer is set. +// Logs a debug message if debugging is enabled and a renderer is present. func WithBorders(borders tw.Border) Option { return func(target *Table) { if target.renderer != nil { @@ -17,16 +35,55 @@ func WithBorders(borders tw.Border) Option { } } -// Deprecated: WithBorders is no longer supported. -// Use [tw.Behavior] directly to configure border settings. +// Behavior is an alias for [tw.Behavior] to configure table behavior settings. +// This type is deprecated and will be removed in a future version. +// +// Deprecated: Use [tw.Behavior] directly to configure settings such as auto-hiding empty +// columns, trimming spaces, or controlling header/footer visibility. +// +// Example migration: +// +// // Old (deprecated) +// var b tablewriter.Behavior = tablewriter.Behavior{AutoHide: tw.On} +// // New (recommended) +// var b tw.Behavior = tw.Behavior{AutoHide: tw.On} type Behavior tw.Behavior -// Deprecated: WithRendererSettings i sno longer supported. +// Settings is an alias for [tw.Settings] to configure renderer settings. +// This type is deprecated and will be removed in a future version. +// +// Deprecated: Use [tw.Settings] directly to configure renderer settings, such as +// separators and line styles. +// +// Example migration: +// +// // Old (deprecated) +// var s tablewriter.Settings = tablewriter.Settings{Separator: "|"} +// // New (recommended) +// var s tw.Settings = tw.Settings{Separator: "|"} type Settings tw.Settings -// WithRendererSettings updates the renderer's settings (e.g., separators, lines). -// Render setting has move to renders directly -// you can also use WithRendition for renders that have rendition support +// WithRendererSettings updates the renderer's settings, such as separators and line styles. +// This function is deprecated and will be removed in a future version. +// +// Deprecated: Use [WithRendition] to update renderer settings for renderers that implement +// [tw.Renditioning], or configure the renderer's [tw.Settings] directly via its +// [tw.Renderer.Config] method. This function has no effect if no renderer is set. +// +// Example migration: +// +// // Old (deprecated) +// table.Options(WithRendererSettings(tw.Settings{Separator: "|"})) +// // New (recommended) +// table.Options(WithRendition(tw.Rendition{Settings: tw.Settings{Separator: "|"}})) +// +// Parameters: +// - settings: The [tw.Settings] configuration to apply to the renderer. +// +// Returns: +// +// An [Option] that updates the renderer's settings if a renderer is set. +// Logs a debug message if debugging is enabled and a renderer is present. func WithRendererSettings(settings tw.Settings) Option { return func(target *Table) { if target.renderer != nil { @@ -38,3 +95,124 @@ func WithRendererSettings(settings tw.Settings) Option { } } } + +// WithAlignment sets the text alignment for footer cells within the formatting configuration. +// This method is deprecated and will be removed in the next version. +// +// Deprecated: Use [FooterConfigBuilder.Alignment] with [AlignmentConfigBuilder.WithGlobal] +// or [AlignmentConfigBuilder.WithPerColumn] to configure footer alignments. +// Alternatively, apply a complete [tw.CellAlignment] configuration using +// [WithFooterAlignmentConfig]. +// +// Example migration: +// +// // Old (deprecated) +// builder.Footer().Formatting().WithAlignment(tw.AlignRight) +// // New (recommended) +// builder.Footer().Alignment().WithGlobal(tw.AlignRight) +// // Or +// table.Options(WithFooterAlignmentConfig(tw.CellAlignment{Global: tw.AlignRight})) +// +// Parameters: +// - align: The [tw.Align] value to set for footer cells. Valid values are +// [tw.AlignLeft], [tw.AlignRight], [tw.AlignCenter], and [tw.AlignNone]. +// Invalid alignments are ignored. +// +// Returns: +// +// The [FooterFormattingBuilder] instance for method chaining. +func (ff *FooterFormattingBuilder) WithAlignment(align tw.Align) *FooterFormattingBuilder { + if align != tw.AlignLeft && align != tw.AlignRight && align != tw.AlignCenter && align != tw.AlignNone { + return ff + } + ff.config.Alignment = align + return ff +} + +// WithAlignment sets the text alignment for header cells within the formatting configuration. +// This method is deprecated and will be removed in the next version. +// +// Deprecated: Use [HeaderConfigBuilder.Alignment] with [AlignmentConfigBuilder.WithGlobal] +// or [AlignmentConfigBuilder.WithPerColumn] to configure header alignments. +// Alternatively, apply a complete [tw.CellAlignment] configuration using +// [WithHeaderAlignmentConfig]. +// +// Example migration: +// +// // Old (deprecated) +// builder.Header().Formatting().WithAlignment(tw.AlignCenter) +// // New (recommended) +// builder.Header().Alignment().WithGlobal(tw.AlignCenter) +// // Or +// table.Options(WithHeaderAlignmentConfig(tw.CellAlignment{Global: tw.AlignCenter})) +// +// Parameters: +// - align: The [tw.Align] value to set for header cells. Valid values are +// [tw.AlignLeft], [tw.AlignRight], [tw.AlignCenter], and [tw.AlignNone]. +// Invalid alignments are ignored. +// +// Returns: +// +// The [HeaderFormattingBuilder] instance for method chaining. +func (hf *HeaderFormattingBuilder) WithAlignment(align tw.Align) *HeaderFormattingBuilder { + if align != tw.AlignLeft && align != tw.AlignRight && align != tw.AlignCenter && align != tw.AlignNone { + return hf + } + hf.config.Alignment = align + return hf +} + +// WithAlignment sets the text alignment for row cells within the formatting configuration. +// This method is deprecated and will be removed in the next version. +// +// Deprecated: Use [RowConfigBuilder.Alignment] with [AlignmentConfigBuilder.WithGlobal] +// or [AlignmentConfigBuilder.WithPerColumn] to configure row alignments. +// Alternatively, apply a complete [tw.CellAlignment] configuration using +// [WithRowAlignmentConfig]. +// +// Example migration: +// +// // Old (deprecated) +// builder.Row().Formatting().WithAlignment(tw.AlignLeft) +// // New (recommended) +// builder.Row().Alignment().WithGlobal(tw.AlignLeft) +// // Or +// table.Options(WithRowAlignmentConfig(tw.CellAlignment{Global: tw.AlignLeft})) +// +// Parameters: +// - align: The [tw.Align] value to set for row cells. Valid values are +// [tw.AlignLeft], [tw.AlignRight], [tw.AlignCenter], and [tw.AlignNone]. +// Invalid alignments are ignored. +// +// Returns: +// +// The [RowFormattingBuilder] instance for method chaining. +func (rf *RowFormattingBuilder) WithAlignment(align tw.Align) *RowFormattingBuilder { + if align != tw.AlignLeft && align != tw.AlignRight && align != tw.AlignCenter && align != tw.AlignNone { + return rf + } + rf.config.Alignment = align + return rf +} + +// WithTableMax sets the maximum width of the entire table in characters. +// Negative values are ignored, and the change is logged if debugging is enabled. +// The width constrains the table's rendering, potentially causing text wrapping or truncation +// based on the configuration's wrapping settings (e.g., tw.WrapTruncate). +// If debug logging is enabled via WithDebug(true), the applied width is logged. +// +// Deprecated: Use WithMaxWidth instead, which provides the same functionality with a clearer name +// and consistent naming across the package. For example: +// +// tablewriter.NewTable(os.Stdout, tablewriter.WithMaxWidth(80)) +func WithTableMax(width int) Option { + return func(target *Table) { + if width < 0 { + return + } + target.config.MaxWidth = width + if target.logger != nil { + target.logger.Debugf("Option: WithTableMax applied to Table: %v", width) + } + } +} diff --git a/vendor/github.com/olekukonko/tablewriter/option.go b/vendor/github.com/olekukonko/tablewriter/option.go new file mode 100644 index 0000000000..1302462cd2 --- /dev/null +++ b/vendor/github.com/olekukonko/tablewriter/option.go @@ -0,0 +1,859 @@ +package tablewriter + +import ( + "github.com/olekukonko/ll" + "github.com/olekukonko/tablewriter/tw" + "reflect" +) + +// Option defines a function type for configuring a Table instance. +type Option func(target *Table) + +// WithAutoHide enables or disables automatic hiding of columns with empty data rows. +// Logs the change if debugging is enabled. +func WithAutoHide(state tw.State) Option { + return func(target *Table) { + target.config.Behavior.AutoHide = state + if target.logger != nil { + target.logger.Debugf("Option: WithAutoHide applied to Table: %v", state) + } + } +} + +// WithColumnMax sets a global maximum column width for the table in streaming mode. +// Negative values are ignored, and the change is logged if debugging is enabled. +func WithColumnMax(width int) Option { + return func(target *Table) { + if width < 0 { + return + } + target.config.Widths.Global = width + if target.logger != nil { + target.logger.Debugf("Option: WithColumnMax applied to Table: %v", width) + } + } +} + +// WithMaxWidth sets a global maximum table width for the table. +// Negative values are ignored, and the change is logged if debugging is enabled. +func WithMaxWidth(width int) Option { + return func(target *Table) { + if width < 0 { + return + } + target.config.MaxWidth = width + if target.logger != nil { + target.logger.Debugf("Option: WithTableMax applied to Table: %v", width) + } + } +} + +// WithWidths sets per-column widths for the table. +// Negative widths are removed, and the change is logged if debugging is enabled. +func WithWidths(width tw.CellWidth) Option { + return func(target *Table) { + target.config.Widths = width + if target.logger != nil { + target.logger.Debugf("Option: WithColumnWidths applied to Table: %v", width) + } + } +} + +// WithColumnWidths sets per-column widths for the table. +// Negative widths are removed, and the change is logged if debugging is enabled. +func WithColumnWidths(widths tw.Mapper[int, int]) Option { + return func(target *Table) { + for k, v := range widths { + if v < 0 { + delete(widths, k) + } + } + target.config.Widths.PerColumn = widths + if target.logger != nil { + target.logger.Debugf("Option: WithColumnWidths applied to Table: %v", widths) + } + } +} + +// WithConfig applies a custom configuration to the table by merging it with the default configuration. +func WithConfig(cfg Config) Option { + return func(target *Table) { + target.config = mergeConfig(defaultConfig(), cfg) + } +} + +// WithDebug enables or disables debug logging and adjusts the logger level accordingly. +// Logs the change if debugging is enabled. +func WithDebug(debug bool) Option { + return func(target *Table) { + target.config.Debug = debug + } +} + +// WithFooter sets the table footers by calling the Footer method. +func WithFooter(footers []string) Option { + return func(target *Table) { + target.Footer(footers) + } +} + +// WithFooterConfig applies a full footer configuration to the table. +// Logs the change if debugging is enabled. +func WithFooterConfig(config tw.CellConfig) Option { + return func(target *Table) { + target.config.Footer = config + if target.logger != nil { + target.logger.Debug("Option: WithFooterConfig applied to Table.") + } + } +} + +// WithFooterAlignmentConfig applies a footer alignment configuration to the table. +// Logs the change if debugging is enabled. +func WithFooterAlignmentConfig(alignment tw.CellAlignment) Option { + return func(target *Table) { + target.config.Footer.Alignment = alignment + if target.logger != nil { + target.logger.Debugf("Option: WithFooterAlignmentConfig applied to Table: %+v", alignment) + } + } +} + +// WithFooterMergeMode sets the merge mode for footer cells. +// Invalid merge modes are ignored, and the change is logged if debugging is enabled. +func WithFooterMergeMode(mergeMode int) Option { + return func(target *Table) { + if mergeMode < tw.MergeNone || mergeMode > tw.MergeHierarchical { + return + } + target.config.Footer.Formatting.MergeMode = mergeMode + if target.logger != nil { + target.logger.Debugf("Option: WithFooterMergeMode applied to Table: %v", mergeMode) + } + } +} + +// WithFooterAutoWrap sets the wrapping behavior for footer cells. +// Invalid wrap modes are ignored, and the change is logged if debugging is enabled. +func WithFooterAutoWrap(wrap int) Option { + return func(target *Table) { + if wrap < tw.WrapNone || wrap > tw.WrapBreak { + return + } + target.config.Footer.Formatting.AutoWrap = wrap + if target.logger != nil { + target.logger.Debugf("Option: WithFooterAutoWrap applied to Table: %v", wrap) + } + } +} + +// WithFooterFilter sets the filter configuration for footer cells. +// Logs the change if debugging is enabled. +func WithFooterFilter(filter tw.CellFilter) Option { + return func(target *Table) { + target.config.Footer.Filter = filter + if target.logger != nil { + target.logger.Debug("Option: WithFooterFilter applied to Table.") + } + } +} + +// WithFooterCallbacks sets the callback configuration for footer cells. +// Logs the change if debugging is enabled. +func WithFooterCallbacks(callbacks tw.CellCallbacks) Option { + return func(target *Table) { + target.config.Footer.Callbacks = callbacks + if target.logger != nil { + target.logger.Debug("Option: WithFooterCallbacks applied to Table.") + } + } +} + +// WithFooterPaddingPerColumn sets per-column padding for footer cells. +// Logs the change if debugging is enabled. +func WithFooterPaddingPerColumn(padding []tw.Padding) Option { + return func(target *Table) { + target.config.Footer.Padding.PerColumn = padding + if target.logger != nil { + target.logger.Debugf("Option: WithFooterPaddingPerColumn applied to Table: %+v", padding) + } + } +} + +// WithFooterMaxWidth sets the maximum content width for footer cells. +// Negative values are ignored, and the change is logged if debugging is enabled. +func WithFooterMaxWidth(maxWidth int) Option { + return func(target *Table) { + if maxWidth < 0 { + return + } + target.config.Footer.ColMaxWidths.Global = maxWidth + if target.logger != nil { + target.logger.Debugf("Option: WithFooterMaxWidth applied to Table: %v", maxWidth) + } + } +} + +// WithHeader sets the table headers by calling the Header method. +func WithHeader(headers []string) Option { + return func(target *Table) { + target.Header(headers) + } +} + +// WithHeaderAlignment sets the text alignment for header cells. +// Invalid alignments are ignored, and the change is logged if debugging is enabled. +func WithHeaderAlignment(align tw.Align) Option { + return func(target *Table) { + if align != tw.AlignLeft && align != tw.AlignRight && align != tw.AlignCenter && align != tw.AlignNone { + return + } + target.config.Header.Alignment.Global = align + if target.logger != nil { + target.logger.Debugf("Option: WithHeaderAlignment applied to Table: %v", align) + } + } +} + +// WithHeaderAutoWrap sets the wrapping behavior for header cells. +// Invalid wrap modes are ignored, and the change is logged if debugging is enabled. +func WithHeaderAutoWrap(wrap int) Option { + return func(target *Table) { + if wrap < tw.WrapNone || wrap > tw.WrapBreak { + return + } + target.config.Header.Formatting.AutoWrap = wrap + if target.logger != nil { + target.logger.Debugf("Option: WithHeaderAutoWrap applied to Table: %v", wrap) + } + } +} + +// WithHeaderMergeMode sets the merge mode for header cells. +// Invalid merge modes are ignored, and the change is logged if debugging is enabled. +func WithHeaderMergeMode(mergeMode int) Option { + return func(target *Table) { + if mergeMode < tw.MergeNone || mergeMode > tw.MergeHierarchical { + return + } + target.config.Header.Formatting.MergeMode = mergeMode + if target.logger != nil { + target.logger.Debugf("Option: WithHeaderMergeMode applied to Table: %v", mergeMode) + } + } +} + +// WithHeaderFilter sets the filter configuration for header cells. +// Logs the change if debugging is enabled. +func WithHeaderFilter(filter tw.CellFilter) Option { + return func(target *Table) { + target.config.Header.Filter = filter + if target.logger != nil { + target.logger.Debug("Option: WithHeaderFilter applied to Table.") + } + } +} + +// WithHeaderCallbacks sets the callback configuration for header cells. +// Logs the change if debugging is enabled. +func WithHeaderCallbacks(callbacks tw.CellCallbacks) Option { + return func(target *Table) { + target.config.Header.Callbacks = callbacks + if target.logger != nil { + target.logger.Debug("Option: WithHeaderCallbacks applied to Table.") + } + } +} + +// WithHeaderPaddingPerColumn sets per-column padding for header cells. +// Logs the change if debugging is enabled. +func WithHeaderPaddingPerColumn(padding []tw.Padding) Option { + return func(target *Table) { + target.config.Header.Padding.PerColumn = padding + if target.logger != nil { + target.logger.Debugf("Option: WithHeaderPaddingPerColumn applied to Table: %+v", padding) + } + } +} + +// WithHeaderMaxWidth sets the maximum content width for header cells. +// Negative values are ignored, and the change is logged if debugging is enabled. +func WithHeaderMaxWidth(maxWidth int) Option { + return func(target *Table) { + if maxWidth < 0 { + return + } + target.config.Header.ColMaxWidths.Global = maxWidth + if target.logger != nil { + target.logger.Debugf("Option: WithHeaderMaxWidth applied to Table: %v", maxWidth) + } + } +} + +// WithRowAlignment sets the text alignment for row cells. +// Invalid alignments are ignored, and the change is logged if debugging is enabled. +func WithRowAlignment(align tw.Align) Option { + return func(target *Table) { + if err := align.Validate(); err != nil { + return + } + target.config.Row.Alignment.Global = align + if target.logger != nil { + target.logger.Debugf("Option: WithRowAlignment applied to Table: %v", align) + } + } +} + +// WithRowAutoWrap sets the wrapping behavior for row cells. +// Invalid wrap modes are ignored, and the change is logged if debugging is enabled. +func WithRowAutoWrap(wrap int) Option { + return func(target *Table) { + if wrap < tw.WrapNone || wrap > tw.WrapBreak { + return + } + target.config.Row.Formatting.AutoWrap = wrap + if target.logger != nil { + target.logger.Debugf("Option: WithRowAutoWrap applied to Table: %v", wrap) + } + } +} + +// WithRowMergeMode sets the merge mode for row cells. +// Invalid merge modes are ignored, and the change is logged if debugging is enabled. +func WithRowMergeMode(mergeMode int) Option { + return func(target *Table) { + if mergeMode < tw.MergeNone || mergeMode > tw.MergeHierarchical { + return + } + target.config.Row.Formatting.MergeMode = mergeMode + if target.logger != nil { + target.logger.Debugf("Option: WithRowMergeMode applied to Table: %v", mergeMode) + } + } +} + +// WithRowFilter sets the filter configuration for row cells. +// Logs the change if debugging is enabled. +func WithRowFilter(filter tw.CellFilter) Option { + return func(target *Table) { + target.config.Row.Filter = filter + if target.logger != nil { + target.logger.Debug("Option: WithRowFilter applied to Table.") + } + } +} + +// WithRowCallbacks sets the callback configuration for row cells. +// Logs the change if debugging is enabled. +func WithRowCallbacks(callbacks tw.CellCallbacks) Option { + return func(target *Table) { + target.config.Row.Callbacks = callbacks + if target.logger != nil { + target.logger.Debug("Option: WithRowCallbacks applied to Table.") + } + } +} + +// WithRowPaddingPerColumn sets per-column padding for row cells. +// Logs the change if debugging is enabled. +func WithRowPaddingPerColumn(padding []tw.Padding) Option { + return func(target *Table) { + target.config.Row.Padding.PerColumn = padding + if target.logger != nil { + target.logger.Debugf("Option: WithRowPaddingPerColumn applied to Table: %+v", padding) + } + } +} + +// WithHeaderAlignmentConfig applies a header alignment configuration to the table. +// Logs the change if debugging is enabled. +func WithHeaderAlignmentConfig(alignment tw.CellAlignment) Option { + return func(target *Table) { + target.config.Header.Alignment = alignment + if target.logger != nil { + target.logger.Debugf("Option: WithHeaderAlignmentConfig applied to Table: %+v", alignment) + } + } +} + +// WithHeaderConfig applies a full header configuration to the table. +// Logs the change if debugging is enabled. +func WithHeaderConfig(config tw.CellConfig) Option { + return func(target *Table) { + target.config.Header = config + if target.logger != nil { + target.logger.Debug("Option: WithHeaderConfig applied to Table.") + } + } +} + +// WithLogger sets a custom logger for the table and updates the renderer if present. +// Logs the change if debugging is enabled. +func WithLogger(logger *ll.Logger) Option { + return func(target *Table) { + target.logger = logger + if target.logger != nil { + target.logger.Debug("Option: WithLogger applied to Table.") + if target.renderer != nil { + target.renderer.Logger(target.logger) + } + } + } +} + +// WithRenderer sets a custom renderer for the table and attaches the logger if present. +// Logs the change if debugging is enabled. +func WithRenderer(f tw.Renderer) Option { + return func(target *Table) { + target.renderer = f + if target.logger != nil { + target.logger.Debugf("Option: WithRenderer applied to Table: %T", f) + f.Logger(target.logger) + } + } +} + +// WithRowConfig applies a full row configuration to the table. +// Logs the change if debugging is enabled. +func WithRowConfig(config tw.CellConfig) Option { + return func(target *Table) { + target.config.Row = config + if target.logger != nil { + target.logger.Debug("Option: WithRowConfig applied to Table.") + } + } +} + +// WithRowAlignmentConfig applies a row alignment configuration to the table. +// Logs the change if debugging is enabled. +func WithRowAlignmentConfig(alignment tw.CellAlignment) Option { + return func(target *Table) { + target.config.Row.Alignment = alignment + if target.logger != nil { + target.logger.Debugf("Option: WithRowAlignmentConfig applied to Table: %+v", alignment) + } + } +} + +// WithRowMaxWidth sets the maximum content width for row cells. +// Negative values are ignored, and the change is logged if debugging is enabled. +func WithRowMaxWidth(maxWidth int) Option { + return func(target *Table) { + if maxWidth < 0 { + return + } + target.config.Row.ColMaxWidths.Global = maxWidth + if target.logger != nil { + target.logger.Debugf("Option: WithRowMaxWidth applied to Table: %v", maxWidth) + } + } +} + +// WithStreaming applies a streaming configuration to the table by merging it with the existing configuration. +// Logs the change if debugging is enabled. +func WithStreaming(c tw.StreamConfig) Option { + return func(target *Table) { + target.config.Stream = mergeStreamConfig(target.config.Stream, c) + if target.logger != nil { + target.logger.Debug("Option: WithStreaming applied to Table.") + } + } +} + +// WithStringer sets a custom stringer function for converting row data and clears the stringer cache. +// Logs the change if debugging is enabled. +func WithStringer(stringer interface{}) Option { + return func(t *Table) { + t.stringer = stringer + t.stringerCacheMu.Lock() + t.stringerCache = make(map[reflect.Type]reflect.Value) + t.stringerCacheMu.Unlock() + if t.logger != nil { + t.logger.Debug("Stringer updated, cache cleared") + } + } +} + +// WithStringerCache enables caching for the stringer function. +// Logs the change if debugging is enabled. +func WithStringerCache() Option { + return func(t *Table) { + t.stringerCacheEnabled = true + if t.logger != nil { + t.logger.Debug("Option: WithStringerCache enabled") + } + } +} + +// WithTrimSpace sets whether leading and trailing spaces are automatically trimmed. +// Logs the change if debugging is enabled. +func WithTrimSpace(state tw.State) Option { + return func(target *Table) { + target.config.Behavior.TrimSpace = state + if target.logger != nil { + target.logger.Debugf("Option: WithTrimSpace applied to Table: %v", state) + } + } +} + +// WithHeaderAutoFormat enables or disables automatic formatting for header cells. +// Logs the change if debugging is enabled. +func WithHeaderAutoFormat(state tw.State) Option { + return func(target *Table) { + target.config.Header.Formatting.AutoFormat = state + if target.logger != nil { + target.logger.Debugf("Option: WithHeaderAutoFormat applied to Table: %v", state) + } + } +} + +// WithFooterAutoFormat enables or disables automatic formatting for footer cells. +// Logs the change if debugging is enabled. +func WithFooterAutoFormat(state tw.State) Option { + return func(target *Table) { + target.config.Footer.Formatting.AutoFormat = state + if target.logger != nil { + target.logger.Debugf("Option: WithFooterAutoFormat applied to Table: %v", state) + } + } +} + +// WithRowAutoFormat enables or disables automatic formatting for row cells. +// Logs the change if debugging is enabled. +func WithRowAutoFormat(state tw.State) Option { + return func(target *Table) { + target.config.Row.Formatting.AutoFormat = state + if target.logger != nil { + target.logger.Debugf("Option: WithRowAutoFormat applied to Table: %v", state) + } + } +} + +// WithHeaderControl sets the control behavior for the table header. +// Logs the change if debugging is enabled. +func WithHeaderControl(control tw.Control) Option { + return func(target *Table) { + target.config.Behavior.Header = control + if target.logger != nil { + target.logger.Debugf("Option: WithHeaderControl applied to Table: %v", control) + } + } +} + +// WithFooterControl sets the control behavior for the table footer. +// Logs the change if debugging is enabled. +func WithFooterControl(control tw.Control) Option { + return func(target *Table) { + target.config.Behavior.Footer = control + if target.logger != nil { + target.logger.Debugf("Option: WithFooterControl applied to Table: %v", control) + } + } +} + +// WithAlignment sets the default column alignment for the header, rows, and footer. +// Logs the change if debugging is enabled. +func WithAlignment(alignment tw.Alignment) Option { + return func(target *Table) { + target.config.Header.Alignment.PerColumn = alignment + target.config.Row.Alignment.PerColumn = alignment + target.config.Footer.Alignment.PerColumn = alignment + if target.logger != nil { + target.logger.Debugf("Option: WithAlignment applied to Table: %+v", alignment) + } + } +} + +// WithBehavior applies a behavior configuration to the table. +// Logs the change if debugging is enabled. +func WithBehavior(behavior tw.Behavior) Option { + return func(target *Table) { + target.config.Behavior = behavior + if target.logger != nil { + target.logger.Debugf("Option: WithBehavior applied to Table: %+v", behavior) + } + } +} + +// WithPadding sets the global padding for the header, rows, and footer. +// Logs the change if debugging is enabled. +func WithPadding(padding tw.Padding) Option { + return func(target *Table) { + target.config.Header.Padding.Global = padding + target.config.Row.Padding.Global = padding + target.config.Footer.Padding.Global = padding + if target.logger != nil { + target.logger.Debugf("Option: WithPadding applied to Table: %+v", padding) + } + } +} + +// WithRendition allows updating the active renderer's rendition configuration +// by merging the provided rendition. +// If the renderer does not implement tw.Renditioning, a warning is logged. +// Logs the change if debugging is enabled. +func WithRendition(rendition tw.Rendition) Option { + return func(target *Table) { + if target.renderer == nil { + if target.logger != nil { + target.logger.Warn("Option: WithRendition: No renderer set on table.") + } + return + } + if ru, ok := target.renderer.(tw.Renditioning); ok { + ru.Rendition(rendition) + if target.logger != nil { + target.logger.Debugf("Option: WithRendition: Applied to renderer via Renditioning.SetRendition(): %+v", rendition) + } + } else { + if target.logger != nil { + target.logger.Warnf("Option: WithRendition: Current renderer type %T does not implement tw.Renditioning. Rendition may not be applied as expected.", target.renderer) + } + } + } +} + +// WithSymbols sets the symbols used for drawing table borders and separators. +// The symbols are applied to the table's renderer configuration, if a renderer is set. +// If no renderer is set (target.renderer is nil), this option has no effect. . +func WithSymbols(symbols tw.Symbols) Option { + return func(target *Table) { + if target.renderer != nil { + cfg := target.renderer.Config() + cfg.Symbols = symbols + + if ru, ok := target.renderer.(tw.Renditioning); ok { + ru.Rendition(cfg) + if target.logger != nil { + target.logger.Debugf("Option: WithRendition: Applied to renderer via Renditioning.SetRendition(): %+v", cfg) + } + } else { + if target.logger != nil { + target.logger.Warnf("Option: WithRendition: Current renderer type %T does not implement tw.Renditioning. Rendition may not be applied as expected.", target.renderer) + } + } + } + } +} + +// defaultConfig returns a default Config with sensible settings for headers, rows, footers, and behavior. +func defaultConfig() Config { + return Config{ + MaxWidth: 0, + Header: tw.CellConfig{ + Formatting: tw.CellFormatting{ + AutoWrap: tw.WrapTruncate, + AutoFormat: tw.On, + MergeMode: tw.MergeNone, + }, + Padding: tw.CellPadding{ + Global: tw.PaddingDefault, + }, + Alignment: tw.CellAlignment{ + Global: tw.AlignCenter, + PerColumn: []tw.Align{}, + }, + }, + Row: tw.CellConfig{ + Formatting: tw.CellFormatting{ + AutoWrap: tw.WrapNormal, + AutoFormat: tw.Off, + MergeMode: tw.MergeNone, + }, + Padding: tw.CellPadding{ + Global: tw.PaddingDefault, + }, + Alignment: tw.CellAlignment{ + Global: tw.AlignLeft, + PerColumn: []tw.Align{}, + }, + }, + Footer: tw.CellConfig{ + Formatting: tw.CellFormatting{ + AutoWrap: tw.WrapNormal, + AutoFormat: tw.Off, + MergeMode: tw.MergeNone, + }, + Padding: tw.CellPadding{ + Global: tw.PaddingDefault, + }, + Alignment: tw.CellAlignment{ + Global: tw.AlignRight, + PerColumn: []tw.Align{}, + }, + }, + Stream: tw.StreamConfig{}, + Debug: false, + Behavior: tw.Behavior{ + AutoHide: tw.Off, + TrimSpace: tw.On, + }, + } +} + +// mergeCellConfig merges a source CellConfig into a destination CellConfig, prioritizing non-default source values. +// It handles deep merging for complex fields like padding and callbacks. +func mergeCellConfig(dst, src tw.CellConfig) tw.CellConfig { + if src.Formatting.Alignment != tw.Empty { + dst.Formatting.Alignment = src.Formatting.Alignment + } + + if src.Formatting.AutoWrap != 0 { + dst.Formatting.AutoWrap = src.Formatting.AutoWrap + } + if src.ColMaxWidths.Global != 0 { + dst.ColMaxWidths.Global = src.ColMaxWidths.Global + } + if src.Formatting.MergeMode != 0 { + dst.Formatting.MergeMode = src.Formatting.MergeMode + } + + dst.Formatting.AutoFormat = src.Formatting.AutoFormat + + if src.Padding.Global.Paddable() { + dst.Padding.Global = src.Padding.Global + } + + if len(src.Padding.PerColumn) > 0 { + if dst.Padding.PerColumn == nil { + dst.Padding.PerColumn = make([]tw.Padding, len(src.Padding.PerColumn)) + } else if len(src.Padding.PerColumn) > len(dst.Padding.PerColumn) { + dst.Padding.PerColumn = append(dst.Padding.PerColumn, make([]tw.Padding, len(src.Padding.PerColumn)-len(dst.Padding.PerColumn))...) + } + for i, pad := range src.Padding.PerColumn { + if pad.Paddable() { + dst.Padding.PerColumn[i] = pad + } + } + } + if src.Callbacks.Global != nil { + dst.Callbacks.Global = src.Callbacks.Global + } + if len(src.Callbacks.PerColumn) > 0 { + if dst.Callbacks.PerColumn == nil { + dst.Callbacks.PerColumn = make([]func(), len(src.Callbacks.PerColumn)) + } else if len(src.Callbacks.PerColumn) > len(dst.Callbacks.PerColumn) { + dst.Callbacks.PerColumn = append(dst.Callbacks.PerColumn, make([]func(), len(src.Callbacks.PerColumn)-len(dst.Callbacks.PerColumn))...) + } + for i, cb := range src.Callbacks.PerColumn { + if cb != nil { + dst.Callbacks.PerColumn[i] = cb + } + } + } + if src.Filter.Global != nil { + dst.Filter.Global = src.Filter.Global + } + if len(src.Filter.PerColumn) > 0 { + if dst.Filter.PerColumn == nil { + dst.Filter.PerColumn = make([]func(string) string, len(src.Filter.PerColumn)) + } else if len(src.Filter.PerColumn) > len(dst.Filter.PerColumn) { + dst.Filter.PerColumn = append(dst.Filter.PerColumn, make([]func(string) string, len(src.Filter.PerColumn)-len(dst.Filter.PerColumn))...) + } + for i, filter := range src.Filter.PerColumn { + if filter != nil { + dst.Filter.PerColumn[i] = filter + } + } + } + + // Merge Alignment + if src.Alignment.Global != tw.Empty { + dst.Alignment.Global = src.Alignment.Global + } + + if len(src.Alignment.PerColumn) > 0 { + if dst.Alignment.PerColumn == nil { + dst.Alignment.PerColumn = make([]tw.Align, len(src.Alignment.PerColumn)) + } else if len(src.Alignment.PerColumn) > len(dst.Alignment.PerColumn) { + dst.Alignment.PerColumn = append(dst.Alignment.PerColumn, make([]tw.Align, len(src.Alignment.PerColumn)-len(dst.Alignment.PerColumn))...) + } + for i, align := range src.Alignment.PerColumn { + if align != tw.Skip { + dst.Alignment.PerColumn[i] = align + } + } + } + + if len(src.ColumnAligns) > 0 { + if dst.ColumnAligns == nil { + dst.ColumnAligns = make([]tw.Align, len(src.ColumnAligns)) + } else if len(src.ColumnAligns) > len(dst.ColumnAligns) { + dst.ColumnAligns = append(dst.ColumnAligns, make([]tw.Align, len(src.ColumnAligns)-len(dst.ColumnAligns))...) + } + for i, align := range src.ColumnAligns { + if align != tw.Skip { + dst.ColumnAligns[i] = align + } + } + } + + if len(src.ColMaxWidths.PerColumn) > 0 { + if dst.ColMaxWidths.PerColumn == nil { + dst.ColMaxWidths.PerColumn = make(map[int]int) + } + for k, v := range src.ColMaxWidths.PerColumn { + if v != 0 { + dst.ColMaxWidths.PerColumn[k] = v + } + } + } + return dst +} + +// mergeConfig merges a source Config into a destination Config, prioritizing non-default source values. +// It performs deep merging for complex types like Header, Row, Footer, and Stream. +func mergeConfig(dst, src Config) Config { + if src.MaxWidth != 0 { + dst.MaxWidth = src.MaxWidth + } + + dst.Debug = src.Debug || dst.Debug + dst.Behavior.AutoHide = src.Behavior.AutoHide + dst.Behavior.TrimSpace = src.Behavior.TrimSpace + dst.Behavior.Compact = src.Behavior.Compact + dst.Behavior.Header = src.Behavior.Header + dst.Behavior.Footer = src.Behavior.Footer + + if src.Widths.Global != 0 { + dst.Widths.Global = src.Widths.Global + } + if len(src.Widths.PerColumn) > 0 { + if dst.Widths.PerColumn == nil { + dst.Widths.PerColumn = make(map[int]int) + } + for k, v := range src.Widths.PerColumn { + if v != 0 { + dst.Widths.PerColumn[k] = v + } + } + } + + dst.Header = mergeCellConfig(dst.Header, src.Header) + dst.Row = mergeCellConfig(dst.Row, src.Row) + dst.Footer = mergeCellConfig(dst.Footer, src.Footer) + dst.Stream = mergeStreamConfig(dst.Stream, src.Stream) + + return dst +} + +// mergeStreamConfig merges a source StreamConfig into a destination StreamConfig, prioritizing non-default source values. +func mergeStreamConfig(dst, src tw.StreamConfig) tw.StreamConfig { + if src.Enable { + dst.Enable = true + } + return dst +} + +// padLine pads a line to the specified column count by appending empty strings as needed. +func padLine(line []string, numCols int) []string { + if len(line) >= numCols { + return line + } + padded := make([]string, numCols) + copy(padded, line) + for i := len(line); i < numCols; i++ { + padded[i] = tw.Empty + } + return padded +} diff --git a/vendor/github.com/olekukonko/tablewriter/renderer/markdown.go b/vendor/github.com/olekukonko/tablewriter/renderer/markdown.go index d9435f3500..525e633842 100644 --- a/vendor/github.com/olekukonko/tablewriter/renderer/markdown.go +++ b/vendor/github.com/olekukonko/tablewriter/renderer/markdown.go @@ -140,10 +140,10 @@ func (m *Markdown) resolveAlignment(ctx tw.Formatting) tw.Alignment { // build default alignment for i := 0; i < total; i++ { - m.alignment = append(m.alignment, tw.AlignLeft) + m.alignment = append(m.alignment, tw.AlignNone) // Default to AlignNone } - // add per colum alignment if it exits + // add per column alignment if it exists for i := 0; i < total; i++ { m.alignment[i] = ctx.Row.Current[i].Align } @@ -255,9 +255,10 @@ func (m *Markdown) formatSeparator(width int, align tw.Align) string { sb.WriteRune(':') sb.WriteString(strings.Repeat("-", targetWidth-2)) sb.WriteRune(':') + case tw.AlignNone: + sb.WriteString(strings.Repeat("-", targetWidth)) default: - sb.WriteRune(':') - sb.WriteString(strings.Repeat("-", targetWidth-1)) + sb.WriteString(strings.Repeat("-", targetWidth)) // Fallback } result := sb.String() @@ -321,12 +322,11 @@ func (m *Markdown) renderMarkdownLine(line []string, ctx tw.Formatting, isHeader defaultPadding := tw.Padding{Left: tw.Space, Right: tw.Space} if !ok { - cellCtx = tw.CellContext{ Data: tw.Empty, Align: align, Padding: defaultPadding, Width: ctx.Row.Widths.Get(colIndex), Merge: tw.MergeState{}, } - } else if cellCtx.Padding == (tw.Padding{}) { + } else if !cellCtx.Padding.Paddable() { cellCtx.Padding = defaultPadding } @@ -339,18 +339,6 @@ func (m *Markdown) renderMarkdownLine(line []string, ctx tw.Formatting, isHeader // Calculate width and span span := 1 - - if align == tw.AlignNone || align == tw.Empty { - if ctx.Row.Position == tw.Header && !isHeaderSep { - align = tw.AlignCenter - } else if ctx.Row.Position == tw.Footer { - align = tw.AlignRight - } else { - align = tw.AlignLeft - } - m.logger.Debugf("renderMarkdownLine: Col %d using default align '%s'", colIndex, align) - } - visualWidth := 0 isHMergeStart := ok && cellCtx.Merge.Horizontal.Present && cellCtx.Merge.Horizontal.Start if isHMergeStart { @@ -383,10 +371,11 @@ func (m *Markdown) renderMarkdownLine(line []string, ctx tw.Formatting, isHeader var formattedSegment string if isHeaderSep { // Use header's alignment from ctx.Row.Previous - headerAlign := tw.AlignCenter // Default for headers + headerAlign := align if headerCellCtx, headerOK := ctx.Row.Previous[colIndex]; headerOK { headerAlign = headerCellCtx.Align - if headerAlign == tw.AlignNone || headerAlign == tw.Empty { + // Preserve tw.AlignNone for separator + if headerAlign != tw.AlignNone && (headerAlign == tw.Empty || headerAlign == tw.Skip) { headerAlign = tw.AlignCenter } } @@ -403,6 +392,16 @@ func (m *Markdown) renderMarkdownLine(line []string, ctx tw.Formatting, isHeader rowAlign = headerCellCtx.Align } } + if rowAlign == tw.AlignNone || rowAlign == tw.Empty { + if ctx.Row.Position == tw.Header { + rowAlign = tw.AlignCenter + } else if ctx.Row.Position == tw.Footer { + rowAlign = tw.AlignRight + } else { + rowAlign = tw.AlignLeft + } + m.logger.Debugf("renderMarkdownLine: Col %d using default align '%s'", colIndex, rowAlign) + } formattedSegment = m.formatCell(content, visualWidth, rowAlign, cellCtx.Padding) } output.WriteString(formattedSegment) diff --git a/vendor/github.com/olekukonko/tablewriter/renderer/ocean.go b/vendor/github.com/olekukonko/tablewriter/renderer/ocean.go index 12f9d45c9f..58a6c9d6bb 100644 --- a/vendor/github.com/olekukonko/tablewriter/renderer/ocean.go +++ b/vendor/github.com/olekukonko/tablewriter/renderer/ocean.go @@ -340,7 +340,7 @@ func (o *Ocean) renderContentLine(ctx tw.Formatting, lineData []string) { if cellCtx.Align.Validate() == nil && cellCtx.Align != tw.AlignNone { align = cellCtx.Align } - if cellCtx.Padding != (tw.Padding{}) { + if cellCtx.Padding.Paddable() { padding = cellCtx.Padding } } else if colIdx < len(lineData) { diff --git a/vendor/github.com/olekukonko/tablewriter/stream.go b/vendor/github.com/olekukonko/tablewriter/stream.go index a990c6e09b..1b230d04de 100644 --- a/vendor/github.com/olekukonko/tablewriter/stream.go +++ b/vendor/github.com/olekukonko/tablewriter/stream.go @@ -113,10 +113,10 @@ func (t *Table) Start() error { // Calculate initial fixed widths if provided in StreamConfig.Widths // These widths will be used for all subsequent rendering in streaming mode. - if t.config.Stream.Widths.PerColumn != nil && t.config.Stream.Widths.PerColumn.Len() > 0 { + if t.config.Widths.PerColumn != nil && t.config.Widths.PerColumn.Len() > 0 { // Use per-column stream widths if set - t.logger.Debugf("Using per-column stream widths from StreamConfig: %v", t.config.Stream.Widths.PerColumn) - t.streamWidths = t.config.Stream.Widths.PerColumn.Clone() + t.logger.Debugf("Using per-column stream widths from StreamConfig: %v", t.config.Widths.PerColumn) + t.streamWidths = t.config.Widths.PerColumn.Clone() // Determine numCols from the highest index in PerColumn map maxColIdx := -1 t.streamWidths.Each(func(col int, width int) { @@ -139,14 +139,14 @@ func (t *Table) Start() error { t.logger.Debugf("PerColumn widths map is effectively empty or contains only negative values, streamNumCols = 0.") } - } else if t.config.Stream.Widths.Global > 0 { + } else if t.config.Widths.Global > 0 { // Global width is set, but we don't know the number of columns yet. // Defer applying global width until the first data (Header or first Row) arrives. // Store a placeholder or flag indicating global width should be used. // The simple way for now: Keep streamWidths empty, signal the global width preference. // The width calculation function called later will need to check StreamConfig.Widths.Global // if streamWidths is empty. - t.logger.Debugf("Global stream width %d set in StreamConfig. Will derive numCols from first data.", t.config.Stream.Widths.Global) + t.logger.Debugf("Global stream width %d set in StreamConfig. Will derive numCols from first data.", t.config.Widths.Global) t.streamWidths = tw.NewMapper[int, int]() // Initialize as empty, will be populated later // Note: No need to store Global width value here, it's available in t.config.Stream.Widths.Global @@ -451,26 +451,26 @@ func (t *Table) streamBuildCellContexts( // The paddingConfig should be the CellPadding config relevant to the sample data (Header/Row/Footer). // Returns the determined number of columns. // This function should only be called when t.streamWidths is currently empty. -func (t *Table) streamCalculateWidths(sampleDataLines []string, sectionConfigForSampleData tw.CellConfig) int { +func (t *Table) streamCalculateWidths(sampling []string, config tw.CellConfig) int { if t.streamWidths != nil && t.streamWidths.Len() > 0 { t.logger.Debug("streamCalculateWidths: Called when streaming widths are already set (%d columns). Reusing existing.", t.streamNumCols) return t.streamNumCols } - t.logger.Debug("streamCalculateWidths: Calculating streaming widths. Sample data cells: %d. Using section config: %+v", len(sampleDataLines), sectionConfigForSampleData.Formatting) + t.logger.Debug("streamCalculateWidths: Calculating streaming widths. Sample data cells: %d. Using section config: %+v", len(sampling), config.Formatting) determinedNumCols := 0 - if t.config.Stream.Widths.PerColumn != nil && t.config.Stream.Widths.PerColumn.Len() > 0 { + if t.config.Widths.PerColumn != nil && t.config.Widths.PerColumn.Len() > 0 { maxColIdx := -1 - t.config.Stream.Widths.PerColumn.Each(func(col int, width int) { + t.config.Widths.PerColumn.Each(func(col int, width int) { if col > maxColIdx { maxColIdx = col } }) determinedNumCols = maxColIdx + 1 t.logger.Debug("streamCalculateWidths: Determined numCols (%d) from StreamConfig.Widths.PerColumn", determinedNumCols) - } else if len(sampleDataLines) > 0 { - determinedNumCols = len(sampleDataLines) + } else if len(sampling) > 0 { + determinedNumCols = len(sampling) t.logger.Debug("streamCalculateWidths: Determined numCols (%d) from sample data length", determinedNumCols) } else { t.logger.Debug("streamCalculateWidths: Cannot determine numCols (no PerColumn config, no sample data)") @@ -482,14 +482,14 @@ func (t *Table) streamCalculateWidths(sampleDataLines []string, sectionConfigFor t.streamNumCols = determinedNumCols t.streamWidths = tw.NewMapper[int, int]() - // Use padding and autowrap from the provided sectionConfigForSampleData - paddingForWidthCalc := sectionConfigForSampleData.Padding - autoWrapForWidthCalc := sectionConfigForSampleData.Formatting.AutoWrap + // Use padding and autowrap from the provided config + paddingForWidthCalc := config.Padding + autoWrapForWidthCalc := config.Formatting.AutoWrap - if t.config.Stream.Widths.PerColumn != nil && t.config.Stream.Widths.PerColumn.Len() > 0 { + if t.config.Widths.PerColumn != nil && t.config.Widths.PerColumn.Len() > 0 { t.logger.Debug("streamCalculateWidths: Using widths from StreamConfig.Widths.PerColumn") for i := 0; i < t.streamNumCols; i++ { - width, ok := t.config.Stream.Widths.PerColumn.OK(i) + width, ok := t.config.Widths.PerColumn.OK(i) if !ok { width = 0 } @@ -501,12 +501,12 @@ func (t *Table) streamCalculateWidths(sampleDataLines []string, sectionConfigFor t.streamWidths.Set(i, width) } } else { - // No PerColumn config, derive from sampleDataLines intelligently + // No PerColumn config, derive from sampling intelligently t.logger.Debug("streamCalculateWidths: Intelligently deriving widths from sample data content and padding.") tempRequiredWidths := tw.NewMapper[int, int]() // Widths from updateWidths (content + padding) - if len(sampleDataLines) > 0 { + if len(sampling) > 0 { // updateWidths calculates: DisplayWidth(content) + padLeft + padRight - t.updateWidths(sampleDataLines, tempRequiredWidths, paddingForWidthCalc) + t.updateWidths(sampling, tempRequiredWidths, paddingForWidthCalc) } ellipsisWidthBuffer := 0 @@ -522,13 +522,13 @@ func (t *Table) streamCalculateWidths(sampleDataLines []string, sectionConfigFor // We need to deconstruct it to apply logic to content_width first. sampleContent := "" - if i < len(sampleDataLines) { - sampleContent = t.Trimmer(sampleDataLines[i]) + if i < len(sampling) { + sampleContent = t.Trimmer(sampling[i]) } sampleContentDisplayWidth := tw.DisplayWidth(sampleContent) colPad := paddingForWidthCalc.Global - if i < len(paddingForWidthCalc.PerColumn) && paddingForWidthCalc.PerColumn[i] != (tw.Padding{}) { + if i < len(paddingForWidthCalc.PerColumn) && paddingForWidthCalc.PerColumn[i].Paddable() { colPad = paddingForWidthCalc.PerColumn[i] } currentPadLWidth := tw.DisplayWidth(colPad.Left) @@ -584,8 +584,8 @@ func (t *Table) streamCalculateWidths(sampleDataLines []string, sectionConfigFor } // Apply Global Constraint (if t.config.Stream.Widths.Global > 0) - if t.config.Stream.Widths.Global > 0 && t.streamNumCols > 0 { - t.logger.Debug("streamCalculateWidths: Applying global stream width constraint %d", t.config.Stream.Widths.Global) + if t.config.Widths.Global > 0 && t.streamNumCols > 0 { + t.logger.Debug("streamCalculateWidths: Applying global stream width constraint %d", t.config.Widths.Global) currentTotalColumnWidthsSum := 0 t.streamWidths.Each(func(_ int, w int) { currentTotalColumnWidthsSum += w @@ -606,11 +606,11 @@ func (t *Table) streamCalculateWidths(sampleDataLines []string, sectionConfigFor totalWidthIncludingSeparators += (t.streamNumCols - 1) * separatorWidth } - if t.config.Stream.Widths.Global < totalWidthIncludingSeparators && totalWidthIncludingSeparators > 0 { // Added check for total > 0 - t.logger.Debug("streamCalculateWidths: Total calculated width (%d incl separators) exceeds global stream width (%d). Shrinking.", totalWidthIncludingSeparators, t.config.Stream.Widths.Global) + if t.config.Widths.Global < totalWidthIncludingSeparators && totalWidthIncludingSeparators > 0 { // Added check for total > 0 + t.logger.Debug("streamCalculateWidths: Total calculated width (%d incl separators) exceeds global stream width (%d). Shrinking.", totalWidthIncludingSeparators, t.config.Widths.Global) // Target sum for column widths only (global limit - total separator width) - targetSumForColumnWidths := t.config.Stream.Widths.Global + targetSumForColumnWidths := t.config.Widths.Global if t.streamNumCols > 1 { targetSumForColumnWidths -= (t.streamNumCols - 1) * separatorWidth } @@ -671,7 +671,7 @@ func (t *Table) streamCalculateWidths(sampleDataLines []string, sectionConfigFor } t.logger.Debug("streamCalculateWidths: Widths after scaling and distribution: %v", t.streamWidths) } else { - t.logger.Debug("streamCalculateWidths: Total calculated width (%d) fits global stream width (%d). No scaling needed.", totalWidthIncludingSeparators, t.config.Stream.Widths.Global) + t.logger.Debug("streamCalculateWidths: Total calculated width (%d) fits global stream width (%d). No scaling needed.", totalWidthIncludingSeparators, t.config.Widths.Global) } } diff --git a/vendor/github.com/olekukonko/tablewriter/tablewriter.go b/vendor/github.com/olekukonko/tablewriter/tablewriter.go index 16961d89a1..4eca55d7a5 100644 --- a/vendor/github.com/olekukonko/tablewriter/tablewriter.go +++ b/vendor/github.com/olekukonko/tablewriter/tablewriter.go @@ -133,6 +133,16 @@ func NewTable(w io.Writer, opts ...Option) *Table { return t } +// NewWriter creates a new table with default settings for backward compatibility. +// It logs the creation if debugging is enabled. +func NewWriter(w io.Writer) *Table { + t := NewTable(w) + if t.logger != nil { + t.logger.Debug("NewWriter created buffered Table") + } + return t +} + // Caption sets the table caption (legacy method). // Defaults to BottomCenter alignment, wrapping to table width. // Use SetCaptionOptions for more control. @@ -397,6 +407,11 @@ func (t *Table) Options(opts ...Option) *Table { t.logger.Suspend() } + // help resolve from deprecation + //if t.config.Stream.Enable { + // t.config.Widths = t.config.Stream.Widths + //} + // send logger to renderer // this will overwrite the default logger t.renderer.Logger(t.logger) @@ -508,19 +523,40 @@ func (t *Table) appendSingle(row interface{}) error { // Parameter config provides alignment settings for the section. // Returns a map of column indices to alignment settings. func (t *Table) buildAligns(config tw.CellConfig) map[int]tw.Align { - t.logger.Debugf("buildAligns INPUT: config.Formatting.Align=%s, config.ColumnAligns=%v", config.Formatting.Alignment, config.ColumnAligns) + // Start with global alignment, preferring deprecated Formatting.Alignment + effectiveGlobalAlign := config.Formatting.Alignment + if effectiveGlobalAlign == tw.Empty || effectiveGlobalAlign == tw.Skip { + effectiveGlobalAlign = config.Alignment.Global + if config.Formatting.Alignment != tw.Empty && config.Formatting.Alignment != tw.Skip { + t.logger.Warnf("Using deprecated CellFormatting.Alignment (%s). Migrate to CellConfig.Alignment.Global.", config.Formatting.Alignment) + } + } + + // Use per-column alignments, preferring deprecated ColumnAligns + effectivePerColumn := config.ColumnAligns + if len(effectivePerColumn) == 0 && len(config.Alignment.PerColumn) > 0 { + effectivePerColumn = make([]tw.Align, len(config.Alignment.PerColumn)) + copy(effectivePerColumn, config.Alignment.PerColumn) + if len(config.ColumnAligns) > 0 { + t.logger.Warnf("Using deprecated CellConfig.ColumnAligns (%v). Migrate to CellConfig.Alignment.PerColumn.", config.ColumnAligns) + } + } + + // Log input for debugging + t.logger.Debugf("buildAligns INPUT: deprecated Formatting.Alignment=%s, deprecated ColumnAligns=%v, config.Alignment.Global=%s, config.Alignment.PerColumn=%v", + config.Formatting.Alignment, config.ColumnAligns, config.Alignment.Global, config.Alignment.PerColumn) + numColsToUse := t.getNumColsToUse() colAlignsResult := make(map[int]tw.Align) for i := 0; i < numColsToUse; i++ { - currentAlign := config.Formatting.Alignment - if i < len(config.ColumnAligns) { - colSpecificAlign := config.ColumnAligns[i] - if colSpecificAlign != tw.Empty && colSpecificAlign != tw.Skip { - currentAlign = colSpecificAlign - } + currentAlign := effectiveGlobalAlign + if i < len(effectivePerColumn) && effectivePerColumn[i] != tw.Empty && effectivePerColumn[i] != tw.Skip { + currentAlign = effectivePerColumn[i] } + // Skip validation here; rely on rendering to handle invalid alignments colAlignsResult[i] = currentAlign } + t.logger.Debugf("Aligns built: %v (length %d)", colAlignsResult, len(colAlignsResult)) return colAlignsResult } @@ -532,7 +568,7 @@ func (t *Table) buildPadding(padding tw.CellPadding) map[int]tw.Padding { numColsToUse := t.getNumColsToUse() colPadding := make(map[int]tw.Padding) for i := 0; i < numColsToUse; i++ { - if i < len(padding.PerColumn) && padding.PerColumn[i] != (tw.Padding{}) { + if i < len(padding.PerColumn) && padding.PerColumn[i].Paddable() { colPadding[i] = padding.PerColumn[i] } else { colPadding[i] = padding.Global @@ -699,8 +735,12 @@ func (t *Table) maxColumns() int { return m } +// printTopBottomCaption prints the table's caption at the specified top or bottom position. +// It wraps the caption text to fit the table width or a user-defined width, aligns it according +// to the specified alignment, and writes it to the provided writer. If the caption text is empty +// or the spot is invalid, it logs the issue and returns without printing. The function handles +// wrapping errors by falling back to splitting on newlines or using the original text. func (t *Table) printTopBottomCaption(w io.Writer, actualTableWidth int) { - // Log the state of t.caption t.logger.Debugf("[printCaption Entry] Text=%q, Spot=%v (type %T), Align=%q, UserWidth=%d, ActualTableWidth=%d", t.caption.Text, t.caption.Spot, t.caption.Spot, t.caption.Align, t.caption.Width, actualTableWidth) @@ -711,12 +751,11 @@ func (t *Table) printTopBottomCaption(w io.Writer, actualTableWidth int) { return } - // Determine captionWrapWidth var captionWrapWidth int if t.caption.Width > 0 { captionWrapWidth = t.caption.Width t.logger.Debugf("[printCaption] Using user-defined caption.Width %d for wrapping.", captionWrapWidth) - } else if actualTableWidth <= 4 { // Empty or minimal table + } else if actualTableWidth <= 4 { captionWrapWidth = tw.DisplayWidth(t.caption.Text) t.logger.Debugf("[printCaption] Empty table, no user caption.Width: Using natural caption width %d.", captionWrapWidth) } else { @@ -724,14 +763,12 @@ func (t *Table) printTopBottomCaption(w io.Writer, actualTableWidth int) { t.logger.Debugf("[printCaption] Non-empty table, no user caption.Width: Using actualTableWidth %d for wrapping.", actualTableWidth) } - // Ensure captionWrapWidth is positive if captionWrapWidth <= 0 { - captionWrapWidth = 10 // Minimum sensible width + captionWrapWidth = 10 t.logger.Warnf("[printCaption] captionWrapWidth was %d (<=0). Setting to minimum %d.", captionWrapWidth, 10) } t.logger.Debugf("[printCaption] Final captionWrapWidth to be used by twwarp: %d", captionWrapWidth) - // Wrap the caption text wrappedCaptionLines, count := twwarp.WrapString(t.caption.Text, captionWrapWidth) if count == 0 { t.logger.Errorf("[printCaption] Error from twwarp.WrapString (width %d): %v. Text: %q", captionWrapWidth, count, t.caption.Text) @@ -754,7 +791,6 @@ func (t *Table) printTopBottomCaption(w io.Writer, actualTableWidth int) { t.logger.Debugf("[printCaption] Wrapped caption into %d lines: %v", len(wrappedCaptionLines), wrappedCaptionLines) } - // Determine padding target width paddingTargetWidth := actualTableWidth if t.caption.Width > 0 { paddingTargetWidth = t.caption.Width @@ -763,7 +799,6 @@ func (t *Table) printTopBottomCaption(w io.Writer, actualTableWidth int) { } t.logger.Debugf("[printCaption] Final paddingTargetWidth for tw.Pad: %d", paddingTargetWidth) - // Print each wrapped line for i, line := range wrappedCaptionLines { align := t.caption.Align if align == "" || align == tw.AlignDefault || align == tw.AlignNone { @@ -791,22 +826,11 @@ func (t *Table) printTopBottomCaption(w io.Writer, actualTableWidth int) { // Parameters include cells to process and config for formatting rules. // Returns a slice of string slices representing processed lines. func (t *Table) prepareContent(cells []string, config tw.CellConfig) [][]string { - // force max width - isStreaming := t.config.Stream.Enable && t.hasPrinted t.logger.Debugf("prepareContent: Processing cells=%v (streaming: %v)", cells, isStreaming) initialInputCellCount := len(cells) result := make([][]string, 0) - // ll.Dbg(t.config.MaxWidth) - // force max width - if t.config.MaxWidth > 0 { - // it has headers - if len(cells) > 0 { - config.ColMaxWidths.Global = int(math.Floor(float64(t.config.MaxWidth) / float64(len(cells)))) - } - } - effectiveNumCols := initialInputCellCount if isStreaming { if t.streamNumCols > 0 { @@ -830,6 +854,16 @@ func (t *Table) prepareContent(cells []string, config tw.CellConfig) [][]string } } + if t.config.MaxWidth > 0 && !t.config.Widths.Constrained() { + if effectiveNumCols > 0 { + derivedSectionGlobalMaxWidth := int(math.Floor(float64(t.config.MaxWidth) / float64(effectiveNumCols))) + config.ColMaxWidths.Global = derivedSectionGlobalMaxWidth + t.logger.Debugf("prepareContent: Table MaxWidth %d active and t.config.Widths not constrained. "+ + "Derived section ColMaxWidths.Global: %d for %d columns. This will be used by calculateContentMaxWidth if no higher priority constraints exist.", + t.config.MaxWidth, config.ColMaxWidths.Global, effectiveNumCols) + } + } + for i := 0; i < effectiveNumCols; i++ { cellContent := "" if i < len(cells) { @@ -841,7 +875,7 @@ func (t *Table) prepareContent(cells []string, config tw.CellConfig) [][]string cellContent = t.Trimmer(cellContent) colPad := config.Padding.Global - if i < len(config.Padding.PerColumn) && config.Padding.PerColumn[i] != (tw.Padding{}) { + if i < len(config.Padding.PerColumn) && config.Padding.PerColumn[i].Paddable() { colPad = config.Padding.PerColumn[i] } @@ -881,50 +915,45 @@ func (t *Table) prepareContent(cells []string, config tw.CellConfig) [][]string case tw.WrapBreak: wrapped := make([]string, 0) currentLine := line + breakCharWidth := tw.DisplayWidth(tw.CharBreak) for tw.DisplayWidth(currentLine) > effectiveContentMaxWidth { - breakPoint := tw.BreakPoint(currentLine, effectiveContentMaxWidth) - if breakPoint <= 0 { - t.logger.Warnf("prepareContent: WrapBreak - BreakPoint <= 0 for line '%s' at width %d. Attempting manual break.", currentLine, effectiveContentMaxWidth) - runes := []rune(currentLine) + targetWidth := effectiveContentMaxWidth - breakCharWidth + if targetWidth < 0 { + targetWidth = 0 + } + breakPoint := tw.BreakPoint(currentLine, targetWidth) + runes := []rune(currentLine) + if breakPoint <= 0 || breakPoint > len(runes) { + t.logger.Warnf("prepareContent: WrapBreak - Invalid BreakPoint %d for line '%s' at width %d. Attempting manual break.", breakPoint, currentLine, targetWidth) actualBreakRuneCount := 0 tempWidth := 0 - for charIdx, r := range currentLine { + for charIdx, r := range runes { runeStr := string(r) rw := tw.DisplayWidth(runeStr) - if tempWidth+rw > effectiveContentMaxWidth && charIdx > 0 { + if tempWidth+rw > targetWidth && charIdx > 0 { break } tempWidth += rw actualBreakRuneCount = charIdx + 1 - if tempWidth >= effectiveContentMaxWidth && charIdx == 0 { + if tempWidth >= targetWidth && charIdx == 0 { break } } if actualBreakRuneCount == 0 && len(runes) > 0 { actualBreakRuneCount = 1 } - if actualBreakRuneCount > 0 && actualBreakRuneCount <= len(runes) { wrapped = append(wrapped, string(runes[:actualBreakRuneCount])+tw.CharBreak) currentLine = string(runes[actualBreakRuneCount:]) } else { - if tw.DisplayWidth(currentLine) > 0 { - wrapped = append(wrapped, currentLine) - currentLine = "" - } - break - } - } else { - runes := []rune(currentLine) - if breakPoint <= len(runes) { - wrapped = append(wrapped, string(runes[:breakPoint])+tw.CharBreak) - currentLine = string(runes[breakPoint:]) - } else { - t.logger.Warnf("prepareContent: WrapBreak - BreakPoint (%d) out of bounds for line runes (%d). Adding full line.", breakPoint, len(runes)) + t.logger.Warnf("prepareContent: WrapBreak - Cannot break line '%s'. Adding as is.", currentLine) wrapped = append(wrapped, currentLine) currentLine = "" break } + } else { + wrapped = append(wrapped, string(runes[:breakPoint])+tw.CharBreak) + currentLine = string(runes[breakPoint:]) } } if tw.DisplayWidth(currentLine) > 0 { @@ -959,7 +988,8 @@ func (t *Table) prepareContent(cells []string, config tw.CellConfig) [][]string if i < len(result[j]) { result[j][i] = cellLineContent } else { - t.logger.Warnf("prepareContent: Column index %d out of bounds (%d) during result matrix population.", i, len(result[j])) + t.logger.Warnf("prepareContent: Column index %d out of bounds (%d) during result matrix population. EffectiveNumCols: %d. This indicates a logic error.", + i, len(result[j]), effectiveNumCols) } } } @@ -1304,6 +1334,7 @@ func (t *Table) prepareWithMerges(content [][]string, config tw.CellConfig, posi // No parameters are required. // Returns an error if rendering fails in any section. func (t *Table) render() error { + t.ensureInitialized() if t.config.Stream.Enable { @@ -1339,9 +1370,8 @@ func (t *Table) render() error { t.logger.Debugf("No caption detected. Rendering table core directly to writer.") } - //Render Table Core --- - t.writer = targetWriter // Set writer only when necessary - + //Render Table Core + t.writer = targetWriter ctx, mctx, err := t.prepareContexts() if err != nil { t.writer = originalWriter @@ -1387,7 +1417,6 @@ func (t *Table) render() error { if renderError { return firstRenderErr // Return error from core rendering if any } - //End Render Table Core --- //Caption Handling & Final Output --- if isTopOrBottomCaption { @@ -1877,17 +1906,54 @@ func (t *Table) renderHeader(ctx *renderContext, mctx *mergeContext) error { if cfg.Settings.Lines.ShowHeaderLine.Enabled() && (len(ctx.rowLines) > 0 || len(ctx.footerLines) > 0) { ctx.logger.Debug("Rendering header separator line") - hctx.rowIdx = 0 - hctx.lineIdx = len(ctx.headerLines) - 1 - hctx.line = padLine(ctx.headerLines[hctx.lineIdx], ctx.numCols) - hctx.location = tw.LocationMiddle resp := t.buildCellContexts(ctx, mctx, hctx, colAligns, colPadding) + + var nextSectionCells map[int]tw.CellContext + var nextSectionWidths tw.Mapper[int, int] + + if len(ctx.rowLines) > 0 { + nextSectionWidths = ctx.widths[tw.Row] + rowColAligns := t.buildAligns(t.config.Row) + rowColPadding := t.buildPadding(t.config.Row.Padding) + firstRowHctx := &helperContext{ + position: tw.Row, + rowIdx: 0, + lineIdx: 0, + } + if len(ctx.rowLines[0]) > 0 { + firstRowHctx.line = padLine(ctx.rowLines[0][0], ctx.numCols) + } else { + firstRowHctx.line = make([]string, ctx.numCols) + } + firstRowResp := t.buildCellContexts(ctx, mctx, firstRowHctx, rowColAligns, rowColPadding) + nextSectionCells = firstRowResp.cells + } else if len(ctx.footerLines) > 0 { + nextSectionWidths = ctx.widths[tw.Row] + footerColAligns := t.buildAligns(t.config.Footer) + footerColPadding := t.buildPadding(t.config.Footer.Padding) + firstFooterHctx := &helperContext{ + position: tw.Footer, + rowIdx: 0, + lineIdx: 0, + } + if len(ctx.footerLines) > 0 { + firstFooterHctx.line = padLine(ctx.footerLines[0], ctx.numCols) + } else { + firstFooterHctx.line = make([]string, ctx.numCols) + } + firstFooterResp := t.buildCellContexts(ctx, mctx, firstFooterHctx, footerColAligns, footerColPadding) + nextSectionCells = firstFooterResp.cells + } else { + nextSectionWidths = ctx.widths[tw.Header] + nextSectionCells = nil + } + f.Line(tw.Formatting{ Row: tw.RowContext{ - Widths: ctx.widths[tw.Header], + Widths: nextSectionWidths, Current: resp.cells, Previous: resp.prevCells, - Next: resp.nextCells, + Next: nextSectionCells, Position: tw.Header, Location: tw.LocationMiddle, }, diff --git a/vendor/github.com/olekukonko/tablewriter/tw/cell.go b/vendor/github.com/olekukonko/tablewriter/tw/cell.go index de2fa0057a..0547f01a4c 100644 --- a/vendor/github.com/olekukonko/tablewriter/tw/cell.go +++ b/vendor/github.com/olekukonko/tablewriter/tw/cell.go @@ -2,13 +2,17 @@ package tw // CellFormatting holds formatting options for table cells. type CellFormatting struct { - Alignment Align // Text alignment within the cell (e.g., Left, Right, Center) - AutoWrap int // Wrapping behavior (e.g., WrapTruncate, WrapNormal) - MergeMode int // Bitmask for merge behavior (e.g., MergeHorizontal, MergeVertical) + AutoWrap int // Wrapping behavior (e.g., WrapTruncate, WrapNormal) + MergeMode int // Bitmask for merge behavior (e.g., MergeHorizontal, MergeVertical) // Changed form bool to State // See https://github.com/olekukonko/tablewriter/issues/261 AutoFormat State // Enables automatic formatting (e.g., title case for headers) + + // Deprecated: kept for compatibility + // will be removed soon + Alignment Align // Text alignment within the cell (e.g., Left, Right, Center) + } // CellPadding defines padding settings for table cells. @@ -30,17 +34,31 @@ type CellCallbacks struct { PerColumn []func() // Column-specific callbacks } +// CellAlignment defines alignment settings for table cells. +type CellAlignment struct { + Global Align // Default alignment applied to all cells + PerColumn []Align // Column-specific alignment overrides +} + // CellConfig combines formatting, padding, and callback settings for a table section. type CellConfig struct { Formatting CellFormatting // Cell formatting options Padding CellPadding // Padding configuration Callbacks CellCallbacks // Callback functions (unused) Filter CellFilter // Function to filter cell content (renamed from Filter Filter) - ColumnAligns []Align // Per-column alignment overrides + Alignment CellAlignment // Alignment configuration for cells ColMaxWidths CellWidth // Per-column maximum width overrides + + // Deprecated: use Alignment.PerColumn instead. Will be removed in a future version. + // will be removed soon + ColumnAligns []Align // Per-column alignment overrides } type CellWidth struct { Global int PerColumn Mapper[int, int] } + +func (c CellWidth) Constrained() bool { + return c.Global > 0 || c.PerColumn.Len() > 0 +} diff --git a/vendor/github.com/olekukonko/tablewriter/tw/fn.go b/vendor/github.com/olekukonko/tablewriter/tw/fn.go index 8183e3ea1d..9610b2734b 100644 --- a/vendor/github.com/olekukonko/tablewriter/tw/fn.go +++ b/vendor/github.com/olekukonko/tablewriter/tw/fn.go @@ -245,7 +245,7 @@ func IsNumeric(s string) bool { return err == nil } -// SplitCamelCase splits a camelCase or PascalCase string into separate words. +// SplitCamelCase splits a camelCase or PascalCase or snake_case string into separate words. // It detects transitions between uppercase, lowercase, digits, and other characters. func SplitCamelCase(src string) (entries []string) { // Validate UTF-8 input; return as single entry if invalid @@ -284,10 +284,11 @@ func SplitCamelCase(src string) (entries []string) { runes[i] = runes[i][:len(runes[i])-1] } } - // Convert rune groups to strings, excluding empty or whitespace-only groups + // Convert rune groups to strings, excluding empty, underscore or whitespace-only groups for _, s := range runes { - if len(s) > 0 && strings.TrimSpace(string(s)) != "" { - entries = append(entries, string(s)) + str := string(s) + if len(s) > 0 && strings.TrimSpace(str) != "" && str != "_" { + entries = append(entries, str) } } return diff --git a/vendor/github.com/olekukonko/tablewriter/tw/preset.go b/vendor/github.com/olekukonko/tablewriter/tw/preset.go new file mode 100644 index 0000000000..acadc25cbe --- /dev/null +++ b/vendor/github.com/olekukonko/tablewriter/tw/preset.go @@ -0,0 +1,18 @@ +package tw + +// BorderNone defines a border configuration with all sides disabled. +var ( + // PaddingNone represents explicitly empty padding (no spacing on any side) + // Equivalent to Padding{Overwrite: true} + PaddingNone = Padding{Left: Empty, Right: Empty, Top: Empty, Bottom: Empty, Overwrite: true} + BorderNone = Border{Left: Off, Right: Off, Top: Off, Bottom: Off} + LinesNone = Lines{ShowTop: Off, ShowBottom: Off, ShowHeaderLine: Off, ShowFooterLine: Off} + SeparatorsNone = Separators{ShowHeader: Off, ShowFooter: Off, BetweenRows: Off, BetweenColumns: Off} +) + +var ( + + // PaddingDefault represents standard single-space padding on left/right + // Equivalent to Padding{Left: " ", Right: " ", Overwrite: true} + PaddingDefault = Padding{Left: " ", Right: " ", Overwrite: true} +) diff --git a/vendor/github.com/olekukonko/tablewriter/tw/renderer.go b/vendor/github.com/olekukonko/tablewriter/tw/renderer.go index 7078a991cc..6caf05e4eb 100644 --- a/vendor/github.com/olekukonko/tablewriter/tw/renderer.go +++ b/vendor/github.com/olekukonko/tablewriter/tw/renderer.go @@ -108,21 +108,18 @@ type Settings struct { // Border defines the visibility states of table borders. type Border struct { - Left State // Left border visibility - Right State // Right border visibility - Top State // Top border visibility - Bottom State // Bottom border visibility + Left State // Left border visibility + Right State // Right border visibility + Top State // Top border visibility + Bottom State // Bottom border visibility + Overwrite bool } -// BorderNone defines a border configuration with all sides disabled. -var ( - PaddingNone = Padding{Left: Empty, Right: Empty, Top: Empty, Bottom: Empty} - BorderNone = Border{Left: Off, Right: Off, Top: Off, Bottom: Off} - LinesNone = Lines{ShowTop: Off, ShowBottom: Off, ShowHeaderLine: Off, ShowFooterLine: Off} - SeparatorsNone = Separators{ShowHeader: Off, ShowFooter: Off, BetweenRows: Off, BetweenColumns: Off} -) - type StreamConfig struct { Enable bool - Widths CellWidth // Cell/column widths + + // Deprecated: Use top-level Config.Widths for streaming width control. + // This field will be removed in a future version. It will be respected if + // Config.Widths is not set and this field is. + Widths CellWidth } diff --git a/vendor/github.com/olekukonko/tablewriter/tw/tw.go b/vendor/github.com/olekukonko/tablewriter/tw/tw.go index afdfbe3a90..020de742c2 100644 --- a/vendor/github.com/olekukonko/tablewriter/tw/tw.go +++ b/vendor/github.com/olekukonko/tablewriter/tw/tw.go @@ -15,6 +15,8 @@ const ( Skip = "" Space = " " NewLine = "\n" + Column = ":" + Dash = "-" ) // Feature State Constants diff --git a/vendor/github.com/olekukonko/tablewriter/tw/types.go b/vendor/github.com/olekukonko/tablewriter/tw/types.go index 1551c67422..29a1862c1d 100644 --- a/vendor/github.com/olekukonko/tablewriter/tw/types.go +++ b/vendor/github.com/olekukonko/tablewriter/tw/types.go @@ -132,22 +132,68 @@ func (c Caption) WithWidth(width int) Caption { return c } -// Padding defines custom padding characters for a cell +type Control struct { + Hide State +} + +// Compact configures compact width optimization for merged cells. +type Compact struct { + Merge State // Merge enables compact width calculation during cell merging, optimizing space allocation. +} + +// Behavior defines settings that control table rendering behaviors, such as column visibility and content formatting. +type Behavior struct { + AutoHide State // AutoHide determines whether empty columns are hidden. Ignored in streaming mode. + TrimSpace State // TrimSpace enables trimming of leading and trailing spaces from cell content. + + Header Control // Header specifies control settings for the table header. + Footer Control // Footer specifies control settings for the table footer. + + // Compact enables optimized width calculation for merged cells, such as in horizontal merges, + // by systematically determining the most efficient width instead of scaling by the number of columns. + Compact Compact +} + +// Padding defines the spacing characters around cell content in all four directions. +// A zero-value Padding struct will use the table's default padding unless Overwrite is true. type Padding struct { Left string Right string Top string Bottom string + + // Overwrite forces tablewriter to use this padding configuration exactly as specified, + // even when empty. When false (default), empty Padding fields will inherit defaults. + // + // For explicit no-padding, use the PaddingNone constant instead of setting Overwrite. + Overwrite bool } -type Control struct { - Hide State +// Common padding configurations for convenience + +// Equals reports whether two Padding configurations are identical in all fields. +// This includes comparing the Overwrite flag as part of the equality check. +func (p Padding) Equals(padding Padding) bool { + return p.Left == padding.Left && + p.Right == padding.Right && + p.Top == padding.Top && + p.Bottom == padding.Bottom && + p.Overwrite == padding.Overwrite } -// Behavior defines table behavior settings that control features like auto-hiding columns and trimming spaces. -type Behavior struct { - AutoHide State // Controls whether empty columns are automatically hidden (ignored in streaming mode) - TrimSpace State // Controls whether leading/trailing spaces are trimmed from cell content - Header Control - Footer Control +// Empty reports whether all padding strings are empty (all fields == ""). +// Note that an Empty padding may still take effect if Overwrite is true. +func (p Padding) Empty() bool { + return p.Left == "" && p.Right == "" && p.Top == "" && p.Bottom == "" +} + +// Paddable reports whether this Padding configuration should override existing padding. +// Returns true if either: +// - Any padding string is non-empty (!p.Empty()) +// - Overwrite flag is true (even with all strings empty) +// +// This is used internally during configuration merging to determine whether to +// apply the padding settings. +func (p Padding) Paddable() bool { + return !p.Empty() || p.Overwrite } diff --git a/vendor/github.com/olekukonko/tablewriter/zoo.go b/vendor/github.com/olekukonko/tablewriter/zoo.go index a7f63d585d..04a0c15a19 100644 --- a/vendor/github.com/olekukonko/tablewriter/zoo.go +++ b/vendor/github.com/olekukonko/tablewriter/zoo.go @@ -6,6 +6,7 @@ import ( "github.com/olekukonko/errors" "github.com/olekukonko/tablewriter/tw" "io" + "math" "reflect" "strconv" "strings" @@ -576,26 +577,23 @@ func (t *Table) buildPaddingLineContents(padChar string, widths tw.Mapper[int, i // Parameter ctx holds rendering state with width maps. // Returns an error if width calculation fails. func (t *Table) calculateAndNormalizeWidths(ctx *renderContext) error { - ctx.logger.Debugf("calculateAndNormalizeWidths: Computing and normalizing widths for %d columns", ctx.numCols) + ctx.logger.Debugf("calculateAndNormalizeWidths: Computing and normalizing widths for %d columns. Compact: %v", + ctx.numCols, t.config.Behavior.Compact.Merge.Enabled()) // Initialize width maps - t.headerWidths = tw.NewMapper[int, int]() - t.rowWidths = tw.NewMapper[int, int]() - t.footerWidths = tw.NewMapper[int, int]() + //t.headerWidths = tw.NewMapper[int, int]() + //t.rowWidths = tw.NewMapper[int, int]() + //t.footerWidths = tw.NewMapper[int, int]() - // Compute header widths + // Compute content-based widths for each section for _, lines := range ctx.headerLines { t.updateWidths(lines, t.headerWidths, t.config.Header.Padding) } - ctx.logger.Debugf("Initial Header widths: %v", t.headerWidths) - - // Cache row widths to avoid re-iteration rowWidthCache := make([]tw.Mapper[int, int], len(ctx.rowLines)) for i, row := range ctx.rowLines { rowWidthCache[i] = tw.NewMapper[int, int]() for _, line := range row { t.updateWidths(line, rowWidthCache[i], t.config.Row.Padding) - // Aggregate into t.rowWidths for col, width := range rowWidthCache[i] { currentMax, _ := t.rowWidths.OK(col) if width > currentMax { @@ -604,41 +602,348 @@ func (t *Table) calculateAndNormalizeWidths(ctx *renderContext) error { } } } - ctx.logger.Debugf("Initial Row widths: %v", t.rowWidths) - - // Compute footer widths for _, lines := range ctx.footerLines { t.updateWidths(lines, t.footerWidths, t.config.Footer.Padding) } - ctx.logger.Debugf("Initial Footer widths: %v", t.footerWidths) - - // Initialize width maps for normalization - ctx.widths[tw.Header] = tw.NewMapper[int, int]() - ctx.widths[tw.Row] = tw.NewMapper[int, int]() - ctx.widths[tw.Footer] = tw.NewMapper[int, int]() + ctx.logger.Debugf("Content-based widths: header=%v, row=%v, footer=%v", t.headerWidths, t.rowWidths, t.footerWidths) + + // Analyze header merges for optimization + var headerMergeSpans map[int]int + if t.config.Header.Formatting.MergeMode&tw.MergeHorizontal != 0 && len(ctx.headerLines) > 0 { + headerMergeSpans = make(map[int]int) + visitedCols := make(map[int]bool) + firstHeaderLine := ctx.headerLines[0] + if len(firstHeaderLine) > 0 { + for i := 0; i < len(firstHeaderLine); { + if visitedCols[i] { + i++ + continue + } + var currentLogicalCellContentBuilder strings.Builder + for _, hLine := range ctx.headerLines { + if i < len(hLine) { + currentLogicalCellContentBuilder.WriteString(hLine[i]) + } + } + currentHeaderCellContent := t.Trimmer(currentLogicalCellContentBuilder.String()) + span := 1 + for j := i + 1; j < len(firstHeaderLine); j++ { + var nextLogicalCellContentBuilder strings.Builder + for _, hLine := range ctx.headerLines { + if j < len(hLine) { + nextLogicalCellContentBuilder.WriteString(hLine[j]) + } + } + nextHeaderCellContent := t.Trimmer(nextLogicalCellContentBuilder.String()) + if currentHeaderCellContent == nextHeaderCellContent && currentHeaderCellContent != "" && currentHeaderCellContent != "-" { + span++ + } else { + break + } + } + if span > 1 { + headerMergeSpans[i] = span + for k := 0; k < span; k++ { + visitedCols[i+k] = true + } + } + i += span + } + } + if len(headerMergeSpans) > 0 { + ctx.logger.Debugf("Header merge spans: %v", headerMergeSpans) + } + } - // Normalize widths by taking the maximum across sections + // Determine natural column widths + naturalColumnWidths := tw.NewMapper[int, int]() for i := 0; i < ctx.numCols; i++ { - maxWidth := 0 - for _, w := range []tw.Mapper[int, int]{t.headerWidths, t.rowWidths, t.footerWidths} { - if wd := w.Get(i); wd > maxWidth { - maxWidth = wd + width := 0 + if colWidth, ok := t.config.Widths.PerColumn.OK(i); ok && colWidth >= 0 { + width = colWidth + ctx.logger.Debugf("Col %d width from Config.Widths.PerColumn: %d", i, width) + } else { + maxRowFooterWidth := tw.Max(t.rowWidths.Get(i), t.footerWidths.Get(i)) + headerCellOriginalWidth := t.headerWidths.Get(i) + if t.config.Behavior.Compact.Merge.Enabled() && + t.config.Header.Formatting.MergeMode&tw.MergeHorizontal != 0 && + headerMergeSpans != nil { + isColInHeaderMerge := false + for startCol, span := range headerMergeSpans { + if i >= startCol && i < startCol+span { + isColInHeaderMerge = true + break + } + } + if isColInHeaderMerge { + width = maxRowFooterWidth + if width == 0 && headerCellOriginalWidth > 0 { + width = headerCellOriginalWidth + } + ctx.logger.Debugf("Col %d (in merge) width: %d (row/footer: %d, header: %d)", i, width, maxRowFooterWidth, headerCellOriginalWidth) + } else { + width = tw.Max(headerCellOriginalWidth, maxRowFooterWidth) + ctx.logger.Debugf("Col %d (not in merge) width: %d", i, width) + } + } else { + width = tw.Max(tw.Max(headerCellOriginalWidth, t.rowWidths.Get(i)), t.footerWidths.Get(i)) + ctx.logger.Debugf("Col %d width (no merge): %d", i, width) + } + if width == 0 && (headerCellOriginalWidth > 0 || t.rowWidths.Get(i) > 0 || t.footerWidths.Get(i) > 0) { + width = tw.Max(tw.Max(headerCellOriginalWidth, t.rowWidths.Get(i)), t.footerWidths.Get(i)) + } + if width == 0 { + width = 1 + } + } + naturalColumnWidths.Set(i, width) + } + ctx.logger.Debugf("Natural column widths: %v", naturalColumnWidths) + + // Expand columns for merged header content if needed + workingWidths := naturalColumnWidths.Clone() + if t.config.Header.Formatting.MergeMode&tw.MergeHorizontal != 0 && headerMergeSpans != nil { + if span, isOneBigMerge := headerMergeSpans[0]; isOneBigMerge && span == ctx.numCols && ctx.numCols > 0 { + var firstHeaderCellLogicalContentBuilder strings.Builder + for _, hLine := range ctx.headerLines { + if 0 < len(hLine) { + firstHeaderCellLogicalContentBuilder.WriteString(hLine[0]) + } + } + mergedContentString := t.Trimmer(firstHeaderCellLogicalContentBuilder.String()) + headerCellPadding := t.config.Header.Padding.Global + if 0 < len(t.config.Header.Padding.PerColumn) && t.config.Header.Padding.PerColumn[0].Paddable() { + headerCellPadding = t.config.Header.Padding.PerColumn[0] + } + actualMergedHeaderContentPhysicalWidth := tw.DisplayWidth(mergedContentString) + + tw.DisplayWidth(headerCellPadding.Left) + + tw.DisplayWidth(headerCellPadding.Right) + currentSumOfColumnWidths := 0 + workingWidths.Each(func(_ int, w int) { currentSumOfColumnWidths += w }) + numSeparatorsInFullSpan := 0 + if ctx.numCols > 1 { + if t.renderer != nil && t.renderer.Config().Settings.Separators.BetweenColumns.Enabled() { + numSeparatorsInFullSpan = (ctx.numCols - 1) * tw.DisplayWidth(t.renderer.Config().Symbols.Column()) + } + } + totalCurrentSpanPhysicalWidth := currentSumOfColumnWidths + numSeparatorsInFullSpan + if actualMergedHeaderContentPhysicalWidth > totalCurrentSpanPhysicalWidth { + ctx.logger.Debugf("Merged header content '%s' (width %d) exceeds total width %d. Expanding.", + mergedContentString, actualMergedHeaderContentPhysicalWidth, totalCurrentSpanPhysicalWidth) + shortfall := actualMergedHeaderContentPhysicalWidth - totalCurrentSpanPhysicalWidth + numNonZeroCols := 0 + workingWidths.Each(func(_ int, w int) { + if w > 0 { + numNonZeroCols++ + } + }) + if numNonZeroCols == 0 && ctx.numCols > 0 { + numNonZeroCols = ctx.numCols + } + if numNonZeroCols > 0 && shortfall > 0 { + extraPerColumn := int(math.Ceil(float64(shortfall) / float64(numNonZeroCols))) + finalSumAfterExpansion := 0 + workingWidths.Each(func(colIdx int, currentW int) { + if currentW > 0 || (numNonZeroCols == ctx.numCols && ctx.numCols > 0) { + newWidth := currentW + extraPerColumn + workingWidths.Set(colIdx, newWidth) + finalSumAfterExpansion += newWidth + ctx.logger.Debugf("Col %d expanded by %d to %d", colIdx, extraPerColumn, newWidth) + } else { + finalSumAfterExpansion += currentW + } + }) + overDistributed := (finalSumAfterExpansion + numSeparatorsInFullSpan) - actualMergedHeaderContentPhysicalWidth + if overDistributed > 0 { + ctx.logger.Debugf("Correcting over-distribution of %d", overDistributed) + // Sort columns for deterministic reduction + sortedCols := workingWidths.SortedKeys() + for i := 0; i < overDistributed; i++ { + // Reduce from highest-indexed column + for j := len(sortedCols) - 1; j >= 0; j-- { + col := sortedCols[j] + if workingWidths.Get(col) > 1 && naturalColumnWidths.Get(col) < workingWidths.Get(col) { + workingWidths.Set(col, workingWidths.Get(col)-1) + ctx.logger.Debugf("Reduced col %d by 1 to %d", col, workingWidths.Get(col)) + break + } + } + } + } + } } } - ctx.widths[tw.Header].Set(i, maxWidth) - ctx.widths[tw.Row].Set(i, maxWidth) - ctx.widths[tw.Footer].Set(i, maxWidth) } - ctx.logger.Debugf("Normalized widths: header=%v, row=%v, footer=%v", ctx.widths[tw.Header], ctx.widths[tw.Row], ctx.widths[tw.Footer]) + ctx.logger.Debugf("Widths after merged header expansion: %v", workingWidths) + + // Apply global width constraint + finalWidths := workingWidths.Clone() + if t.config.Widths.Global > 0 { + ctx.logger.Debugf("Applying global width constraint: %d", t.config.Widths.Global) + currentSumOfFinalColWidths := 0 + finalWidths.Each(func(_ int, w int) { currentSumOfFinalColWidths += w }) + numSeparators := 0 + if ctx.numCols > 1 && t.renderer != nil && t.renderer.Config().Settings.Separators.BetweenColumns.Enabled() { + numSeparators = (ctx.numCols - 1) * tw.DisplayWidth(t.renderer.Config().Symbols.Column()) + } + totalCurrentTablePhysicalWidth := currentSumOfFinalColWidths + numSeparators + if totalCurrentTablePhysicalWidth > t.config.Widths.Global { + ctx.logger.Debugf("Table width %d exceeds global limit %d. Shrinking.", totalCurrentTablePhysicalWidth, t.config.Widths.Global) + targetTotalColumnContentWidth := t.config.Widths.Global - numSeparators + if targetTotalColumnContentWidth < 0 { + targetTotalColumnContentWidth = 0 + } + if ctx.numCols > 0 && targetTotalColumnContentWidth < ctx.numCols { + targetTotalColumnContentWidth = ctx.numCols + } + hardMinimums := tw.NewMapper[int, int]() + sumOfHardMinimums := 0 + isHeaderContentHardToWrap := !(t.config.Header.Formatting.AutoWrap == tw.WrapNormal || t.config.Header.Formatting.AutoWrap == tw.WrapBreak) + for i := 0; i < ctx.numCols; i++ { + minW := 1 + if isHeaderContentHardToWrap && len(ctx.headerLines) > 0 { + headerColNaturalWidthWithPadding := t.headerWidths.Get(i) + if headerColNaturalWidthWithPadding > minW { + minW = headerColNaturalWidthWithPadding + } + } + hardMinimums.Set(i, minW) + sumOfHardMinimums += minW + } + ctx.logger.Debugf("Hard minimums: %v (sum: %d)", hardMinimums, sumOfHardMinimums) + if targetTotalColumnContentWidth < sumOfHardMinimums && sumOfHardMinimums > 0 { + ctx.logger.Warnf("Target width %d below minimums %d. Scaling.", targetTotalColumnContentWidth, sumOfHardMinimums) + scaleFactorMin := float64(targetTotalColumnContentWidth) / float64(sumOfHardMinimums) + if scaleFactorMin < 0 { + scaleFactorMin = 0 + } + tempSum := 0 + scaledHardMinimums := tw.NewMapper[int, int]() + hardMinimums.Each(func(colIdx int, currentMinW int) { + scaledMinW := int(math.Round(float64(currentMinW) * scaleFactorMin)) + if scaledMinW < 1 && targetTotalColumnContentWidth > 0 { + scaledMinW = 1 + } else if scaledMinW < 0 { + scaledMinW = 0 + } + scaledHardMinimums.Set(colIdx, scaledMinW) + tempSum += scaledMinW + }) + errorDiffMin := targetTotalColumnContentWidth - tempSum + if errorDiffMin != 0 && scaledHardMinimums.Len() > 0 { + sortedKeys := scaledHardMinimums.SortedKeys() + for i := 0; i < int(math.Abs(float64(errorDiffMin))); i++ { + keyToAdjust := sortedKeys[i%len(sortedKeys)] + val := scaledHardMinimums.Get(keyToAdjust) + adj := 1 + if errorDiffMin < 0 { + adj = -1 + } + if val+adj >= 1 || (val+adj == 0 && targetTotalColumnContentWidth == 0) { + scaledHardMinimums.Set(keyToAdjust, val+adj) + } else if adj > 0 { + scaledHardMinimums.Set(keyToAdjust, val+adj) + } + } + } + finalWidths = scaledHardMinimums.Clone() + ctx.logger.Debugf("Scaled minimums: %v", finalWidths) + } else { + finalWidths = hardMinimums.Clone() + widthAllocatedByMinimums := sumOfHardMinimums + remainingWidthToDistribute := targetTotalColumnContentWidth - widthAllocatedByMinimums + ctx.logger.Debugf("Target: %d, minimums: %d, remaining: %d", targetTotalColumnContentWidth, widthAllocatedByMinimums, remainingWidthToDistribute) + if remainingWidthToDistribute > 0 { + sumOfFlexiblePotentialBase := 0 + flexibleColsOriginalWidths := tw.NewMapper[int, int]() + for i := 0; i < ctx.numCols; i++ { + naturalW := workingWidths.Get(i) + minW := hardMinimums.Get(i) + if naturalW > minW { + sumOfFlexiblePotentialBase += (naturalW - minW) + flexibleColsOriginalWidths.Set(i, naturalW) + } + } + ctx.logger.Debugf("Flexible potential: %d, flexible widths: %v", sumOfFlexiblePotentialBase, flexibleColsOriginalWidths) + if sumOfFlexiblePotentialBase > 0 { + distributedExtraSum := 0 + sortedFlexKeys := flexibleColsOriginalWidths.SortedKeys() + for _, colIdx := range sortedFlexKeys { + naturalWOfCol := flexibleColsOriginalWidths.Get(colIdx) + hardMinOfCol := hardMinimums.Get(colIdx) + flexiblePartOfCol := naturalWOfCol - hardMinOfCol + proportion := 0.0 + if sumOfFlexiblePotentialBase > 0 { + proportion = float64(flexiblePartOfCol) / float64(sumOfFlexiblePotentialBase) + } else if len(sortedFlexKeys) > 0 { + proportion = 1.0 / float64(len(sortedFlexKeys)) + } + extraForThisCol := int(math.Round(float64(remainingWidthToDistribute) * proportion)) + currentAssignedW := finalWidths.Get(colIdx) + finalWidths.Set(colIdx, currentAssignedW+extraForThisCol) + distributedExtraSum += extraForThisCol + } + errorInDist := remainingWidthToDistribute - distributedExtraSum + ctx.logger.Debugf("Distributed %d, error: %d", distributedExtraSum, errorInDist) + if errorInDist != 0 && len(sortedFlexKeys) > 0 { + for i := 0; i < int(math.Abs(float64(errorInDist))); i++ { + colToAdjust := sortedFlexKeys[i%len(sortedFlexKeys)] + w := finalWidths.Get(colToAdjust) + adj := 1 + if errorInDist < 0 { + adj = -1 + } + if !(adj < 0 && w+adj < hardMinimums.Get(colToAdjust)) { + finalWidths.Set(colToAdjust, w+adj) + } else if adj > 0 { + finalWidths.Set(colToAdjust, w+adj) + } + } + } + } else { + if ctx.numCols > 0 { + extraPerCol := remainingWidthToDistribute / ctx.numCols + rem := remainingWidthToDistribute % ctx.numCols + for i := 0; i < ctx.numCols; i++ { + currentW := finalWidths.Get(i) + add := extraPerCol + if i < rem { + add++ + } + finalWidths.Set(i, currentW+add) + } + } + } + } + } + finalSumCheck := 0 + finalWidths.Each(func(idx int, w int) { + if w < 1 && targetTotalColumnContentWidth > 0 { + finalWidths.Set(idx, 1) + } else if w < 0 { + finalWidths.Set(idx, 0) + } + finalSumCheck += finalWidths.Get(idx) + }) + ctx.logger.Debugf("Final widths after scaling: %v (sum: %d, target: %d)", finalWidths, finalSumCheck, targetTotalColumnContentWidth) + } + } + + // Assign final widths to context + ctx.widths[tw.Header] = finalWidths.Clone() + ctx.widths[tw.Row] = finalWidths.Clone() + ctx.widths[tw.Footer] = finalWidths.Clone() + ctx.logger.Debugf("Final normalized widths: header=%v, row=%v, footer=%v", ctx.widths[tw.Header], ctx.widths[tw.Row], ctx.widths[tw.Footer]) return nil } // calculateContentMaxWidth computes the maximum content width for a column, accounting for padding and mode-specific constraints. // Returns the effective content width (after subtracting padding) for the given column index. func (t *Table) calculateContentMaxWidth(colIdx int, config tw.CellConfig, padLeftWidth, padRightWidth int, isStreaming bool) int { - var effectiveContentMaxWidth int + if isStreaming { + // Existing streaming logic remains unchanged totalColumnWidthFromStream := t.streamWidths.Get(colIdx) if totalColumnWidthFromStream < 0 { totalColumnWidthFromStream = 0 @@ -652,28 +957,57 @@ func (t *Table) calculateContentMaxWidth(colIdx int, config tw.CellConfig, padLe if totalColumnWidthFromStream == 0 { effectiveContentMaxWidth = 0 } - t.logger.Debugf("calculateContentMaxWidth: Streaming col %d, TotalColWd=%d, PadL=%d, PadR=%d -> ContentMaxWd=%d", - colIdx, totalColumnWidthFromStream, padLeftWidth, padRightWidth, effectiveContentMaxWidth) + t.logger.Debugf("calculateContentMaxWidth: Streaming col %d, TotalColWd=%d, PadL=%d, PadR=%d -> ContentMaxWd=%d", colIdx, totalColumnWidthFromStream, padLeftWidth, padRightWidth, effectiveContentMaxWidth) } else { - hasConstraint := false + // New priority-based width constraint checking constraintTotalCellWidth := 0 - if config.ColMaxWidths.PerColumn != nil { + hasConstraint := false + + // 1. Check new Widths.PerColumn (highest priority) + if t.config.Widths.Constrained() { + + if colWidth, ok := t.config.Widths.PerColumn.OK(colIdx); ok && colWidth > 0 { + constraintTotalCellWidth = colWidth + hasConstraint = true + t.logger.Debugf("calculateContentMaxWidth: Using Widths.PerColumn[%d] = %d", + colIdx, constraintTotalCellWidth) + } + + // 2. Check new Widths.Global + if !hasConstraint && t.config.Widths.Global > 0 { + constraintTotalCellWidth = t.config.Widths.Global + hasConstraint = true + t.logger.Debugf("calculateContentMaxWidth: Using Widths.Global = %d", constraintTotalCellWidth) + } + } + + // 3. Fall back to legacy ColMaxWidths.PerColumn (backward compatibility) + if !hasConstraint && config.ColMaxWidths.PerColumn != nil { if colMax, ok := config.ColMaxWidths.PerColumn.OK(colIdx); ok && colMax > 0 { constraintTotalCellWidth = colMax hasConstraint = true - t.logger.Debugf("calculateContentMaxWidth: Batch col %d using config.ColMaxWidths.PerColumn (as total cell width constraint): %d", colIdx, constraintTotalCellWidth) + t.logger.Debugf("calculateContentMaxWidth: Using legacy ColMaxWidths.PerColumn[%d] = %d", + colIdx, constraintTotalCellWidth) } } + + // 4. Fall back to legacy ColMaxWidths.Global if !hasConstraint && config.ColMaxWidths.Global > 0 { constraintTotalCellWidth = config.ColMaxWidths.Global hasConstraint = true - t.logger.Debugf("calculateContentMaxWidth: Batch col %d using config.Formatting.MaxWidth (as total cell width constraint): %d", colIdx, constraintTotalCellWidth) + t.logger.Debugf("calculateContentMaxWidth: Using legacy ColMaxWidths.Global = %d", + constraintTotalCellWidth) } + + // 5. Fall back to table MaxWidth if auto-wrapping if !hasConstraint && t.config.MaxWidth > 0 && config.Formatting.AutoWrap != tw.WrapNone { constraintTotalCellWidth = t.config.MaxWidth hasConstraint = true - t.logger.Debugf("calculateContentMaxWidth: Batch col %d using t.config.MaxWidth (as total cell width constraint, due to AutoWrap != WrapNone): %d", colIdx, constraintTotalCellWidth) + t.logger.Debugf("calculateContentMaxWidth: Using table MaxWidth = %d (AutoWrap enabled)", + constraintTotalCellWidth) } + + // Calculate effective width based on found constraint if hasConstraint { effectiveContentMaxWidth = constraintTotalCellWidth - padLeftWidth - padRightWidth if effectiveContentMaxWidth < 1 && constraintTotalCellWidth > (padLeftWidth+padRightWidth) { @@ -681,13 +1015,14 @@ func (t *Table) calculateContentMaxWidth(colIdx int, config tw.CellConfig, padLe } else if effectiveContentMaxWidth < 0 { effectiveContentMaxWidth = 0 } - t.logger.Debugf("calculateContentMaxWidth: Batch col %d, ConstraintTotalCellWidth=%d, PadL=%d, PadR=%d -> EffectiveContentMaxWidth=%d", - colIdx, constraintTotalCellWidth, padLeftWidth, padRightWidth, effectiveContentMaxWidth) + t.logger.Debugf("calculateContentMaxWidth: ConstraintTotalCellWidth=%d, PadL=%d, PadR=%d -> EffectiveContentMaxWidth=%d", + constraintTotalCellWidth, padLeftWidth, padRightWidth, effectiveContentMaxWidth) } else { effectiveContentMaxWidth = 0 - t.logger.Debugf("calculateContentMaxWidth: Batch col %d, No applicable MaxWidth constraint. EffectiveContentMaxWidth set to 0 (unlimited for this stage).", colIdx) + t.logger.Debugf("calculateContentMaxWidth: No width constraints found for column %d", colIdx) } } + return effectiveContentMaxWidth } @@ -698,6 +1033,8 @@ func (t *Table) convertToStringer(input interface{}) ([]string, error) { return nil, errors.New("internal error: convertToStringer called with nil t.stringer") } + t.logger.Debugf("convertToString attempt %v using %v", input, t.stringer) + inputType := reflect.TypeOf(input) stringerFuncVal := reflect.ValueOf(t.stringer) stringerFuncType := stringerFuncVal.Type() @@ -1310,7 +1647,8 @@ func (t *Table) updateWidths(row []string, widths tw.Mapper[int, int], padding t t.logger.Debugf("Updating widths for row: %v", row) for i, cell := range row { colPad := padding.Global - if i < len(padding.PerColumn) && padding.PerColumn[i] != (tw.Padding{}) { + + if i < len(padding.PerColumn) && padding.PerColumn[i].Paddable() { colPad = padding.PerColumn[i] t.logger.Debugf(" Col %d: Using per-column padding: L:'%s' R:'%s'", i, colPad.Left, colPad.Right) } else { diff --git a/vendor/github.com/pion/interceptor/.golangci.yml b/vendor/github.com/pion/interceptor/.golangci.yml index a3235bec28..120faf29be 100644 --- a/vendor/github.com/pion/interceptor/.golangci.yml +++ b/vendor/github.com/pion/interceptor/.golangci.yml @@ -19,23 +19,42 @@ linters-settings: recommendations: - errors forbidigo: + analyze-types: true forbid: - ^fmt.Print(f|ln)?$ - ^log.(Panic|Fatal|Print)(f|ln)?$ - ^os.Exit$ - ^panic$ - ^print(ln)?$ + - p: ^testing.T.(Error|Errorf|Fatal|Fatalf|Fail|FailNow)$ + pkg: ^testing$ + msg: "use testify/assert instead" + varnamelen: + max-distance: 12 + min-name-length: 2 + ignore-type-assert-ok: true + ignore-map-index-ok: true + ignore-chan-recv-ok: true + ignore-decls: + - i int + - n int + - w io.Writer + - r io.Reader + - b []byte linters: enable: - asciicheck # Simple linter to check that your code does not contain non-ASCII identifiers - bidichk # Checks for dangerous unicode character sequences - bodyclose # checks whether HTTP response body is closed successfully + - containedctx # containedctx is a linter that detects struct contained context.Context field - contextcheck # check the function whether use a non-inherited context + - cyclop # checks function and package cyclomatic complexity - decorder # check declaration order and count of types, constants, variables and functions - dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f()) - dupl # Tool for code clone detection - durationcheck # check for two durations multiplied together + - err113 # Golang linter to check the errors handling expressions - errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases - errchkjson # Checks types passed to the json encoding functions. Reports unsupported types and optionally reports occations, where the check for the returned error can be omitted. - errname # Checks that sentinel errors are prefixed with the `Err` and error types are suffixed with the `Error`. @@ -46,18 +65,17 @@ linters: - forcetypeassert # finds forced type assertions - gci # Gci control golang package import order and make it always deterministic. - gochecknoglobals # Checks that no globals are present in Go code - - gochecknoinits # Checks that no init functions are present in Go code - gocognit # Computes and checks the cognitive complexity of functions - goconst # Finds repeated strings that could be replaced by a constant - gocritic # The most opinionated Go source code linter + - gocyclo # Computes and checks the cyclomatic complexity of functions + - godot # Check if comments end in a period - godox # Tool for detection of FIXME, TODO and other comment keywords - - err113 # Golang linter to check the errors handling expressions - gofmt # Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification - gofumpt # Gofumpt checks whether code was gofumpt-ed. - goheader # Checks is file header matches to pattern - goimports # Goimports does everything that gofmt does. Additionally it checks unused imports - gomoddirectives # Manage the use of 'replace', 'retract', and 'excludes' directives in go.mod. - - gomodguard # Allow and block list linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations. - goprintffuncname # Checks that printf-like functions are named with `f` at the end - gosec # Inspects source code for security problems - gosimple # Linter for Go source code that specializes in simplifying a code @@ -65,9 +83,15 @@ linters: - grouper # An analyzer to analyze expression groups. - importas # Enforces consistent import aliases - ineffassign # Detects when assignments to existing variables are not used + - lll # Reports long lines + - maintidx # maintidx measures the maintainability index of each function. + - makezero # Finds slice declarations with non-zero initial length - misspell # Finds commonly misspelled English words in comments + - nakedret # Finds naked returns in functions greater than a specified function length + - nestif # Reports deeply nested if statements - nilerr # Finds the code that returns nil even if it checks that the error is not nil. - nilnil # Checks that there is no simultaneous return of `nil` error and an invalid value. + - nlreturn # nlreturn checks for a new line before return and branch statements to increase code clarity - noctx # noctx finds sending http request without context.Context - predeclared # find code that shadows one of Go's predeclared identifiers - revive # golint replacement, finds style mistakes @@ -75,28 +99,22 @@ linters: - stylecheck # Stylecheck is a replacement for golint - tagliatelle # Checks the struct tags. - tenv # tenv is analyzer that detects using os.Setenv instead of t.Setenv since Go1.17 - - tparallel # tparallel detects inappropriate usage of t.Parallel() method in your Go test codes + - thelper # thelper detects golang test helpers without t.Helper() call and checks the consistency of test helpers - typecheck # Like the front-end of a Go compiler, parses and type-checks Go code - unconvert # Remove unnecessary type conversions - unparam # Reports unused function parameters - unused # Checks Go code for unused constants, variables, functions and types + - varnamelen # checks that the length of a variable's name matches its scope - wastedassign # wastedassign finds wasted assignment statements - whitespace # Tool for detection of leading and trailing whitespace disable: - depguard # Go linter that checks if package imports are in a list of acceptable packages - - containedctx # containedctx is a linter that detects struct contained context.Context field - - cyclop # checks function and package cyclomatic complexity - funlen # Tool for detection of long functions - - gocyclo # Computes and checks the cyclomatic complexity of functions - - godot # Check if comments end in a period - - gomnd # An analyzer to detect magic numbers. + - gochecknoinits # Checks that no init functions are present in Go code + - gomodguard # Allow and block list linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations. + - interfacebloat # A linter that checks length of interface. - ireturn # Accept Interfaces, Return Concrete Types - - lll # Reports long lines - - maintidx # maintidx measures the maintainability index of each function. - - makezero # Finds slice declarations with non-zero initial length - - nakedret # Finds naked returns in functions greater than a specified function length - - nestif # Reports deeply nested if statements - - nlreturn # nlreturn checks for a new line before return and branch statements to increase code clarity + - mnd # An analyzer to detect magic numbers - nolintlint # Reports ill-formed or insufficient nolint directives - paralleltest # paralleltest detects missing usage of t.Parallel() method in your Go test - prealloc # Finds slice declarations that could potentially be preallocated @@ -104,8 +122,7 @@ linters: - rowserrcheck # checks whether Err of rows is checked successfully - sqlclosecheck # Checks that sql.Rows and sql.Stmt are closed. - testpackage # linter that makes you use a separate _test package - - thelper # thelper detects golang test helpers without t.Helper() call and checks the consistency of test helpers - - varnamelen # checks that the length of a variable's name matches its scope + - tparallel # tparallel detects inappropriate usage of t.Parallel() method in your Go test codes - wrapcheck # Checks that errors returned from external packages are wrapped - wsl # Whitespace Linter - Forces you to use empty lines! @@ -114,9 +131,12 @@ issues: exclude-dirs-use-default: false exclude-rules: # Allow complex tests and examples, better to be self contained - - path: (examples|main\.go|_test\.go) + - path: (examples|main\.go) linters: + - gocognit - forbidigo + - path: _test\.go + linters: - gocognit # Allow forbidden identifiers in CLI commands diff --git a/vendor/github.com/pion/interceptor/README.md b/vendor/github.com/pion/interceptor/README.md index 5db5d44143..46a9ca6a2d 100644 --- a/vendor/github.com/pion/interceptor/README.md +++ b/vendor/github.com/pion/interceptor/README.md @@ -3,10 +3,10 @@ Pion Interceptor
-

RTCP and RTCP processors for building real time communications

+

RTP and RTCP processors for building real time communications

Pion Interceptor - Slack Widget + join us on Discord Follow us on Bluesky
GitHub Workflow Status Go Reference @@ -36,12 +36,12 @@ by anyone. With the following tenets in mind. * [Google Congestion Control](https://github.com/pion/interceptor/tree/master/pkg/gcc) * [Stats](https://github.com/pion/interceptor/tree/master/pkg/stats) A [webrtc-stats](https://www.w3.org/TR/webrtc-stats/) compliant statistics generation * [Interval PLI](https://github.com/pion/interceptor/tree/master/pkg/intervalpli) Generate PLI on a interval. Useful when no decoder is available. +* [FlexFec](https://github.com/pion/interceptor/tree/master/pkg/flexfec) – [FlexFEC-03](https://datatracker.ietf.org/doc/html/draft-ietf-payload-flexible-fec-scheme-03) encoder implementation ### Planned Interceptors * Bandwidth Estimation - [NADA](https://tools.ietf.org/html/rfc8698) * JitterBuffer, re-order packets and wait for arrival -* [FlexFec](https://tools.ietf.org/html/draft-ietf-payload-flexible-fec-scheme-20) * [RTCP Feedback for Congestion Control](https://datatracker.ietf.org/doc/html/rfc8888) the standardized alternative to TWCC. ### Interceptor Public API @@ -70,9 +70,9 @@ You should also look in [pion/webrtc](https://github.com/pion/webrtc) for real w The library is used as a part of our WebRTC implementation. Please refer to that [roadmap](https://github.com/pion/webrtc/issues/9) to track our major milestones. ### Community -Pion has an active community on the [Slack](https://pion.ly/slack). +Pion has an active community on the [Discord](https://discord.gg/PngbdqpFbt). -Follow the [Pion Twitter](https://twitter.com/_pion) for project updates and important WebRTC news. +Follow the [Pion Bluesky](https://bsky.app/profile/pion.ly) or [Pion Twitter](https://twitter.com/_pion) for project updates and important WebRTC news. We are always looking to support **your projects**. Please reach out if you have something to build! If you need commercial support or don't want to use public methods you can contact us at [team@pion.ly](mailto:team@pion.ly) diff --git a/vendor/github.com/pion/interceptor/attributes.go b/vendor/github.com/pion/interceptor/attributes.go index 8b6d0f5cf6..e4257b8da2 100644 --- a/vendor/github.com/pion/interceptor/attributes.go +++ b/vendor/github.com/pion/interceptor/attributes.go @@ -19,7 +19,7 @@ const ( var errInvalidType = errors.New("found value of invalid type in attributes map") -// Attributes are a generic key/value store used by interceptors +// Attributes are a generic key/value store used by interceptors. type Attributes map[interface{}]interface{} // Get returns the attribute associated with key. @@ -39,6 +39,7 @@ func (a Attributes) GetRTPHeader(raw []byte) (*rtp.Header, error) { if header, ok := val.(*rtp.Header); ok { return header, nil } + return nil, errInvalidType } header := &rtp.Header{} @@ -46,6 +47,7 @@ func (a Attributes) GetRTPHeader(raw []byte) (*rtp.Header, error) { return nil, err } a[rtpHeaderKey] = header + return header, nil } @@ -57,6 +59,7 @@ func (a Attributes) GetRTCPPackets(raw []byte) ([]rtcp.Packet, error) { if packets, ok := val.([]rtcp.Packet); ok { return packets, nil } + return nil, errInvalidType } pkts, err := rtcp.Unmarshal(raw) @@ -64,5 +67,6 @@ func (a Attributes) GetRTCPPackets(raw []byte) ([]rtcp.Packet, error) { return nil, err } a[rtcpPacketsKey] = pkts + return pkts, nil } diff --git a/vendor/github.com/pion/interceptor/chain.go b/vendor/github.com/pion/interceptor/chain.go index 267f366510..62c0aeb12e 100644 --- a/vendor/github.com/pion/interceptor/chain.go +++ b/vendor/github.com/pion/interceptor/chain.go @@ -50,7 +50,8 @@ func (i *Chain) UnbindLocalStream(ctx *StreamInfo) { } } -// BindRemoteStream lets you modify any incoming RTP packets. It is called once for per RemoteStream. The returned method +// BindRemoteStream lets you modify any incoming RTP packets. +// It is called once for per RemoteStream. The returned method // will be called once per rtp packet. func (i *Chain) BindRemoteStream(ctx *StreamInfo, reader RTPReader) RTPReader { for _, interceptor := range i.interceptors { diff --git a/vendor/github.com/pion/interceptor/errors.go b/vendor/github.com/pion/interceptor/errors.go index 3dafee3ef0..02c79539c8 100644 --- a/vendor/github.com/pion/interceptor/errors.go +++ b/vendor/github.com/pion/interceptor/errors.go @@ -18,6 +18,7 @@ func flattenErrs(errs []error) error { if len(errs2) == 0 { return nil } + return multiError(errs2) } @@ -50,5 +51,6 @@ func (me multiError) Is(err error) bool { } } } + return false } diff --git a/vendor/github.com/pion/interceptor/interceptor.go b/vendor/github.com/pion/interceptor/interceptor.go index c6ba53242e..10d3e32fca 100644 --- a/vendor/github.com/pion/interceptor/interceptor.go +++ b/vendor/github.com/pion/interceptor/interceptor.go @@ -12,7 +12,7 @@ import ( "github.com/pion/rtp" ) -// Factory provides an interface for constructing interceptors +// Factory provides an interface for constructing interceptors. type Factory interface { NewInterceptor(id string) (Interceptor, error) } @@ -35,7 +35,8 @@ type Interceptor interface { // UnbindLocalStream is called when the Stream is removed. It can be used to clean up any data related to that track. UnbindLocalStream(info *StreamInfo) - // BindRemoteStream lets you modify any incoming RTP packets. It is called once for per RemoteStream. The returned method + // BindRemoteStream lets you modify any incoming RTP packets. + // It is called once for per RemoteStream. The returned method // will be called once per rtp packet. BindRemoteStream(info *StreamInfo, reader RTPReader) RTPReader @@ -69,34 +70,34 @@ type RTCPReader interface { Read([]byte, Attributes) (int, Attributes, error) } -// RTPWriterFunc is an adapter for RTPWrite interface +// RTPWriterFunc is an adapter for RTPWrite interface. type RTPWriterFunc func(header *rtp.Header, payload []byte, attributes Attributes) (int, error) -// RTPReaderFunc is an adapter for RTPReader interface +// RTPReaderFunc is an adapter for RTPReader interface. type RTPReaderFunc func([]byte, Attributes) (int, Attributes, error) -// RTCPWriterFunc is an adapter for RTCPWriter interface +// RTCPWriterFunc is an adapter for RTCPWriter interface. type RTCPWriterFunc func(pkts []rtcp.Packet, attributes Attributes) (int, error) -// RTCPReaderFunc is an adapter for RTCPReader interface +// RTCPReaderFunc is an adapter for RTCPReader interface. type RTCPReaderFunc func([]byte, Attributes) (int, Attributes, error) -// Write a rtp packet +// Write a rtp packet. func (f RTPWriterFunc) Write(header *rtp.Header, payload []byte, attributes Attributes) (int, error) { return f(header, payload, attributes) } -// Read a rtp packet +// Read a rtp packet. func (f RTPReaderFunc) Read(b []byte, a Attributes) (int, Attributes, error) { return f(b, a) } -// Write a batch of rtcp packets +// Write a batch of rtcp packets. func (f RTCPWriterFunc) Write(pkts []rtcp.Packet, attributes Attributes) (int, error) { return f(pkts, attributes) } -// Read a batch of rtcp packets +// Read a batch of rtcp packets. func (f RTCPReaderFunc) Read(b []byte, a Attributes) (int, Attributes, error) { return f(b, a) } diff --git a/vendor/github.com/pion/interceptor/internal/ntp/ntp.go b/vendor/github.com/pion/interceptor/internal/ntp/ntp.go index 69f98d6c98..266f13d887 100644 --- a/vendor/github.com/pion/interceptor/internal/ntp/ntp.go +++ b/vendor/github.com/pion/interceptor/internal/ntp/ntp.go @@ -9,7 +9,7 @@ import ( "time" ) -// ToNTP converts a time.Time oboject to an uint64 NTP timestamp +// ToNTP converts a time.Time oboject to an uint64 NTP timestamp. func ToNTP(t time.Time) uint64 { // seconds since 1st January 1900 s := (float64(t.UnixNano()) / 1000000000) + 2208988800 @@ -17,14 +17,31 @@ func ToNTP(t time.Time) uint64 { // higher 32 bits are the integer part, lower 32 bits are the fractional part integerPart := uint32(s) fractionalPart := uint32((s - float64(integerPart)) * 0xFFFFFFFF) - return uint64(integerPart)<<32 | uint64(fractionalPart) + + return uint64(integerPart)<<32 | uint64(fractionalPart) //nolint:gosec // G115 +} + +// ToNTP32 converts a time.Time object to a uint32 NTP timestamp. +func ToNTP32(t time.Time) uint32 { + return uint32(ToNTP(t) >> 16) //nolint:gosec // G115 } -// ToTime converts a uint64 NTP timestamps to a time.Time object +// ToTime converts a uint64 NTP timestamps to a time.Time object. func ToTime(t uint64) time.Time { seconds := (t & 0xFFFFFFFF00000000) >> 32 fractional := float64(t&0x00000000FFFFFFFF) / float64(0xFFFFFFFF) + //nolint:gosec // G115 d := time.Duration(seconds)*time.Second + time.Duration(fractional*1e9)*time.Nanosecond return time.Unix(0, 0).Add(-2208988800 * time.Second).Add(d) } + +// ToTime32 converts a uint32 NTP timestamp to a time.Time object, using the +// highest 16 bit of the reference to recover the lost bits. The low 16 bits are +// not recovered. +func ToTime32(t uint32, reference time.Time) time.Time { + referenceNTP := ToNTP(reference) & 0xFFFF000000000000 + tu64 := ((uint64(t) << 16) & 0x0000FFFFFFFF0000) | referenceNTP + + return ToTime(tu64) +} diff --git a/vendor/github.com/pion/interceptor/internal/rtpbuffer/errors.go b/vendor/github.com/pion/interceptor/internal/rtpbuffer/errors.go new file mode 100644 index 0000000000..57fca02189 --- /dev/null +++ b/vendor/github.com/pion/interceptor/internal/rtpbuffer/errors.go @@ -0,0 +1,15 @@ +// SPDX-FileCopyrightText: 2023 The Pion community +// SPDX-License-Identifier: MIT + +package rtpbuffer + +import "errors" + +// ErrInvalidSize is returned by newReceiveLog/newRTPBuffer, when an incorrect buffer size is supplied. +var ErrInvalidSize = errors.New("invalid buffer size") + +var ( + errPacketReleased = errors.New("could not retain packet, already released") + errFailedToCastHeaderPool = errors.New("could not access header pool, failed cast") + errFailedToCastPayloadPool = errors.New("could not access payload pool, failed cast") +) diff --git a/vendor/github.com/pion/interceptor/internal/rtpbuffer/packet_factory.go b/vendor/github.com/pion/interceptor/internal/rtpbuffer/packet_factory.go new file mode 100644 index 0000000000..3836acc394 --- /dev/null +++ b/vendor/github.com/pion/interceptor/internal/rtpbuffer/packet_factory.go @@ -0,0 +1,140 @@ +// SPDX-FileCopyrightText: 2023 The Pion community +// SPDX-License-Identifier: MIT + +package rtpbuffer + +import ( + "encoding/binary" + "io" + "sync" + + "github.com/pion/rtp" +) + +const rtxSsrcByteLength = 2 + +// PacketFactory allows custom logic around the handle of RTP Packets before they added to the RTPBuffer. +// The NoOpPacketFactory doesn't copy packets, while the RetainablePacket will take a copy before adding. +type PacketFactory interface { + NewPacket(header *rtp.Header, payload []byte, rtxSsrc uint32, rtxPayloadType uint8) (*RetainablePacket, error) +} + +// PacketFactoryCopy is PacketFactory that takes a copy of packets when added to the RTPBuffer. +type PacketFactoryCopy struct { + headerPool *sync.Pool + payloadPool *sync.Pool + rtxSequencer rtp.Sequencer +} + +// NewPacketFactoryCopy constructs a PacketFactory that takes a copy of packets when added to the RTPBuffer. +func NewPacketFactoryCopy() *PacketFactoryCopy { + return &PacketFactoryCopy{ + headerPool: &sync.Pool{ + New: func() interface{} { + return &rtp.Header{} + }, + }, + payloadPool: &sync.Pool{ + New: func() interface{} { + buf := make([]byte, maxPayloadLen) + + return &buf + }, + }, + rtxSequencer: rtp.NewRandomSequencer(), + } +} + +// NewPacket constructs a new RetainablePacket that can be added to the RTPBuffer. +// +//nolint:cyclop +func (m *PacketFactoryCopy) NewPacket( + header *rtp.Header, payload []byte, rtxSsrc uint32, rtxPayloadType uint8, +) (*RetainablePacket, error) { + if len(payload) > maxPayloadLen { + return nil, io.ErrShortBuffer + } + + retainablePacket := &RetainablePacket{ + onRelease: m.releasePacket, + sequenceNumber: header.SequenceNumber, + // new packets have retain count of 1 + count: 1, + } + + var ok bool + retainablePacket.header, ok = m.headerPool.Get().(*rtp.Header) + if !ok { + return nil, errFailedToCastHeaderPool + } + + *retainablePacket.header = header.Clone() + + if payload != nil { + retainablePacket.buffer, ok = m.payloadPool.Get().(*[]byte) + if !ok { + return nil, errFailedToCastPayloadPool + } + if rtxSsrc != 0 && rtxPayloadType != 0 { + size := copy((*retainablePacket.buffer)[rtxSsrcByteLength:], payload) + retainablePacket.payload = (*retainablePacket.buffer)[:size+rtxSsrcByteLength] + } else { + size := copy(*retainablePacket.buffer, payload) + retainablePacket.payload = (*retainablePacket.buffer)[:size] + } + } + + if rtxSsrc != 0 && rtxPayloadType != 0 { + if payload == nil { + retainablePacket.buffer, ok = m.payloadPool.Get().(*[]byte) + if !ok { + return nil, errFailedToCastPayloadPool + } + retainablePacket.payload = (*retainablePacket.buffer)[:rtxSsrcByteLength] + } + // Write the original sequence number at the beginning of the payload. + binary.BigEndian.PutUint16(retainablePacket.payload, retainablePacket.header.SequenceNumber) + + // Rewrite the SSRC. + retainablePacket.header.SSRC = rtxSsrc + // Rewrite the payload type. + retainablePacket.header.PayloadType = rtxPayloadType + // Rewrite the sequence number. + retainablePacket.header.SequenceNumber = m.rtxSequencer.NextSequenceNumber() + // Remove padding if present. + if retainablePacket.header.Padding && retainablePacket.payload != nil && len(retainablePacket.payload) > 0 { + paddingLength := int(retainablePacket.payload[len(retainablePacket.payload)-1]) + retainablePacket.header.Padding = false + retainablePacket.payload = (*retainablePacket.buffer)[:len(retainablePacket.payload)-paddingLength] + } + } + + return retainablePacket, nil +} + +func (m *PacketFactoryCopy) releasePacket(header *rtp.Header, payload *[]byte) { + m.headerPool.Put(header) + if payload != nil { + m.payloadPool.Put(payload) + } +} + +// PacketFactoryNoOp is a PacketFactory implementation that doesn't copy packets. +type PacketFactoryNoOp struct{} + +// NewPacket constructs a new RetainablePacket that can be added to the RTPBuffer. +func (f *PacketFactoryNoOp) NewPacket( + header *rtp.Header, payload []byte, _ uint32, _ uint8, +) (*RetainablePacket, error) { + return &RetainablePacket{ + onRelease: f.releasePacket, + count: 1, + header: header, + payload: payload, + sequenceNumber: header.SequenceNumber, + }, nil +} + +func (f *PacketFactoryNoOp) releasePacket(_ *rtp.Header, _ *[]byte) { + // no-op +} diff --git a/vendor/github.com/pion/interceptor/internal/rtpbuffer/retainable_packet.go b/vendor/github.com/pion/interceptor/internal/rtpbuffer/retainable_packet.go new file mode 100644 index 0000000000..82ade097c9 --- /dev/null +++ b/vendor/github.com/pion/interceptor/internal/rtpbuffer/retainable_packet.go @@ -0,0 +1,62 @@ +// SPDX-FileCopyrightText: 2023 The Pion community +// SPDX-License-Identifier: MIT + +package rtpbuffer + +import ( + "sync" + + "github.com/pion/rtp" +) + +// RetainablePacket is a referenced counted RTP packet. +type RetainablePacket struct { + onRelease func(*rtp.Header, *[]byte) + + countMu sync.Mutex + count int + + header *rtp.Header + buffer *[]byte + payload []byte + + sequenceNumber uint16 +} + +// Header returns the RTP Header of the RetainablePacket. +func (p *RetainablePacket) Header() *rtp.Header { + return p.header +} + +// Payload returns the RTP Payload of the RetainablePacket. +func (p *RetainablePacket) Payload() []byte { + return p.payload +} + +// Retain increases the reference count of the RetainablePacket. +func (p *RetainablePacket) Retain() error { + p.countMu.Lock() + defer p.countMu.Unlock() + if p.count == 0 { + // already released + return errPacketReleased + } + p.count++ + + return nil +} + +// Release decreases the reference count of the RetainablePacket and frees if needed. +func (p *RetainablePacket) Release() { + p.countMu.Lock() + defer p.countMu.Unlock() + p.count-- + + if p.count == 0 { + // release back to pool + p.onRelease(p.header, p.buffer) + p.header = nil + p.buffer = nil + p.payload = nil + } +} diff --git a/vendor/github.com/pion/interceptor/internal/rtpbuffer/rtpbuffer.go b/vendor/github.com/pion/interceptor/internal/rtpbuffer/rtpbuffer.go new file mode 100644 index 0000000000..94c7adfe1e --- /dev/null +++ b/vendor/github.com/pion/interceptor/internal/rtpbuffer/rtpbuffer.go @@ -0,0 +1,107 @@ +// SPDX-FileCopyrightText: 2023 The Pion community +// SPDX-License-Identifier: MIT + +// Package rtpbuffer provides a buffer for storing RTP packets +package rtpbuffer + +import ( + "fmt" +) + +const ( + // Uint16SizeHalf is half of a math.Uint16. + Uint16SizeHalf = 1 << 15 + + maxPayloadLen = 1460 +) + +// RTPBuffer stores RTP packets and allows custom logic +// around the lifetime of them via the PacketFactory. +type RTPBuffer struct { + packets []*RetainablePacket + size uint16 + highestAdded uint16 + started bool +} + +// NewRTPBuffer constructs a new RTPBuffer. +func NewRTPBuffer(size uint16) (*RTPBuffer, error) { + allowedSizes := make([]uint16, 0) + correctSize := false + for i := 0; i < 16; i++ { + if size == 1<= Uint16SizeHalf { + return nil + } + + if diff >= r.size { + return nil + } + + pkt := r.packets[seq%r.size] + if pkt != nil { + if pkt.sequenceNumber != seq { + return nil + } + // already released + if err := pkt.Retain(); err != nil { + return nil + } + } + + return pkt +} diff --git a/vendor/github.com/pion/interceptor/internal/sequencenumber/unwrapper.go b/vendor/github.com/pion/interceptor/internal/sequencenumber/unwrapper.go index 311ff09df6..48500b3cc5 100644 --- a/vendor/github.com/pion/interceptor/internal/sequencenumber/unwrapper.go +++ b/vendor/github.com/pion/interceptor/internal/sequencenumber/unwrapper.go @@ -9,7 +9,7 @@ const ( breakpoint = 32768 // half of max uint16 ) -// Unwrapper stores an unwrapped sequence number +// Unwrapper stores an unwrapped sequence number. type Unwrapper struct { init bool lastUnwrapped int64 @@ -19,18 +19,20 @@ func isNewer(value, previous uint16) bool { if value-previous == breakpoint { return value > previous } + return value != previous && (value-previous) < breakpoint } -// Unwrap unwraps the next sequencenumber +// Unwrap unwraps the next sequencenumber. func (u *Unwrapper) Unwrap(i uint16) int64 { if !u.init { u.init = true u.lastUnwrapped = int64(i) + return u.lastUnwrapped } - lastWrapped := uint16(u.lastUnwrapped) + lastWrapped := uint16(u.lastUnwrapped) //nolint:gosec // G115 delta := int64(i - lastWrapped) if isNewer(i, lastWrapped) { if delta < 0 { @@ -41,5 +43,6 @@ func (u *Unwrapper) Unwrap(i uint16) int64 { } u.lastUnwrapped += delta + return u.lastUnwrapped } diff --git a/vendor/github.com/pion/interceptor/noop.go b/vendor/github.com/pion/interceptor/noop.go index b0fc2a69a9..964a330e84 100644 --- a/vendor/github.com/pion/interceptor/noop.go +++ b/vendor/github.com/pion/interceptor/noop.go @@ -28,7 +28,8 @@ func (i *NoOp) BindLocalStream(_ *StreamInfo, writer RTPWriter) RTPWriter { // UnbindLocalStream is called when the Stream is removed. It can be used to clean up any data related to that track. func (i *NoOp) UnbindLocalStream(_ *StreamInfo) {} -// BindRemoteStream lets you modify any incoming RTP packets. It is called once for per RemoteStream. The returned method +// BindRemoteStream lets you modify any incoming RTP packets. +// It is called once for per RemoteStream. The returned method // will be called once per rtp packet. func (i *NoOp) BindRemoteStream(_ *StreamInfo, reader RTPReader) RTPReader { return reader diff --git a/vendor/github.com/pion/interceptor/pkg/nack/errors.go b/vendor/github.com/pion/interceptor/pkg/nack/errors.go index b47ec39c26..8b0958d015 100644 --- a/vendor/github.com/pion/interceptor/pkg/nack/errors.go +++ b/vendor/github.com/pion/interceptor/pkg/nack/errors.go @@ -3,13 +3,7 @@ package nack -import "errors" +import "github.com/pion/interceptor/internal/rtpbuffer" -// ErrInvalidSize is returned by newReceiveLog/newSendBuffer, when an incorrect buffer size is supplied. -var ErrInvalidSize = errors.New("invalid buffer size") - -var ( - errPacketReleased = errors.New("could not retain packet, already released") - errFailedToCastHeaderPool = errors.New("could not access header pool, failed cast") - errFailedToCastPayloadPool = errors.New("could not access payload pool, failed cast") -) +// ErrInvalidSize is returned by newReceiveLog/newRTPBuffer, when an incorrect buffer size is supplied. +var ErrInvalidSize = rtpbuffer.ErrInvalidSize diff --git a/vendor/github.com/pion/interceptor/pkg/nack/generator_interceptor.go b/vendor/github.com/pion/interceptor/pkg/nack/generator_interceptor.go index ab2bb2c52a..10c8a01b48 100644 --- a/vendor/github.com/pion/interceptor/pkg/nack/generator_interceptor.go +++ b/vendor/github.com/pion/interceptor/pkg/nack/generator_interceptor.go @@ -13,14 +13,14 @@ import ( "github.com/pion/rtcp" ) -// GeneratorInterceptorFactory is a interceptor.Factory for a GeneratorInterceptor +// GeneratorInterceptorFactory is a interceptor.Factory for a GeneratorInterceptor. type GeneratorInterceptorFactory struct { opts []GeneratorOption } -// NewInterceptor constructs a new ReceiverInterceptor +// NewInterceptor constructs a new ReceiverInterceptor. func (g *GeneratorInterceptorFactory) NewInterceptor(_ string) (interceptor.Interceptor, error) { - i := &GeneratorInterceptor{ + generatorInterceptor := &GeneratorInterceptor{ streamsFilter: streamSupportNack, size: 512, skipLastN: 0, @@ -33,16 +33,16 @@ func (g *GeneratorInterceptorFactory) NewInterceptor(_ string) (interceptor.Inte } for _, opt := range g.opts { - if err := opt(i); err != nil { + if err := opt(generatorInterceptor); err != nil { return nil, err } } - if _, err := newReceiveLog(i.size); err != nil { + if _, err := newReceiveLog(generatorInterceptor.size); err != nil { return nil, err } - return i, nil + return generatorInterceptor, nil } // GeneratorInterceptor interceptor generates nack feedback messages. @@ -63,13 +63,13 @@ type GeneratorInterceptor struct { receiveLogsMu sync.Mutex } -// NewGeneratorInterceptor returns a new GeneratorInterceptorFactory +// NewGeneratorInterceptor returns a new GeneratorInterceptorFactory. func NewGeneratorInterceptor(opts ...GeneratorOption) (*GeneratorInterceptorFactory, error) { return &GeneratorInterceptorFactory{opts}, nil } -// BindRTCPWriter lets you modify any outgoing RTCP packets. It is called once per PeerConnection. The returned method -// will be called once per packet batch. +// BindRTCPWriter lets you modify any outgoing RTCP packets. It is called once per PeerConnection. +// The returned method will be called once per packet batch. func (n *GeneratorInterceptor) BindRTCPWriter(writer interceptor.RTCPWriter) interceptor.RTCPWriter { n.m.Lock() defer n.m.Unlock() @@ -85,9 +85,11 @@ func (n *GeneratorInterceptor) BindRTCPWriter(writer interceptor.RTCPWriter) int return writer } -// BindRemoteStream lets you modify any incoming RTP packets. It is called once for per RemoteStream. The returned method -// will be called once per rtp packet. -func (n *GeneratorInterceptor) BindRemoteStream(info *interceptor.StreamInfo, reader interceptor.RTPReader) interceptor.RTPReader { +// BindRemoteStream lets you modify any incoming RTP packets. It is called once for per RemoteStream. +// The returned method will be called once per rtp packet. +func (n *GeneratorInterceptor) BindRemoteStream( + info *interceptor.StreamInfo, reader interceptor.RTPReader, +) interceptor.RTPReader { if !n.streamsFilter(info) { return reader } @@ -124,7 +126,7 @@ func (n *GeneratorInterceptor) UnbindRemoteStream(info *interceptor.StreamInfo) n.receiveLogsMu.Unlock() } -// Close closes the interceptor +// Close closes the interceptor. func (n *GeneratorInterceptor) Close() error { defer n.wg.Wait() n.m.Lock() @@ -137,12 +139,15 @@ func (n *GeneratorInterceptor) Close() error { return nil } -// nolint:gocognit +// nolint:gocognit,cyclop func (n *GeneratorInterceptor) loop(rtcpWriter interceptor.RTCPWriter) { defer n.wg.Done() senderSSRC := rand.Uint32() // #nosec + missingPacketSeqNums := make([]uint16, n.size) + filteredMissingPacket := make([]uint16, n.size) + ticker := time.NewTicker(n.interval) defer ticker.Stop() for { @@ -153,7 +158,7 @@ func (n *GeneratorInterceptor) loop(rtcpWriter interceptor.RTCPWriter) { defer n.receiveLogsMu.Unlock() for ssrc, receiveLog := range n.receiveLogs { - missing := receiveLog.missingSeqNumbers(n.skipLastN) + missing := receiveLog.missingSeqNumbers(n.skipLastN, missingPacketSeqNums) if len(missing) == 0 || n.nackCountLogs[ssrc] == nil { n.nackCountLogs[ssrc] = map[uint16]uint16{} @@ -162,22 +167,33 @@ func (n *GeneratorInterceptor) loop(rtcpWriter interceptor.RTCPWriter) { continue } - filteredMissing := []uint16{} + nack := &rtcp.TransportLayerNack{} // nolint:ineffassign,wastedassign + + c := 0 // nolint:varnamelen, if n.maxNacksPerPacket > 0 { for _, missingSeq := range missing { if n.nackCountLogs[ssrc][missingSeq] < n.maxNacksPerPacket { - filteredMissing = append(filteredMissing, missingSeq) + filteredMissingPacket[c] = missingSeq + c++ } n.nackCountLogs[ssrc][missingSeq]++ } - } else { - filteredMissing = missing - } - nack := &rtcp.TransportLayerNack{ - SenderSSRC: senderSSRC, - MediaSSRC: ssrc, - Nacks: rtcp.NackPairsFromSequenceNumbers(filteredMissing), + if c == 0 { + continue + } + + nack = &rtcp.TransportLayerNack{ + SenderSSRC: senderSSRC, + MediaSSRC: ssrc, + Nacks: rtcp.NackPairsFromSequenceNumbers(filteredMissingPacket[:c]), + } + } else { + nack = &rtcp.TransportLayerNack{ + SenderSSRC: senderSSRC, + MediaSSRC: ssrc, + Nacks: rtcp.NackPairsFromSequenceNumbers(missing), + } } for nackSeq := range n.nackCountLogs[ssrc] { @@ -185,6 +201,7 @@ func (n *GeneratorInterceptor) loop(rtcpWriter interceptor.RTCPWriter) { for _, missingSeq := range missing { if missingSeq == nackSeq { isMissing = true + break } } @@ -193,10 +210,6 @@ func (n *GeneratorInterceptor) loop(rtcpWriter interceptor.RTCPWriter) { } } - if len(filteredMissing) == 0 { - continue - } - if _, err := rtcpWriter.Write([]rtcp.Packet{nack}, interceptor.Attributes{}); err != nil { n.log.Warnf("failed sending nack: %+v", err) } diff --git a/vendor/github.com/pion/interceptor/pkg/nack/generator_option.go b/vendor/github.com/pion/interceptor/pkg/nack/generator_option.go index 5403e3eee7..db84093ae2 100644 --- a/vendor/github.com/pion/interceptor/pkg/nack/generator_option.go +++ b/vendor/github.com/pion/interceptor/pkg/nack/generator_option.go @@ -10,56 +10,63 @@ import ( "github.com/pion/logging" ) -// GeneratorOption can be used to configure GeneratorInterceptor +// GeneratorOption can be used to configure GeneratorInterceptor. type GeneratorOption func(r *GeneratorInterceptor) error // GeneratorSize sets the size of the interceptor. -// Size must be one of: 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768 +// Size must be one of: 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768. func GeneratorSize(size uint16) GeneratorOption { return func(r *GeneratorInterceptor) error { r.size = size + return nil } } -// GeneratorSkipLastN sets the number of packets (n-1 packets before the last received packets) to ignore when generating -// nack requests. +// GeneratorSkipLastN sets the number of packets (n-1 packets before the last received packets) +// +// to ignore when generating nack requests. func GeneratorSkipLastN(skipLastN uint16) GeneratorOption { return func(r *GeneratorInterceptor) error { r.skipLastN = skipLastN + return nil } } // GeneratorMaxNacksPerPacket sets the maximum number of NACKs sent per missing packet, e.g. if set to 2, a missing -// packet will only be NACKed at most twice. If set to 0 (default), max number of NACKs is unlimited +// packet will only be NACKed at most twice. If set to 0 (default), max number of NACKs is unlimited. func GeneratorMaxNacksPerPacket(maxNacks uint16) GeneratorOption { return func(r *GeneratorInterceptor) error { r.maxNacksPerPacket = maxNacks + return nil } } -// GeneratorLog sets a logger for the interceptor +// GeneratorLog sets a logger for the interceptor. func GeneratorLog(log logging.LeveledLogger) GeneratorOption { return func(r *GeneratorInterceptor) error { r.log = log + return nil } } -// GeneratorInterval sets the nack send interval for the interceptor +// GeneratorInterval sets the nack send interval for the interceptor. func GeneratorInterval(interval time.Duration) GeneratorOption { return func(r *GeneratorInterceptor) error { r.interval = interval + return nil } } -// GeneratorStreamsFilter sets filter for generator streams +// GeneratorStreamsFilter sets filter for generator streams. func GeneratorStreamsFilter(filter func(info *interceptor.StreamInfo) bool) GeneratorOption { return func(r *GeneratorInterceptor) error { r.streamsFilter = filter + return nil } } diff --git a/vendor/github.com/pion/interceptor/pkg/nack/receive_log.go b/vendor/github.com/pion/interceptor/pkg/nack/receive_log.go index 6a19996e76..c37407d124 100644 --- a/vendor/github.com/pion/interceptor/pkg/nack/receive_log.go +++ b/vendor/github.com/pion/interceptor/pkg/nack/receive_log.go @@ -6,6 +6,8 @@ package nack import ( "fmt" "sync" + + "github.com/pion/interceptor/internal/rtpbuffer" ) type receiveLog struct { @@ -23,6 +25,7 @@ func newReceiveLog(size uint16) (*receiveLog, error) { for i := 6; i < 16; i++ { if size == 1< end (with counting for rollovers) for i := s.end + 1; i != seq; i++ { // clear packets between end and seq (these may contain packets from a "size" ago) @@ -82,7 +86,7 @@ func (s *receiveLog) get(seq uint16) bool { defer s.m.RUnlock() diff := s.end - seq - if diff >= uint16SizeHalf { + if diff >= rtpbuffer.Uint16SizeHalf { return false } @@ -93,24 +97,25 @@ func (s *receiveLog) get(seq uint16) bool { return s.getReceived(seq) } -func (s *receiveLog) missingSeqNumbers(skipLastN uint16) []uint16 { +func (s *receiveLog) missingSeqNumbers(skipLastN uint16, missingPacketSeqNums []uint16) []uint16 { s.m.RLock() defer s.m.RUnlock() until := s.end - skipLastN - if until-s.lastConsecutive >= uint16SizeHalf { + if until-s.lastConsecutive >= rtpbuffer.Uint16SizeHalf { // until < s.lastConsecutive (counting for rollover) return nil } - missingPacketSeqNums := make([]uint16, 0) + c := 0 for i := s.lastConsecutive + 1; i != until+1; i++ { if !s.getReceived(i) { - missingPacketSeqNums = append(missingPacketSeqNums, i) + missingPacketSeqNums[c] = i + c++ } } - return missingPacketSeqNums + return missingPacketSeqNums[:c] } func (s *receiveLog) setReceived(seq uint16) { @@ -125,6 +130,7 @@ func (s *receiveLog) delReceived(seq uint16) { func (s *receiveLog) getReceived(seq uint16) bool { pos := seq % s.size + return (s.packets[pos/64] & (1 << (pos % 64))) != 0 } diff --git a/vendor/github.com/pion/interceptor/pkg/nack/responder_interceptor.go b/vendor/github.com/pion/interceptor/pkg/nack/responder_interceptor.go index 22d038ba4d..8b5585ac9b 100644 --- a/vendor/github.com/pion/interceptor/pkg/nack/responder_interceptor.go +++ b/vendor/github.com/pion/interceptor/pkg/nack/responder_interceptor.go @@ -7,23 +7,20 @@ import ( "sync" "github.com/pion/interceptor" + "github.com/pion/interceptor/internal/rtpbuffer" "github.com/pion/logging" "github.com/pion/rtcp" "github.com/pion/rtp" ) -// ResponderInterceptorFactory is a interceptor.Factory for a ResponderInterceptor +// ResponderInterceptorFactory is a interceptor.Factory for a ResponderInterceptor. type ResponderInterceptorFactory struct { opts []ResponderOption } -type packetFactory interface { - NewPacket(header *rtp.Header, payload []byte, rtxSsrc uint32, rtxPayloadType uint8) (*retainablePacket, error) -} - -// NewInterceptor constructs a new ResponderInterceptor +// NewInterceptor constructs a new ResponderInterceptor. func (r *ResponderInterceptorFactory) NewInterceptor(_ string) (interceptor.Interceptor, error) { - i := &ResponderInterceptor{ + responderInterceptor := &ResponderInterceptor{ streamsFilter: streamSupportNack, size: 1024, log: logging.NewDefaultLoggerFactory().NewLogger("nack_responder"), @@ -31,40 +28,41 @@ func (r *ResponderInterceptorFactory) NewInterceptor(_ string) (interceptor.Inte } for _, opt := range r.opts { - if err := opt(i); err != nil { + if err := opt(responderInterceptor); err != nil { return nil, err } } - if i.packetFactory == nil { - i.packetFactory = newPacketManager() + if responderInterceptor.packetFactory == nil { + responderInterceptor.packetFactory = rtpbuffer.NewPacketFactoryCopy() } - if _, err := newSendBuffer(i.size); err != nil { + if _, err := rtpbuffer.NewRTPBuffer(responderInterceptor.size); err != nil { return nil, err } - return i, nil + return responderInterceptor, nil } -// ResponderInterceptor responds to nack feedback messages +// ResponderInterceptor responds to nack feedback messages. type ResponderInterceptor struct { interceptor.NoOp streamsFilter func(info *interceptor.StreamInfo) bool size uint16 log logging.LeveledLogger - packetFactory packetFactory + packetFactory rtpbuffer.PacketFactory streams map[uint32]*localStream streamsMu sync.Mutex } type localStream struct { - sendBuffer *sendBuffer - rtpWriter interceptor.RTPWriter + rtpBuffer *rtpbuffer.RTPBuffer + rtpBufferMutex sync.RWMutex + rtpWriter interceptor.RTPWriter } -// NewResponderInterceptor returns a new ResponderInterceptorFactor +// NewResponderInterceptor returns a new ResponderInterceptorFactor. func NewResponderInterceptor(opts ...ResponderOption) (*ResponderInterceptorFactory, error) { return &ResponderInterceptorFactory{opts}, nil } @@ -98,30 +96,44 @@ func (n *ResponderInterceptor) BindRTCPReader(reader interceptor.RTCPReader) int }) } -// BindLocalStream lets you modify any outgoing RTP packets. It is called once for per LocalStream. The returned method -// will be called once per rtp packet. -func (n *ResponderInterceptor) BindLocalStream(info *interceptor.StreamInfo, writer interceptor.RTPWriter) interceptor.RTPWriter { +// BindLocalStream lets you modify any outgoing RTP packets. It is called once for per LocalStream. +// The returned method will be called once per rtp packet. +func (n *ResponderInterceptor) BindLocalStream( + info *interceptor.StreamInfo, writer interceptor.RTPWriter, +) interceptor.RTPWriter { if !n.streamsFilter(info) { return writer } // error is already checked in NewGeneratorInterceptor - sendBuffer, _ := newSendBuffer(n.size) - n.streamsMu.Lock() - n.streams[info.SSRC] = &localStream{ - sendBuffer: sendBuffer, - rtpWriter: writer, + rtpBuffer, _ := rtpbuffer.NewRTPBuffer(n.size) + stream := &localStream{ + rtpBuffer: rtpBuffer, + rtpWriter: writer, } + n.streamsMu.Lock() + n.streams[info.SSRC] = stream n.streamsMu.Unlock() - return interceptor.RTPWriterFunc(func(header *rtp.Header, payload []byte, attributes interceptor.Attributes) (int, error) { - pkt, err := n.packetFactory.NewPacket(header, payload, info.SSRCRetransmission, info.PayloadTypeRetransmission) - if err != nil { - return 0, err - } - sendBuffer.add(pkt) - return writer.Write(header, payload, attributes) - }) + return interceptor.RTPWriterFunc( + func(header *rtp.Header, payload []byte, attributes interceptor.Attributes) (int, error) { + // If this packet doesn't belong to the main SSRC, do not add it to rtpBuffer + if header.SSRC != info.SSRC { + return writer.Write(header, payload, attributes) + } + + pkt, err := n.packetFactory.NewPacket(header, payload, info.SSRCRetransmission, info.PayloadTypeRetransmission) + if err != nil { + return 0, err + } + stream.rtpBufferMutex.Lock() + defer stream.rtpBufferMutex.Unlock() + + rtpBuffer.Add(pkt) + + return writer.Write(header, payload, attributes) + }, + ) } // UnbindLocalStream is called when the Stream is removed. It can be used to clean up any data related to that track. @@ -141,7 +153,10 @@ func (n *ResponderInterceptor) resendPackets(nack *rtcp.TransportLayerNack) { for i := range nack.Nacks { nack.Nacks[i].Range(func(seq uint16) bool { - if p := stream.sendBuffer.get(seq); p != nil { + stream.rtpBufferMutex.Lock() + defer stream.rtpBufferMutex.Unlock() + + if p := stream.rtpBuffer.Get(seq); p != nil { if _, err := stream.rtpWriter.Write(p.Header(), p.Payload(), interceptor.Attributes{}); err != nil { n.log.Warnf("failed resending nacked packet: %+v", err) } diff --git a/vendor/github.com/pion/interceptor/pkg/nack/responder_option.go b/vendor/github.com/pion/interceptor/pkg/nack/responder_option.go index 24c7c4693b..ea9435810f 100644 --- a/vendor/github.com/pion/interceptor/pkg/nack/responder_option.go +++ b/vendor/github.com/pion/interceptor/pkg/nack/responder_option.go @@ -5,42 +5,47 @@ package nack import ( "github.com/pion/interceptor" + "github.com/pion/interceptor/internal/rtpbuffer" "github.com/pion/logging" ) -// ResponderOption can be used to configure ResponderInterceptor +// ResponderOption can be used to configure ResponderInterceptor. type ResponderOption func(s *ResponderInterceptor) error // ResponderSize sets the size of the interceptor. -// Size must be one of: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768 +// Size must be one of: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768. func ResponderSize(size uint16) ResponderOption { return func(r *ResponderInterceptor) error { r.size = size + return nil } } -// ResponderLog sets a logger for the interceptor +// ResponderLog sets a logger for the interceptor. func ResponderLog(log logging.LeveledLogger) ResponderOption { return func(r *ResponderInterceptor) error { r.log = log + return nil } } // DisableCopy bypasses copy of underlying packets. It should be used when -// you are not re-using underlying buffers of packets that have been written +// you are not re-using underlying buffers of packets that have been written. func DisableCopy() ResponderOption { return func(s *ResponderInterceptor) error { - s.packetFactory = &noOpPacketFactory{} + s.packetFactory = &rtpbuffer.PacketFactoryNoOp{} + return nil } } -// ResponderStreamsFilter sets filter for local streams +// ResponderStreamsFilter sets filter for local streams. func ResponderStreamsFilter(filter func(info *interceptor.StreamInfo) bool) ResponderOption { return func(r *ResponderInterceptor) error { r.streamsFilter = filter + return nil } } diff --git a/vendor/github.com/pion/interceptor/pkg/nack/retainable_packet.go b/vendor/github.com/pion/interceptor/pkg/nack/retainable_packet.go deleted file mode 100644 index 18c533a8a8..0000000000 --- a/vendor/github.com/pion/interceptor/pkg/nack/retainable_packet.go +++ /dev/null @@ -1,162 +0,0 @@ -// SPDX-FileCopyrightText: 2023 The Pion community -// SPDX-License-Identifier: MIT - -package nack - -import ( - "encoding/binary" - "io" - "sync" - - "github.com/pion/rtp" -) - -const maxPayloadLen = 1460 - -type packetManager struct { - headerPool *sync.Pool - payloadPool *sync.Pool - rtxSequencer rtp.Sequencer -} - -func newPacketManager() *packetManager { - return &packetManager{ - headerPool: &sync.Pool{ - New: func() interface{} { - return &rtp.Header{} - }, - }, - payloadPool: &sync.Pool{ - New: func() interface{} { - buf := make([]byte, maxPayloadLen) - return &buf - }, - }, - rtxSequencer: rtp.NewRandomSequencer(), - } -} - -func (m *packetManager) NewPacket(header *rtp.Header, payload []byte, rtxSsrc uint32, rtxPayloadType uint8) (*retainablePacket, error) { - if len(payload) > maxPayloadLen { - return nil, io.ErrShortBuffer - } - - p := &retainablePacket{ - onRelease: m.releasePacket, - sequenceNumber: header.SequenceNumber, - // new packets have retain count of 1 - count: 1, - } - - var ok bool - p.header, ok = m.headerPool.Get().(*rtp.Header) - if !ok { - return nil, errFailedToCastHeaderPool - } - - *p.header = header.Clone() - - if payload != nil { - p.buffer, ok = m.payloadPool.Get().(*[]byte) - if !ok { - return nil, errFailedToCastPayloadPool - } - - size := copy(*p.buffer, payload) - p.payload = (*p.buffer)[:size] - } - - if rtxSsrc != 0 && rtxPayloadType != 0 { - // Store the original sequence number and rewrite the sequence number. - originalSequenceNumber := p.header.SequenceNumber - p.header.SequenceNumber = m.rtxSequencer.NextSequenceNumber() - - // Rewrite the SSRC. - p.header.SSRC = rtxSsrc - // Rewrite the payload type. - p.header.PayloadType = rtxPayloadType - - // Remove padding if present. - paddingLength := 0 - if p.header.Padding && p.payload != nil && len(p.payload) > 0 { - paddingLength = int(p.payload[len(p.payload)-1]) - p.header.Padding = false - } - - // Write the original sequence number at the beginning of the payload. - payload := make([]byte, 2) - binary.BigEndian.PutUint16(payload, originalSequenceNumber) - p.payload = append(payload, p.payload[:len(p.payload)-paddingLength]...) - } - - return p, nil -} - -func (m *packetManager) releasePacket(header *rtp.Header, payload *[]byte) { - m.headerPool.Put(header) - if payload != nil { - m.payloadPool.Put(payload) - } -} - -type noOpPacketFactory struct{} - -func (f *noOpPacketFactory) NewPacket(header *rtp.Header, payload []byte, _ uint32, _ uint8) (*retainablePacket, error) { - return &retainablePacket{ - onRelease: f.releasePacket, - count: 1, - header: header, - payload: payload, - sequenceNumber: header.SequenceNumber, - }, nil -} - -func (f *noOpPacketFactory) releasePacket(_ *rtp.Header, _ *[]byte) { - // no-op -} - -type retainablePacket struct { - onRelease func(*rtp.Header, *[]byte) - - countMu sync.Mutex - count int - - header *rtp.Header - buffer *[]byte - payload []byte - - sequenceNumber uint16 -} - -func (p *retainablePacket) Header() *rtp.Header { - return p.header -} - -func (p *retainablePacket) Payload() []byte { - return p.payload -} - -func (p *retainablePacket) Retain() error { - p.countMu.Lock() - defer p.countMu.Unlock() - if p.count == 0 { - // already released - return errPacketReleased - } - p.count++ - return nil -} - -func (p *retainablePacket) Release() { - p.countMu.Lock() - defer p.countMu.Unlock() - p.count-- - - if p.count == 0 { - // release back to pool - p.onRelease(p.header, p.buffer) - p.header = nil - p.buffer = nil - p.payload = nil - } -} diff --git a/vendor/github.com/pion/interceptor/pkg/nack/send_buffer.go b/vendor/github.com/pion/interceptor/pkg/nack/send_buffer.go deleted file mode 100644 index 2b3b076f5f..0000000000 --- a/vendor/github.com/pion/interceptor/pkg/nack/send_buffer.go +++ /dev/null @@ -1,104 +0,0 @@ -// SPDX-FileCopyrightText: 2023 The Pion community -// SPDX-License-Identifier: MIT - -package nack - -import ( - "fmt" - "sync" -) - -const ( - uint16SizeHalf = 1 << 15 -) - -type sendBuffer struct { - packets []*retainablePacket - size uint16 - lastAdded uint16 - started bool - - m sync.RWMutex -} - -func newSendBuffer(size uint16) (*sendBuffer, error) { - allowedSizes := make([]uint16, 0) - correctSize := false - for i := 0; i < 16; i++ { - if size == 1<= uint16SizeHalf { - return nil - } - - if diff >= s.size { - return nil - } - - pkt := s.packets[seq%s.size] - if pkt != nil { - if pkt.sequenceNumber != seq { - return nil - } - // already released - if err := pkt.Retain(); err != nil { - return nil - } - } - return pkt -} diff --git a/vendor/github.com/pion/interceptor/pkg/report/receiver_interceptor.go b/vendor/github.com/pion/interceptor/pkg/report/receiver_interceptor.go index 0afbd08f5b..91b513c550 100644 --- a/vendor/github.com/pion/interceptor/pkg/report/receiver_interceptor.go +++ b/vendor/github.com/pion/interceptor/pkg/report/receiver_interceptor.go @@ -12,14 +12,14 @@ import ( "github.com/pion/rtcp" ) -// ReceiverInterceptorFactory is a interceptor.Factory for a ReceiverInterceptor +// ReceiverInterceptorFactory is a interceptor.Factory for a ReceiverInterceptor. type ReceiverInterceptorFactory struct { opts []ReceiverOption } -// NewInterceptor constructs a new ReceiverInterceptor +// NewInterceptor constructs a new ReceiverInterceptor. func (r *ReceiverInterceptorFactory) NewInterceptor(_ string) (interceptor.Interceptor, error) { - i := &ReceiverInterceptor{ + receiverInterceptor := &ReceiverInterceptor{ interval: 1 * time.Second, now: time.Now, log: logging.NewDefaultLoggerFactory().NewLogger("receiver_interceptor"), @@ -27,15 +27,15 @@ func (r *ReceiverInterceptorFactory) NewInterceptor(_ string) (interceptor.Inter } for _, opt := range r.opts { - if err := opt(i); err != nil { + if err := opt(receiverInterceptor); err != nil { return nil, err } } - return i, nil + return receiverInterceptor, nil } -// NewReceiverInterceptor returns a new ReceiverInterceptorFactory +// NewReceiverInterceptor returns a new ReceiverInterceptorFactory. func NewReceiverInterceptor(opts ...ReceiverOption) (*ReceiverInterceptorFactory, error) { return &ReceiverInterceptorFactory{opts}, nil } @@ -103,7 +103,9 @@ func (r *ReceiverInterceptor) loop(rtcpWriter interceptor.RTCPWriter) { r.streams.Range(func(_, value interface{}) bool { if stream, ok := value.(*receiverStream); !ok { r.log.Warnf("failed to cast ReceiverInterceptor stream") - } else if _, err := rtcpWriter.Write([]rtcp.Packet{stream.generateReport(now)}, interceptor.Attributes{}); err != nil { + } else if _, err := rtcpWriter.Write( + []rtcp.Packet{stream.generateReport(now)}, interceptor.Attributes{}, + ); err != nil { r.log.Warnf("failed sending: %+v", err) } @@ -116,9 +118,11 @@ func (r *ReceiverInterceptor) loop(rtcpWriter interceptor.RTCPWriter) { } } -// BindRemoteStream lets you modify any incoming RTP packets. It is called once for per RemoteStream. The returned method -// will be called once per rtp packet. -func (r *ReceiverInterceptor) BindRemoteStream(info *interceptor.StreamInfo, reader interceptor.RTPReader) interceptor.RTPReader { +// BindRemoteStream lets you modify any incoming RTP packets. It is called once for per RemoteStream. +// The returned method will be called once per rtp packet. +func (r *ReceiverInterceptor) BindRemoteStream( + info *interceptor.StreamInfo, reader interceptor.RTPReader, +) interceptor.RTPReader { stream := newReceiverStream(info.SSRC, info.ClockRate) r.streams.Store(info.SSRC, stream) diff --git a/vendor/github.com/pion/interceptor/pkg/report/receiver_option.go b/vendor/github.com/pion/interceptor/pkg/report/receiver_option.go index 337a341421..4a91561d9e 100644 --- a/vendor/github.com/pion/interceptor/pkg/report/receiver_option.go +++ b/vendor/github.com/pion/interceptor/pkg/report/receiver_option.go @@ -16,6 +16,7 @@ type ReceiverOption func(r *ReceiverInterceptor) error func ReceiverLog(log logging.LeveledLogger) ReceiverOption { return func(r *ReceiverInterceptor) error { r.log = log + return nil } } @@ -24,6 +25,7 @@ func ReceiverLog(log logging.LeveledLogger) ReceiverOption { func ReceiverInterval(interval time.Duration) ReceiverOption { return func(r *ReceiverInterceptor) error { r.interval = interval + return nil } } @@ -32,6 +34,7 @@ func ReceiverInterval(interval time.Duration) ReceiverOption { func ReceiverNow(f func() time.Time) ReceiverOption { return func(r *ReceiverInterceptor) error { r.now = f + return nil } } diff --git a/vendor/github.com/pion/interceptor/pkg/report/receiver_stream.go b/vendor/github.com/pion/interceptor/pkg/report/receiver_stream.go index ebe08473d2..92913538b7 100644 --- a/vendor/github.com/pion/interceptor/pkg/report/receiver_stream.go +++ b/vendor/github.com/pion/interceptor/pkg/report/receiver_stream.go @@ -41,6 +41,7 @@ type receiverStream struct { func newReceiverStream(ssrc uint32, clockRate uint32) *receiverStream { receiverSSRC := rand.Uint32() // #nosec + return &receiverStream{ ssrc: ssrc, receiverSSRC: receiverSSRC, @@ -54,6 +55,7 @@ func (stream *receiverStream) processRTP(now time.Time, pktHeader *rtp.Header) { stream.m.Lock() defer stream.m.Unlock() + //nolint:nestif if !stream.started { // first frame stream.started = true stream.setReceived(pktHeader.SequenceNumber) @@ -104,6 +106,7 @@ func (stream *receiverStream) delReceived(seq uint16) { func (stream *receiverStream) getReceived(seq uint16) bool { pos := seq % (stream.size * packetsPerHistoryEntry) + return (stream.packets[pos/packetsPerHistoryEntry] & (1 << (pos % packetsPerHistoryEntry))) != 0 } @@ -111,7 +114,7 @@ func (stream *receiverStream) processSenderReport(now time.Time, sr *rtcp.Sender stream.m.Lock() defer stream.m.Unlock() - stream.lastSenderReport = uint32(sr.NTPTime >> 16) + stream.lastSenderReport = uint32(sr.NTPTime >> 16) //nolint:gosec // G115 stream.lastSenderReportTime = now } @@ -131,6 +134,7 @@ func (stream *receiverStream) generateReport(now time.Time) *rtcp.ReceiverReport ret++ } } + return ret }() stream.totalLost += totalLostSinceReport @@ -143,7 +147,7 @@ func (stream *receiverStream) generateReport(now time.Time) *rtcp.ReceiverReport stream.totalLost = 0xFFFFFF } - r := &rtcp.ReceiverReport{ + receiverReport := &rtcp.ReceiverReport{ SSRC: stream.receiverSSRC, Reports: []rtcp.ReceptionReport{ { @@ -156,6 +160,7 @@ func (stream *receiverStream) generateReport(now time.Time) *rtcp.ReceiverReport if stream.lastSenderReportTime.IsZero() { return 0 } + return uint32(now.Sub(stream.lastSenderReportTime).Seconds() * 65536) }(), Jitter: uint32(stream.jitter), @@ -165,5 +170,5 @@ func (stream *receiverStream) generateReport(now time.Time) *rtcp.ReceiverReport stream.lastReportSeqnum = stream.lastSeqnum - return r + return receiverReport } diff --git a/vendor/github.com/pion/interceptor/pkg/report/sender_interceptor.go b/vendor/github.com/pion/interceptor/pkg/report/sender_interceptor.go index 1b6f4b2521..40c7f9f7a9 100644 --- a/vendor/github.com/pion/interceptor/pkg/report/sender_interceptor.go +++ b/vendor/github.com/pion/interceptor/pkg/report/sender_interceptor.go @@ -13,17 +13,17 @@ import ( "github.com/pion/rtp" ) -// TickerFactory is a factory to create new tickers +// TickerFactory is a factory to create new tickers. type TickerFactory func(d time.Duration) Ticker -// SenderInterceptorFactory is a interceptor.Factory for a SenderInterceptor +// SenderInterceptorFactory is a interceptor.Factory for a SenderInterceptor. type SenderInterceptorFactory struct { opts []SenderOption } -// NewInterceptor constructs a new SenderInterceptor +// NewInterceptor constructs a new SenderInterceptor. func (s *SenderInterceptorFactory) NewInterceptor(_ string) (interceptor.Interceptor, error) { - i := &SenderInterceptor{ + senderInterceptor := &SenderInterceptor{ interval: 1 * time.Second, now: time.Now, newTicker: func(d time.Duration) Ticker { @@ -34,15 +34,15 @@ func (s *SenderInterceptorFactory) NewInterceptor(_ string) (interceptor.Interce } for _, opt := range s.opts { - if err := opt(i); err != nil { + if err := opt(senderInterceptor); err != nil { return nil, err } } - return i, nil + return senderInterceptor, nil } -// NewSenderInterceptor returns a new SenderInterceptorFactory +// NewSenderInterceptor returns a new SenderInterceptorFactory. func NewSenderInterceptor(opts ...SenderOption) (*SenderInterceptorFactory, error) { return &SenderInterceptorFactory{opts}, nil } @@ -119,7 +119,9 @@ func (s *SenderInterceptor) loop(rtcpWriter interceptor.RTCPWriter) { s.streams.Range(func(_, value interface{}) bool { if stream, ok := value.(*senderStream); !ok { s.log.Warnf("failed to cast SenderInterceptor stream") - } else if _, err := rtcpWriter.Write([]rtcp.Packet{stream.generateReport(now)}, interceptor.Attributes{}); err != nil { + } else if _, err := rtcpWriter.Write( + []rtcp.Packet{stream.generateReport(now)}, interceptor.Attributes{}, + ); err != nil { s.log.Warnf("failed sending: %+v", err) } @@ -134,7 +136,9 @@ func (s *SenderInterceptor) loop(rtcpWriter interceptor.RTCPWriter) { // BindLocalStream lets you modify any outgoing RTP packets. It is called once for per LocalStream. The returned method // will be called once per rtp packet. -func (s *SenderInterceptor) BindLocalStream(info *interceptor.StreamInfo, writer interceptor.RTPWriter) interceptor.RTPWriter { +func (s *SenderInterceptor) BindLocalStream( + info *interceptor.StreamInfo, writer interceptor.RTPWriter, +) interceptor.RTPWriter { stream := newSenderStream(info.SSRC, info.ClockRate, s.useLatestPacket) s.streams.Store(info.SSRC, stream) diff --git a/vendor/github.com/pion/interceptor/pkg/report/sender_option.go b/vendor/github.com/pion/interceptor/pkg/report/sender_option.go index 1e489b0b36..07a4f0875c 100644 --- a/vendor/github.com/pion/interceptor/pkg/report/sender_option.go +++ b/vendor/github.com/pion/interceptor/pkg/report/sender_option.go @@ -16,6 +16,7 @@ type SenderOption func(r *SenderInterceptor) error func SenderLog(log logging.LeveledLogger) SenderOption { return func(r *SenderInterceptor) error { r.log = log + return nil } } @@ -24,6 +25,7 @@ func SenderLog(log logging.LeveledLogger) SenderOption { func SenderInterval(interval time.Duration) SenderOption { return func(r *SenderInterceptor) error { r.interval = interval + return nil } } @@ -32,6 +34,7 @@ func SenderInterval(interval time.Duration) SenderOption { func SenderNow(f func() time.Time) SenderOption { return func(r *SenderInterceptor) error { r.now = f + return nil } } @@ -40,6 +43,7 @@ func SenderNow(f func() time.Time) SenderOption { func SenderTicker(f TickerFactory) SenderOption { return func(r *SenderInterceptor) error { r.newTicker = f + return nil } } @@ -49,6 +53,7 @@ func SenderTicker(f TickerFactory) SenderOption { func SenderUseLatestPacket() SenderOption { return func(r *SenderInterceptor) error { r.useLatestPacket = true + return nil } } @@ -58,6 +63,7 @@ func SenderUseLatestPacket() SenderOption { func enableStartTracking(startedCh chan struct{}) SenderOption { return func(r *SenderInterceptor) error { r.started = startedCh + return nil } } diff --git a/vendor/github.com/pion/interceptor/pkg/report/sender_stream.go b/vendor/github.com/pion/interceptor/pkg/report/sender_stream.go index 44658e2464..6bb37dbff6 100644 --- a/vendor/github.com/pion/interceptor/pkg/report/sender_stream.go +++ b/vendor/github.com/pion/interceptor/pkg/report/sender_stream.go @@ -48,7 +48,7 @@ func (stream *senderStream) processRTP(now time.Time, header *rtp.Header, payloa } stream.packetCount++ - stream.octetCount += uint32(len(payload)) + stream.octetCount += uint32(len(payload)) //nolint:gosec // G115 } func (stream *senderStream) generateReport(now time.Time) *rtcp.SenderReport { diff --git a/vendor/github.com/pion/interceptor/pkg/rfc8888/interceptor.go b/vendor/github.com/pion/interceptor/pkg/rfc8888/interceptor.go index efe2df29c3..344ec1be17 100644 --- a/vendor/github.com/pion/interceptor/pkg/rfc8888/interceptor.go +++ b/vendor/github.com/pion/interceptor/pkg/rfc8888/interceptor.go @@ -14,17 +14,17 @@ import ( "github.com/pion/rtcp" ) -// TickerFactory is a factory to create new tickers +// TickerFactory is a factory to create new tickers. type TickerFactory func(d time.Duration) ticker -// SenderInterceptorFactory is a interceptor.Factory for a SenderInterceptor +// SenderInterceptorFactory is a interceptor.Factory for a SenderInterceptor. type SenderInterceptorFactory struct { opts []Option } -// NewInterceptor constructs a new SenderInterceptor +// NewInterceptor constructs a new SenderInterceptor. func (s *SenderInterceptorFactory) NewInterceptor(_ string) (interceptor.Interceptor, error) { - i := &SenderInterceptor{ + senderInterceptor := &SenderInterceptor{ NoOp: interceptor.NoOp{}, log: logging.NewDefaultLoggerFactory().NewLogger("rfc8888_interceptor"), lock: sync.Mutex{}, @@ -40,12 +40,13 @@ func (s *SenderInterceptorFactory) NewInterceptor(_ string) (interceptor.Interce close: make(chan struct{}), } for _, opt := range s.opts { - err := opt(i) + err := opt(senderInterceptor) if err != nil { return nil, err } } - return i, nil + + return senderInterceptor, nil } // NewSenderInterceptor returns a new SenderInterceptorFactory configured with the given options. @@ -91,9 +92,12 @@ func (s *SenderInterceptor) BindRTCPWriter(writer interceptor.RTCPWriter) interc return writer } -// BindRemoteStream lets you modify any incoming RTP packets. It is called once for per RemoteStream. The returned method -// will be called once per rtp packet. -func (s *SenderInterceptor) BindRemoteStream(_ *interceptor.StreamInfo, reader interceptor.RTPReader) interceptor.RTPReader { +// BindRemoteStream lets you modify any incoming RTP packets. +// It is called once for per RemoteStream. The returned method +// will be called once per rtp packet.. +func (s *SenderInterceptor) BindRemoteStream( + _ *interceptor.StreamInfo, reader interceptor.RTPReader, +) interceptor.RTPReader { return interceptor.RTPReaderFunc(func(b []byte, a interceptor.Attributes) (int, interceptor.Attributes, error) { i, attr, err := reader.Read(b, a) if err != nil { @@ -115,6 +119,7 @@ func (s *SenderInterceptor) BindRemoteStream(_ *interceptor.StreamInfo, reader i ecn: 0, // ECN is not supported (yet). } s.packetChan <- p + return i, attr, nil }) } @@ -157,16 +162,19 @@ func (s *SenderInterceptor) loop(writer interceptor.RTCPWriter) { select { case <-s.close: t.Stop() + return case pkt := <-s.packetChan: s.log.Tracef("got packet: %v", pkt) s.recorder.AddPacket(pkt.arrival, pkt.ssrc, pkt.sequenceNumber, pkt.ecn) - case now := <-t.Ch(): + case <-t.Ch(): + now := s.now() s.log.Tracef("report triggered at %v", now) if writer == nil { s.log.Trace("no writer added, continue") + continue } pkts := s.recorder.BuildReport(now, int(s.maxReportSize)) diff --git a/vendor/github.com/pion/interceptor/pkg/rfc8888/option.go b/vendor/github.com/pion/interceptor/pkg/rfc8888/option.go index 1214868bf3..236073b5d2 100644 --- a/vendor/github.com/pion/interceptor/pkg/rfc8888/option.go +++ b/vendor/github.com/pion/interceptor/pkg/rfc8888/option.go @@ -5,13 +5,14 @@ package rfc8888 import "time" -// An Option is a function that can be used to configure a SenderInterceptor +// An Option is a function that can be used to configure a SenderInterceptor. type Option func(*SenderInterceptor) error // SenderTicker sets an alternative for time.Ticker. func SenderTicker(f TickerFactory) Option { return func(i *SenderInterceptor) error { i.newTicker = f + return nil } } @@ -20,14 +21,16 @@ func SenderTicker(f TickerFactory) Option { func SenderNow(f func() time.Time) Option { return func(i *SenderInterceptor) error { i.now = f + return nil } } -// SendInterval sets the feedback send interval for the interceptor +// SendInterval sets the feedback send interval for the interceptor. func SendInterval(interval time.Duration) Option { return func(s *SenderInterceptor) error { s.interval = interval + return nil } } diff --git a/vendor/github.com/pion/interceptor/pkg/rfc8888/recorder.go b/vendor/github.com/pion/interceptor/pkg/rfc8888/recorder.go index 4423df1e30..a1a2cf266e 100644 --- a/vendor/github.com/pion/interceptor/pkg/rfc8888/recorder.go +++ b/vendor/github.com/pion/interceptor/pkg/rfc8888/recorder.go @@ -6,6 +6,7 @@ package rfc8888 import ( "time" + "github.com/pion/interceptor/internal/ntp" "github.com/pion/rtcp" ) @@ -21,7 +22,7 @@ type Recorder struct { streams map[uint32]*streamLog } -// NewRecorder creates a new Recorder +// NewRecorder creates a new Recorder. func NewRecorder() *Recorder { return &Recorder{ streams: map[uint32]*streamLog{}, @@ -44,7 +45,7 @@ func (r *Recorder) BuildReport(now time.Time, maxSize int) *rtcp.CCFeedbackRepor report := &rtcp.CCFeedbackReport{ SenderSSRC: r.ssrc, ReportBlocks: []rtcp.CCFeedbackReportBlock{}, - ReportTimestamp: ntpTime32(now), + ReportTimestamp: ntp.ToNTP32(now), } maxReportBlocks := (maxSize - 12 - (8 * len(r.streams))) / 2 @@ -65,14 +66,3 @@ func (r *Recorder) BuildReport(now time.Time, maxSize int) *rtcp.CCFeedbackRepor return report } - -func ntpTime32(t time.Time) uint32 { - // seconds since 1st January 1900 - s := (float64(t.UnixNano()) / 1000000000.0) + 2208988800 - - integerPart := uint32(s) - fractionalPart := uint32((s - float64(integerPart)) * 0xFFFFFFFF) - - // higher 32 bits are the integer part, lower 32 bits are the fractional part - return uint32(((uint64(integerPart)<<32 | uint64(fractionalPart)) >> 16) & 0xFFFFFFFF) -} diff --git a/vendor/github.com/pion/interceptor/pkg/rfc8888/stream_log.go b/vendor/github.com/pion/interceptor/pkg/rfc8888/stream_log.go index 8dfb14fc96..ed41f9528c 100644 --- a/vendor/github.com/pion/interceptor/pkg/rfc8888/stream_log.go +++ b/vendor/github.com/pion/interceptor/pkg/rfc8888/stream_log.go @@ -6,6 +6,7 @@ package rfc8888 import ( "time" + "github.com/pion/interceptor/internal/sequencenumber" "github.com/pion/rtcp" ) @@ -13,7 +14,7 @@ const maxReportsPerReportBlock = 16384 type streamLog struct { ssrc uint32 - sequence unwrapper + sequence sequencenumber.Unwrapper init bool nextSequenceNumberToReport int64 // next to report lastSequenceNumberReceived int64 // highest received @@ -23,7 +24,7 @@ type streamLog struct { func newStreamLog(ssrc uint32) *streamLog { return &streamLog{ ssrc: ssrc, - sequence: unwrapper{}, + sequence: sequencenumber.Unwrapper{}, init: false, nextSequenceNumberToReport: 0, lastSequenceNumberReceived: 0, @@ -32,7 +33,7 @@ func newStreamLog(ssrc uint32) *streamLog { } func (l *streamLog) add(ts time.Time, sequenceNumber uint16, ecn uint8) { - unwrappedSequenceNumber := l.sequence.unwrap(sequenceNumber) + unwrappedSequenceNumber := l.sequence.Unwrap(sequenceNumber) if !l.init { l.init = true l.nextSequenceNumberToReport = unwrappedSequenceNumber @@ -52,7 +53,7 @@ func (l *streamLog) metricsAfter(reference time.Time, maxReportBlocks int64) rtc if len(l.log) == 0 { return rtcp.CCFeedbackReportBlock{ MediaSSRC: l.ssrc, - BeginSequence: uint16(l.nextSequenceNumberToReport), + BeginSequence: uint16(l.nextSequenceNumberToReport), //nolint:gosec // G115 MetricBlocks: []rtcp.CCFeedbackMetricBlock{}, } } @@ -65,7 +66,7 @@ func (l *streamLog) metricsAfter(reference time.Time, maxReportBlocks int64) rtc offset := l.nextSequenceNumberToReport lastReceived := l.nextSequenceNumberToReport gapDetected := false - for i := offset; i <= l.lastSequenceNumberReceived; i++ { + for i := offset; i <= l.lastSequenceNumberReceived; i++ { //nolint:varnamelen // i int64 received := false ecn := uint8(0) ato := uint16(0) @@ -91,9 +92,10 @@ func (l *streamLog) metricsAfter(reference time.Time, maxReportBlocks int64) rtc } } } + return rtcp.CCFeedbackReportBlock{ MediaSSRC: l.ssrc, - BeginSequence: uint16(offset), + BeginSequence: uint16(offset), //nolint:gosec // G115 MetricBlocks: metricBlocks, } } @@ -106,5 +108,6 @@ func getArrivalTimeOffset(base time.Time, arrival time.Time) uint16 { if ato > 0x1FFD { return 0x1FFE } + return ato } diff --git a/vendor/github.com/pion/interceptor/pkg/rfc8888/unwrapper.go b/vendor/github.com/pion/interceptor/pkg/rfc8888/unwrapper.go deleted file mode 100644 index f15f33e6ef..0000000000 --- a/vendor/github.com/pion/interceptor/pkg/rfc8888/unwrapper.go +++ /dev/null @@ -1,42 +0,0 @@ -// SPDX-FileCopyrightText: 2023 The Pion community -// SPDX-License-Identifier: MIT - -package rfc8888 - -const ( - maxSequenceNumberPlusOne = int64(65536) - breakpoint = 32768 // half of max uint16 -) - -type unwrapper struct { - init bool - lastUnwrapped int64 -} - -func isNewer(value, previous uint16) bool { - if value-previous == breakpoint { - return value > previous - } - return value != previous && (value-previous) < breakpoint -} - -func (u *unwrapper) unwrap(i uint16) int64 { - if !u.init { - u.init = true - u.lastUnwrapped = int64(i) - return u.lastUnwrapped - } - - lastWrapped := uint16(u.lastUnwrapped) - delta := int64(i - lastWrapped) - if isNewer(i, lastWrapped) { - if delta < 0 { - delta += maxSequenceNumberPlusOne - } - } else if delta > 0 && u.lastUnwrapped+delta-maxSequenceNumberPlusOne >= 0 { - delta -= maxSequenceNumberPlusOne - } - - u.lastUnwrapped += delta - return u.lastUnwrapped -} diff --git a/vendor/github.com/pion/interceptor/pkg/twcc/arrival_time_map.go b/vendor/github.com/pion/interceptor/pkg/twcc/arrival_time_map.go index d3e2a8af36..a496ad73ce 100644 --- a/vendor/github.com/pion/interceptor/pkg/twcc/arrival_time_map.go +++ b/vendor/github.com/pion/interceptor/pkg/twcc/arrival_time_map.go @@ -12,6 +12,8 @@ const ( // of the arrival times of packets. It is used by the TWCC interceptor to build feedback // packets. // See https://source.chromium.org/chromium/chromium/src/+/refs/heads/main:third_party/webrtc/modules/remote_bitrate_estimator/packet_arrival_map.h;drc=b5cd13bb6d5d157a5fbe3628b2dd1c1e106203c6 +// +//nolint:lll type packetArrivalTimeMap struct { // arrivalTimes is a circular buffer, where the packet with sequence number sn is stored // in slot sn % len(arrivalTimes) @@ -31,12 +33,14 @@ func (m *packetArrivalTimeMap) AddPacket(sequenceNumber int64, arrivalTime int64 m.beginSequenceNumber = sequenceNumber m.endSequenceNumber = sequenceNumber + 1 m.arrivalTimes[m.index(sequenceNumber)] = arrivalTime + return } if sequenceNumber >= m.beginSequenceNumber && sequenceNumber < m.endSequenceNumber { // The packet is within the buffer, no need to resize. m.arrivalTimes[m.index(sequenceNumber)] = arrivalTime + return } @@ -53,6 +57,7 @@ func (m *packetArrivalTimeMap) AddPacket(sequenceNumber int64, arrivalTime int64 m.arrivalTimes[m.index(sequenceNumber)] = arrivalTime m.setNotReceived(sequenceNumber+1, m.beginSequenceNumber) m.beginSequenceNumber = sequenceNumber + return } @@ -64,6 +69,7 @@ func (m *packetArrivalTimeMap) AddPacket(sequenceNumber int64, arrivalTime int64 m.beginSequenceNumber = sequenceNumber m.endSequenceNumber = newEndSequenceNumber m.arrivalTimes[m.index(sequenceNumber)] = arrivalTime + return } @@ -99,12 +105,15 @@ func (m *packetArrivalTimeMap) EndSequenceNumber() int64 { // FindNextAtOrAfter returns the sequence number and timestamp of the first received packet that has a sequence number // greator or equal to sequenceNumber. -func (m *packetArrivalTimeMap) FindNextAtOrAfter(sequenceNumber int64) (foundSequenceNumber int64, arrivalTime int64, ok bool) { +func (m *packetArrivalTimeMap) FindNextAtOrAfter(sequenceNumber int64) ( + foundSequenceNumber int64, arrivalTime int64, ok bool, +) { for sequenceNumber = m.Clamp(sequenceNumber); sequenceNumber < m.endSequenceNumber; sequenceNumber++ { if t := m.get(sequenceNumber); t >= 0 { return sequenceNumber, t, true } } + return -1, -1, false } @@ -116,6 +125,7 @@ func (m *packetArrivalTimeMap) EraseTo(sequenceNumber int64) { if sequenceNumber >= m.endSequenceNumber { // Erase all. m.beginSequenceNumber = m.endSequenceNumber + return } // Remove some @@ -138,7 +148,7 @@ func (m *packetArrivalTimeMap) HasReceived(sequenceNumber int64) bool { return m.get(sequenceNumber) >= 0 } -// Clamp returns sequenceNumber clamped to [beginSequenceNumber, endSequenceNumber] +// Clamp returns sequenceNumber clamped to [beginSequenceNumber, endSequenceNumber]. func (m *packetArrivalTimeMap) Clamp(sequenceNumber int64) int64 { if sequenceNumber < m.beginSequenceNumber { return m.beginSequenceNumber @@ -146,6 +156,7 @@ func (m *packetArrivalTimeMap) Clamp(sequenceNumber int64) int64 { if m.endSequenceNumber < sequenceNumber { return m.endSequenceNumber } + return sequenceNumber } @@ -153,6 +164,7 @@ func (m *packetArrivalTimeMap) get(sequenceNumber int64) int64 { if sequenceNumber < m.beginSequenceNumber || sequenceNumber >= m.endSequenceNumber { return -1 } + return m.arrivalTimes[m.index(sequenceNumber)] } diff --git a/vendor/github.com/pion/interceptor/pkg/twcc/header_extension_interceptor.go b/vendor/github.com/pion/interceptor/pkg/twcc/header_extension_interceptor.go index 791b145923..52d9e84aa6 100644 --- a/vendor/github.com/pion/interceptor/pkg/twcc/header_extension_interceptor.go +++ b/vendor/github.com/pion/interceptor/pkg/twcc/header_extension_interceptor.go @@ -13,20 +13,20 @@ import ( var errHeaderIsNil = errors.New("header is nil") -// HeaderExtensionInterceptorFactory is a interceptor.Factory for a HeaderExtensionInterceptor +// HeaderExtensionInterceptorFactory is a interceptor.Factory for a HeaderExtensionInterceptor. type HeaderExtensionInterceptorFactory struct{} -// NewInterceptor constructs a new HeaderExtensionInterceptor +// NewInterceptor constructs a new HeaderExtensionInterceptor. func (h *HeaderExtensionInterceptorFactory) NewInterceptor(_ string) (interceptor.Interceptor, error) { return &HeaderExtensionInterceptor{}, nil } -// NewHeaderExtensionInterceptor returns a HeaderExtensionInterceptorFactory +// NewHeaderExtensionInterceptor returns a HeaderExtensionInterceptorFactory. func NewHeaderExtensionInterceptor() (*HeaderExtensionInterceptorFactory, error) { return &HeaderExtensionInterceptorFactory{}, nil } -// HeaderExtensionInterceptor adds transport wide sequence numbers as header extension to each RTP packet +// HeaderExtensionInterceptor adds transport wide sequence numbers as header extension to each RTP packet. type HeaderExtensionInterceptor struct { interceptor.NoOp nextSequenceNr uint32 @@ -36,31 +36,39 @@ const transportCCURI = "http://www.ietf.org/id/draft-holmer-rmcat-transport-wide // BindLocalStream returns a writer that adds a rtp.TransportCCExtension // header with increasing sequence numbers to each outgoing packet. -func (h *HeaderExtensionInterceptor) BindLocalStream(info *interceptor.StreamInfo, writer interceptor.RTPWriter) interceptor.RTPWriter { +func (h *HeaderExtensionInterceptor) BindLocalStream( + info *interceptor.StreamInfo, + writer interceptor.RTPWriter, +) interceptor.RTPWriter { var hdrExtID uint8 for _, e := range info.RTPHeaderExtensions { if e.URI == transportCCURI { - hdrExtID = uint8(e.ID) + hdrExtID = uint8(e.ID) //nolint:gosec // G115 + break } } if hdrExtID == 0 { // Don't add header extension if ID is 0, because 0 is an invalid extension ID return writer } - return interceptor.RTPWriterFunc(func(header *rtp.Header, payload []byte, attributes interceptor.Attributes) (int, error) { - sequenceNumber := atomic.AddUint32(&h.nextSequenceNr, 1) - 1 - tcc, err := (&rtp.TransportCCExtension{TransportSequence: uint16(sequenceNumber)}).Marshal() - if err != nil { - return 0, err - } - if header == nil { - return 0, errHeaderIsNil - } - err = header.SetExtension(hdrExtID, tcc) - if err != nil { - return 0, err - } - return writer.Write(header, payload, attributes) - }) + return interceptor.RTPWriterFunc( + func(header *rtp.Header, payload []byte, attributes interceptor.Attributes) (int, error) { + sequenceNumber := atomic.AddUint32(&h.nextSequenceNr, 1) - 1 + //nolint:gosec // G115 + tcc, err := (&rtp.TransportCCExtension{TransportSequence: uint16(sequenceNumber)}).Marshal() + if err != nil { + return 0, err + } + if header == nil { + return 0, errHeaderIsNil + } + err = header.SetExtension(hdrExtID, tcc) + if err != nil { + return 0, err + } + + return writer.Write(header, payload, attributes) + }, + ) } diff --git a/vendor/github.com/pion/interceptor/pkg/twcc/sender_interceptor.go b/vendor/github.com/pion/interceptor/pkg/twcc/sender_interceptor.go index d7906fc682..229330ad44 100644 --- a/vendor/github.com/pion/interceptor/pkg/twcc/sender_interceptor.go +++ b/vendor/github.com/pion/interceptor/pkg/twcc/sender_interceptor.go @@ -14,16 +14,16 @@ import ( "github.com/pion/rtp" ) -// SenderInterceptorFactory is a interceptor.Factory for a SenderInterceptor +// SenderInterceptorFactory is a interceptor.Factory for a SenderInterceptor. type SenderInterceptorFactory struct { opts []Option } var errClosed = errors.New("interceptor is closed") -// NewInterceptor constructs a new SenderInterceptor +// NewInterceptor constructs a new SenderInterceptor. func (s *SenderInterceptorFactory) NewInterceptor(_ string) (interceptor.Interceptor, error) { - i := &SenderInterceptor{ + senderInterceptor := &SenderInterceptor{ log: logging.NewDefaultLoggerFactory().NewLogger("twcc_sender_interceptor"), packetChan: make(chan packet), close: make(chan struct{}), @@ -32,13 +32,13 @@ func (s *SenderInterceptorFactory) NewInterceptor(_ string) (interceptor.Interce } for _, opt := range s.opts { - err := opt(i) + err := opt(senderInterceptor) if err != nil { return nil, err } } - return i, nil + return senderInterceptor, nil } // NewSenderInterceptor returns a new SenderInterceptorFactory configured with the given options. @@ -64,7 +64,7 @@ type SenderInterceptor struct { packetChan chan packet } -// An Option is a function that can be used to configure a SenderInterceptor +// An Option is a function that can be used to configure a SenderInterceptor. type Option func(*SenderInterceptor) error // SendInterval sets the interval at which the interceptor @@ -72,6 +72,7 @@ type Option func(*SenderInterceptor) error func SendInterval(interval time.Duration) Option { return func(s *SenderInterceptor) error { s.interval = interval + return nil } } @@ -102,54 +103,63 @@ type packet struct { ssrc uint32 } -// BindRemoteStream lets you modify any incoming RTP packets. It is called once for per RemoteStream. The returned method +// BindRemoteStream lets you modify any incoming RTP packets. +// It is called once for per RemoteStream. The returned method // will be called once per rtp packet. -func (s *SenderInterceptor) BindRemoteStream(info *interceptor.StreamInfo, reader interceptor.RTPReader) interceptor.RTPReader { +// +//nolint:cyclop +func (s *SenderInterceptor) BindRemoteStream( + info *interceptor.StreamInfo, reader interceptor.RTPReader, +) interceptor.RTPReader { var hdrExtID uint8 for _, e := range info.RTPHeaderExtensions { if e.URI == transportCCURI { - hdrExtID = uint8(e.ID) + hdrExtID = uint8(e.ID) //nolint:gosec // G115 + break } } if hdrExtID == 0 { // Don't try to read header extension if ID is 0, because 0 is an invalid extension ID return reader } - return interceptor.RTPReaderFunc(func(buf []byte, attributes interceptor.Attributes) (int, interceptor.Attributes, error) { - i, attr, err := reader.Read(buf, attributes) - if err != nil { - return 0, nil, err - } - if attr == nil { - attr = make(interceptor.Attributes) - } - header, err := attr.GetRTPHeader(buf[:i]) - if err != nil { - return 0, nil, err - } - var tccExt rtp.TransportCCExtension - if ext := header.GetExtension(hdrExtID); ext != nil { - err = tccExt.Unmarshal(ext) + return interceptor.RTPReaderFunc( + func(buf []byte, attributes interceptor.Attributes) (int, interceptor.Attributes, error) { + i, attr, err := reader.Read(buf, attributes) if err != nil { return 0, nil, err } - p := packet{ - hdr: header, - sequenceNumber: tccExt.TransportSequence, - arrivalTime: time.Since(s.startTime).Microseconds(), - ssrc: info.SSRC, + if attr == nil { + attr = make(interceptor.Attributes) + } + header, err := attr.GetRTPHeader(buf[:i]) + if err != nil { + return 0, nil, err } - select { - case <-s.close: - return 0, nil, errClosed - case s.packetChan <- p: + var tccExt rtp.TransportCCExtension + if ext := header.GetExtension(hdrExtID); ext != nil { + err = tccExt.Unmarshal(ext) + if err != nil { + return 0, nil, err + } + + p := packet{ + hdr: header, + sequenceNumber: tccExt.TransportSequence, + arrivalTime: time.Since(s.startTime).Microseconds(), + ssrc: info.SSRC, + } + select { + case <-s.close: + return 0, nil, errClosed + case s.packetChan <- p: + } } - } - return i, attr, nil - }) + return i, attr, nil + }, + ) } // Close closes the interceptor. @@ -174,7 +184,7 @@ func (s *SenderInterceptor) isClosed() bool { } } -func (s *SenderInterceptor) loop(w interceptor.RTCPWriter) { +func (s *SenderInterceptor) loop(writer interceptor.RTCPWriter) { defer s.wg.Done() select { @@ -189,6 +199,7 @@ func (s *SenderInterceptor) loop(w interceptor.RTCPWriter) { select { case <-s.close: ticker.Stop() + return case p := <-s.packetChan: s.recorder.Record(p.ssrc, p.sequenceNumber, p.arrivalTime) @@ -199,7 +210,7 @@ func (s *SenderInterceptor) loop(w interceptor.RTCPWriter) { if len(pkts) == 0 { continue } - if _, err := w.Write(pkts, nil); err != nil { + if _, err := writer.Write(pkts, nil); err != nil { s.log.Error(err.Error()) } } diff --git a/vendor/github.com/pion/interceptor/pkg/twcc/twcc.go b/vendor/github.com/pion/interceptor/pkg/twcc/twcc.go index 6464c77bcf..750e4541d6 100644 --- a/vendor/github.com/pion/interceptor/pkg/twcc/twcc.go +++ b/vendor/github.com/pion/interceptor/pkg/twcc/twcc.go @@ -71,12 +71,13 @@ func (r *Recorder) Record(mediaSSRC uint32, sequenceNumber uint16, arrivalTime i } func (r *Recorder) maybeCullOldPackets(sequenceNumber int64, arrivalTime int64) { - if r.startSequenceNumber != nil && *r.startSequenceNumber >= r.arrivalTimeMap.EndSequenceNumber() && arrivalTime >= packetWindowMicroseconds { + if r.startSequenceNumber != nil && *r.startSequenceNumber >= r.arrivalTimeMap.EndSequenceNumber() && + arrivalTime >= packetWindowMicroseconds { r.arrivalTimeMap.RemoveOldPackets(sequenceNumber, arrivalTime-packetWindowMicroseconds) } } -// PacketsHeld returns the number of received packets currently held by the recorder +// PacketsHeld returns the number of received packets currently held by the recorder. func (r *Recorder) PacketsHeld() int { return r.packetsHeld } @@ -101,6 +102,7 @@ func (r *Recorder) BuildFeedbackPacket() []rtcp.Packet { // old. } r.packetsHeld = 0 + return feedbacks } @@ -109,6 +111,7 @@ func (r *Recorder) BuildFeedbackPacket() []rtcp.Packet { func (r *Recorder) maybeBuildFeedbackPacket(beginSeqNumInclusive, endSeqNumExclusive int64) *feedback { // NOTE: The logic of this method is inspired by the implementation in Chrome. // See https://source.chromium.org/chromium/chromium/src/+/refs/heads/main:third_party/webrtc/modules/remote_bitrate_estimator/remote_estimator_proxy.cc;l=276;drc=b5cd13bb6d5d157a5fbe3628b2dd1c1e106203c6 + //nolint:lll startSNInclusive, endSNExclusive := r.arrivalTimeMap.Clamp(beginSeqNumInclusive), r.arrivalTimeMap.Clamp(endSeqNumExclusive) // Create feedback on demand, as we don't yet know if there are packets in the range that have been @@ -136,18 +139,19 @@ func (r *Recorder) maybeBuildFeedbackPacket(beginSeqNumInclusive, endSeqNumExclu // baseSequenceNumber is the expected first sequence number. This is known, // but we may not have actually received it, so the base time should be the time // of the first received packet in the feedback. - fb.setBase(uint16(baseSequenceNumber), arrivalTime) + fb.setBase(uint16(baseSequenceNumber), arrivalTime) //nolint:gosec // G115 - if !fb.addReceived(uint16(seq), arrivalTime) { + if !fb.addReceived(uint16(seq), arrivalTime) { //nolint:gosec // G115 // Could not add a single received packet to the feedback. // This is unexpected to actually occur, but if it does, we'll // try again after skipping any missing packets. // NOTE: It's fine that we already incremented fbPktCnt, as in essence // we did actually "skip" a feedback (and this matches Chrome's behavior). r.startSequenceNumber = &seq + return nil } - } else if !fb.addReceived(uint16(seq), arrivalTime) { + } else if !fb.addReceived(uint16(seq), arrivalTime) { //nolint:gosec // G115 // Could not add timestamp. Packet may be full. Return // and try again with a fresh packet. break @@ -157,6 +161,7 @@ func (r *Recorder) maybeBuildFeedbackPacket(beginSeqNumInclusive, endSeqNumExclu } r.startSequenceNumber = &nextSequenceNumber + return fb } @@ -192,7 +197,7 @@ func (f *feedback) setBase(sequenceNumber uint16, timeUS int64) { func (f *feedback) getRTCP() *rtcp.TransportLayerCC { f.rtcp.PacketStatusCount = f.sequenceNumberCount - f.rtcp.ReferenceTime = uint32(f.refTimestamp64MS) + f.rtcp.ReferenceTime = uint32(f.refTimestamp64MS) //nolint:gosec // G115 f.rtcp.BaseSequenceNumber = f.baseSequenceNumber for len(f.lastChunk.deltas) > 0 { f.chunks = append(f.chunks, f.lastChunk.encode()) @@ -200,7 +205,8 @@ func (f *feedback) getRTCP() *rtcp.TransportLayerCC { f.rtcp.PacketChunks = append(f.rtcp.PacketChunks, f.chunks...) f.rtcp.RecvDeltas = f.deltas - padLen := 20 + len(f.rtcp.PacketChunks)*2 + f.len // 4 bytes header + 16 bytes twcc header + 2 bytes for each chunk + length of deltas + // 4 bytes header + 16 bytes twcc header + 2 bytes for each chunk + length of deltas + padLen := 20 + len(f.rtcp.PacketChunks)*2 + f.len padding := padLen%4 != 0 for padLen%4 != 0 { padLen++ @@ -209,7 +215,7 @@ func (f *feedback) getRTCP() *rtcp.TransportLayerCC { Count: rtcp.FormatTCC, Type: rtcp.TypeTransportSpecificFeedback, Padding: padding, - Length: uint16((padLen / 4) - 1), + Length: uint16((padLen / 4) - 1), //nolint:gosec // G115 } return f.rtcp @@ -223,7 +229,8 @@ func (f *feedback) addReceived(sequenceNumber uint16, timestampUS int64) bool { } else { delta250US = (deltaUS - rtcp.TypeTCCDeltaScaleFactor/2) / rtcp.TypeTCCDeltaScaleFactor } - if delta250US < math.MinInt16 || delta250US > math.MaxInt16 { // delta doesn't fit into 16 bit, need to create new packet + // delta doesn't fit into 16 bit, need to create new packet + if delta250US < math.MinInt16 || delta250US > math.MaxInt16 { return false } deltaUSRounded := delta250US * rtcp.TypeTCCDeltaScaleFactor @@ -257,6 +264,7 @@ func (f *feedback) addReceived(sequenceNumber uint16, timestampUS int64) bool { f.lastTimestampUS += deltaUSRounded f.sequenceNumberCount++ f.nextSequenceNumber++ + return true } @@ -282,6 +290,7 @@ func (c *chunk) canAdd(delta uint16) bool { if len(c.deltas) < maxRunLengthCap && !c.hasDifferentTypes && delta == c.deltas[0] { return true } + return false } @@ -294,13 +303,15 @@ func (c *chunk) add(delta uint16) { func (c *chunk) encode() rtcp.PacketStatusChunk { if !c.hasDifferentTypes { defer c.reset() + return &rtcp.RunLengthChunk{ PacketStatusSymbol: c.deltas[0], - RunLength: uint16(len(c.deltas)), + RunLength: uint16(len(c.deltas)), //nolint:gosec // G115 } } if len(c.deltas) == maxOneBitCap { defer c.reset() + return &rtcp.StatusVectorChunk{ SymbolSize: rtcp.TypeTCCSymbolSizeOneBit, SymbolList: c.deltas, @@ -341,6 +352,7 @@ func maxInt(a, b int) int { if a > b { return a } + return b } @@ -348,6 +360,7 @@ func minInt(a, b int) int { if a < b { return a } + return b } @@ -355,6 +368,7 @@ func max64(a, b int64) int64 { if a > b { return a } + return b } @@ -362,5 +376,6 @@ func min64(a, b int64) int64 { if a < b { return a } + return b } diff --git a/vendor/github.com/pion/interceptor/registry.go b/vendor/github.com/pion/interceptor/registry.go index e36ef6bfb6..9c12014267 100644 --- a/vendor/github.com/pion/interceptor/registry.go +++ b/vendor/github.com/pion/interceptor/registry.go @@ -13,7 +13,7 @@ func (r *Registry) Add(f Factory) { r.factories = append(r.factories, f) } -// Build constructs a single Interceptor from a InterceptorRegistry +// Build constructs a single Interceptor from a InterceptorRegistry. func (r *Registry) Build(id string) (Interceptor, error) { if len(r.factories) == 0 { return &NoOp{}, nil diff --git a/vendor/github.com/pion/interceptor/streaminfo.go b/vendor/github.com/pion/interceptor/streaminfo.go index cb261304f8..d622312f30 100644 --- a/vendor/github.com/pion/interceptor/streaminfo.go +++ b/vendor/github.com/pion/interceptor/streaminfo.go @@ -9,7 +9,7 @@ type RTPHeaderExtension struct { ID int } -// StreamInfo is the Context passed when a StreamLocal or StreamRemote has been Binded or Unbinded +// StreamInfo is the Context passed when a StreamLocal or StreamRemote has been Binded or Unbinded. type StreamInfo struct { ID string Attributes Attributes diff --git a/vendor/github.com/pion/rtp/packet.go b/vendor/github.com/pion/rtp/packet.go index c51d8c7d8e..16a3606969 100644 --- a/vendor/github.com/pion/rtp/packet.go +++ b/vendor/github.com/pion/rtp/packet.go @@ -29,6 +29,10 @@ type Header struct { ExtensionProfile uint16 Extensions []Extension + // PaddingLength is the length of the padding in bytes. It is not part of the RTP header + // (it is sent in the last byte of RTP packet padding), but logically it belongs here. + PaddingSize byte + // Deprecated: will be removed in a future version. PayloadOffset int } @@ -36,11 +40,16 @@ type Header struct { // Packet represents an RTP Packet. type Packet struct { Header - Payload []byte - PaddingSize byte + Payload []byte + + PaddingSize byte // Deprecated: will be removed in a future version. Use Header.PaddingSize instead. // Deprecated: will be removed in a future version. Raw []byte + + // Please do not add any new field directly to Packet struct unless you know that it is safe. + // pion internally passes Header and Payload separately, what causes bugs like + // https://github.com/pion/webrtc/issues/2403 . } const ( @@ -219,11 +228,12 @@ func (p *Packet) Unmarshal(buf []byte) error { if end <= n { return errTooSmall } - p.PaddingSize = buf[end-1] - end -= int(p.PaddingSize) + p.Header.PaddingSize = buf[end-1] + end -= int(p.Header.PaddingSize) } else { - p.PaddingSize = 0 + p.Header.PaddingSize = 0 } + p.PaddingSize = p.Header.PaddingSize if end < n { return errTooSmall } @@ -490,7 +500,7 @@ func (p Packet) Marshal() (buf []byte, err error) { // MarshalTo serializes the packet and writes to the buffer. func (p *Packet) MarshalTo(buf []byte) (n int, err error) { - if p.Header.Padding && p.PaddingSize == 0 { + if p.Header.Padding && p.paddingSize() == 0 { return 0, errInvalidRTPPadding } @@ -499,23 +509,28 @@ func (p *Packet) MarshalTo(buf []byte) (n int, err error) { return 0, err } + return marshalPayloadAndPaddingTo(buf, n, &p.Header, p.Payload, p.paddingSize()) +} + +func marshalPayloadAndPaddingTo(buf []byte, offset int, header *Header, payload []byte, paddingSize byte, +) (n int, err error) { // Make sure the buffer is large enough to hold the packet. - if n+len(p.Payload)+int(p.PaddingSize) > len(buf) { + if offset+len(payload)+int(paddingSize) > len(buf) { return 0, io.ErrShortBuffer } - m := copy(buf[n:], p.Payload) + m := copy(buf[offset:], payload) - if p.Header.Padding { - buf[n+m+int(p.PaddingSize-1)] = p.PaddingSize + if header.Padding { + buf[offset+m+int(paddingSize-1)] = paddingSize } - return n + m + int(p.PaddingSize), nil + return offset + m + int(paddingSize), nil } // MarshalSize returns the size of the packet once marshaled. func (p Packet) MarshalSize() int { - return p.Header.MarshalSize() + len(p.Payload) + int(p.PaddingSize) + return p.Header.MarshalSize() + len(p.Payload) + int(p.paddingSize()) } // Clone returns a deep copy of p. @@ -552,3 +567,30 @@ func (h Header) Clone() Header { return clone } + +func (p *Packet) paddingSize() byte { + if p.Header.PaddingSize > 0 { + return p.Header.PaddingSize + } + + return p.PaddingSize +} + +// MarshalPacketTo serializes the header and payload into bytes. +// Parts of pion code passes RTP header and payload separately, so this function +// is provided to help with that. +func MarshalPacketTo(buf []byte, header *Header, payload []byte) (int, error) { + n, err := header.MarshalTo(buf) + if err != nil { + return 0, err + } + + return marshalPayloadAndPaddingTo(buf, n, header, payload, header.PaddingSize) +} + +// PacketMarshalSize returns the size of the header and payload once marshaled. +// Parts of pion code passes RTP header and payload separately, so this function +// is provided to help with that. +func PacketMarshalSize(header *Header, payload []byte) int { + return header.MarshalSize() + len(payload) + int(header.PaddingSize) +} diff --git a/vendor/github.com/pion/sdp/v3/util.go b/vendor/github.com/pion/sdp/v3/util.go index 4550feece5..7cf17a9611 100644 --- a/vendor/github.com/pion/sdp/v3/util.go +++ b/vendor/github.com/pion/sdp/v3/util.go @@ -19,16 +19,12 @@ const ( ) var ( - errExtractCodecRtpmap = errors.New("could not extract codec from rtpmap") - errExtractCodecFmtp = errors.New("could not extract codec from fmtp") - errExtractCodecRtcpFb = errors.New("could not extract codec from rtcp-fb") - errMultipleName = errors.New("codec has multiple names defined") - errMultipleClockRate = errors.New("codec has multiple clock rates") - errMultipleEncodingParameters = errors.New("codec has multiple encoding parameters") - errMultipleFmtp = errors.New("codec has multiple fmtp values") - errPayloadTypeNotFound = errors.New("payload type not found") - errCodecNotFound = errors.New("codec not found") - errSyntaxError = errors.New("SyntaxError") + errExtractCodecRtpmap = errors.New("could not extract codec from rtpmap") + errExtractCodecFmtp = errors.New("could not extract codec from fmtp") + errExtractCodecRtcpFb = errors.New("could not extract codec from rtcp-fb") + errPayloadTypeNotFound = errors.New("payload type not found") + errCodecNotFound = errors.New("codec not found") + errSyntaxError = errors.New("SyntaxError") ) // ConnectionRole indicates which of the end points should initiate the connection establishment. @@ -207,49 +203,30 @@ func parseRtcpFb(rtcpFb string) (codec Codec, isWildcard bool, err error) { return codec, isWildcard, nil } -func mergeCodecs(codec Codec, codecs map[uint8]Codec) error { // nolint: cyclop +func mergeCodecs(codec Codec, codecs map[uint8]Codec) { savedCodec := codecs[codec.PayloadType] - savedCodec.PayloadType = codec.PayloadType - - if codec.Name != "" { - if savedCodec.Name != "" && savedCodec.Name != codec.Name { - return errMultipleName - } + if savedCodec.PayloadType == 0 { + savedCodec.PayloadType = codec.PayloadType + } + if savedCodec.Name == "" { savedCodec.Name = codec.Name } - - if codec.ClockRate != 0 { - if savedCodec.ClockRate != 0 && savedCodec.ClockRate != codec.ClockRate { - return errMultipleClockRate - } - + if savedCodec.ClockRate == 0 { savedCodec.ClockRate = codec.ClockRate } - - if codec.EncodingParameters != "" { - if savedCodec.EncodingParameters != "" && savedCodec.EncodingParameters != codec.EncodingParameters { - return errMultipleEncodingParameters - } - + if savedCodec.EncodingParameters == "" { savedCodec.EncodingParameters = codec.EncodingParameters } - - if codec.Fmtp != "" { - if savedCodec.Fmtp != "" && savedCodec.Fmtp != codec.Fmtp { - return errMultipleFmtp - } - + if savedCodec.Fmtp == "" { savedCodec.Fmtp = codec.Fmtp } - savedCodec.RTCPFeedback = append(savedCodec.RTCPFeedback, codec.RTCPFeedback...) - codecs[savedCodec.PayloadType] = savedCodec - return nil + codecs[savedCodec.PayloadType] = savedCodec } -func (s *SessionDescription) buildCodecMap() (map[uint8]Codec, error) { //nolint:cyclop, gocognit +func (s *SessionDescription) buildCodecMap() map[uint8]Codec { //nolint:cyclop codecs := map[uint8]Codec{ // static codecs that do not require a rtpmap 0: { @@ -272,16 +249,12 @@ func (s *SessionDescription) buildCodecMap() (map[uint8]Codec, error) { //nolint case strings.HasPrefix(attr, "rtpmap:"): codec, err := parseRtpmap(attr) if err == nil { - if err = mergeCodecs(codec, codecs); err != nil { - return nil, err - } + mergeCodecs(codec, codecs) } case strings.HasPrefix(attr, "fmtp:"): codec, err := parseFmtp(attr) if err == nil { - if err = mergeCodecs(codec, codecs); err != nil { - return nil, err - } + mergeCodecs(codec, codecs) } case strings.HasPrefix(attr, "rtcp-fb:"): codec, isWildcard, err := parseRtcpFb(attr) @@ -290,9 +263,7 @@ func (s *SessionDescription) buildCodecMap() (map[uint8]Codec, error) { //nolint case isWildcard: wildcardRTCPFeedback = append(wildcardRTCPFeedback, codec.RTCPFeedback...) default: - if err = mergeCodecs(codec, codecs); err != nil { - return nil, err - } + mergeCodecs(codec, codecs) } } } @@ -306,7 +277,7 @@ func (s *SessionDescription) buildCodecMap() (map[uint8]Codec, error) { //nolint codecs[i] = codec } - return codecs, nil + return codecs } func equivalentFmtp(want, got string) bool { @@ -350,10 +321,7 @@ func codecsMatch(wanted, got Codec) bool { // GetCodecForPayloadType scans the SessionDescription for the given payload type and returns the codec. func (s *SessionDescription) GetCodecForPayloadType(payloadType uint8) (Codec, error) { - codecs, err := s.buildCodecMap() - if err != nil { - return Codec{}, err - } + codecs := s.buildCodecMap() codec, ok := codecs[payloadType] if ok { @@ -366,10 +334,7 @@ func (s *SessionDescription) GetCodecForPayloadType(payloadType uint8) (Codec, e // GetPayloadTypeForCodec scans the SessionDescription for a codec that matches the provided codec // as closely as possible and returns its payload type. func (s *SessionDescription) GetPayloadTypeForCodec(wanted Codec) (uint8, error) { - codecs, err := s.buildCodecMap() - if err != nil { - return 0, err - } + codecs := s.buildCodecMap() for payloadType, codec := range codecs { if codecsMatch(wanted, codec) { diff --git a/vendor/modules.txt b/vendor/modules.txt index d8a7e94136..44773832e8 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -78,8 +78,8 @@ github.com/DataDog/zstd ## explicit; go 1.14 github.com/RoaringBitmap/roaring github.com/RoaringBitmap/roaring/internal -# github.com/VictoriaMetrics/fastcache v1.12.2 -## explicit; go 1.13 +# github.com/VictoriaMetrics/fastcache v1.12.4 +## explicit; go 1.24.0 github.com/VictoriaMetrics/fastcache # github.com/ajwerner/btree v0.0.0-20211221152037-f427b3e689c0 ## explicit; go 1.18 @@ -595,7 +595,7 @@ github.com/go-llsqlite/adapter/sqlitex github.com/go-llsqlite/crawshaw github.com/go-llsqlite/crawshaw/c github.com/go-llsqlite/crawshaw/sqlitex -# github.com/go-logr/logr v1.4.2 +# github.com/go-logr/logr v1.4.3 ## explicit; go 1.18 github.com/go-logr/logr github.com/go-logr/logr/funcr @@ -848,7 +848,7 @@ github.com/olekukonko/errors github.com/olekukonko/ll github.com/olekukonko/ll/lh github.com/olekukonko/ll/lx -# github.com/olekukonko/tablewriter v1.0.6 +# github.com/olekukonko/tablewriter v1.0.7 ## explicit; go 1.21 github.com/olekukonko/tablewriter github.com/olekukonko/tablewriter/pkg/twwarp @@ -918,10 +918,11 @@ github.com/pion/ice/v4/internal/atomic github.com/pion/ice/v4/internal/fakenet github.com/pion/ice/v4/internal/stun github.com/pion/ice/v4/internal/taskloop -# github.com/pion/interceptor v0.1.37 +# github.com/pion/interceptor v0.1.38 ## explicit; go 1.20 github.com/pion/interceptor github.com/pion/interceptor/internal/ntp +github.com/pion/interceptor/internal/rtpbuffer github.com/pion/interceptor/internal/sequencenumber github.com/pion/interceptor/pkg/nack github.com/pion/interceptor/pkg/report @@ -939,7 +940,7 @@ github.com/pion/randutil # github.com/pion/rtcp v1.2.15 ## explicit; go 1.20 github.com/pion/rtcp -# github.com/pion/rtp v1.8.15 +# github.com/pion/rtp v1.8.16 ## explicit; go 1.20 github.com/pion/rtp github.com/pion/rtp/codecs @@ -948,7 +949,7 @@ github.com/pion/rtp/codecs/vp9 # github.com/pion/sctp v1.8.39 ## explicit; go 1.20 github.com/pion/sctp -# github.com/pion/sdp/v3 v3.0.12 +# github.com/pion/sdp/v3 v3.0.13 ## explicit; go 1.20 github.com/pion/sdp/v3 # github.com/pion/srtp/v3 v3.0.4 @@ -1197,7 +1198,7 @@ golang.org/x/crypto/ripemd160 golang.org/x/crypto/scrypt golang.org/x/crypto/sha3 golang.org/x/crypto/ssh/terminal -# golang.org/x/exp v0.0.0-20250506013437-ce4c2cf36ca6 +# golang.org/x/exp v0.0.0-20250530174510-65e920069ea6 ## explicit; go 1.23.0 golang.org/x/exp/constraints golang.org/x/exp/rand @@ -1357,7 +1358,7 @@ gopkg.in/yaml.v3 lukechampine.com/blake3 lukechampine.com/blake3/bao lukechampine.com/blake3/guts -# modernc.org/libc v1.65.7 +# modernc.org/libc v1.65.8 ## explicit; go 1.23.0 modernc.org/libc modernc.org/libc/errno @@ -1398,7 +1399,7 @@ modernc.org/sqlite/lib ## explicit; go 1.17 rsc.io/tmplfunc rsc.io/tmplfunc/internal/parse -# zombiezen.com/go/sqlite v1.4.0 -## explicit; go 1.20 +# zombiezen.com/go/sqlite v1.4.2 +## explicit; go 1.23.0 zombiezen.com/go/sqlite zombiezen.com/go/sqlite/sqlitex diff --git a/vendor/zombiezen.com/go/sqlite/CHANGELOG.md b/vendor/zombiezen.com/go/sqlite/CHANGELOG.md index 65a1328e01..9330d68d8d 100644 --- a/vendor/zombiezen.com/go/sqlite/CHANGELOG.md +++ b/vendor/zombiezen.com/go/sqlite/CHANGELOG.md @@ -5,7 +5,35 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -[Unreleased]: https://github.com/zombiezen/go-sqlite/compare/v1.4.0...main +[Unreleased]: https://github.com/zombiezen/go-sqlite/compare/v1.4.2...main + +## [1.4.2][] - 2025-05-23 + +Version 1.4.2 updates the `modernc.org/sqlite` version to 1.37.1. + +[1.4.2]: https://github.com/zombiezen/go-sqlite/releases/tag/v1.4.2 + +### Changed + +- The minimum `modernc.org/sqlite` version updated to 1.37.1. + +## [1.4.1][] - 2025-05-23 + +Version 1.4.1 updates the `modernc.org/sqlite` version to 1.36.1 +and includes a couple small improvements. + +[1.4.1]: https://github.com/zombiezen/go-sqlite/releases/tag/v1.4.1 + +### Changed + +- The minimum `modernc.org/sqlite` version updated to 1.36.1. + +### Fixed + +- `*Stmt.ColumnName` no longer performs an allocation + ([#101](https://github.com/zombiezen/go-sqlite/issues/118)). +- The doc comment for `OpenFlags` has been rewritten for clarity + ([#114](https://github.com/zombiezen/go-sqlite/pull/114)). ## [1.4.0][] - 2024-09-23 diff --git a/vendor/zombiezen.com/go/sqlite/flake.lock b/vendor/zombiezen.com/go/sqlite/flake.lock index eee5b9a903..32f3f585b1 100644 --- a/vendor/zombiezen.com/go/sqlite/flake.lock +++ b/vendor/zombiezen.com/go/sqlite/flake.lock @@ -19,11 +19,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1701626906, - "narHash": "sha256-ugr1QyzzwNk505ICE4VMQzonHQ9QS5W33xF2FXzFQ00=", + "lastModified": 1746152631, + "narHash": "sha256-zBuvmL6+CUsk2J8GINpyy8Hs1Zp4PP6iBWSmZ4SCQ/s=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "0c6d8c783336a59f4c59d4a6daed6ab269c4b361", + "rev": "032bc6539bd5f14e9d0c51bd79cfe9a055b094c3", "type": "github" }, "original": { diff --git a/vendor/zombiezen.com/go/sqlite/flake.nix b/vendor/zombiezen.com/go/sqlite/flake.nix index 5578528a28..3709ca3bc0 100644 --- a/vendor/zombiezen.com/go/sqlite/flake.nix +++ b/vendor/zombiezen.com/go/sqlite/flake.nix @@ -15,7 +15,7 @@ devShells.default = pkgs.mkShell { packages = [ pkgs.go-tools # staticcheck - pkgs.go_1_20 + pkgs.go_1_24 pkgs.gotools # godoc, etc. ]; diff --git a/vendor/zombiezen.com/go/sqlite/go.work b/vendor/zombiezen.com/go/sqlite/go.work index b83ccf5dd0..bc25faf0be 100644 --- a/vendor/zombiezen.com/go/sqlite/go.work +++ b/vendor/zombiezen.com/go/sqlite/go.work @@ -1,4 +1,6 @@ -go 1.19 +go 1.23.0 + +toolchain go1.24.2 use ( . diff --git a/vendor/zombiezen.com/go/sqlite/go.work.sum b/vendor/zombiezen.com/go/sqlite/go.work.sum index 461f09aa2d..9daa7039dc 100644 --- a/vendor/zombiezen.com/go/sqlite/go.work.sum +++ b/vendor/zombiezen.com/go/sqlite/go.work.sum @@ -1,39 +1,58 @@ +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ= github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo= +github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd/go.mod h1:kf6iHlnVGwgKolg33glAes7Yg/8iWP8ukqeldJSO7jw= +github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic= golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY= golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= +golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= +golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE= +golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457/go.mod h1:pRgIJT+bRLFKnoM1ldnzKoxTIn14Yxz928LQRYYgIN0= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= +golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= +golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4= +golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc= golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps= -golang.org/x/tools v0.19.0 h1:tfGCXNR1OsFG+sVdLAitlpjAvD/I6dHDKnYrpEZUHkw= golang.org/x/tools v0.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc= +golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= +golang.org/x/tools v0.33.0/go.mod h1:CIJMaWEY88juyUfo7UbgPqbC8rU2OqfAV1h2Qp0oMYI= +lukechampine.com/uint128 v1.2.0/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk= +modernc.org/cc/v3 v3.41.0/go.mod h1:Ni4zjJYJ04CDOhG7dn640WGfwBzfE0ecX8TyMB0Fv0Y= +modernc.org/ccgo/v3 v3.17.0/go.mod h1:Sg3fwVpmLvCUTaqEUjiBDAvshIaKDB0RXaf+zgqFu8I= modernc.org/httpfs v1.0.6 h1:AAgIpFZRXuYnkjftxTAZwMIiwEqAfk8aVB2/oA6nAeM= modernc.org/httpfs v1.0.6/go.mod h1:7dosgurJGp0sPaRanU53W4xZYKh14wfzX420oZADeHM= modernc.org/tcl v1.15.1 h1:mOQwiEK4p7HruMZcwKTZPw/aqtGM4aY00uzWhlKKYws= diff --git a/vendor/zombiezen.com/go/sqlite/openflags.go b/vendor/zombiezen.com/go/sqlite/openflags.go index 7f35aad5f4..d40e74bc35 100644 --- a/vendor/zombiezen.com/go/sqlite/openflags.go +++ b/vendor/zombiezen.com/go/sqlite/openflags.go @@ -11,11 +11,12 @@ import ( ) // OpenFlags are [flags] used when opening a [Conn] via [OpenConn]. +// Either [OpenReadOnly] or [OpenReadWrite] must always be present. // // [flags]: https://www.sqlite.org/c3ref/c_open_autoproxy.html type OpenFlags uint -// One of the following flags must be passed to [OpenConn]. +// Required flags, one of which must be passed to [OpenConn]. const ( // OpenReadOnly opens the database in read-only mode. // If the database does not already exist, an error is returned. diff --git a/vendor/zombiezen.com/go/sqlite/sqlite.go b/vendor/zombiezen.com/go/sqlite/sqlite.go index 3bacac1d77..20e73c7b58 100644 --- a/vendor/zombiezen.com/go/sqlite/sqlite.go +++ b/vendor/zombiezen.com/go/sqlite/sqlite.go @@ -869,7 +869,12 @@ func (stmt *Stmt) ColumnCount() int { // // https://sqlite.org/c3ref/column_name.html func (stmt *Stmt) ColumnName(col int) string { - return libc.GoString(lib.Xsqlite3_column_name(stmt.conn.tls, stmt.stmt, int32(col))) + for name, namedCol := range stmt.colNames { + if namedCol == col { + return name + } + } + return "" } // BindParamCount reports the number of parameters in stmt.