mailr16581 - /branches/uf_redesign/prompt/uf_docstring.py


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by edward on June 01, 2012 - 10:58:
Author: bugman
Date: Fri Jun  1 10:58:27 2012
New Revision: 16581

URL: http://svn.gna.org/viewcvs/relax?rev=16581&view=rev
Log:
Created an algorithm for wrapping table columns to fit within 
status.text_width.

This if for the prompt user function docstring tables.  The columns are now 
dynamically wrapped so
that the table will not be wider than the given width.  For example the 
value.set user function
description table would normally have a total width of 238 characters, but 
this now fits within one
screen of text under all operating systems.


Modified:
    branches/uf_redesign/prompt/uf_docstring.py

Modified: branches/uf_redesign/prompt/uf_docstring.py
URL: 
http://svn.gna.org/viewcvs/relax/branches/uf_redesign/prompt/uf_docstring.py?rev=16581&r1=16580&r2=16581&view=diff
==============================================================================
--- branches/uf_redesign/prompt/uf_docstring.py (original)
+++ branches/uf_redesign/prompt/uf_docstring.py Fri Jun  1 10:58:27 2012
@@ -24,6 +24,7 @@
 """The base class for all the user function classes."""
 
 # Python module imports.
+from copy import deepcopy
 from string import split
 from textwrap import wrap
 
@@ -117,21 +118,114 @@
     free_space = status.text_width - used
 
     # The total table width.
-    total_width = sum(widths)
+    free_width = sum(widths)
+    total_width = free_width + used
+
+    # Column wrapping.
+    if free_width > free_space:
+        # Debugging printouts.
+        if status.debug:
+            print
+            print("Table column wrapping algorithm:")
+            print("%-20s %s" % ("num_cols:", num_cols))
+            print("%-20s %s" % ("free space:", free_space))
+            print("%-20s %s" % ("total_width:", total_width))
+
+        # New structures.
+        new_widths = deepcopy(widths)
+        num_cols_wrap = num_cols
+        free_space_wrap = free_space
+        col_wrap = [True] * num_cols
+
+        # Loop.
+        while 1:
+            # The average column width.
+            ave_width = free_space_wrap / num_cols_wrap
+
+            # Debugging printout.
+            if status.debug:
+                print("    %-20s %s" % ("ave_width:", ave_width))
+
+            # Rescale.
+            rescale = False
+            for i in range(num_cols):
+                # Remove the column from wrapping if smaller than the 
average wrapped width.
+                if col_wrap[i] and new_widths[i] < ave_width:
+                    # Recalculate.
+                    free_space_wrap = free_space_wrap - new_widths[i]
+                    num_cols_wrap -= 1
+                    rescale = True
+
+                    # Remove the column from wrapping.
+                    col_wrap[i] = False
+
+                    # Debugging printout.
+                    if status.debug:
+                        print("        %-20s %s" % ("remove column:", i))
+
+            # Done.
+            if not rescale:
+                # Set the column widths.
+                for i in range(num_cols):
+                    if new_widths[i] > ave_width:
+                        new_widths[i] = ave_width
+                break
+
+            # New total width.
+            total_width = status.text_width
+
+        # Debugging printouts.
+        if status.debug:
+            print("    %-20s %s" % ("widths:", widths))
+            print("    %-20s %s" % ("new_widths:", new_widths))
+            print("    %-20s %s" % ("num_cols:", num_cols))
+            print("    %-20s %s" % ("num_cols_wrap:", num_cols_wrap))
+            print("    %-20s %s" % ("free_space:", free_space))
+            print("    %-20s %s" % ("free_space_wrap:", free_space_wrap))
+            print("    %-20s %s" % ("col_wrap:", col_wrap))
+
+    # No column wrapping.
+    else:
+        new_widths = widths
+        col_wrap = [False] * num_cols
 
     # The header.
-    text += "_" * (total_width+used) + "\n"    # Top rule.
-    text += table_line(widths=widths)    # Blank line.
-    text += table_line(text=table[0], widths=widths)    # The headers.
-    text += table_line(widths=widths, bottom=True)    # Middle rule.
+    text += "_" * total_width + "\n"    # Top rule.
+    text += table_line(widths=new_widths)    # Blank line.
+    text += table_line(text=table[0], widths=new_widths)    # The headers.
+    text += table_line(widths=new_widths, bottom=True)    # Middle rule.
 
     # The table contents.
     for i in range(1, num_rows):
-        text += table_line(widths=widths)    # Blank line.
-        text += table_line(text=table[i], widths=widths)    # The contents.
+        # Column text, with wrapping.
+        col_text = [table[i]]
+        num_lines = 1
+        for j in range(num_cols):
+            if col_wrap[j]:
+                # Wrap.
+                lines = wrap(col_text[0][j], new_widths[j])
+
+                # Count the lines.
+                num_lines = len(lines)
+
+                # Replace the column text.
+                for k in range(num_lines):
+                    # New row of empty text.
+                    if len(col_text) <= k:
+                        col_text.append(['']*num_cols)
+
+                    # Pack the data.
+                    col_text[k][j] = lines[k]
+
+        # Blank line.
+        text += table_line(widths=new_widths)
+
+        # The contents.
+        for k in range(num_lines):
+            text += table_line(text=col_text[k], widths=new_widths)
 
     # The bottom.
-    text += table_line(widths=widths, bottom=True)    # Bottom rule.
+    text += table_line(widths=new_widths, bottom=True)    # Bottom rule.
 
     # Add a newline.
     text += '\n'




Related Messages


Powered by MHonArc, Updated Fri Jun 01 11:20:01 2012