Permalink
Browse files

refactored grid formatter

  • Loading branch information...
lavelle committed Feb 10, 2013
1 parent 97ef934 commit 054c201f41ff70aafc49caaaa470d0dd72d8e191
Showing with 19 additions and 7 deletions.
  1. +14 −6 gridformat.py
  2. +4 −0 readme.md
  3. +1 −1 setup.py
View
@@ -16,8 +16,8 @@ def format_grid(grid, divider_positions=[], truncatable_column=None):
# Find the longest cell in each column so the minimum width is known
widths = [max([len(cell) for cell in row]) for row in zip(*grid)]
+ # Total number of characters in the contents of the widest row in the table
total = sum(widths)
-
# Number of columns in the grid
num_columns = len(grid[0])
# Number of characters in the string used to separate columns
@@ -26,6 +26,12 @@ def format_grid(grid, divider_positions=[], truncatable_column=None):
separators_width = num_columns * separator_width
# Default width for a lot of systems
terminal_width = 80
+ # Format string for each cell of the table
+ cell_template = ' {:%d} |'
+ # Format string for each row of the table
+ row_template = (cell_template * len(widths))[:-1]
+ # String used to signify that a string has been truncated
+ truncation_marker = '...'
# Try to determine the width of the user's terminal window in characters
# so the rows can be truncated if they are too long
@@ -47,12 +53,15 @@ def format_grid(grid, divider_positions=[], truncatable_column=None):
if truncatable_column is not None:
terminal_diff = terminal_width - total - separators_width
# Reduce the width of the column by the difference
- widths[2] += terminal_diff
+ widths[truncatable_column] += terminal_diff
else:
print 'The grid is wider than your terminal but no columns can be truncated so ' \
'some columns may not be visible.'
+ # Interpolate the widths of each column of the table into the row format string
+ row_template = row_template % tuple(widths)
+
# Final grid array
a = []
@@ -61,15 +70,14 @@ def format_grid(grid, divider_positions=[], truncatable_column=None):
# id, url and date don't vary much in width, only the local path is worth truncating
if truncatable_column is not None and terminal_diff is not None and \
len(row[truncatable_column]) > widths[truncatable_column]:
- row[truncatable_column] = row[truncatable_column][:terminal_diff - 3] + '...'
+ row[truncatable_column] = row[truncatable_column][:terminal_diff - len(truncation_marker)] + truncation_marker
# Format the cells into a string with dividers between and add the row to the final grid
# and pad with spaces so all cells in the column are the same width
- s = ((' {:%d} |' * len(widths))[:-1] % tuple(widths)).format(*row)
- a.append(s)
+ a.append(row_template.format(*row))
# Add a horizontal divider below the column titles
- width = len(s)
+ width = len(a[0])
for pos, i in zip(divider_positions, xrange(len(divider_positions))):
a.insert(pos + i, '-' * width)
View
@@ -33,6 +33,10 @@ cd ClipCloud
## Changelog
+### 0.2.1
+- Improved grid formatter
+- Improved formatting of code and comments
+
### 0.2
- Fixed screenshot feature
View
@@ -5,7 +5,7 @@
setup(
name='ClipCloud',
- version='0.2',
+ version='0.2.1',
url='https://github.com/lavelle/ClipCloud',
author='Giles Lavelle',

0 comments on commit 054c201

Please sign in to comment.