#!/usr/bin/python
import os
import sys
import stat
import string
# get the page width
f = os.popen("stty size","r")
ret_string = f.read()
ret_split = string.split(ret_string)
if len(ret_split)>1:
cols = int(ret_split[1])
else:
cols = 80
# parse the command line
path = "."
if len(sys.argv)>1:
if sys.argv[1]=="--help" or sys.argv[1]=="-h":
print "mat -- directory listing, sorted by type/extention"
print " version 1.1 pbeirne@mitelknowledge.com"
sys.exit(1)
path = sys.argv[1]
if path[-1]!='/':
path += '/'
mat = os.listdir(path)
def extractExt(x):
s = os.lstat(path+x)
if stat.S_ISDIR(s[0]):
return "<dir>",x
if x[0]==".":
return ".",x[1:]
r = x.rfind(".")
if r>-1:
return x[r:],x[:r] #,x[r+1:]
if x[-1]=="~":
return "~",x[:-1] #,"~"
return "",x
mat_exts = map(extractExt,mat)
mat_exts.sort()
curext = None
pos = 0
for ext,fn in mat_exts:
# new ext -> new line
if ext!=curext:
if curext!=None:
print
print ext,
pos = len(ext)+1
curext = ext
else:
# right wrap -> another reason for new line
if ((pos+9)&~7)+len(fn) > cols:
print
pos = 0
print '\t'+fn,
# add 8 for the tab, one for the space, then the text
pos = ((pos+9)&~7) + len(fn)
print