Esempio n. 1
0
    def create_copy_document(self):
        """
        Create a Document instance and token list that can be used in copy
        mode.
        """
        data_buffer = self.screen.pt_screen.data_buffer
        text = []
        token_list = []

        first_row = min(data_buffer.keys())
        last_row = max(data_buffer.keys())

        def token_has_no_background(token):
            try:
                # Token looks like ('C', color, bgcolor, bold, underline, ...)
                return token[2] is None
            except IndexError:
                return True

        for row_index in range(first_row, last_row + 1):
            row = data_buffer[row_index]
            max_column = max(row.keys()) if row else 0

            # Remove trailing whitespace. (If the background is transparent.)
            row_data = [row[x] for x in range(0, max_column + 1)]

            while (row_data and row_data[-1].char.isspace()
                   and token_has_no_background(row_data[-1].token)):
                row_data.pop()

            # Walk through row.
            char_iter = iter(range(len(row_data)))

            for x in char_iter:
                c = row[x]
                text.append(c.char)
                token_list.append((c.token, c.char))

                # Skip next cell when this is a double width character.
                if c.width == 2:
                    next(char_iter)

            # Add newline.
            text.append('\n')
            token_list.append((Token, '\n'))

        # Remove newlines at the end.
        while text and text[-1] == '\n':
            text.pop()
            token_list.pop()

        # Calculate cursor position.
        d = Document(text=''.join(text))

        return Document(
            text=d.text,
            cursor_position=d.translate_row_col_to_index(
                row=self.screen.pt_screen.cursor_position.y,
                col=self.screen.pt_screen.cursor_position.x)), token_list
Esempio n. 2
0
    def create_copy_document(self):
        """
        Create a Document instance and token list that can be used in copy
        mode.
        """
        data_buffer = self.screen.pt_screen.data_buffer
        text = []
        token_list = []

        first_row = min(data_buffer.keys())
        last_row = max(data_buffer.keys())

        def token_has_no_background(token):
            try:
                # Token looks like ('C', color, bgcolor, bold, underline, ...)
                return token[2] is None
            except IndexError:
                return True

        for row_index in range(first_row, last_row + 1):
            row = data_buffer[row_index]
            max_column = max(row.keys()) if row else 0

            # Remove trailing whitespace. (If the background is transparent.)
            row_data = [row[x] for x in range(0, max_column + 1)]

            while (row_data and row_data[-1].char.isspace() and
                   token_has_no_background(row_data[-1].token)):
                row_data.pop()

            # Walk through row.
            char_iter = iter(range(len(row_data)))

            for x in char_iter:
                c = row[x]
                text.append(c.char)
                token_list.append((c.token, c.char))

                # Skip next cell when this is a double width character.
                if c.width == 2:
                    next(char_iter)

            # Add newline.
            text.append('\n')
            token_list.append((Token, '\n'))

        # Remove newlines at the end.
        while text and text[-1] == '\n':
            text.pop()
            token_list.pop()

        # Calculate cursor position.
        d = Document(text=''.join(text))

        return Document(text=d.text,
                        cursor_position=d.translate_row_col_to_index(
                            row=self.screen.pt_screen.cursor_position.y,
                            col=self.screen.pt_screen.cursor_position.x)), token_list
Esempio n. 3
0
    def _get_source_code_document(self, filename):
        """
        Return source code around current line as string.
        """
        source_code = ''.join(linecache.getlines(filename))
        document = Document(source_code)

        return Document(document.text, document.translate_row_col_to_index(
            row=self.curframe.f_lineno - 1, col=0))
Esempio n. 4
0
    def _get_source_code_document(self, filename):
        """
        Return source code around current line as string.
        """
        source_code = linecache.getlines(filename)
        if six.PY2:
            source_code = [l.decode('utf-8') for l in source_code]

        source_code = ''.join(source_code)
        document = Document(source_code)

        return Document(
            document.text,
            document.translate_row_col_to_index(row=self.curframe.f_lineno - 1,
                                                col=0))