| 204 | |
| 205 | |
| 206 | class MultiTable: |
| 207 | def __init__( |
| 208 | self, |
| 209 | terminal_width=None, |
| 210 | initial_section=True, |
| 211 | column_separator='|', |
| 212 | terminal=None, |
| 213 | styler=None, |
| 214 | auto_reformat=True, |
| 215 | ): |
| 216 | self._auto_reformat = auto_reformat |
| 217 | if initial_section: |
| 218 | self._current_section = Section() |
| 219 | self._sections = [self._current_section] |
| 220 | else: |
| 221 | self._current_section = None |
| 222 | self._sections = [] |
| 223 | if styler is None: |
| 224 | # Move out to factory. |
| 225 | if is_a_tty(): |
| 226 | self._styler = ColorizedStyler() |
| 227 | else: |
| 228 | self._styler = Styler() |
| 229 | else: |
| 230 | self._styler = styler |
| 231 | self._rendering_index = 0 |
| 232 | self._column_separator = column_separator |
| 233 | if terminal_width is None: |
| 234 | self._terminal_width = determine_terminal_width() |
| 235 | |
| 236 | def add_title(self, title): |
| 237 | self._current_section.add_title(title) |
| 238 | |
| 239 | def add_row_header(self, headers): |
| 240 | self._current_section.add_header(headers) |
| 241 | |
| 242 | def add_row(self, row_elements): |
| 243 | self._current_section.add_row(row_elements) |
| 244 | |
| 245 | def new_section(self, title, indent_level=0): |
| 246 | self._current_section = Section() |
| 247 | self._sections.append(self._current_section) |
| 248 | self._current_section.add_title(title) |
| 249 | self._current_section.indent_level = indent_level |
| 250 | |
| 251 | def render(self, stream): |
| 252 | max_width = self._calculate_max_width() |
| 253 | should_convert_table = self._determine_conversion_needed(max_width) |
| 254 | if should_convert_table: |
| 255 | convert_to_vertical_table(self._sections) |
| 256 | max_width = self._calculate_max_width() |
| 257 | stream.write('-' * max_width + '\n') |
| 258 | for section in self._sections: |
| 259 | self._render_section(section, max_width, stream) |
| 260 | |
| 261 | def _determine_conversion_needed(self, max_width): |
| 262 | # If we don't know the width of the controlling terminal, |
| 263 | # then we don't try to resize the table. |