bar filler: calc filled one time
Vladimir Bauer
5 years ago
| 152 | 152 | w.Write(s.components[iLbound].bytes) |
| 153 | 153 | defer w.Write(s.components[iRbound].bytes) |
| 154 | 154 | |
| 155 | refWidth, filled := 0, 0 | |
| 156 | 155 | curWidth := int(internal.PercentageRound(stat.Total, stat.Current, width)) |
| 156 | refWidth, filled := 0, curWidth | |
| 157 | 157 | filling := make([][]byte, 0, curWidth) |
| 158 | 158 | |
| 159 | 159 | if curWidth > 0 && curWidth != width { |
| 160 | 160 | tipFrame := s.tip.frames[s.tip.count%uint(len(s.tip.frames))] |
| 161 | 161 | filling = append(filling, tipFrame.bytes) |
| 162 | filled += tipFrame.width | |
| 163 | 162 | curWidth -= tipFrame.width |
| 164 | 163 | s.tip.count++ |
| 165 | 164 | } |
| 166 | 165 | |
| 167 | if stat.Refill > 0 { | |
| 166 | if stat.Refill > 0 && curWidth > 0 { | |
| 168 | 167 | refWidth = int(internal.PercentageRound(stat.Total, int64(stat.Refill), width)) |
| 169 | 168 | if refWidth > curWidth { |
| 170 | 169 | refWidth = curWidth |
| 174 | 173 | |
| 175 | 174 | for curWidth > 0 && curWidth >= s.components[iFiller].width { |
| 176 | 175 | filling = append(filling, s.components[iFiller].bytes) |
| 177 | filled += s.components[iFiller].width | |
| 178 | 176 | curWidth -= s.components[iFiller].width |
| 179 | 177 | if s.components[iFiller].width == 0 { |
| 180 | 178 | break |
| 183 | 181 | |
| 184 | 182 | for refWidth > 0 && refWidth >= s.components[iRefiller].width { |
| 185 | 183 | filling = append(filling, s.components[iRefiller].bytes) |
| 186 | filled += s.components[iRefiller].width | |
| 187 | 184 | refWidth -= s.components[iRefiller].width |
| 188 | 185 | if s.components[iRefiller].width == 0 { |
| 189 | 186 | break |
| 190 | 187 | } |
| 191 | 188 | } |
| 192 | 189 | |
| 190 | filled -= curWidth + refWidth | |
| 193 | 191 | padWidth := width - filled |
| 194 | 192 | padding := make([][]byte, 0, padWidth) |
| 195 | 193 | for padWidth > 0 && padWidth >= s.components[iPadding].width { |
| 196 | 194 | padding = append(padding, s.components[iPadding].bytes) |
| 197 | filled += s.components[iPadding].width | |
| 198 | 195 | padWidth -= s.components[iPadding].width |
| 199 | 196 | if s.components[iPadding].width == 0 { |
| 200 | 197 | break |
| 201 | 198 | } |
| 202 | 199 | } |
| 203 | 200 | |
| 204 | truncWidth := width - filled | |
| 205 | for truncWidth > 0 { | |
| 201 | for padWidth > 0 { | |
| 206 | 202 | padding = append(padding, []byte("…")) |
| 207 | truncWidth-- | |
| 203 | padWidth-- | |
| 208 | 204 | } |
| 209 | 205 | |
| 210 | 206 | s.flush(w, filling, padding) |