Separated text formatting module and log module to 2 files and added a simple test for the formatting module.

This commit is contained in:
The Wobbler 2025-01-09 16:26:15 +01:00
parent 56a74ec420
commit 1baac24427
3 changed files with 72 additions and 63 deletions

13
tests/text/test_format.py Normal file
View file

@ -0,0 +1,13 @@
#!/usr/bin/python3
from wobbl_tools.text.format import format_string
from wobbl_tools.text import format
formats = list(format.color_ansi.keys())
formats.remove("rs")
formats.remove("reset")
print("\n\nIs this shit looking right?\n")
for format_name in formats:
print(f"{format_name.title().replace("_", " ")} text: {format_string(f"§{format_name}Text")}")

View file

@ -1,17 +1,15 @@
#!/usr/bin/python3
import random
from . import buntcheck
from .buntcheck import color_ansi, get_text_colors
def format_string(
text: str,
prefix: str = "§",
suffix: str = "",
auto_rs: bool = True
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])
@ -21,23 +19,8 @@ def format_string(
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()
color_codes = get_text_colors()
text_out = ""
@ -59,42 +42,6 @@ def example(): # just an example of this script
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
@ -138,7 +85,3 @@ def find_nth_occurrence(string: str, substring: str, n: int):
rep = "b" * len(substring)
return string.replace(substring, rep, n).find(substring)
if __name__ == "__main__":
example()

53
wobbl_tools/text/log.py Normal file
View file

@ -0,0 +1,53 @@
#!/usr/bin/python3
from .format import format_string
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)
}
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