diff --git a/go.mod b/go.mod index 0f739ba..3218735 100644 --- a/go.mod +++ b/go.mod @@ -17,7 +17,7 @@ require ( github.com/google/uuid v1.6.0 github.com/opencontainers/image-spec v1.1.0 github.com/rs/zerolog v1.33.0 - github.com/schollz/progressbar/v3 v3.17.0 + github.com/schollz/progressbar/v3 v3.14.6 github.com/stretchr/testify v1.9.0 github.com/thoas/go-funk v0.9.3 golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c diff --git a/go.sum b/go.sum index 810389b..e78318e 100644 --- a/go.sum +++ b/go.sum @@ -14,8 +14,6 @@ github.com/avast/retry-go/v4 v4.6.0/go.mod h1:gvWlPhBVsvBbLkVGDg/KwvBv0bEkCOLRRS github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w= github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM= github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= -github.com/chengxilo/virtualterm v1.0.4 h1:Z6IpERbRVlfB8WkOmtbHiDbBANU7cimRIof7mk9/PwM= -github.com/chengxilo/virtualterm v1.0.4/go.mod h1:DyxxBZz/x1iqJjFxTFcr6/x+jSpqN0iwWCOK1q10rlY= github.com/cloudquery/cloudquery-api-go v1.13.1 h1:jU/mpVjgamRXZUWEu+ucNZcLk9OIm5YV6q9B5en5rqQ= github.com/cloudquery/cloudquery-api-go v1.13.1/go.mod h1:5oo8HHnv2Y7NgcVvZn59xFlYKJUyeP0tcN8JH3IP2Aw= github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I= @@ -61,6 +59,7 @@ github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrj github.com/hashicorp/go-retryablehttp v0.7.5 h1:bJj+Pj19UZMIweq/iie+1u5YCdGrnxCT9yvm0e+Nd5M= github.com/hashicorp/go-retryablehttp v0.7.5/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8= github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE= +github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= @@ -75,8 +74,6 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/ github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= -github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ= github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw= github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0= @@ -104,8 +101,8 @@ github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUc github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= -github.com/schollz/progressbar/v3 v3.17.0 h1:Fv+vG6O6jnJwdjCelvfyYO7sF2jaUGQVmdH4CxcZdsQ= -github.com/schollz/progressbar/v3 v3.17.0/go.mod h1:5H4fLgifX+KeQCsEJnZTOepgZLe1jFF1lpPXb68IJTA= +github.com/schollz/progressbar/v3 v3.14.6 h1:GyjwcWBAf+GFDMLziwerKvpuS7ZF+mNTAXIB2aspiZs= +github.com/schollz/progressbar/v3 v3.14.6/go.mod h1:Nrzpuw3Nl0srLY0VlTvC4V6RL50pcEymjy6qyJAaLa0= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0= @@ -166,8 +163,10 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4= golang.org/x/term v0.25.0 h1:WtHI/ltw4NvSUig5KARz9h521QvRC8RmF/cuYqifU24= golang.org/x/term v0.25.0/go.mod h1:RPyXicDX+6vLxogjjRxjgD2TKtmAO6NZBsBRfrOLu7M= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/tests/progressbar_test.go b/tests/progressbar_test.go new file mode 100644 index 0000000..357c609 --- /dev/null +++ b/tests/progressbar_test.go @@ -0,0 +1,68 @@ +package test + +import ( + "strings" + "testing" + "time" + + "github.com/schollz/progressbar/v3" +) + +func expectBuffer(t *testing.T, buf *strings.Builder, expect string) { + t.Helper() + current := strings.TrimSpace(buf.String()) + if current != expect { + r := strings.NewReplacer("\r", "\\R", "\n", "\\N") + current, expect = r.Replace(current), r.Replace(expect) + t.Fatalf("Render mismatch\nResult: '%s'\nExpect: '%s'\n", current, expect) + } +} + +func TestRegression(t *testing.T) { + buf := strings.Builder{} + + bar := progressbar.NewOptions(-1, + progressbar.OptionSetWriter(&buf), + progressbar.OptionSetDescription("Syncing resources..."), + progressbar.OptionSetItsString("resources"), + progressbar.OptionShowIts(), + progressbar.OptionSetElapsedTime(true), + progressbar.OptionShowCount(), + ) + bar.Reset() + time.Sleep(1 * time.Second) + expectBuffer(t, &buf, "") + _ = bar.Add(5) + expectBuffer(t, &buf, "- Syncing resources... (5/-, 5 resources/s) [1s]") + time.Sleep(1 * time.Second) + _ = bar.Add(5) + expectBuffer(t, &buf, "- Syncing resources... (5/-, 5 resources/s) [1s] \r \r\r| Syncing resources... (10/-, 5 resources/s) [2s]") + time.Sleep(1 * time.Second) + _ = bar.Finish() + expectBuffer(t, &buf, "- Syncing resources... (5/-, 5 resources/s) [1s] \r \r\r| Syncing resources... (10/-, 5 resources/s) [2s] \r \r\r- Syncing resources... (10/-, 3 resources/s) [3s]") +} + +func TestRegressionClearOnFinish(t *testing.T) { + buf := strings.Builder{} + + bar := progressbar.NewOptions(-1, + progressbar.OptionSetWriter(&buf), + progressbar.OptionSetDescription("Syncing resources..."), + progressbar.OptionSetItsString("resources"), + progressbar.OptionShowIts(), + progressbar.OptionSetElapsedTime(true), + progressbar.OptionShowCount(), + progressbar.OptionClearOnFinish(), + ) + bar.Reset() + time.Sleep(1 * time.Second) + expectBuffer(t, &buf, "") + _ = bar.Add(5) + expectBuffer(t, &buf, "- Syncing resources... (5/-, 5 resources/s) [1s]") + time.Sleep(1 * time.Second) + _ = bar.Add(5) + expectBuffer(t, &buf, "- Syncing resources... (5/-, 5 resources/s) [1s] \r \r\r| Syncing resources... (10/-, 5 resources/s) [2s]") + time.Sleep(1 * time.Second) + _ = bar.Finish() + expectBuffer(t, &buf, "- Syncing resources... (5/-, 5 resources/s) [1s] \r \r\r| Syncing resources... (10/-, 5 resources/s) [2s]") +}