2023-08-25 16:15:01 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import random
|
2025-01-09 16:26:15 +01:00
|
|
|
from .buntcheck import color_ansi, get_text_colors
|
2024-02-25 15:03:11 +01:00
|
|
|
|
2023-08-25 16:15:01 +02:00
|
|
|
|
2024-12-05 18:47:13 +01:00
|
|
|
def format_string(
|
2025-01-09 16:26:15 +01:00
|
|
|
text: str,
|
|
|
|
prefix: str = "§",
|
|
|
|
suffix: str = "",
|
|
|
|
auto_rs: bool = True
|
2024-12-05 18:47:13 +01:00
|
|
|
): # formats the text e.g. text after "§red" is colored red
|
2023-08-25 16:15:01 +02:00
|
|
|
for color in color_ansi:
|
|
|
|
text = text.replace(prefix + color + suffix, color_ansi[color])
|
|
|
|
|
|
|
|
if auto_rs:
|
|
|
|
text += color_ansi["reset"]
|
|
|
|
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
|
|
|
def rainbow(text): # makes the string rainbow-colored
|
2025-01-09 16:26:15 +01:00
|
|
|
color_codes = get_text_colors()
|
2023-08-25 16:15:01 +02:00
|
|
|
|
|
|
|
text_out = ""
|
|
|
|
|
|
|
|
for character in text:
|
|
|
|
text_out += random.choice(color_codes) + character
|
|
|
|
|
|
|
|
return text_out
|
|
|
|
|
|
|
|
|
|
|
|
def example(): # just an example of this script
|
|
|
|
print('This:'
|
|
|
|
'\ntext = "§boldThis§rs §underlineis§rs §italican "'
|
|
|
|
'\ntext += rainbow("Example!")'
|
|
|
|
'\nprint(color_name_to_ansi(text))'
|
|
|
|
'\n\nmakes this:')
|
|
|
|
|
|
|
|
text = "§boldThis§rs §underlineis§rs §italican "
|
|
|
|
text += rainbow("Example!")
|
|
|
|
print(format_string(text))
|
|
|
|
|
|
|
|
|
2024-01-05 22:14:36 +01:00
|
|
|
def asap(old: str, add: str, position: int):
|
|
|
|
"""
|
|
|
|
ASAP = Add String At Position
|
|
|
|
|
|
|
|
(0 is the first character.)
|
|
|
|
"""
|
|
|
|
|
|
|
|
return old[:position] + add + old[position:]
|
|
|
|
|
|
|
|
|
2024-01-05 22:30:21 +01:00
|
|
|
def rsap(old: str, replace: str, position: int):
|
2024-01-05 22:14:36 +01:00
|
|
|
"""
|
|
|
|
RSAP = Replace String At Position
|
|
|
|
|
|
|
|
(0 is the first character.)
|
|
|
|
"""
|
|
|
|
|
2024-01-05 22:30:21 +01:00
|
|
|
return old[:position] + replace + old[position + 1:]
|
2024-01-05 22:14:36 +01:00
|
|
|
|
|
|
|
|
2024-03-24 18:42:43 +01:00
|
|
|
def find_nth_occurrence(string: str, substring: str, n: int):
|
|
|
|
"""
|
|
|
|
Find the nth occurrence of a substring in a string.
|
2024-12-05 16:58:38 +01:00
|
|
|
|
2024-03-24 18:42:43 +01:00
|
|
|
:param int n: 0 is the first occurrence
|
|
|
|
:return: -1 when the substring was not found, 0 is the first character.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if len(substring) == 1: # make sure the replace string is not the substring (in rare cases, it might not work)
|
|
|
|
if substring == "a":
|
|
|
|
rep = "b"
|
|
|
|
|
|
|
|
else:
|
|
|
|
rep = "a"
|
|
|
|
|
|
|
|
else:
|
|
|
|
if not substring in "a" * len(substring):
|
|
|
|
rep = "a" * len(substring)
|
|
|
|
|
|
|
|
else:
|
|
|
|
rep = "b" * len(substring)
|
|
|
|
|
2024-03-25 14:25:05 +01:00
|
|
|
return string.replace(substring, rep, n).find(substring)
|