Clean up find command, move code to utils.

This commit is contained in:
Todd Gamblin
2014-04-14 12:53:23 -07:00
parent 1e51daab08
commit 1f9dfeb9b5
5 changed files with 99 additions and 58 deletions

View File

@@ -31,6 +31,22 @@
# Ignore emacs backups when listing modules
ignore_modules = [r'^\.#', '~$']
def partition_list(elements, predicate):
"""Partition a list into two lists, the first containing elements
for which the predicate evaluates to true, the second containing
those for which it is false.
"""
trues = []
falses = []
for elt in elements:
if predicate(elt):
trues.append(elt)
else:
falses.append(elt)
return trues, falses
def caller_locals():
"""This will return the locals of the *parent* of the caller.
This allows a fucntion to insert variables into its caller's

View File

@@ -23,7 +23,10 @@
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
import sys
import os
import textwrap
from StringIO import StringIO
from llnl.util.tty.color import *
debug = False
@@ -97,3 +100,61 @@ def get_number(prompt, **kwargs):
elif default is not None:
number = default
return number
def hline(label=None, **kwargs):
"""Draw an optionally colored or labeled horizontal line.
Options:
char Char to draw the line with. Default '-'
color Color of the label. Default is no color.
max_width Maximum width of the line. Default is 64 chars.
See tty.color for possible color formats.
"""
char = kwargs.get('char', '-')
color = kwargs.get('color', '')
max_width = kwargs.get('max_width', 64)
cols, rows = terminal_size()
if not cols:
cols = max_width
else:
cols -= 2
cols = min(max_width, cols)
label = str(label)
prefix = char * 2 + " " + label + " "
suffix = (cols - len(prefix)) * char
out = StringIO()
if color:
prefix = char * 2 + " " + color + cescape(label) + "@. "
cwrite(prefix, stream=out, color=True)
else:
out.write(prefix)
out.write(suffix)
print out.getvalue()
def terminal_size():
"""Gets the dimensions of the console: cols, rows."""
def ioctl_GWINSZ(fd):
try:
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 = (os.environ.get('LINES', 25), os.environ.get('COLUMNS', 80))
return int(cr[1]), int(cr[0])

View File

@@ -38,27 +38,7 @@
import termios
import struct
def get_terminal_size():
"""Get the dimensions of the console."""
def ioctl_GWINSZ(fd):
try:
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 = (os.environ.get('LINES', 25), os.environ.get('COLUMNS', 80))
return int(cr[1]), int(cr[0])
from llnl.util.tty import terminal_size
class ColumnConfig:
def __init__(self, cols):
@@ -135,7 +115,7 @@ def colify(elts, **options):
console_cols = options.get("cols", None)
if not console_cols:
console_cols, console_rows = get_terminal_size()
console_cols, console_rows = terminal_size()
elif type(console_cols) != int:
raise ValueError("Number of columns must be an int")
console_cols = max(1, console_cols - indent)
@@ -170,7 +150,7 @@ def colify(elts, **options):
if __name__ == "__main__":
import optparse
cols, rows = get_terminal_size()
cols, rows = terminal_size()
parser = optparse.OptionParser()
parser.add_option("-u", "--uniform", action="store_true", default=False,
help="Use uniformly sized columns instead of variable-size.")