Skip to content

Commit

Permalink
Adding lscolumns to the retriever explicitly, since there are still w…
Browse files Browse the repository at this point in the history
…eird install issues.
  • Loading branch information
bendmorris committed Apr 1, 2013
1 parent 31a7d8e commit a480a74
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
41 changes: 41 additions & 0 deletions lscolumns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import sys
from term_size import get_terminal_size


def get_columns(values, cols):
columns = []
col_size = len(values) / cols
extra = len(values) % cols
n = 0
for i in range(cols):
s = col_size
if i+1 <= extra: s += 1
this_column = values[n:n+s]
columns.append(this_column)
n += s
return columns


def printls(values, max_width=None, spacing=2):
if sys.stdout.isatty() and max_width is None:
cols,lines = get_terminal_size()
max_width = cols

if max_width:
# if output to terminal or max_width is specified, use column output

for cols in [int(len(values) / float(i) + 0.5) for i in range(1, len(values) + 1)]:
columns = get_columns(values, cols)
widths = [max([len(c) for c in column])+spacing for column in columns]
if sum(widths) < max_width:
break

for pos in range(len(columns[0])):
for column, width in zip(columns, widths):
if len(column) > pos:
print column[pos].ljust(width-1),
print

else:
# otherwise, just output each value, one per line
for value in values: print value
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ def clean_version(v):
},
install_requires=[
'xlrd',
'lscolumns',
],

# py2exe flags
Expand Down
24 changes: 24 additions & 0 deletions term_size.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import os

def get_terminal_size():
env = os.environ
def ioctl_GWINSZ(fd):
try:
import fcntl, termios, struct, os
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,
'1234'))
except:
return
return cr
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
cr = (env.get('LINES', 25), env.get('COLUMNS', 80))

return int(cr[1]), int(cr[0])

0 comments on commit a480a74

Please sign in to comment.