2022-05-14 09:43:02 +00:00
|
|
|
from typing import Optional
|
2024-08-30 20:10:46 +00:00
|
|
|
import re
|
2022-05-14 09:43:02 +00:00
|
|
|
|
|
|
|
def formatSeconds(seconds: int | float, joiner: Optional[str] = None) -> str:
|
2022-05-10 20:23:12 +00:00
|
|
|
seconds = round(seconds)
|
2024-08-30 14:59:02 +00:00
|
|
|
timeValues = { "h": seconds // 3600, "m": seconds // 60 % 60, "s": seconds % 60 }
|
2022-05-10 21:57:06 +00:00
|
|
|
if not joiner:
|
2022-05-10 20:23:12 +00:00
|
|
|
return "".join(str(v) + k for k, v in timeValues.items() if v > 0)
|
2022-05-10 21:57:06 +00:00
|
|
|
if timeValues["h"] == 0:
|
2022-05-10 20:23:12 +00:00
|
|
|
del timeValues["h"]
|
|
|
|
return joiner.join(str(v).rjust(2, "0") for v in timeValues.values())
|
2024-02-13 08:52:27 +00:00
|
|
|
|
|
|
|
def truncate(text: str, maxLength: int) -> str:
|
|
|
|
if len(text) > maxLength:
|
|
|
|
text = text[:maxLength-3] + "..."
|
|
|
|
return text
|
2024-08-30 20:10:46 +00:00
|
|
|
|
|
|
|
def stripNonAscii(text: str) -> str:
|
|
|
|
return re.sub(r"[^\x00-\x7f]", "", text)
|