Codebase list golang-github-vbauerster-mpb / d58636e
OnComplete wrap decorator Vladimir Bauer 8 years ago
2 changed file(s) with 74 addition(s) and 38 deletion(s). Raw diff Collapse all Expand all
4545 // DecoratorFunc is a function that can be prepended and appended to the progress bar
4646 type DecoratorFunc func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string
4747
48 // StaticName to be used, when there is no plan to change the name during whole
49 // life of a progress rendering process
50 func StaticName(name string, minWidth int, conf byte) DecoratorFunc {
51 nameFn := func(s *Statistics) string {
48 // OnComplete wraps provided decorator `fn` with on complete event `message`.
49 // If you set `DwidthSync` bit in `conf` param, `minWidth` is ignored.
50 // `DwidthSync` is effective with multiple bars only, if set decorator will participate
51 // in width synchronization process with other decorators in the same column group.
52 func OnComplete(fn DecoratorFunc, message string, minWidth, conf int) DecoratorFunc {
53 msgDecorator := StaticName(message, minWidth, conf)
54 return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
55 if s.Completed {
56 return msgDecorator(s, widthAccumulator, widthDistributor)
57 }
58 return fn(s, widthAccumulator, widthDistributor)
59 }
60 }
61
62 // StaticName is a simple name/message decorator.
63 // If you set `DwidthSync` bit in `conf` param, `minWidth` is ignored.
64 // `DwidthSync` is effective with multiple bars only, if set decorator will participate
65 // in width synchronization process with other decorators in the same column group.
66 func StaticName(name string, minWidth, conf int) DecoratorFunc {
67 nameFn := func(*Statistics) string {
5268 return name
5369 }
5470 return DynamicName(nameFn, minWidth, conf)
5571 }
5672
57 // DynamicName to be used, when there is a plan to change the name once or
58 // several times during progress rendering process. If there're more than one
59 // bar, and you'd like to synchronize column width, conf param should have
60 // DwidthSync bit set.
61 func DynamicName(nameFn func(*Statistics) string, minWidth int, conf byte) DecoratorFunc {
62 format := "%%"
63 if (conf & DidentRight) != 0 {
64 format += "-"
65 }
66 format += "%ds"
67 return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
68 name := nameFn(s)
73 // DynamicName is a name/message decorator, with ability to change message via provided `messageFn`.
74 // If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored.
75 // `DwidthSync` is effective with multiple bars only, if set decorator will participate
76 // in width synchronization process with other decorators in the same column group.
77 func DynamicName(messageFn func(*Statistics) string, minWidth, conf int) DecoratorFunc {
78 format := "%%"
79 if (conf & DidentRight) != 0 {
80 format += "-"
81 }
82 format += "%ds"
83 return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
84 name := messageFn(s)
6985 if (conf & DwidthSync) != 0 {
7086 widthAccumulator <- utf8.RuneCountInString(name)
7187 max := <-widthDistributor
7995 }
8096
8197 // CountersNoUnit returns raw counters decorator
82 func CountersNoUnit(pairFormat string, minWidth int, conf byte) DecoratorFunc {
98 // If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored.
99 // `DwidthSync` is effective with multiple bars only, if set decorator will participate
100 // in width synchronization process with other decorators in the same column group.
101 func CountersNoUnit(pairFormat string, minWidth, conf int) DecoratorFunc {
83102 return Counters(pairFormat, 0, minWidth, conf)
84103 }
85104
86 // CountersKibiByte returns human friendly byte counters decorator, where
87 // counters unit is multiple by 1024.
88 func CountersKibiByte(pairFormat string, minWidth int, conf byte) DecoratorFunc {
105 // CountersKibiByte returns human friendly byte counters decorator,
106 // where counters unit is multiple by 1024.
107 // `pairFormat` must contain two printf compatible verbs, like "%f" or "%d".
108 // First verb substituted with Current, second one with Total.
109 // Example: `"%.1f / %.1f" = "1.0MiB / 12.0MiB"` or `"% .1f / % .1f" = "1.0 MiB / 12.0 MiB"`.
110 // If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored.
111 // `DwidthSync` is effective with multiple bars only, if set decorator will participate
112 // in width synchronization process with other decorators in the same column group.
113 func CountersKibiByte(pairFormat string, minWidth, conf int) DecoratorFunc {
89114 return Counters(pairFormat, Unit_KiB, minWidth, conf)
90115 }
91116
92117 // CountersKiloByte returns human friendly byte counters decorator, where
93118 // counters unit is multiple by 1000.
94 func CountersKiloByte(pairFormat string, minWidth int, conf byte) DecoratorFunc {
119 // `pairFormat` must contain two printf compatible verbs, like "%f" or "%d".
120 // First verb substituted with Current, second one with Total.
121 // Example: `"%.1f / %.1f" = "1.0MiB / 12.0MiB"` or `"% .1f / % .1f" = "1.0 MiB / 12.0 MiB"`.
122 // If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored.
123 // `DwidthSync` is effective with multiple bars only, if set decorator will participate
124 // in width synchronization process with other decorators in the same column group.
125 func CountersKiloByte(pairFormat string, minWidth, conf int) DecoratorFunc {
95126 return Counters(pairFormat, Unit_kB, minWidth, conf)
96127 }
97128
98129 // Counters provides basic counters decorator.
99 // pairFormat must contain two printf compatible verbs, like "%f" or "%d".
100 // First verb substituted with Current, second one with Total. For example (assuming decor.Unit_KiB used):
101 // "%.1f / %.1f" = "1.0MiB / 12.0MiB" or "% .1f / % .1f" = "1.0 MiB / 12.0 MiB"
102 // unit is one of decor.Unit_KiB/decor.Unit_kB or just zero if you need raw unitless numbers.
103 func Counters(pairFormat string, unit Unit, minWidth int, conf byte) DecoratorFunc {
130 // `pairFormat` must contain two printf compatible verbs, like "%f" or "%d".
131 // First verb substituted with Current, second one with Total.
132 // Example: `"%.1f / %.1f" = "1.0MiB / 12.0MiB"` or `"% .1f / % .1f" = "1.0 MiB / 12.0 MiB"`.
133 // `unit` is one of decor.Unit_KiB/decor.Unit_kB or just zero if you need raw unitless numbers.
134 // If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored.
135 // `DwidthSync` is effective with multiple bars only, if set decorator will participate
136 // in width synchronization process with other decorators in the same column group.
137 func Counters(pairFormat string, unit Unit, minWidth, conf int) DecoratorFunc {
104138 format := "%%"
105139 if (conf & DidentRight) != 0 {
106140 format += "-"
129163 }
130164
131165 // ETA provides exponential-weighted-moving-average ETA decorator.
132 // If there're more than one bar, and you'd like to synchronize column width,
133 // conf param should have DwidthSync bit set.
134 func ETA(minWidth int, conf byte) DecoratorFunc {
166 // If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored.
167 // `DwidthSync` is effective with multiple bars only, if set decorator will participate
168 // in width synchronization process with other decorators in the same column group.
169 func ETA(minWidth, conf int) DecoratorFunc {
135170 format := "%%"
136171 if (conf & DidentRight) != 0 {
137172 format += "-"
152187 }
153188
154189 // Elapsed provides elapsed time decorator.
155 // If there're more than one bar, and you'd like to synchronize column width,
156 // conf param should have DwidthSync bit set.
157 func Elapsed(minWidth int, conf byte) DecoratorFunc {
190 // If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored.
191 // `DwidthSync` is effective with multiple bars only, if set decorator will participate
192 // in width synchronization process with other decorators in the same column group.
193 func Elapsed(minWidth, conf int) DecoratorFunc {
158194 format := "%%"
159195 if (conf & DidentRight) != 0 {
160196 format += "-"
175211 }
176212
177213 // Percentage provides percentage decorator.
178 // If there're more than one bar, and you'd like to synchronize column width,
179 // conf param should have DwidthSync bit set.
180 func Percentage(minWidth int, conf byte) DecoratorFunc {
214 // If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored.
215 // `DwidthSync` is effective with multiple bars only, if set decorator will participate
216 // in width synchronization process with other decorators in the same column group.
217 func Percentage(minWidth, conf int) DecoratorFunc {
181218 format := "%%"
182219 if (conf & DidentRight) != 0 {
183220 format += "-"
2727 }
2828 b := p.AddBar(int64(total),
2929 mpb.PrependDecorators(
30 decor.StaticName(name, 0, decor.DwidthSync|decor.DidentRight),
31 decor.ETA(4, decor.DSyncSpace),
30 decor.StaticName(name, 0, decor.DwidthSync),
31 decor.OnComplete(decor.ETA(4, 0), "Done", 0, decor.DSyncSpace),
3232 ),
3333 mpb.AppendDecorators(
3434 decor.Percentage(5, 0),
4545 }
4646
4747 p.Wait()
48 fmt.Println("done")
4948 }