Skip to content

Commit

Permalink
fix(prog): no terminal size in actions
Browse files Browse the repository at this point in the history
  • Loading branch information
iyear committed Nov 16, 2023
1 parent ae6cdab commit 01f02cc
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 26 deletions.
6 changes: 1 addition & 5 deletions app/chat/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/antonmedv/expr"
"github.com/fatih/color"
"github.com/go-faster/errors"
"github.com/go-faster/jx"
"github.com/gotd/contrib/middleware/ratelimit"
"github.com/gotd/td/telegram/peers"
Expand Down Expand Up @@ -104,10 +103,7 @@ func Export(ctx context.Context, opts *ExportOptions) error {

color.Blue("Type: %s | Input: %v", opts.Type, opts.Input)

pw, err := prog.New(progress.FormatNumber)
if err != nil {
return errors.Wrap(err, "create progress")
}
pw := prog.New(progress.FormatNumber)
pw.SetUpdateFrequency(200 * time.Millisecond)
pw.Style().Visibility.TrackerOverall = false
pw.Style().Visibility.ETA = false
Expand Down
5 changes: 1 addition & 4 deletions app/chat/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,7 @@ func Users(ctx context.Context, opts UsersOptions) error {
defer enc.ObjEnd()
enc.Field("id", func(e *jx.Encoder) { e.Int64(peer.ID()) })

pw, err := prog.New(progress.FormatNumber)
if err != nil {
return errors.Wrap(err, "create progress")
}
pw := prog.New(progress.FormatNumber)
pw.SetUpdateFrequency(200 * time.Millisecond)
pw.Style().Visibility.TrackerOverall = false
pw.Style().Visibility.ETA = true
Expand Down
7 changes: 1 addition & 6 deletions pkg/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,8 @@ type Options struct {
}

func New(opts Options) (*Downloader, error) {
pw, err := prog.New(formatter)
if err != nil {
return nil, errors.Wrap(err, "create progress")
}

return &Downloader{
pw: pw,
pw: prog.New(formatter),
opts: opts,
}, nil
}
Expand Down
12 changes: 5 additions & 7 deletions pkg/prog/prog.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@ import (
"time"

"github.com/fatih/color"
"github.com/go-faster/errors"
"github.com/jedib0t/go-pretty/v6/progress"
"github.com/jedib0t/go-pretty/v6/text"
tsize "github.com/kopoli/go-terminal-size"
)

func New(formatter progress.UnitsFormatter) (progress.Writer, error) {
func New(formatter progress.UnitsFormatter) progress.Writer {
pw := progress.NewWriter()
pw.SetAutoStop(false)

size, err := tsize.GetSize()
if err != nil {
return nil, errors.Wrap(err, "get terminal size")
width := 50
if size, err := tsize.GetSize(); err == nil {
width = size.Width
}
width := size.Width
if width > 100 {
width = 100
}
Expand All @@ -41,7 +39,7 @@ func New(formatter progress.UnitsFormatter) (progress.Writer, error) {
pw.Style().Options.ErrorString = color.RedString("failed!")
pw.Style().Options.DoneString = color.GreenString("done!")

return pw, nil
return pw
}

func Wait(pw progress.Writer) {
Expand Down
5 changes: 1 addition & 4 deletions pkg/uploader/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ type Options struct {
}

func New(o Options) (*Uploader, error) {
pw, err := prog.New(formatter)
if err != nil {
return nil, errors.Wrap(err, "create progress")
}
pw := prog.New(formatter)

return &Uploader{
pw: pw,
Expand Down

0 comments on commit 01f02cc

Please sign in to comment.