Show More
@@ -5,7 +5,6 b' import sys' | |||||
5 | from PyQt4 import QtCore, QtGui |
|
5 | from PyQt4 import QtCore, QtGui | |
6 |
|
6 | |||
7 | # Local imports |
|
7 | # Local imports | |
8 | from IPython.external.columnize import columnize |
|
|||
9 | from ansi_code_processor import QtAnsiCodeProcessor |
|
8 | from ansi_code_processor import QtAnsiCodeProcessor | |
10 | from completion_widget import CompletionWidget |
|
9 | from completion_widget import CompletionWidget | |
11 |
|
10 | |||
@@ -580,7 +579,7 b' class ConsoleWidget(QtGui.QPlainTextEdit):' | |||||
580 | text = self.format_as_columns(items) |
|
579 | text = self.format_as_columns(items) | |
581 | self._append_plain_text_keeping_prompt(text) |
|
580 | self._append_plain_text_keeping_prompt(text) | |
582 |
|
581 | |||
583 |
def format_as_columns(self, items, separator=' ' |
|
582 | def format_as_columns(self, items, separator=' '): | |
584 | """ Transform a list of strings into a single string with columns. |
|
583 | """ Transform a list of strings into a single string with columns. | |
585 |
|
584 | |||
586 | Parameters |
|
585 | Parameters | |
@@ -591,19 +590,61 b' class ConsoleWidget(QtGui.QPlainTextEdit):' | |||||
591 | separator : str, optional [default is two spaces] |
|
590 | separator : str, optional [default is two spaces] | |
592 | The string that separates columns. |
|
591 | The string that separates columns. | |
593 |
|
592 | |||
594 | vertical: bool, optional [default True] |
|
|||
595 | If set, consecutive items will be arranged from top to bottom, then |
|
|||
596 | from left to right. Otherwise, consecutive items will be aranged |
|
|||
597 | from left to right, then from top to bottom. |
|
|||
598 |
|
||||
599 | Returns |
|
593 | Returns | |
600 | ------- |
|
594 | ------- | |
601 | The formatted string. |
|
595 | The formatted string. | |
602 | """ |
|
596 | """ | |
|
597 | # Note: this code is adapted from columnize 0.3.2. | |||
|
598 | # See http://code.google.com/p/pycolumnize/ | |||
|
599 | ||||
603 | font_metrics = QtGui.QFontMetrics(self.font) |
|
600 | font_metrics = QtGui.QFontMetrics(self.font) | |
604 | width = self.width() / font_metrics.width(' ') |
|
601 | displaywidth = max(5, (self.width() / font_metrics.width(' ')) - 1) | |
605 | return columnize(items, displaywidth=width, |
|
602 | ||
606 | colsep=separator, arrange_vertical=vertical) |
|
603 | # Some degenerate cases | |
|
604 | size = len(items) | |||
|
605 | if size == 0: | |||
|
606 | return "\n" | |||
|
607 | elif size == 1: | |||
|
608 | return '%s\n' % str(items[0]) | |||
|
609 | ||||
|
610 | # Try every row count from 1 upwards | |||
|
611 | array_index = lambda nrows, row, col: nrows*col + row | |||
|
612 | for nrows in range(1, size): | |||
|
613 | ncols = (size + nrows - 1) // nrows | |||
|
614 | colwidths = [] | |||
|
615 | totwidth = -len(separator) | |||
|
616 | for col in range(ncols): | |||
|
617 | # Get max column width for this column | |||
|
618 | colwidth = 0 | |||
|
619 | for row in range(nrows): | |||
|
620 | i = array_index(nrows, row, col) | |||
|
621 | if i >= size: break | |||
|
622 | x = items[i] | |||
|
623 | colwidth = max(colwidth, len(x)) | |||
|
624 | colwidths.append(colwidth) | |||
|
625 | totwidth += colwidth + len(separator) | |||
|
626 | if totwidth > displaywidth: | |||
|
627 | break | |||
|
628 | if totwidth <= displaywidth: | |||
|
629 | break | |||
|
630 | ||||
|
631 | # The smallest number of rows computed and the max widths for each | |||
|
632 | # column has been obtained. Now we just have to format each of the rows. | |||
|
633 | string = '' | |||
|
634 | for row in range(nrows): | |||
|
635 | texts = [] | |||
|
636 | for col in range(ncols): | |||
|
637 | i = row + nrows*col | |||
|
638 | if i >= size: | |||
|
639 | texts.append('') | |||
|
640 | else: | |||
|
641 | texts.append(items[i]) | |||
|
642 | while texts and not texts[-1]: | |||
|
643 | del texts[-1] | |||
|
644 | for col in range(len(texts)): | |||
|
645 | texts[col] = texts[col].ljust(colwidths[col]) | |||
|
646 | string += "%s\n" % str(separator.join(texts)) | |||
|
647 | return string | |||
607 |
|
648 | |||
608 | def _get_block_plain_text(self, block): |
|
649 | def _get_block_plain_text(self, block): | |
609 | """ Given a QTextBlock, return its unformatted text. |
|
650 | """ Given a QTextBlock, return its unformatted text. |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now