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