from itertools import zip_longest from typing import (
TYPE_CHECKING,
Iterable,
Iterator,
List,
Optional,
TypeVar,
Union,
overload,
)
if TYPE_CHECKING: from .console import (
Console,
ConsoleOptions,
JustifyMethod,
OverflowMethod,
RenderResult,
RenderableType,
) from .text import Text
from .cells import cell_len from .measure import Measurement
T = TypeVar("T")
class Renderables: """A list subclass which renders its contents to the console."""
def pop(self, index: int = -1) -> "Text": return self._lines.pop(index)
def justify(
self,
console: "Console",
width: int,
justify: "JustifyMethod" = "left",
overflow: "OverflowMethod" = "fold",
) -> None: """Justify and overflow text to a given width.
Args:
console (Console): Console instance.
width (int): Number of cells available per line.
justify (str, optional): Default justify method for text: "left", "center", "full"or"right". Defaults to "left".
overflow (str, optional): Default overflow for text: "crop", "fold", or"ellipsis". Defaults to "fold".
""" from .text import Text
if justify == "left": for line in self._lines:
line.truncate(width, overflow=overflow, pad=True) elif justify == "center": for line in self._lines:
line.rstrip()
line.truncate(width, overflow=overflow)
line.pad_left((width - cell_len(line.plain)) // 2)
line.pad_right(width - cell_len(line.plain)) elif justify == "right": for line in self._lines:
line.rstrip()
line.truncate(width, overflow=overflow)
line.pad_left(width - cell_len(line.plain)) elif justify == "full": for line_index, line in enumerate(self._lines): if line_index == len(self._lines) - 1: break
words = line.split(" ")
words_size = sum(cell_len(word.plain) for word in words)
num_spaces = len(words) - 1
spaces = [1 for _ in range(num_spaces)]
index = 0 if spaces: while words_size + num_spaces < width:
spaces[len(spaces) - index - 1] += 1
num_spaces += 1
index = (index + 1) % len(spaces)
tokens: List[Text] = [] for index, (word, next_word) in enumerate(
zip_longest(words, words[1:])
):
tokens.append(word) if index < len(spaces):
style = word.get_style_at_offset(console, -1)
next_style = next_word.get_style_at_offset(console, 0)
space_style = style if style == next_style else line.style
tokens.append(Text(" " * spaces[index], style=space_style))
self[line_index] = Text("").join(tokens)
Messung V0.5
¤ Dauer der Verarbeitung: 0.14 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.