1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-11-24 05:33:00 +00:00
kiwi-scp/kiwi_scp/wstring.py

98 lines
2.5 KiB
Python
Raw Permalink Normal View History

2021-11-27 16:39:43 +00:00
import re
from enum import Enum, auto
from typing import List
import attr
import wcwidth
class WAlignment(Enum):
LEFT = auto()
RIGHT = auto()
CENTER = auto()
@attr.s
class WString:
s: str = attr.ib()
# from https://stackoverflow.com/a/38662876
ANSI_ESCAPES = re.compile(r"(?:\x1B[@-_]|[\x80-\x9F])[0-?]*[ -/]*[@-~]")
def __str__(self) -> str:
return self.s
def __len__(self) -> int:
return wcwidth.wcswidth(WString.ANSI_ESCAPES.sub("", self.s))
def pad(self, alignment: WAlignment = WAlignment.CENTER, wlen: int = 0, char: str = " ") -> "WString":
char = char[0]
if alignment is WAlignment.LEFT:
return WString(f"{self}{char * wlen}")
elif alignment is WAlignment.RIGHT:
return WString(f"{char * wlen}{self}")
else:
pad_l, pad_r = wlen // 2, wlen - (wlen // 2)
return WString(f"{char * pad_l}{self}{char * pad_r}")
@attr.s
class WParagraph:
lines: List[WString] = attr.ib()
def __str__(self) -> str:
return "\n".join(
str(line)
2021-11-27 17:25:38 +00:00
for line in self.lines
2021-11-27 16:39:43 +00:00
)
@classmethod
def from_strings(cls, *source: str) -> "WParagraph":
return cls([
WString(line)
2021-11-27 17:25:38 +00:00
for line in source
2021-11-27 16:39:43 +00:00
])
def align(self, alignment: WAlignment = WAlignment.CENTER, padding: int = 0, char: str = " ") -> "WParagraph":
total_length = max(
len(line)
2021-11-27 17:25:38 +00:00
for line in self.lines
2021-11-27 16:39:43 +00:00
) + padding
pad_lengths = (
total_length - len(line)
2021-11-27 17:25:38 +00:00
for line in self.lines
2021-11-27 16:39:43 +00:00
)
return WParagraph([
line.pad(alignment, wlen, char)
2021-11-27 17:25:38 +00:00
for line, wlen in zip(self.lines, pad_lengths)
2021-11-27 16:39:43 +00:00
])
def surround(self, char: str, padding: int = 1) -> "WParagraph":
char = char[0]
padding = " " * padding
l_border, r_border = char + padding, padding + char
lines = [
WString(f"{l_border}{line}{r_border}")
2021-11-27 17:25:38 +00:00
for line in self.lines
2021-11-27 16:39:43 +00:00
]
extra_line = char * len(lines[0])
return WParagraph([
extra_line,
*lines,
extra_line,
])
def emphasize(self, count: int = 3, padding: int = 1) -> "WParagraph":
padding = " " * padding
l_border, r_border = (">" * count) + padding, padding + ("<" * count)
return WParagraph([
WString(f"{l_border}{line}{r_border}")
2021-11-27 17:25:38 +00:00
for line in self.lines
2021-11-27 16:39:43 +00:00
])