Added automatic detection of terminal width on *NIX systems.
1 parent 208b4ab commit 7def838339216d55fb5d4f1c3a9d0b9c81ed627a
@tundra tundra authored on 12 Feb 2014
Showing 1 changed file
View
136
tdir
#!/usr//bin/env python
"""
tdir - Display Formatted Directory Listings
Copyright (c) 2001-2006 TundraWare Inc., All Rights Reserved.
Copyright (c) 2001-2014 TundraWare Inc., All Rights Reserved.
"""
 
# python Library Imports
 
import sys
 
# Version info
 
VERSION = "$Id: tdir,v 1.70 2006/05/12 21:31:01 tundra Exp $"
 
 
# Booleans
 
FALSE = 0 == 1
TRUE = not FALSE
VERSION = "$Id: tdir,v 1.71 2014/02/12 17:42:04 tundra Exp $"
 
 
# Supporting Functions
 
# Found at: http://stackoverflow.com/questions/566746/how-to-get-console-window-width-in-python
 
def GetTerminalSize():
 
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))
 
### Use get(key[, default]) instead of a try/catch
#try:
# cr = (env['LINES'], env['COLUMNS'])
#except:
# cr = (25, 80)
return int(cr[1]), int(cr[0])
 
# End of 'GetTerminalSize()'
 
 
# Output formatting constants.
 
OWIDTH = 80 # Output width
OWIDTH = 80 # Default output width fixed for non-*NIX systems
 
if os.name == 'posix': # On *NIX get the current window parameter instead
OWIDTH = GetTerminalSize()[0] - 1
 
COLWIDTH = 19 # Width of each column
TWIDTH = COLWIDTH - 1 # Text width
MAXCOL, INDENT = divmod(OWIDTH, COLWIDTH) # No of output cols & indent
PAD = " " # Padding character
DOTFILE = '.' # Dotfiles start with this
 
# Defaults
 
RECURSE = FALSE # No recursion
SHOWDIR = TRUE # Show directories in listing
SHOWDOT = TRUE # Show dotfiles in listing
SHOWFILE = TRUE # Show files in listing
SORTBYEXT = TRUE # Sort file names by extension
RECURSE = False # No recursion
SHOWDIR = True # Show directories in listing
SHOWDOT = True # Show dotfiles in listing
SHOWFILE = True # Show files in listing
SORTBYEXT = True # Sort file names by extension
 
 
def OrderByExtension(list):
ExtList = {}
if SHOWDIR or SHOWFILE :
sys.stdout.write("\n")
if len(DirList) > 0:
DirList.sort()
OutputColumns(DirList, TRUE)
OutputColumns(DirList, True)
if len(FileList) > 0:
if SORTBYEXT:
ExtList = OrderByExtension(FileList)
Ext = ExtList.keys()
FileList = ExtList[x]
if len(FileList) > 0:
FileList.sort()
sys.stdout.write("(" + x + ")\n")
OutputColumns(FileList, FALSE)
OutputColumns(FileList, False)
 
else:
FileList.sort()
OutputColumns(FileList, FALSE)
OutputColumns(FileList, False)
 
def BuildFileList (args, dir, files):
 
def Usage():
UsageInfo = (
("tdir " + VERSION.split()[2] +
" - Copyright (c) 2001-2006 TundraWare Inc., All Rights Reserved. \n", ""),
" - Copyright (c) 2001-2014 TundraWare Inc., All Rights Reserved. \n", ""),
(" usage: tdir [-DRdefhtv] [-c #] [-s c] [-w #] [dir...] where,\n\n", ""),
("-D", "Do not display dot files\n"),
("-R", "Recurse down each named directory tree\n"),
("-c #", "Column width\n"),
sys.exit(2)
for opt, val in opts:
if opt == "-D":
SHOWDOT = FALSE
SHOWDOT = False
if opt == "-R":
RECURSE = TRUE
RECURSE = True
if opt == "-c":
COLWIDTH = int(val)
if opt == "-d":
SHOWDIR = FALSE
SHOWDIR = False
if opt == "-e":
SORTBYEXT = FALSE
SORTBYEXT = False
if opt == "-f":
SHOWFILE = FALSE
SHOWFILE = False
if opt == "-h":
Usage()
sys.exit(0)
if opt == "-s":
SEP = val[0]
if opt == "-t":
RECURSE = TRUE
SHOWDIR = FALSE
SHOWFILE = FALSE
RECURSE = True
SHOWDIR = False
SHOWFILE = False
if opt == "-v":
sys.stdout.write(VERSION + "\n")
sys.exit(0)
if opt == "-w":