1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 """The base class for all the user function classes."""
25
26
27 from copy import deepcopy
28 from string import split
29 from textwrap import wrap
30
31
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
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
51 new_text = ''
52
53
54 for i in range(len(text)):
55 new_text += "%s\b%s" % (text[i], text[i])
56
57
58 return new_text
59
60
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
75 if start_nl:
76 new = "\n"
77 else:
78 new = ""
79
80
81 if bold:
82 new += "%s\n\n" % bold_text(text)
83
84
85 else:
86 new += "%s\n%s\n\n" % (text, "~"*len(text))
87
88
89 return new
90
91
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
102 table = uf_tables.get_table(label)
103
104
105 text = ''
106 num_rows = len(table.cells)
107 num_cols = len(table.headings)
108
109
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
116 if len(table.cells[i][j]) > widths[j]:
117 widths[j] = len(table.cells[i][j])
118
119
120 used = 0
121 used += 2
122 used += 2
123 used += 3 * (num_cols - 1)
124 free_space = status.text_width - used
125
126
127 free_width = sum(widths)
128
129
130 if free_width > free_space:
131
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
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
145 while 1:
146
147 ave_width = free_space_wrap / num_cols_wrap
148
149
150 if status.debug:
151 print(" %-20s %s" % ("ave_width:", ave_width))
152
153
154 rescale = False
155 for i in range(num_cols):
156
157 if col_wrap[i] and new_widths[i] < ave_width:
158
159 free_space_wrap = free_space_wrap - new_widths[i]
160 num_cols_wrap -= 1
161 rescale = True
162
163
164 col_wrap[i] = False
165
166
167 if status.debug:
168 print(" %-20s %s" % ("remove column:", i))
169
170
171 if not rescale:
172
173 for i in range(num_cols):
174 if new_widths[i] > ave_width:
175 new_widths[i] = ave_width
176 break
177
178
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
189 else:
190 new_widths = widths
191 col_wrap = [False] * num_cols
192
193
194 total_width = sum(new_widths) + used
195
196
197 text += " " + "_" * (total_width - 2) + "\n"
198 text += table_line(widths=new_widths)
199 text += table_line(text=table.headings, widths=new_widths)
200 text += table_line(widths=new_widths, bottom=True)
201
202
203 for i in range(num_rows):
204
205 col_text = [table.cells[i]]
206 num_lines = 1
207 for j in range(num_cols):
208 if col_wrap[j]:
209
210 lines = wrap(col_text[0][j], new_widths[j])
211
212
213 num_lines = len(lines)
214
215
216 for k in range(num_lines):
217
218 if len(col_text) <= k:
219 col_text.append(['']*num_cols)
220
221
222 col_text[k][j] = lines[k]
223
224
225 if table.spacing or i == 0:
226 text += table_line(widths=new_widths)
227
228
229 for k in range(num_lines):
230 text += table_line(text=col_text[k], widths=new_widths)
231
232
233 text += table_line(widths=new_widths, bottom=True)
234
235
236 text += '\n'
237
238
239 return text
240
241
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
252 new_text = ""
253
254
255 for wrapped_line in wrap(text, status.text_width):
256 new_text += wrapped_line + "\n"
257
258
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
276 if bottom:
277 line = " _"
278 else:
279 line = " "
280
281
282 for i in range(len(widths)):
283
284 if i > 0:
285 if bottom:
286 line += "___"
287 else:
288 line += " "
289
290
291 if bottom:
292 line += "_" * widths[i]
293
294
295 elif text == None:
296 line += " " * widths[i]
297
298
299 else:
300 line += text[i]
301 line += " " * (widths[i] - len(text[i]))
302
303
304 if bottom:
305 line += "_ \n"
306 else:
307 line += " \n"
308
309
310 return line
311