Word wrap the text. Args: console (Console): Console instance. width (int): Number of cells available per line. justify (str, optional): Justify method: "default", "left", "center", "full", "right". Defaults to "default". overflow (str, option
(
self,
console: "Console",
width: int,
*,
justify: Optional["JustifyMethod"] = None,
overflow: Optional["OverflowMethod"] = None,
tab_size: int = 8,
no_wrap: Optional[bool] = None,
)
| 1199 | self._length -= amount |
| 1200 | |
| 1201 | def wrap( |
| 1202 | self, |
| 1203 | console: class="st">"Console", |
| 1204 | width: int, |
| 1205 | *, |
| 1206 | justify: Optional[class="st">"JustifyMethod"] = None, |
| 1207 | overflow: Optional[class="st">"OverflowMethod"] = None, |
| 1208 | tab_size: int = 8, |
| 1209 | no_wrap: Optional[bool] = None, |
| 1210 | ) -> Lines: |
| 1211 | class="st">"""Word wrap the text. |
| 1212 | |
| 1213 | Args: |
| 1214 | console (Console): Console instance. |
| 1215 | width (int): Number of cells available per line. |
| 1216 | justify (str, optional): Justify method: class="st">"default", class="st">"left", class="st">"center", class="st">"full", class="st">"right". Defaults to class="st">"default". |
| 1217 | overflow (str, optional): Overflow method: class="st">"crop", class="st">"fold", or class="st">"ellipsis". Defaults to None. |
| 1218 | tab_size (int, optional): Default tab size. Defaults to 8. |
| 1219 | no_wrap (bool, optional): Disable wrapping, Defaults to False. |
| 1220 | |
| 1221 | Returns: |
| 1222 | Lines: Number of lines. |
| 1223 | class="st">""" |
| 1224 | wrap_justify = justify or self.justify or DEFAULT_JUSTIFY |
| 1225 | wrap_overflow = overflow or self.overflow or DEFAULT_OVERFLOW |
| 1226 | |
| 1227 | no_wrap = pick_bool(no_wrap, self.no_wrap, False) or overflow == class="st">"ignore" |
| 1228 | |
| 1229 | lines = Lines() |
| 1230 | for line in self.split(allow_blank=True): |
| 1231 | if class="st">"\t" in line: |
| 1232 | line.expand_tabs(tab_size) |
| 1233 | if no_wrap: |
| 1234 | if overflow == class="st">"ignore": |
| 1235 | lines.append(line) |
| 1236 | continue |
| 1237 | new_lines = Lines([line]) |
| 1238 | else: |
| 1239 | offsets = divide_line(str(line), width, fold=wrap_overflow == class="st">"fold") |
| 1240 | new_lines = line.divide(offsets) |
| 1241 | for line in new_lines: |
| 1242 | line.rstrip_end(width) |
| 1243 | if wrap_justify: |
| 1244 | new_lines.justify( |
| 1245 | console, width, justify=wrap_justify, overflow=wrap_overflow |
| 1246 | ) |
| 1247 | for line in new_lines: |
| 1248 | line.truncate(width, overflow=wrap_overflow) |
| 1249 | lines.extend(new_lines) |
| 1250 | return lines |
| 1251 | |
| 1252 | def fit(self, width: int) -> Lines: |
| 1253 | class="st">"""Fit the text in to given width by chopping in to lines. |