Skip to content

Commit

Permalink
Merge pull request #1188 from ripienaar/progress
Browse files Browse the repository at this point in the history
Revamps progress bars
  • Loading branch information
ripienaar authored Nov 26, 2024
2 parents f16976c + 9b46ff5 commit 005a0a7
Show file tree
Hide file tree
Showing 29 changed files with 298 additions and 238 deletions.
2 changes: 1 addition & 1 deletion cli/account_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (c *actCmd) reportServerStats(_ *fisk.ParseContext) error {
return fmt.Errorf("did not get results from any servers")
}

table := newTableWriter("Server Statistics")
table := iu.NewTableWriter(opts(), "Server Statistics")
table.AddHeaders("Server", "Cluster", "Version", "Tags", "Connections", "Subscriptions", "Leafnodes", "Sent Bytes", "Sent Messages", "Received Bytes", "Received Messages", "Slow Consumers")

var (
Expand Down
12 changes: 6 additions & 6 deletions cli/auth_account_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"time"

au "github.com/nats-io/natscli/internal/auth"
"github.com/nats-io/natscli/internal/util"
iu "github.com/nats-io/natscli/internal/util"

"github.com/AlecAivazis/survey/v2"
"github.com/choria-io/fisk"
Expand Down Expand Up @@ -492,7 +492,7 @@ func (c *authAccountCommand) skAddAction(_ *fisk.ParseContext) error {
}

if c.skRole == "" {
err := util.AskOne(&survey.Input{
err := iu.AskOne(&survey.Input{
Message: "Role Name",
Help: "The role to associate with this key",
}, &c.skRole, survey.WithValidator(survey.Required))
Expand Down Expand Up @@ -601,10 +601,10 @@ func (c *authAccountCommand) skListAction(_ *fisk.ParseContext) error {
return err
}

var table *tbl
var table *iu.Table

if len(acct.ScopedSigningKeys().List()) > 0 {
table = newTableWriter("Scoped Signing Keys")
table = iu.NewTableWriter(opts(), "Scoped Signing Keys")
table.AddHeaders("Role", "Key", "Description", "Max Subscriptions", "Pub Perms", "Sub Perms")
for _, sk := range acct.ScopedSigningKeys().List() {
scope, _ := acct.ScopedSigningKeys().GetScope(sk)
Expand Down Expand Up @@ -765,7 +765,7 @@ func (c *authAccountCommand) lsAction(_ *fisk.ParseContext) error {
return nil
}

table := newTableWriter("Accounts")
table := iu.NewTableWriter(opts(), "Accounts")
table.AddHeaders("Name", "Subject", "Users", "JetStream", "System")
for _, acct := range list {
system := ""
Expand Down Expand Up @@ -894,7 +894,7 @@ func (c *authAccountCommand) addAction(_ *fisk.ParseContext) error {
}

if c.accountName == "" {
err := util.AskOne(&survey.Input{
err := iu.AskOne(&survey.Input{
Message: "Account Name",
Help: "A unique name for the Account being added",
}, &c.accountName, survey.WithValidator(survey.Required))
Expand Down
2 changes: 1 addition & 1 deletion cli/auth_account_exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func (c *authAccountCommand) exportLsAction(_ *fisk.ParseContext) error {

exports := c.exportBySubject(acct)

tbl := newTableWriter("Exports for account %s", acct.Name())
tbl := util.NewTableWriter(opts(), "Exports for account %s", acct.Name())
tbl.AddHeaders("Name", "Kind", "Subject", "Activation Required", "Advertised", "Token Position", "Revocations")

for _, e := range exports {
Expand Down
2 changes: 1 addition & 1 deletion cli/auth_account_imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (c *authAccountCommand) importLsAction(_ *fisk.ParseContext) error {

imports := c.importsBySubject(acct)

tbl := newTableWriter("Imports for account %s", acct.Name())
tbl := util.NewTableWriter(opts(), "Imports for account %s", acct.Name())
tbl.AddHeaders("Name", "Kind", "Source", "Local Subject", "Remote Subject", "Allows Tracing", "Sharing Connection Info")

for _, i := range imports {
Expand Down
2 changes: 1 addition & 1 deletion cli/auth_operator_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ func (c *authOperatorCommand) lsAction(_ *fisk.ParseContext) error {
return nil
}

table := newTableWriter("Operators")
table := iu.NewTableWriter(opts(), "Operators")
table.AddHeaders("Name", "Subject", "Accounts", "Account Server", "Signing Keys")
for _, op := range list {
table.AddRow(op.Name(), op.Subject(), len(op.Accounts().List()), op.AccountServerURL(), len(op.SigningKeys().List()))
Expand Down
2 changes: 1 addition & 1 deletion cli/auth_user_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func (c *authUserCommand) lsAction(_ *fisk.ParseContext) error {
return nil
}

table := newTableWriter(fmt.Sprintf("Users in account %s", acct.Name()))
table := iu.NewTableWriter(opts(), fmt.Sprintf("Users in account %s", acct.Name()))
table.AddHeaders("Name", "Subject", "Scoped", "Sub Perms", "Pub Perms", "Max Subscriptions")
for _, user := range users {
limits := ab.UserLimits(user)
Expand Down
6 changes: 3 additions & 3 deletions cli/consumer_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ func (c *consumerCmd) lsAction(pc *fisk.ParseContext) error {

func (c *consumerCmd) renderConsumerAsTable(stream *jsm.Stream) (string, error) {
var out bytes.Buffer
table := newTableWriter("Consumers")
table := iu.NewTableWriter(opts(), "Consumers")
table.AddHeaders("Name", "Description", "Created", "Ack Pending", "Unprocessed", "Last Delivery")

missing, err := stream.EachConsumer(func(cons *jsm.Consumer) {
Expand Down Expand Up @@ -2305,7 +2305,7 @@ func (c *consumerCmd) reportAction(_ *fisk.ParseContext) error {

leaders := make(map[string]*raftLeader)

table := newTableWriter(fmt.Sprintf("Consumer report for %s with %s consumers", c.stream, f(ss.Consumers)))
table := iu.NewTableWriter(opts(), fmt.Sprintf("Consumer report for %s with %s consumers", c.stream, f(ss.Consumers)))
table.AddHeaders("Consumer", "Mode", "Ack Policy", "Ack Wait", "Ack Pending", "Redelivered", "Unprocessed", "Ack Floor", "Cluster")
missing, err := s.EachConsumer(func(cons *jsm.Consumer) {
cs, err := cons.LatestState()
Expand Down Expand Up @@ -2372,7 +2372,7 @@ func (c *consumerCmd) renderMissing(out io.Writer, missing []string) {
if len(missing) > 0 {
fmt.Fprintln(out)
sort.Strings(missing)
table := newTableWriter("Inaccessible Consumers")
table := iu.NewTableWriter(opts(), "Inaccessible Consumers")
iu.SliceGroups(missing, 4, func(names []string) {
table.AddRow(toany(names)...)
})
Expand Down
2 changes: 1 addition & 1 deletion cli/context_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func (c *ctxCommand) renderListTable(current string, known []*natscontext.Contex
return
}

table := newTableWriter("Known Contexts")
table := iu.NewTableWriter(opts(), "Known Contexts")
table.AddHeaders("Name", "Description")

for _, nctx := range known {
Expand Down
2 changes: 1 addition & 1 deletion cli/errors_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (c *errCmd) listAction(_ *fisk.ParseContext) error {
}
})

table := newTableWriter("NATS Errors")
table := iu.NewTableWriter(opts(), "NATS Errors")
table.AddHeaders("NATS Code", "HTTP Error Code", "Description", "Comment", "Go Constant")
for _, v := range matched {
table.AddRow(v.ErrCode, v.Code, v.Description, v.Comment, v.Constant)
Expand Down
6 changes: 3 additions & 3 deletions cli/kv_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func (c *kvCommand) displayKeyInfo(kv jetstream.KeyValue, keys jetstream.KeyList
return found, errors.New("key value cannot be nil")
}

table := newTableWriter(fmt.Sprintf("Contents for bucket '%s'", c.bucket))
table := util.NewTableWriter(opts(), fmt.Sprintf("Contents for bucket '%s'", c.bucket))

if c.lsVerboseDisplayValue {
table.AddHeaders("Key", "Created", "Delta", "Revision", "Value")
Expand Down Expand Up @@ -325,7 +325,7 @@ func (c *kvCommand) lsBuckets() error {
return info.State.Bytes < jnfo.State.Bytes
})

table := newTableWriter("Key-Value Buckets")
table := util.NewTableWriter(opts(), "Key-Value Buckets")
table.AddHeaders("Bucket", "Description", "Created", "Size", "Values", "Last Update")
for _, s := range found {
nfo, _ := s.LatestInformation()
Expand Down Expand Up @@ -400,7 +400,7 @@ func (c *kvCommand) historyAction(_ *fisk.ParseContext) error {
return err
}

table := newTableWriter(fmt.Sprintf("History for %s > %s", c.bucket, c.key))
table := util.NewTableWriter(opts(), fmt.Sprintf("History for %s > %s", c.bucket, c.key))
table.AddHeaders("Key", "Revision", "Op", "Created", "Length", "Value")
for _, r := range history {
val := base64IfNotPrintable(r.Value())
Expand Down
70 changes: 41 additions & 29 deletions cli/object_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"
"encoding/base64"
"fmt"
"github.com/jedib0t/go-pretty/v6/progress"
"io"
"os"
"path/filepath"
Expand All @@ -25,13 +26,12 @@ import (
"time"

"github.com/nats-io/nats.go/jetstream"
"github.com/nats-io/natscli/internal/util"
iu "github.com/nats-io/natscli/internal/util"

"github.com/AlecAivazis/survey/v2"
"github.com/choria-io/fisk"
"github.com/dustin/go-humanize"
"github.com/fatih/color"
"github.com/gosuri/uiprogress"
"github.com/nats-io/jsm.go"
"github.com/nats-io/nats.go"
)
Expand Down Expand Up @@ -401,7 +401,7 @@ func (c *objCommand) listBuckets() error {
return info.State.Bytes < jnfo.State.Bytes
})

table := newTableWriter("Object Store Buckets")
table := iu.NewTableWriter(opts(), "Object Store Buckets")
table.AddHeaders("Bucket", "Description", "Created", "Size", "Last Update")
for _, s := range found {
nfo, _ := s.LatestInformation()
Expand Down Expand Up @@ -444,7 +444,7 @@ func (c *objCommand) lsAction(_ *fisk.ParseContext) error {
return nil
}

table := newTableWriter("Bucket Contents")
table := iu.NewTableWriter(opts(), "Bucket Contents")
table.AddHeaders("Name", "Size", "Time")

for _, i := range contents {
Expand Down Expand Up @@ -484,6 +484,7 @@ func (c *objCommand) putAction(_ *fisk.ParseContext) error {
if !ok {
return nil
}
fmt.Println()
}

hdr, err := parseStringsToHeader(c.hdrs, 0)
Expand Down Expand Up @@ -519,28 +520,35 @@ func (c *objCommand) putAction(_ *fisk.ParseContext) error {
Headers: hdr,
}

var progress *uiprogress.Bar
var progbar progress.Writer
var tracker *progress.Tracker

stop := func() {}

if !opts().Trace && c.progress && stat != nil && stat.Size() > 20480 {
hs := humanize.IBytes(uint64(stat.Size()))
progress = uiprogress.AddBar(int(stat.Size())).PrependFunc(func(b *uiprogress.Bar) string {
return fmt.Sprintf("%s / %s", humanize.IBytes(uint64(b.Current())), hs)
progbar, tracker, err = iu.NewProgress(opts(), &progress.Tracker{
Total: stat.Size(),
Units: iu.ProgressUnitsIBytes,
})
progress.Width = util.ProgressWidth()
if err != nil {
return err
}

fmt.Println()
uiprogress.Start()
stop = func() { uiprogress.Stop(); fmt.Println() }
pr = &progressRW{p: progress, r: pr}
stop = func() {
time.Sleep(300 * time.Millisecond)
progbar.Stop()
fmt.Println()
}
pr = &progressRW{p: progbar, t: tracker, r: pr}
}

nfo, err = obj.Put(ctx, meta, pr)
nfo, err = obj.Put(context.TODO(), meta, pr)
stop()
if err != nil {
return err
}

fmt.Println()
c.showObjectInfo(nfo)

return nil
Expand All @@ -552,10 +560,7 @@ func (c *objCommand) getAction(_ *fisk.ParseContext) error {
return err
}

ctx, cancel := context.WithTimeout(ctx, opts().Timeout)
defer cancel()

res, err := obj.Get(ctx, c.file)
res, err := obj.Get(context.Background(), c.file)
if err != nil {
return err
}
Expand Down Expand Up @@ -597,21 +602,28 @@ func (c *objCommand) getAction(_ *fisk.ParseContext) error {
}
defer of.Close()

var progress *uiprogress.Bar
var progbar progress.Writer
var tracker *progress.Tracker

pw := io.Writer(of)
stop := func() {}

if !opts().Trace && c.progress && nfo.Size > 20480 {
hs := humanize.IBytes(nfo.Size)
progress = uiprogress.AddBar(int(nfo.Size)).PrependFunc(func(b *uiprogress.Bar) string {
return fmt.Sprintf("%s / %s", humanize.IBytes(uint64(b.Current())), hs)
fmt.Println()
progbar, tracker, err = iu.NewProgress(opts(), &progress.Tracker{
Total: int64(nfo.Size),
Units: iu.ProgressUnitsIBytes,
})
progress.Width = util.ProgressWidth()
if err != nil {
return err
}
stop = func() {
time.Sleep(300 * time.Millisecond)
progbar.Stop()
fmt.Println()
}

fmt.Println()
uiprogress.Start()
stop = func() { uiprogress.Stop(); fmt.Println() }
pw = &progressRW{p: progress, w: of}
pw = &progressRW{p: progbar, t: tracker, w: of}
}

start := time.Now()
Expand Down Expand Up @@ -693,10 +705,10 @@ func (c *objCommand) loadBucket() (*nats.Conn, jetstream.JetStream, jetstream.Ob
return nil, nil, nil, fmt.Errorf("no Object buckets found")
}

err = util.AskOne(&survey.Select{
err = iu.AskOne(&survey.Select{
Message: "Select a Bucket",
Options: known,
PageSize: util.SelectPageSize(len(known)),
PageSize: iu.SelectPageSize(len(known)),
}, &c.bucket)
if err != nil {
return nil, nil, nil, err
Expand Down
Loading

0 comments on commit 005a0a7

Please sign in to comment.