From 0abdcdfb83faecbb40e8802fbdb0cefdee76e181 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Thu, 12 Jan 2023 23:59:24 +0300 Subject: [PATCH 1/2] [progress] Fix bug with updating progress settings --- CHANGELOG.md | 4 +++ ek.go | 2 +- progress/progress.go | 63 ++++++++++++++++++++++++++++++++++++++- progress/progress_test.go | 9 ++++++ 4 files changed, 76 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba8ef1e5..b5af4e77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## Changelog +### 12.57.1 + +* `[progress]` Fixed bug with updating progress settings + ### 12.57.0 * `[log]` Added interface for compatible loggers diff --git a/ek.go b/ek.go index 8157a750..161fb3bb 100644 --- a/ek.go +++ b/ek.go @@ -20,7 +20,7 @@ import ( // ////////////////////////////////////////////////////////////////////////////////// // // VERSION is current ek package version -const VERSION = "12.57.0" +const VERSION = "12.57.1" // ////////////////////////////////////////////////////////////////////////////////// // diff --git a/progress/progress.go b/progress/progress.go index 26e5af8b..7d6256df 100644 --- a/progress/progress.go +++ b/progress/progress.go @@ -171,7 +171,7 @@ func (b *Bar) Finish() { // UpdateSettings updates progress settings func (b *Bar) UpdateSettings(s Settings) { b.mu.Lock() - b.settings = s + b.settings = mergeSettings(b.settings, s) b.mu.Unlock() } @@ -660,3 +660,64 @@ func formatSpeedNum(s float64) string { return fmt.Sprintf("%6g%s/s", fmtutil.Float(s/mod), label) } + +// mergeSettings merges two settings objects +func mergeSettings(current, candidate Settings) Settings { + return Settings{ + NameColorTag: compStr(current.NameColorTag, candidate.NameColorTag), + BarFgColorTag: compStr(current.BarFgColorTag, candidate.BarFgColorTag), + BarBgColorTag: compStr(current.BarBgColorTag, candidate.BarBgColorTag), + PercentColorTag: compStr(current.PercentColorTag, candidate.PercentColorTag), + ProgressColorTag: compStr(current.ProgressColorTag, candidate.ProgressColorTag), + SpeedColorTag: compStr(current.SpeedColorTag, candidate.SpeedColorTag), + RemainingColorTag: compStr(current.RemainingColorTag, candidate.RemainingColorTag), + + ShowSpeed: compBool(current.ShowSpeed, candidate.ShowSpeed), + ShowName: compBool(current.ShowName, candidate.ShowName), + ShowPercentage: compBool(current.ShowPercentage, candidate.ShowPercentage), + ShowProgress: compBool(current.ShowProgress, candidate.ShowProgress), + ShowRemaining: compBool(current.ShowRemaining, candidate.ShowRemaining), + IsSize: compBool(current.IsSize, candidate.IsSize), + + Width: compInt(current.Width, candidate.Width), + NameSize: compInt(current.NameSize, candidate.NameSize), + + RefreshRate: compDur(current.RefreshRate, candidate.RefreshRate), + } +} + +// compStr compares two string values +func compStr(current, candidate string) string { + if candidate != "" { + return candidate + } + + return current +} + +// compBool compares two bool values +func compBool(current, candidate bool) bool { + if current != candidate { + return candidate + } + + return current +} + +// compInt compares two int values +func compInt(current, candidate int) int { + if current != candidate && candidate > 0 { + return candidate + } + + return current +} + +// compDur compares two duration values +func compDur(current, candidate time.Duration) time.Duration { + if current != candidate && candidate > 0 { + return candidate + } + + return current +} diff --git a/progress/progress_test.go b/progress/progress_test.go index b89e9641..6ed4e9f1 100644 --- a/progress/progress_test.go +++ b/progress/progress_test.go @@ -363,6 +363,15 @@ func (s *ProgressSuite) TestAux(c *C) { c.Assert(formatSpeedNum(123.0*1000.0), Equals, " 123K/s") c.Assert(formatSpeedNum(123.0*1000.0*1000.0), Equals, " 123M/s") c.Assert(formatSpeedNum(123.0*1000.0*1000.0*1000.0), Equals, " 123B/s") + + c.Assert(compStr("a", "b"), Equals, "b") + c.Assert(compStr("a", ""), Equals, "a") + c.Assert(compBool(true, false), Equals, false) + c.Assert(compBool(true, true), Equals, true) + c.Assert(compInt(1, 2), Equals, 2) + c.Assert(compInt(3, 0), Equals, 3) + c.Assert(compDur(time.Second, time.Minute), Equals, time.Minute) + c.Assert(compDur(time.Second, 0), Equals, time.Second) } // ////////////////////////////////////////////////////////////////////////////////// // From f9c5b43553e10029914798a00037f716633b1584 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Fri, 13 Jan 2023 00:30:35 +0300 Subject: [PATCH 2/2] [progress] Fix bug with updating progress settings --- progress/examples_test.go | 2 +- progress/progress.go | 70 ++++----------------------------------- progress/progress_test.go | 12 ++----- 3 files changed, 11 insertions(+), 73 deletions(-) diff --git a/progress/examples_test.go b/progress/examples_test.go index 3e4b5cd6..b18f6d18 100644 --- a/progress/examples_test.go +++ b/progress/examples_test.go @@ -26,7 +26,7 @@ func SimpleExample() { pbs.RefreshRate = 50 * time.Millisecond pbs.IsSize = false - // You can update all settings except RefreshRate before and after] + // You can update all settings except RefreshRate before and after // calling Start method. RefreshRate must be set before calling // Start method. pb.UpdateSettings(pbs) diff --git a/progress/progress.go b/progress/progress.go index 7d6256df..c8e4536e 100644 --- a/progress/progress.go +++ b/progress/progress.go @@ -141,10 +141,15 @@ func (b *Bar) Start() { b.started = true b.finished = false b.startTime = time.Now() - b.ticker = time.NewTicker(b.settings.RefreshRate) b.finishChan = make(chan bool) b.finishGroup = sync.WaitGroup{} + if b.settings.RefreshRate >= time.Millisecond { + b.ticker = time.NewTicker(b.settings.RefreshRate) + } else { + b.ticker = time.NewTicker(100 * time.Millisecond) + } + if b.total > 0 { b.passThruCalc = NewPassThruCalc(b.total, 10.0) } @@ -171,7 +176,7 @@ func (b *Bar) Finish() { // UpdateSettings updates progress settings func (b *Bar) UpdateSettings(s Settings) { b.mu.Lock() - b.settings = mergeSettings(b.settings, s) + b.settings = s b.mu.Unlock() } @@ -660,64 +665,3 @@ func formatSpeedNum(s float64) string { return fmt.Sprintf("%6g%s/s", fmtutil.Float(s/mod), label) } - -// mergeSettings merges two settings objects -func mergeSettings(current, candidate Settings) Settings { - return Settings{ - NameColorTag: compStr(current.NameColorTag, candidate.NameColorTag), - BarFgColorTag: compStr(current.BarFgColorTag, candidate.BarFgColorTag), - BarBgColorTag: compStr(current.BarBgColorTag, candidate.BarBgColorTag), - PercentColorTag: compStr(current.PercentColorTag, candidate.PercentColorTag), - ProgressColorTag: compStr(current.ProgressColorTag, candidate.ProgressColorTag), - SpeedColorTag: compStr(current.SpeedColorTag, candidate.SpeedColorTag), - RemainingColorTag: compStr(current.RemainingColorTag, candidate.RemainingColorTag), - - ShowSpeed: compBool(current.ShowSpeed, candidate.ShowSpeed), - ShowName: compBool(current.ShowName, candidate.ShowName), - ShowPercentage: compBool(current.ShowPercentage, candidate.ShowPercentage), - ShowProgress: compBool(current.ShowProgress, candidate.ShowProgress), - ShowRemaining: compBool(current.ShowRemaining, candidate.ShowRemaining), - IsSize: compBool(current.IsSize, candidate.IsSize), - - Width: compInt(current.Width, candidate.Width), - NameSize: compInt(current.NameSize, candidate.NameSize), - - RefreshRate: compDur(current.RefreshRate, candidate.RefreshRate), - } -} - -// compStr compares two string values -func compStr(current, candidate string) string { - if candidate != "" { - return candidate - } - - return current -} - -// compBool compares two bool values -func compBool(current, candidate bool) bool { - if current != candidate { - return candidate - } - - return current -} - -// compInt compares two int values -func compInt(current, candidate int) int { - if current != candidate && candidate > 0 { - return candidate - } - - return current -} - -// compDur compares two duration values -func compDur(current, candidate time.Duration) time.Duration { - if current != candidate && candidate > 0 { - return candidate - } - - return current -} diff --git a/progress/progress_test.go b/progress/progress_test.go index 6ed4e9f1..9ba5ceeb 100644 --- a/progress/progress_test.go +++ b/progress/progress_test.go @@ -87,6 +87,9 @@ func (s *ProgressSuite) TestBar(c *C) { pb.Finish() // should be skipped (not started) c.Assert(pb.IsFinished(), Equals, false) + pbs.RefreshRate = 0 + pb.UpdateSettings(pbs) + pb.Start() pb.renderElements(false) @@ -363,15 +366,6 @@ func (s *ProgressSuite) TestAux(c *C) { c.Assert(formatSpeedNum(123.0*1000.0), Equals, " 123K/s") c.Assert(formatSpeedNum(123.0*1000.0*1000.0), Equals, " 123M/s") c.Assert(formatSpeedNum(123.0*1000.0*1000.0*1000.0), Equals, " 123B/s") - - c.Assert(compStr("a", "b"), Equals, "b") - c.Assert(compStr("a", ""), Equals, "a") - c.Assert(compBool(true, false), Equals, false) - c.Assert(compBool(true, true), Equals, true) - c.Assert(compInt(1, 2), Equals, 2) - c.Assert(compInt(3, 0), Equals, 3) - c.Assert(compDur(time.Second, time.Minute), Equals, time.Minute) - c.Assert(compDur(time.Second, 0), Equals, time.Second) } // ////////////////////////////////////////////////////////////////////////////////// //