Skip to content

Commit

Permalink
Reduce jumping when using subcounters
Browse files Browse the repository at this point in the history
  • Loading branch information
avylove committed Sep 6, 2024
1 parent 4e598e1 commit 9db944c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
16 changes: 9 additions & 7 deletions enlighten/_counter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2017 - 2023 Avram Lubkin, All Rights Reserved
# Copyright 2017 - 2024 Avram Lubkin, All Rights Reserved

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down Expand Up @@ -722,9 +722,9 @@ def _format_bar(self, fields, iterations, width, elapsed, force_float):
barText = u''

if subcounters:
block_count = [int(barWidth * fields['percentage_0'] / 100)]
partial_len = (barLen - 1) if fields['count_0'] else barLen
remaining = []
remainder, count = math.modf(barWidth * fields['percentage_0'] / 100)
block_count = [int(count)]
remaining = [(remainder, 0)]

# Get full blocks for subcounters and preserve remainders
for idx, entry in enumerate(subcounters, 1):
Expand All @@ -734,8 +734,10 @@ def _format_bar(self, fields, iterations, width, elapsed, force_float):

# Until blocks are accounted for, add full blocks for highest remainders
remaining.sort()
while sum(block_count) < partial_len and remaining:
block_count[remaining.pop()[1]] += 1
while sum(block_count) < barLen and remaining:
remainder, idx = remaining.pop()
if remainder >= 0.85:
block_count[idx] += 1

# Format partial bars
for idx, subLen in reversed(list(enumerate(block_count))):
Expand All @@ -756,7 +758,7 @@ def _format_bar(self, fields, iterations, width, elapsed, force_float):

# If bar isn't complete, add partial block and fill
if barLen < barWidth:
if fields.get('count_0', self.count):
if self.count and not subcounters:
barText += self.series[int(round((complete - barLen) * (len(self.series) - 1)))]
partial_len += 1
barText += self.series[0] * (barWidth - partial_len)
Expand Down
18 changes: 17 additions & 1 deletion tests/test_subcounter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2017 - 2023 Avram Lubkin, All Rights Reserved
# Copyright 2017 - 2024 Avram Lubkin, All Rights Reserved

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down Expand Up @@ -354,6 +354,22 @@ def test_subcounter_count_0(self):
ctr.add_subcounter('blue', count=4)
ctr.add_subcounter('red', count=2)

formatted = ctr.format(width=80)
bartext = term.red(BLOCK) + term.blue(BLOCK*3) + term.yellow(BLOCK*35) + ' ' * 41
self.assertEqual(formatted, bartext)

def test_subcounter_roundinf(self):
"""
Extend subcounters to account for remainders
"""

ctr = self.manager.counter(stream=self.tty.stdout, total=300, bar_format=u'{bar}')
term = ctr.manager.term
ctr.count = 151
ctr.add_subcounter('yellow', count=132)
ctr.add_subcounter('blue', count=12)
ctr.add_subcounter('red', count=7)

formatted = ctr.format(width=80)
bartext = term.red(BLOCK*2) + term.blue(BLOCK*3) + term.yellow(BLOCK*35) + ' ' * 40
self.assertEqual(formatted, bartext)
Expand Down

0 comments on commit 9db944c

Please sign in to comment.