Permalink
Browse files

More reusable and less lines

  • Loading branch information...
1 parent 6d66e9d commit 204ab2e7b52ae42ea9e465fb2704c4310a9de5ce @lavelle committed Apr 21, 2012
Showing with 18 additions and 16 deletions.
  1. +17 −15 lib/gridformat.py
  2. +1 −1 lib/history.py
View
32 lib/gridformat.py
@@ -1,18 +1,16 @@
-def format_grid(grid, divider_positions=[]):
+def format_grid(grid, divider_positions=[], truncatable_column=None):
"""
Format a 2d array of values in to a well-formatted, aligned gridstring
Arguments:
- grid: A rectangular 2d array of values
- - divider_positions: an array of integers specifying the rows of the grid to add dividers to
- defaults to no dividers.
+ - divider_positions: an array of integers specifying the rows of the grid to add dividers
+ to defaults to no dividers
+ - truncatable_column: An integer specifying a column whose data can be sensibly truncated
Returns: The grid as a nicely formatted string, with padding, dividers and
- truncation if it is wider than the terminal window.
+ truncation if it is wider than the terminal window
"""
- # find the longest cell in the column so the minimum width is known
- widths = []
- for y in [x for x in zip(*grid)]:
- widths.append(max([len(z) for z in y]))
-
+ # find the longest cell in each column so the minimum width is known
+ widths = [max([len(cell) for cell in row]) for row in [x for x in zip(*grid)]]
total = sum(widths)
# try to determine the width of the user's terminal window in characters
@@ -21,23 +19,27 @@ def format_grid(grid, divider_positions=[]):
import subprocess
terminal_width = int(subprocess.check_output('stty size').split()[1])
except:
- print 'The size of your terminal window could not be determined'
- print 'The layout of the grid below may be broken due to text wrapping'
+ print 'The size of your terminal window could not be determined so ' \
+ 'the layout of the grid below may be broken due to text wrapping.'
terminal_width = 80 # default width for a lot of systems
# if the grid is wider than the terminal window work out the difference between them
# this is the amount to truncate the grid by
terminal_diff = None
if total > terminal_width:
- terminal_diff = terminal_width - total - 12 # 12 is the number of dividers and spaces added
- widths[2] += terminal_diff # reduce the width of the column by the difference
+ if truncatable_column is not None:
+ terminal_diff = terminal_width - total - 12 # 12 is the number of dividers and spaces added
+ widths[2] += terminal_diff # reduce the width of the column by the difference
+ else:
+ print 'The grid is wider than your terminal but no columns can be truncated so ' \
+ 'some columns may not be visible.'
a = [] # final grid array
for row in grid:
# truncate column 3 if the table would be wider than the terminal window
# id, url and date don't vary much in width, only the local path is worth truncating
- if terminal_diff is not None and len(row[2]) > widths[2]:
- row[2] = row[2][:terminal_diff - 3] + '...'
+ 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] + '...'
# 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
View
2 lib/history.py
@@ -43,7 +43,7 @@ def display(self, limit, sort_by, direction, start):
limit -= 1
- print format_grid(grid, divider_positions=[1])
+ print format_grid(grid, divider_positions=[1], truncatable_column=2)
def add(self, path, url):
"""

0 comments on commit 204ab2e

Please sign in to comment.