Converts tabs to spaces. Args: tab_size (int, optional): Size of tabs. Defaults to 8.
(self, tab_size: Optional[int] = None)
| 815 | return new_text |
| 816 | |
| 817 | def expand_tabs(self, tab_size: Optional[int] = None) -> None: |
| 818 | """Converts tabs to spaces. |
| 819 | |
| 820 | Args: |
| 821 | tab_size (int, optional): Size of tabs. Defaults to 8. |
| 822 | |
| 823 | """ |
| 824 | if "\t" not in self.plain: |
| 825 | return |
| 826 | if tab_size is None: |
| 827 | tab_size = self.tab_size |
| 828 | if tab_size is None: |
| 829 | tab_size = 8 |
| 830 | |
| 831 | new_text: List[Text] = [] |
| 832 | append = new_text.append |
| 833 | |
| 834 | for line in self.split("\n", include_separator=True): |
| 835 | if "\t" not in line.plain: |
| 836 | append(line) |
| 837 | else: |
| 838 | cell_position = 0 |
| 839 | parts = line.split("\t", include_separator=True) |
| 840 | for part in parts: |
| 841 | if part.plain.endswith("\t"): |
| 842 | part._text[-1] = part._text[-1][:-1] + " " |
| 843 | cell_position += part.cell_len |
| 844 | tab_remainder = cell_position % tab_size |
| 845 | if tab_remainder: |
| 846 | spaces = tab_size - tab_remainder |
| 847 | part.extend_style(spaces) |
| 848 | cell_position += spaces |
| 849 | else: |
| 850 | cell_position += part.cell_len |
| 851 | append(part) |
| 852 | |
| 853 | result = Text("").join(new_text) |
| 854 | |
| 855 | self._text = [result.plain] |
| 856 | self._length = len(self.plain) |
| 857 | self._spans[:] = result._spans |
| 858 | |
| 859 | def truncate( |
| 860 | self, |