MCPcopy Create free account
hub / github.com/aws/aws-cli / calculate_column_widths

Method calculate_column_widths

awscli/table.py:398–425  ·  view source on GitHub ↗
(self, padding=0, max_width=None)

Source from the content-addressed store, hash-verified

396 )
397
398 def calculate_column_widths(self, padding=0, max_width=None):
399 # postcondition: sum(widths) == max_width
400 unscaled_widths = [w + padding for w in self._max_widths]
401 if max_width is None:
402 return unscaled_widths
403 if not unscaled_widths:
404 return unscaled_widths
405 else:
406 # Compute scale factor for max_width.
407 scale_factor = max_width / float(sum(unscaled_widths))
408 scaled = [int(round(scale_factor * w)) for w in unscaled_widths]
409 # Once we've scaled the columns, we may be slightly over/under
410 # the amount we need so we have to adjust the columns.
411 off_by = sum(scaled) - max_width
412 while off_by != 0:
413 iter_order = range(len(scaled))
414 if off_by < 0:
415 iter_order = reversed(iter_order)
416 for i in iter_order:
417 if off_by > 0:
418 scaled[i] -= 1
419 off_by -= 1
420 else:
421 scaled[i] += 1
422 off_by += 1
423 if off_by == 0:
424 break
425 return scaled
426
427 def total_width(self, padding=0, with_border=False, outer_padding=0):
428 total = 0

Calls

no outgoing calls