From bf50fa1d0671ed7e85bb7b002734ebadc40d72fe Mon Sep 17 00:00:00 2001 From: Tsukasa Nishiyama Date: Sun, 22 Oct 2023 15:46:26 +0900 Subject: [PATCH] fix: line break problem in progress bar I explicitly specified the options to minimize the issue of the progress bar breaking into new lines to the greatest extent possible. refs #7 --- cmd/subcommand/cmd_host.go | 11 ++++++--- cmd/subcommand/cmd_service.go | 11 ++++++--- go.mod | 4 ++- go.sum | 2 ++ internal/progress/progress.go | 46 +++++++++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 internal/progress/progress.go diff --git a/cmd/subcommand/cmd_host.go b/cmd/subcommand/cmd_host.go index d1ff45c..63e700c 100644 --- a/cmd/subcommand/cmd_host.go +++ b/cmd/subcommand/cmd_host.go @@ -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" ) @@ -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) @@ -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 diff --git a/cmd/subcommand/cmd_service.go b/cmd/subcommand/cmd_service.go index 8242a40..6fc5510 100644 --- a/cmd/subcommand/cmd_service.go +++ b/cmd/subcommand/cmd_service.go @@ -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" ) @@ -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) @@ -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 diff --git a/go.mod b/go.mod index 273ab0f..40438af 100644 --- a/go.mod +++ b/go.mod @@ -3,16 +3,19 @@ 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 @@ -20,6 +23,5 @@ require ( 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 ) diff --git a/go.sum b/go.sum index 5a98ef2..b992207 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/progress/progress.go b/internal/progress/progress.go new file mode 100644 index 0000000..dd8b409 --- /dev/null +++ b/internal/progress/progress.go @@ -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) +}