Created
April 9, 2015 13:22
-
-
Save eni23/e50c0732ec7fe84543ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf8 -*- | |
import os | |
import sys | |
import math | |
import fcntl | |
import struct | |
import termios | |
def termsize(): | |
import os | |
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]) | |
def table(header, data, tablewidth=100): | |
head_start='\033[1m' | |
head_end='\033[0m' | |
trmwidth=termsize()[0] | |
width=math.floor(float(float(trmwidth)/100)*tablewidth) | |
headstr='' | |
divider='' | |
widths=[] | |
for col in header: | |
cwidth = int( ( float(width) / 100 ) * col['width'] ) | |
if (len(col['title'])) > cwidth: | |
col['title']=col['title'][0:cwidth-5] + ".." | |
divider+='+' | |
for c in range(int(cwidth)-1): divider+='-' | |
cstr='| ' + head_start + col['title'].ljust(cwidth-2,' ') + head_end | |
headstr+=cstr | |
widths.append(cwidth) | |
divider+='+' | |
headstr+='|' | |
table=divider+'\n'+headstr+'\n'+divider+'\n' | |
tdata='' | |
for row in data: | |
i=0 | |
cstr='' | |
for col in row: | |
if (len(col)) > widths[i]: | |
col=col[0:widths[i]-5] + ".." | |
cstr+='| ' + col.ljust(widths[i]-2,' ') | |
i+=1 | |
tdata+=cstr+'|\n'+divider+'\n' | |
table+=tdata[:-1] | |
return table | |
if __name__ == '__main__': | |
header = [ | |
{ 'title':'Time', 'width': 10 }, | |
{ 'title':'Customer', 'width': 10 }, | |
{ 'title':'Project', 'width': 10 }, | |
{ 'title':'Activtiy', 'width': 70 }, | |
] | |
data = [ | |
['d11','d12','jhdfjkhdghdfghsdfuighsfduighsduihsduighsgduih','test'], | |
['d21','d22','d23','test2'], | |
['d31','d32','d33','fuba'], | |
['d41','d42','d43','baz'], | |
['d51','d52','d53','1udh'], | |
['d61','d62','d63','quoo'], | |
] | |
print table(header, data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment