Package prompt :: Module uf_docstring
[hide private]
[frames] | no frames]

Source Code for Module prompt.uf_docstring

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2009-2012 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax.                                     # 
  6  #                                                                             # 
  7  # relax is free software; you can redistribute it and/or modify               # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation; either version 2 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # relax is distributed in the hope that it will be useful,                    # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with relax; if not, write to the Free Software                        # 
 19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23  # Module docstring. 
 24  """The base class for all the user function classes.""" 
 25   
 26  # Python module imports. 
 27  from copy import deepcopy 
 28  from string import split 
 29  from textwrap import wrap 
 30   
 31  # relax module imports. 
 32  import ansi 
 33  import help 
 34  from relax_string import strip_lead 
 35  from status import Status; status = Status() 
 36  from user_functions.data import Uf_tables; uf_tables = Uf_tables() 
 37   
 38   
39 -def bold_text(text):
40 """Convert the text to bold. 41 42 This is for use in the help system. 43 44 @param text: The text to make bold. 45 @type text: str 46 @return: The bold text. 47 @rtype: str 48 """ 49 50 # Init. 51 new_text = '' 52 53 # Add the bold character to all characters. 54 for i in range(len(text)): 55 new_text += "%s\b%s" % (text[i], text[i]) 56 57 # Return the text. 58 return new_text
59 60
61 -def build_subtitle(text, bold=True, start_nl=True):
62 """Create the formatted subtitle string. 63 64 @param text: The name of the subtitle. 65 @type text: str 66 @keyword bold: A flag which if true will return bold text. Otherwise an underlined title will be returned. 67 @type bold: bool 68 @keyword start_nl: A flag which if True will add a newline to the start of the text. 69 @type start_nl: bool 70 @return: The formatted subtitle. 71 @rtype: str 72 """ 73 74 # Starting newline. 75 if start_nl: 76 new = "\n" 77 else: 78 new = "" 79 80 # Bold. 81 if bold: 82 new += "%s\n\n" % bold_text(text) 83 84 # Underline. 85 else: 86 new += "%s\n%s\n\n" % (text, "~"*len(text)) 87 88 # Return the subtitle. 89 return new
90 91
92 -def create_table(label):
93 """Format and return the table as text. 94 95 @param label: The unique table label. 96 @type label: str 97 @return: The formatted table. 98 @rtype: str 99 """ 100 101 # Get the table. 102 table = uf_tables.get_table(label) 103 104 # Initialise some variables. 105 text = '' 106 num_rows = len(table.cells) 107 num_cols = len(table.headings) 108 109 # The column widths. 110 widths = [] 111 for j in range(num_cols): 112 widths.append(len(table.headings[j])) 113 for i in range(num_rows): 114 for j in range(num_cols): 115 # The element is larger than the previous. 116 if len(table.cells[i][j]) > widths[j]: 117 widths[j] = len(table.cells[i][j]) 118 119 # The free space for the text. 120 used = 0 121 used += 2 # Start of the table ' '. 122 used += 2 # End of the table ' '. 123 used += 3 * (num_cols - 1) # Middle of the table ' '. 124 free_space = status.text_width - used 125 126 # The maximal width for all cells. 127 free_width = sum(widths) 128 129 # Column wrapping. 130 if free_width > free_space: 131 # Debugging printouts. 132 if status.debug: 133 print 134 print("Table column wrapping algorithm:") 135 print("%-20s %s" % ("num_cols:", num_cols)) 136 print("%-20s %s" % ("free space:", free_space)) 137 138 # New structures. 139 new_widths = deepcopy(widths) 140 num_cols_wrap = num_cols 141 free_space_wrap = free_space 142 col_wrap = [True] * num_cols 143 144 # Loop. 145 while 1: 146 # The average column width. 147 ave_width = free_space_wrap / num_cols_wrap 148 149 # Debugging printout. 150 if status.debug: 151 print(" %-20s %s" % ("ave_width:", ave_width)) 152 153 # Rescale. 154 rescale = False 155 for i in range(num_cols): 156 # Remove the column from wrapping if smaller than the average wrapped width. 157 if col_wrap[i] and new_widths[i] < ave_width: 158 # Recalculate. 159 free_space_wrap = free_space_wrap - new_widths[i] 160 num_cols_wrap -= 1 161 rescale = True 162 163 # Remove the column from wrapping. 164 col_wrap[i] = False 165 166 # Debugging printout. 167 if status.debug: 168 print(" %-20s %s" % ("remove column:", i)) 169 170 # Done. 171 if not rescale: 172 # Set the column widths. 173 for i in range(num_cols): 174 if new_widths[i] > ave_width: 175 new_widths[i] = ave_width 176 break 177 178 # Debugging printouts. 179 if status.debug: 180 print(" %-20s %s" % ("widths:", widths)) 181 print(" %-20s %s" % ("new_widths:", new_widths)) 182 print(" %-20s %s" % ("num_cols:", num_cols)) 183 print(" %-20s %s" % ("num_cols_wrap:", num_cols_wrap)) 184 print(" %-20s %s" % ("free_space:", free_space)) 185 print(" %-20s %s" % ("free_space_wrap:", free_space_wrap)) 186 print(" %-20s %s" % ("col_wrap:", col_wrap)) 187 188 # No column wrapping. 189 else: 190 new_widths = widths 191 col_wrap = [False] * num_cols 192 193 # The total table width. 194 total_width = sum(new_widths) + used 195 196 # The header. 197 text += " " + "_" * (total_width - 2) + "\n" # Top rule. 198 text += table_line(widths=new_widths) # Blank line. 199 text += table_line(text=table.headings, widths=new_widths) # The headings. 200 text += table_line(widths=new_widths, bottom=True) # Middle rule. 201 202 # The table contents. 203 for i in range(num_rows): 204 # Column text, with wrapping. 205 col_text = [table.cells[i]] 206 num_lines = 1 207 for j in range(num_cols): 208 if col_wrap[j]: 209 # Wrap. 210 lines = wrap(col_text[0][j], new_widths[j]) 211 212 # Count the lines. 213 num_lines = len(lines) 214 215 # Replace the column text. 216 for k in range(num_lines): 217 # New row of empty text. 218 if len(col_text) <= k: 219 col_text.append(['']*num_cols) 220 221 # Pack the data. 222 col_text[k][j] = lines[k] 223 224 # Blank line (between rows when asked, and for the first row after the header). 225 if table.spacing or i == 0: 226 text += table_line(widths=new_widths) 227 228 # The contents. 229 for k in range(num_lines): 230 text += table_line(text=col_text[k], widths=new_widths) 231 232 # The bottom. 233 text += table_line(widths=new_widths, bottom=True) # Bottom rule. 234 235 # Add a newline. 236 text += '\n' 237 238 # Return the table text. 239 return text
240 241
242 -def format_text(text):
243 """Format the line of text by wrapping. 244 245 @param text: The line of text to wrap. 246 @type text: str 247 @return: The wrapped text. 248 @rtype: str 249 """ 250 251 # Then wrap each line. 252 new_text = "" 253 254 # Wrap the line. 255 for wrapped_line in wrap(text, status.text_width): 256 new_text += wrapped_line + "\n" 257 258 # Return the formatted text. 259 return new_text
260 261
262 -def table_line(text=None, widths=None, bottom=False):
263 """Format a line of a table. 264 265 @keyword text: The list of table elements. If not given, an empty line will be be produced. 266 @type text: list of str or None 267 @keyword widths: The list of column widths for the table. 268 @type widths: list of int 269 @keyword botton: A flag which if True will cause a table bottom line to be produced. 270 @type bottom: bool 271 @return: The table line. 272 @rtype: str 273 """ 274 275 # Initialise. 276 if bottom: 277 line = " _" 278 else: 279 line = " " 280 281 # Loop over the columns. 282 for i in range(len(widths)): 283 # The column separator. 284 if i > 0: 285 if bottom: 286 line += "___" 287 else: 288 line += " " 289 290 # A bottom line. 291 if bottom: 292 line += "_" * widths[i] 293 294 # Empty line. 295 elif text == None: 296 line += " " * widths[i] 297 298 # The text. 299 else: 300 line += text[i] 301 line += " " * (widths[i] - len(text[i])) 302 303 # Close the line. 304 if bottom: 305 line += "_ \n" 306 else: 307 line += " \n" 308 309 # Return the text. 310 return line
311