Show More
@@ -19,6 +19,7 b' from IPython.external.qt import QtCore, QtGui' | |||||
19 | from IPython.config.configurable import Configurable |
|
19 | from IPython.config.configurable import Configurable | |
20 | from IPython.frontend.qt.rich_text import HtmlExporter |
|
20 | from IPython.frontend.qt.rich_text import HtmlExporter | |
21 | from IPython.frontend.qt.util import MetaQObjectHasTraits, get_font |
|
21 | from IPython.frontend.qt.util import MetaQObjectHasTraits, get_font | |
|
22 | from IPython.utils.text import columnize | |||
22 | from IPython.utils.traitlets import Bool, Enum, Int, Unicode |
|
23 | from IPython.utils.traitlets import Bool, Enum, Int, Unicode | |
23 | from ansi_code_processor import QtAnsiCodeProcessor |
|
24 | from ansi_code_processor import QtAnsiCodeProcessor | |
24 | from completion_widget import CompletionWidget |
|
25 | from completion_widget import CompletionWidget | |
@@ -1295,52 +1296,7 b' class ConsoleWidget(Configurable, QtGui.QWidget):' | |||||
1295 | width = self._control.viewport().width() |
|
1296 | width = self._control.viewport().width() | |
1296 | char_width = QtGui.QFontMetrics(self.font).width(' ') |
|
1297 | char_width = QtGui.QFontMetrics(self.font).width(' ') | |
1297 | displaywidth = max(10, (width / char_width) - 1) |
|
1298 | displaywidth = max(10, (width / char_width) - 1) | |
1298 |
|
1299 | return columnize(items, separator, displaywidth) | ||
1299 | # Some degenerate cases. |
|
|||
1300 | size = len(items) |
|
|||
1301 | if size == 0: |
|
|||
1302 | return '\n' |
|
|||
1303 | elif size == 1: |
|
|||
1304 | return '%s\n' % items[0] |
|
|||
1305 |
|
||||
1306 | # Try every row count from 1 upwards |
|
|||
1307 | array_index = lambda nrows, row, col: nrows*col + row |
|
|||
1308 | for nrows in range(1, size): |
|
|||
1309 | ncols = (size + nrows - 1) // nrows |
|
|||
1310 | colwidths = [] |
|
|||
1311 | totwidth = -len(separator) |
|
|||
1312 | for col in range(ncols): |
|
|||
1313 | # Get max column width for this column |
|
|||
1314 | colwidth = 0 |
|
|||
1315 | for row in range(nrows): |
|
|||
1316 | i = array_index(nrows, row, col) |
|
|||
1317 | if i >= size: break |
|
|||
1318 | x = items[i] |
|
|||
1319 | colwidth = max(colwidth, len(x)) |
|
|||
1320 | colwidths.append(colwidth) |
|
|||
1321 | totwidth += colwidth + len(separator) |
|
|||
1322 | if totwidth > displaywidth: |
|
|||
1323 | break |
|
|||
1324 | if totwidth <= displaywidth: |
|
|||
1325 | break |
|
|||
1326 |
|
||||
1327 | # The smallest number of rows computed and the max widths for each |
|
|||
1328 | # column has been obtained. Now we just have to format each of the rows. |
|
|||
1329 | string = '' |
|
|||
1330 | for row in range(nrows): |
|
|||
1331 | texts = [] |
|
|||
1332 | for col in range(ncols): |
|
|||
1333 | i = row + nrows*col |
|
|||
1334 | if i >= size: |
|
|||
1335 | texts.append('') |
|
|||
1336 | else: |
|
|||
1337 | texts.append(items[i]) |
|
|||
1338 | while texts and not texts[-1]: |
|
|||
1339 | del texts[-1] |
|
|||
1340 | for col in range(len(texts)): |
|
|||
1341 | texts[col] = texts[col].ljust(colwidths[col]) |
|
|||
1342 | string += '%s\n' % separator.join(texts) |
|
|||
1343 | return string |
|
|||
1344 |
|
1300 | |||
1345 | def _get_block_plain_text(self, block): |
|
1301 | def _get_block_plain_text(self, block): | |
1346 | """ Given a QTextBlock, return its unformatted text. |
|
1302 | """ Given a QTextBlock, return its unformatted text. |
@@ -611,3 +611,69 b' class EvalFormatter(Formatter):' | |||||
611 | raise KeyError(key) |
|
611 | raise KeyError(key) | |
612 |
|
612 | |||
613 |
|
613 | |||
|
614 | def columnize(items, separator=' ', displaywidth=80): | |||
|
615 | """ Transform a list of strings into a single string with columns. | |||
|
616 | ||||
|
617 | Parameters | |||
|
618 | ---------- | |||
|
619 | items : sequence of strings | |||
|
620 | The strings to process. | |||
|
621 | ||||
|
622 | separator : str, optional [default is two spaces] | |||
|
623 | The string that separates columns. | |||
|
624 | ||||
|
625 | displaywidth : int, optional [default is 80] | |||
|
626 | Width of the display in number of characters. | |||
|
627 | ||||
|
628 | Returns | |||
|
629 | ------- | |||
|
630 | The formatted string. | |||
|
631 | """ | |||
|
632 | # Note: this code is adapted from columnize 0.3.2. | |||
|
633 | # See http://code.google.com/p/pycolumnize/ | |||
|
634 | ||||
|
635 | # Some degenerate cases. | |||
|
636 | size = len(items) | |||
|
637 | if size == 0: | |||
|
638 | return '\n' | |||
|
639 | elif size == 1: | |||
|
640 | return '%s\n' % items[0] | |||
|
641 | ||||
|
642 | # Try every row count from 1 upwards | |||
|
643 | array_index = lambda nrows, row, col: nrows*col + row | |||
|
644 | for nrows in range(1, size): | |||
|
645 | ncols = (size + nrows - 1) // nrows | |||
|
646 | colwidths = [] | |||
|
647 | totwidth = -len(separator) | |||
|
648 | for col in range(ncols): | |||
|
649 | # Get max column width for this column | |||
|
650 | colwidth = 0 | |||
|
651 | for row in range(nrows): | |||
|
652 | i = array_index(nrows, row, col) | |||
|
653 | if i >= size: break | |||
|
654 | x = items[i] | |||
|
655 | colwidth = max(colwidth, len(x)) | |||
|
656 | colwidths.append(colwidth) | |||
|
657 | totwidth += colwidth + len(separator) | |||
|
658 | if totwidth > displaywidth: | |||
|
659 | break | |||
|
660 | if totwidth <= displaywidth: | |||
|
661 | break | |||
|
662 | ||||
|
663 | # The smallest number of rows computed and the max widths for each | |||
|
664 | # column has been obtained. Now we just have to format each of the rows. | |||
|
665 | string = '' | |||
|
666 | for row in range(nrows): | |||
|
667 | texts = [] | |||
|
668 | for col in range(ncols): | |||
|
669 | i = row + nrows*col | |||
|
670 | if i >= size: | |||
|
671 | texts.append('') | |||
|
672 | else: | |||
|
673 | texts.append(items[i]) | |||
|
674 | while texts and not texts[-1]: | |||
|
675 | del texts[-1] | |||
|
676 | for col in range(len(texts)): | |||
|
677 | texts[col] = texts[col].ljust(colwidths[col]) | |||
|
678 | string += '%s\n' % separator.join(texts) | |||
|
679 | return string |
General Comments 0
You need to be logged in to leave comments.
Login now