changeset 9:71fb16088d54

add file for indentation; wth did my work go??? :(
author Jeff Hammel <k0scist@gmail.com>
date Fri, 17 Jan 2014 18:17:59 -0800
parents 22c830449604
children 466386702968
files textshaper/indent.py textshaper/main.py
diffstat 2 files changed, 54 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/textshaper/indent.py	Fri Jan 17 18:17:59 2014 -0800
@@ -0,0 +1,50 @@
+#!/usr/bin/env python
+
+"""
+indentation of text blocks
+"""
+
+import optparse
+import os
+import sys
+
+def indent(text, indentation=4, space=' ', strict=False):
+    """
+    indent a block of text
+
+    text -- lines of text to indent
+    indentation -- number of spaces to indent
+    space -- what to indent with
+    strict -- whether to enforce required whitespace for negative indentation
+    """
+
+    if not indentation:
+        # nothing to do
+        return text
+
+    if indentation > 0:
+        retval = [space * indentation + line for line in text]
+    else:
+        # negative indentation
+        indentation = -indentation
+        retval = []
+        for line in text:
+            prefix = line[:indentation]
+            for index, char in enumerate(prefix):
+                if not char == space:
+                    if strict:
+                        raise AssertionError("Found non-'%s' charcter at column %d for indentation -%d" % (space, index, indentation))
+                    break
+            else:
+                index = indentation
+            retval.append(line[index:])
+
+def main(args=sys.argv[1:]):
+
+    usage = '%prog [options]'
+    parser = optparse.OptionParser(usage=usage, description=__doc__)
+    options, args = parser.parse_args(args)
+
+
+if __name__ == '__main__':
+    main()
--- a/textshaper/main.py	Sun Oct 20 18:07:06 2013 -0700
+++ b/textshaper/main.py	Fri Jan 17 18:17:59 2014 -0800
@@ -20,6 +20,10 @@
 def add_options(parser):
     """add options to the OptionParser instance"""
 
+    # TODO
+    #    parser.add_option('-c', '--clip', '--copy', dest='copy_to_clipboard',
+    #                  help="copy to given program on exit")
+
 def main(args=sys.argv[1:]):
 
     # parse command line options