diff --git a/app/dl/dl.go b/app/dl/dl.go index e603250ea1..be4a911ab6 100644 --- a/app/dl/dl.go +++ b/app/dl/dl.go @@ -99,7 +99,12 @@ func Run(ctx context.Context, c *telegram.Client, kvd storage.Storage, opts Opti dlProgress := prog.New(utils.Byte.FormatBinaryBytes) dlProgress.SetNumTrackersExpected(it.Total()) - prog.EnablePS(ctx, dlProgress) + stopPS := func() {} + if viper.GetBool(consts.FlagProgressPS) { + stopPS = prog.EnablePS(ctx, dlProgress) + } else { + dlProgress.Style().Visibility.Pinned = false + } options := downloader.Options{ Pool: pool, @@ -120,6 +125,7 @@ func Run(ctx context.Context, c *telegram.Client, kvd storage.Storage, opts Opti go dlProgress.Render() defer func() { + stopPS() prog.Wait(ctx, dlProgress) // Notify user if any messages were skipped due to deletion diff --git a/app/forward/forward.go b/app/forward/forward.go index e46d5e44f2..35b007a2bc 100644 --- a/app/forward/forward.go +++ b/app/forward/forward.go @@ -80,7 +80,12 @@ func Run(ctx context.Context, c *telegram.Client, kvd storage.Storage, opts Opti fwProgress := prog.New(pw.FormatNumber) fwProgress.SetNumTrackersExpected(totalMessages(dialogs)) - prog.EnablePS(ctx, fwProgress) + stopPS := func() {} + if viper.GetBool(consts.FlagProgressPS) { + stopPS = prog.EnablePS(ctx, fwProgress) + } else { + fwProgress.Style().Visibility.Pinned = false + } fw := forwarder.New(forwarder.Options{ Pool: pool, @@ -101,7 +106,10 @@ func Run(ctx context.Context, c *telegram.Client, kvd storage.Storage, opts Opti }) go fwProgress.Render() - defer prog.Wait(ctx, fwProgress) + defer func() { + stopPS() + prog.Wait(ctx, fwProgress) + }() return fw.Forward(ctx) } diff --git a/app/up/up.go b/app/up/up.go index cf33430320..94de8b3c3c 100644 --- a/app/up/up.go +++ b/app/up/up.go @@ -90,7 +90,12 @@ func Run(ctx context.Context, c *telegram.Client, kvd storage.Storage, opts Opti upProgress := prog.New(utils.Byte.FormatBinaryBytes) upProgress.SetNumTrackersExpected(len(files)) - prog.EnablePS(ctx, upProgress) + stopPS := func() {} + if viper.GetBool(consts.FlagProgressPS) { + stopPS = prog.EnablePS(ctx, upProgress) + } else { + upProgress.Style().Visibility.Pinned = false + } options := uploader.Options{ Client: pool.Default(ctx), @@ -102,7 +107,10 @@ func Run(ctx context.Context, c *telegram.Client, kvd storage.Storage, opts Opti up := uploader.New(options) go upProgress.Render() - defer prog.Wait(ctx, upProgress) + defer func() { + stopPS() + prog.Wait(ctx, upProgress) + }() return up.Upload(ctx, viper.GetInt(consts.FlagLimit)) } diff --git a/cmd/root.go b/cmd/root.go index bd14a639ab..665c9394d1 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -163,6 +163,7 @@ func New() *cobra.Command { cmd.PersistentFlags().IntP(consts.FlagLimit, "l", 2, "max number of concurrent tasks") cmd.PersistentFlags().Int(consts.FlagPoolSize, 8, "specify the size of the DC pool, zero means infinity") cmd.PersistentFlags().Duration(consts.FlagDelay, 0, "delay between each task, zero means no delay") + cmd.PersistentFlags().Bool(consts.FlagProgressPS, true, "show pinned CPU/memory/goroutines progress info") cmd.PersistentFlags().String(consts.FlagNTP, "", "ntp server host, if not set, use system time") cmd.PersistentFlags().Duration(consts.FlagReconnectTimeout, 5*time.Minute, "Telegram client reconnection backoff timeout, infinite if set to 0") // #158 diff --git a/go.work.sum b/go.work.sum index 6229ccb358..489e553442 100644 --- a/go.work.sum +++ b/go.work.sum @@ -36,6 +36,8 @@ github.com/cenkalti/backoff/v3 v3.0.0 h1:ske+9nBpD9qZsTBoF41nW5L+AIuFBKMeze18XQ3 github.com/cenkalti/backoff/v3 v3.0.0/go.mod h1:cIeZDE3IrqwwJl6VUwCN6trj1oXrTS4rc0ij+ULvLYs= github.com/census-instrumentation/opencensus-proto v0.4.1 h1:iKLQ0xPNFxR/2hzXZMrBo8f1j86j5WHzznCCQxV/b8g= github.com/census-instrumentation/opencensus-proto v0.4.1/go.mod h1:4T9NM4+4Vw91VeyqjLS6ao50K5bOcLKN6Q42XnYaRYw= +github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= +github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/chzyer/readline v1.5.1 h1:upd/6fQk4src78LMRzh5vItIt361/o4uq553V8B5sGI= @@ -116,8 +118,6 @@ github.com/hashicorp/vault/api v1.12.2 h1:7YkCTE5Ni90TcmYHDBExdt4WGJxhpzaHqR6uGb github.com/hashicorp/vault/api v1.12.2/go.mod h1:LSGf1NGT1BnvFFnKVtnvcaLBM2Lz+gJdpL6HUYed8KE= github.com/ianlancetaylor/demangle v0.0.0-20240312041847-bd984b5ce465 h1:KwWnWVWCNtNq/ewIX7HIKnELmEx2nDP42yskD/pi7QE= github.com/ianlancetaylor/demangle v0.0.0-20240312041847-bd984b5ce465/go.mod h1:gx7rwoVhcfuVKG5uya9Hs3Sxj7EIvldVofAWIUtGouw= -github.com/iyear/tdl/core v0.20.1/go.mod h1:oIMvODKNqz52VmAk3M2+otHTPai4xo3y1aQ2VZE25eY= -github.com/iyear/tdl/extension v0.20.1/go.mod h1:zM8ZaR+q25JrtVJIP5H7fknDpge/r6TVFsS4DNJfpIE= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/k0kubun/pp/v3 v3.4.1 h1:1WdFZDRRqe8UsR61N/2RoOZ3ziTEqgTPVqKrHeb779Y= diff --git a/pkg/consts/flag.go b/pkg/consts/flag.go index 1df7b7dd79..800e874389 100644 --- a/pkg/consts/flag.go +++ b/pkg/consts/flag.go @@ -13,4 +13,5 @@ const ( FlagNTP = "ntp" FlagReconnectTimeout = "reconnect-timeout" FlagDlTemplate = "template" + FlagProgressPS = "progress-ps" ) diff --git a/pkg/prog/tracker.go b/pkg/prog/tracker.go index 03a3eb9824..554230d75a 100644 --- a/pkg/prog/tracker.go +++ b/pkg/prog/tracker.go @@ -25,9 +25,14 @@ func AppendTracker(pw progress.Writer, formatter progress.UnitsFormatter, messag return &tracker } -// EnablePS enables pinned messages with ps info: cpu, memory, goroutines -func EnablePS(ctx context.Context, pw progress.Writer) { +// EnablePS enables pinned messages with ps info: cpu, memory, goroutines. +// It returns a stop function to clear the pinned message and stop updates. +func EnablePS(ctx context.Context, pw progress.Writer) func() { + ctx, cancel := context.WithCancel(ctx) + done := make(chan struct{}) + go func() { + defer close(done) f := func() { pw.SetPinnedMessages(strings.Join(ps.Humanize(ctx), " ")) } f() @@ -44,4 +49,9 @@ func EnablePS(ctx context.Context, pw progress.Writer) { } } }() + + return func() { + cancel() + <-done + } }