Skip to content

Commit

Permalink
fix: line break problem in progress bar
Browse files Browse the repository at this point in the history
I explicitly specified the options to minimize the issue of the progress bar breaking into new lines to the greatest extent possible.

refs #7
  • Loading branch information
tukaelu committed Oct 22, 2023
1 parent 22df91d commit bf50fa1
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 9 deletions.
11 changes: 7 additions & 4 deletions cmd/subcommand/cmd_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
"time"

"github.com/mackerelio/mackerel-client-go"
"github.com/schollz/progressbar/v3"
"github.com/urfave/cli/v2"

"github.com/tukaelu/sabadashi/internal/converter"
"github.com/tukaelu/sabadashi/internal/definition"
"github.com/tukaelu/sabadashi/internal/exporter"
"github.com/tukaelu/sabadashi/internal/fileutil"
"github.com/tukaelu/sabadashi/internal/progress"
"github.com/tukaelu/sabadashi/internal/retriever"
)

Expand All @@ -39,7 +39,10 @@ func (c *hostCommand) run(ctx *cli.Context) error {
return err
}

progress := progressbar.Default(attempts * int64(len(metricNames)))
bar := progress.NewProgress(
attempts*int64(len(metricNames)),
fmt.Sprintf("Donwload host metrics (create %d file(s))", len(metricNames)),
)

ch := make(chan exporter.Result, definition.CONCURRENT_FILE_OPERATION)
defer close(ch)
Expand Down Expand Up @@ -94,12 +97,12 @@ func (c *hostCommand) run(ctx *cli.Context) error {

time.Sleep(350 * time.Millisecond)

_ = progress.Add(1)
bar.Increment()
}
wg.Wait()

from += interval
_ = progress.Add(1)
bar.Increment()
}

return nil
Expand Down
11 changes: 7 additions & 4 deletions cmd/subcommand/cmd_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
"time"

"github.com/mackerelio/mackerel-client-go"
"github.com/schollz/progressbar/v3"
"github.com/urfave/cli/v2"

"github.com/tukaelu/sabadashi/internal/converter"
"github.com/tukaelu/sabadashi/internal/definition"
"github.com/tukaelu/sabadashi/internal/exporter"
"github.com/tukaelu/sabadashi/internal/fileutil"
"github.com/tukaelu/sabadashi/internal/progress"
"github.com/tukaelu/sabadashi/internal/retriever"
)

Expand Down Expand Up @@ -52,7 +52,10 @@ func (c *serviceCommand) run(ctx *cli.Context) error {
return fmt.Errorf("There was no service metric available for export.")
}

progress := progressbar.Default(attempts * int64(len(metricNames)))
bar := progress.NewProgress(
attempts*int64(len(metricNames)),
fmt.Sprintf("Download service metrics (create %d file(s))", len(metricNames)),
)

ch := make(chan exporter.Result, definition.CONCURRENT_FILE_OPERATION)
defer close(ch)
Expand Down Expand Up @@ -107,12 +110,12 @@ func (c *serviceCommand) run(ctx *cli.Context) error {

time.Sleep(350 * time.Millisecond)

_ = progress.Add(1)
bar.Increment()
}
wg.Wait()

from += interval
_ = progress.Add(1)
bar.Increment()
}

return nil
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,25 @@ module github.com/tukaelu/sabadashi
go 1.19

require (
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213
github.com/mackerelio/mackerel-client-go v0.26.0
github.com/schollz/progressbar/v3 v3.13.1
github.com/stretchr/testify v1.8.4
github.com/urfave/cli/v2 v2.25.7
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
golang.org/x/term v0.6.0
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.6.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213 h1:qGQQKEcAR99REcMpsXCp3lJ03zYT1PkRd3kQGPn9GVg=
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/mackerelio/mackerel-client-go v0.26.0 h1:72hj2zw/rqTUNMNokenRxs6KfJ1aVxRHpZT8X4eOUuA=
github.com/mackerelio/mackerel-client-go v0.26.0/go.mod h1:b4qVMQi+w4rxtKQIFycLWXNBtIi9d0r571RzYmg/aXo=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
Expand Down
46 changes: 46 additions & 0 deletions internal/progress/progress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package progress

import (
"fmt"
"os"

"github.com/k0kubun/go-ansi"
"github.com/schollz/progressbar/v3"
"golang.org/x/term"
)

type Progress struct {
Bar *progressbar.ProgressBar
}

func NewProgress(max int64, desc string) *Progress {
width, _, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil {
// If the terminal size cannot be taken, it shall be roughly 35.
width = 35
} else {
// adjust the width roughly...
width = width / 3
}

return &Progress{
Bar: progressbar.NewOptions64(
max,
progressbar.OptionSetWriter(ansi.NewAnsiStdout()),
progressbar.OptionEnableColorCodes(true),
progressbar.OptionSetWidth(width),
progressbar.OptionSetDescription(fmt.Sprintf("🐟 %s", desc)),
progressbar.OptionSetTheme(progressbar.Theme{
Saucer: "[blue]=[reset]",
SaucerHead: "[blue]>[reset]",
SaucerPadding: " ",
BarStart: "[",
BarEnd: "]",
}),
),
}
}

func (p *Progress) Increment() {
_ = p.Bar.Add(1)
}

0 comments on commit bf50fa1

Please sign in to comment.