summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorluke@maurits.id.au <luke@maurits.id.au@0f58610c-415a-11de-9c03-5d6cfad8e937>2013-10-07 19:39:00 +0000
committerluke@maurits.id.au <luke@maurits.id.au@0f58610c-415a-11de-9c03-5d6cfad8e937>2013-10-07 19:39:00 +0000
commit7a3fd33c87fc5acdd971dd618439d3bed892cfcc (patch)
tree842fe2e1e01c603aab62baffebbd883a9c796f08
parentd84a07698867b5b4a1e2e55b0e55a1e1294f9fa6 (diff)
downloadpython-prettytable-7a3fd33c87fc5acdd971dd618439d3bed892cfcc.tar.gz
Add padding to titles and make table expand to fit title if necessary.
git-svn-id: http://prettytable.googlecode.com/svn/trunk@141 0f58610c-415a-11de-9c03-5d6cfad8e937
-rw-r--r--prettytable.py25
1 files changed, 18 insertions, 7 deletions
diff --git a/prettytable.py b/prettytable.py
index 37c06ce..5bb4365 100644
--- a/prettytable.py
+++ b/prettytable.py
@@ -33,11 +33,12 @@ __version__ = "trunk"
import copy
import csv
+import itertools
+import math
import random
import re
import sys
import textwrap
-import itertools
import unicodedata
py3k = sys.version_info[0] >= 3
@@ -1001,16 +1002,24 @@ class PrettyTable(object):
if table_width > self._max_table_width:
# Shrink widths in proportion
scale = 1.0*self._max_table_width / table_width
- widths = [int(w*scale) for w in widths]
+ widths = [int(math.floor(w*scale)) for w in widths]
self._widths = widths
- # Are we under min_table_width?
- if self._min_table_width:
+ # Are we under min_table_width or title width?
+ if self._min_table_width or options["title"]:
+ if options["title"]:
+ title_width = len(options["title"])+sum(self._get_padding_widths(options))
+ if options["vrules"] in (FRAME, ALL):
+ title_width += 2
+ else:
+ title_width = 0
+ min_table_width = self.min_table_width or 0
+ min_width = max(title_width, min_table_width)
table_width = self._compute_table_width(options)
- if table_width < self._min_table_width:
+ if table_width < min_width:
# Grow widths in proportion
- scale = 1.0*self._min_table_width / table_width
- widths = [int(w*scale) for w in widths]
+ scale = 1.0*min_width / table_width
+ widths = [int(math.ceil(w*scale)) for w in widths]
self._widths = widths
def _get_padding_widths(self, options):
@@ -1159,6 +1168,7 @@ class PrettyTable(object):
def _stringify_title(self, title, options):
lines = []
+ lpad, rpad = self._get_padding_widths(options)
if options["border"]:
if options["vrules"] == ALL:
options["vrules"] = FRAME
@@ -1169,6 +1179,7 @@ class PrettyTable(object):
bits = []
endpoint = options["vertical_char"] if options["vrules"] in (ALL, FRAME) else " "
bits.append(endpoint)
+ title = " "*lpad + title + " "*rpad
bits.append(self._justify(title, len(self._hrule)-2, "c"))
bits.append(endpoint)
lines.append("".join(bits))