Commit 192b8a91 authored by Mario Hock's avatar Mario Hock
Browse files

fixed rounding issues in colored CPU bar

parent 630c76f0
...@@ -116,6 +116,7 @@ def display(measurement): ...@@ -116,6 +116,7 @@ def display(measurement):
# static labels # static labels
stdscr.addstr(y, 1, 'CPU{0}'.format( num ), curses.color_pair(1)) stdscr.addstr(y, 1, 'CPU{0}'.format( num ), curses.color_pair(1))
stdscr.addstr(y, 20, 'util:', curses.color_pair(2)) stdscr.addstr(y, 20, 'util:', curses.color_pair(2))
stdscr.addstr(y, 46, '|', curses.color_pair(2))
stdscr.addstr(y, 50, 'user:', curses.color_pair(2)) stdscr.addstr(y, 50, 'user:', curses.color_pair(2))
stdscr.addstr(y, 66, 'system:', curses.color_pair(2)) stdscr.addstr(y, 66, 'system:', curses.color_pair(2))
......
...@@ -126,21 +126,32 @@ def split_proprtionally(text, weights, size=0, fill=" "): ...@@ -126,21 +126,32 @@ def split_proprtionally(text, weights, size=0, fill=" "):
total_weights = float( sum(weights) ) total_weights = float( sum(weights) )
## Calculate an int for each weight so that they sum appropriately to |size|. ## Calculate an int for each weight so that they sum appropriately to |size|.
weighted_lengths = [ int(round( (w / total_weights)*size )) for w in weights ] float_lengths = [ (w / total_weights)*size for w in weights ]
weighted_lengths = [ int(round( f )) for f in float_lengths ]
## XXX debugging...
if( sum(weighted_lengths) != size ): ## Compensate rounding-related inconsistencies.
## TODO can this fail due to rounding issues? # XXX I hope this actually does what's supposed to do...
## --> What do we do then? Expand smallest/shrink biggest? # (Increase/decrease the fields with the biggest rounding differences in order to fit the size)
diff = size - sum(weighted_lengths)
while( diff != 0 ):
## XXX Hotfix.. sign = -1 if diff < 0 else 1
if( sum(weighted_lengths)+1 == size ):
weighted_lengths[0] += 1 ## Calculate index where the rounding produced the biggest difference.
else: # (On equality, the latter one wins.)
print( weighted_lengths ) max_diff = 0
index_of_max_diff = None
assert( sum(weighted_lengths) == size ) for i in range( len(weighted_lengths) ):
cur_diff = ( float_lengths[i] - weighted_lengths[i] ) * sign
if ( cur_diff >= max_diff ):
max_diff = cur_diff
index_of_max_diff = i
## Increase the just found index by 1.
weighted_lengths[i] += sign
diff -= sign
assert( sum(weighted_lengths) == size )
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment