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