Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/dl/dl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
12 changes: 10 additions & 2 deletions app/forward/forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
Expand Down
12 changes: 10 additions & 2 deletions app/up/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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))
}
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
1 change: 1 addition & 0 deletions pkg/consts/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ const (
FlagNTP = "ntp"
FlagReconnectTimeout = "reconnect-timeout"
FlagDlTemplate = "template"
FlagProgressPS = "progress-ps"
)
14 changes: 12 additions & 2 deletions pkg/prog/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -44,4 +49,9 @@ func EnablePS(ctx context.Context, pw progress.Writer) {
}
}
}()

return func() {
cancel()
<-done
}
}