#!/usr/bin/python3 import random from wobbl_tools import buntcheck def format_string(text: str, prefix: str = "§", suffix: str = "", auto_rs: bool = True): # formats the text e.g. text after "§red" is colored red color_ansi = buntcheck.color_ansi for color in color_ansi: text = text.replace(prefix + color + suffix, color_ansi[color]) if auto_rs: text += color_ansi["reset"] return text msg_types_ncl = { "info": "[info]: ", "ok": format_string("[OK]: "), "warning": format_string("[WARNING]: "), "error": format_string("[ERROR]: ") } msg_types = { "info": "[info]: ", "ok": format_string("[§green§boldOK§rs]: "), "warning": format_string("§yellow§bold[WARNING]: ", auto_rs=False), "error": format_string("§red§bold[ERROR]: ", auto_rs=False) } def rainbow(text): # makes the string rainbow-colored color_codes = buntcheck.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)) class Log: def __init__(self, log_path: str=None, no_console: bool=False, no_colors: bool=False): self.log_path = log_path self.no_console = no_console self.no_colors = no_colors self.log = "" def write(self, msg: str, msg_type: str = "info", prefix: str = ""): if self.no_colors: msg = prefix + msg_types_ncl[msg_type] + msg else: msg = prefix + msg_types[msg_type] + msg msg += format_string("§rs") if not self.no_console: print(msg) if self.log_path is not None: log_file = open(self.log_path, "a") log_file.write(msg + "\n") log_file.close() self.log += msg + "\n" def read(self): if self.log_path is not None: log_file = open(self.log_path, "r") log_content = log_file.read() log_file.close() return log_content else: return self.log 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) if __name__ == "__main__": example()