87 lines
2 KiB
Python
87 lines
2 KiB
Python
#!/usr/bin/python3
|
|
|
|
import random
|
|
from .buntcheck import color_ansi, get_text_colors
|
|
|
|
|
|
def format_string(
|
|
text: str,
|
|
prefix: str = "§",
|
|
suffix: str = "",
|
|
auto_rs: bool = True
|
|
): # formats the text e.g. text after "§red" is colored red
|
|
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
|
|
color_codes = get_text_colors()
|
|
|
|
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))
|
|
|
|
|
|
def asap(old: str, add: str, position: int):
|
|
"""
|
|
ASAP = Add String At Position
|
|
|
|
(0 is the first character.)
|
|
"""
|
|
|
|
return old[:position] + add + old[position:]
|
|
|
|
|
|
def rsap(old: str, replace: str, position: int):
|
|
"""
|
|
RSAP = Replace String At Position
|
|
|
|
(0 is the first character.)
|
|
"""
|
|
|
|
return old[:position] + replace + old[position + 1:]
|
|
|
|
|
|
def find_nth_occurrence(string: str, substring: str, n: int):
|
|
"""
|
|
Find the nth occurrence of a substring in a string.
|
|
|
|
: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)
|
|
|
|
return string.replace(substring, rep, n).find(substring)
|