diff --git a/src/caelestia/subcommands/resizer.py b/src/caelestia/subcommands/resizer.py index fc126624..4b6eb790 100644 --- a/src/caelestia/subcommands/resizer.py +++ b/src/caelestia/subcommands/resizer.py @@ -7,7 +7,7 @@ from pathlib import Path from typing import Any, Dict, Optional from caelestia.utils import hypr -from caelestia.utils.logging import log_message +from caelestia.utils.io import log_message from caelestia.utils.paths import user_config_path diff --git a/src/caelestia/utils/io.py b/src/caelestia/utils/io.py new file mode 100644 index 00000000..0786119a --- /dev/null +++ b/src/caelestia/utils/io.py @@ -0,0 +1,57 @@ +import sys +from time import strftime + + +def log_message(message: str) -> None: + timestamp = strftime("%Y-%m-%d %H:%M:%S") + print(f"[{timestamp}] {message}") + + +def log_exception(func): + """Log exceptions to stdout instead of raising + + Used by the `apply_()` functions so that an exception, when applying + a theme, does not prevent the other themes from being applied. + """ + + def wrapper(*args, **kwargs): + try: + func(*args, **kwargs) + except Exception as e: + log_message(f'Error during execution of "{func.__name__}()": {str(e)}') + + return wrapper + + +def _format_msg(colour: int, msg: str) -> str: + return f"\033[{colour}m:: {msg}\033[0m" + + +def log(msg: str) -> None: + print(_format_msg(2, msg)) + + +def info(msg: str) -> None: + print(_format_msg(0, msg)) + + +def warn(msg: str) -> None: + print(_format_msg(33, f"Warning: {msg}")) + + +def error(msg: str) -> None: + print(_format_msg(31, f"Error: {msg}"), file=sys.stderr) + + +def fatal(msg: str) -> None: + print(_format_msg(31, f"Fatal: {msg}"), file=sys.stderr) + sys.exit(1) + + +def prompt(msg: str) -> str: + return input(_format_msg(36, msg) + " ") + + +def pause() -> None: + input("\033[2m\033[3m(Ctrl+C to exit, enter to continue)\033[0m") + print("\033[1A\r\033[2K", end="") # Clear pause prompt diff --git a/src/caelestia/utils/logging.py b/src/caelestia/utils/logging.py deleted file mode 100644 index 228936e5..00000000 --- a/src/caelestia/utils/logging.py +++ /dev/null @@ -1,22 +0,0 @@ -from time import strftime - - -def log_message(message: str) -> None: - timestamp = strftime("%Y-%m-%d %H:%M:%S") - print(f"[{timestamp}] {message}") - - -def log_exception(func): - """Log exceptions to stdout instead of raising - - Used by the `apply_()` functions so that an exception, when applying - a theme, does not prevent the other themes from being applied. - """ - - def wrapper(*args, **kwargs): - try: - func(*args, **kwargs) - except Exception as e: - log_message(f'Error during execution of "{func.__name__}()": {str(e)}') - - return wrapper diff --git a/src/caelestia/utils/theme.py b/src/caelestia/utils/theme.py index deec33a0..cc258f6f 100644 --- a/src/caelestia/utils/theme.py +++ b/src/caelestia/utils/theme.py @@ -8,7 +8,8 @@ import tempfile from pathlib import Path from caelestia.utils.colour import get_dynamic_colours -from caelestia.utils.logging import log_exception +from caelestia.utils.hypr import is_lua_config +from caelestia.utils.io import log_exception from caelestia.utils.paths import ( c_state_dir, config_dir, @@ -19,7 +20,6 @@ from caelestia.utils.paths import ( user_templates_dir, ) from caelestia.utils.scheme import get_scheme -from caelestia.utils.hypr import is_lua_config def gen_conf(colours: dict[str, str]) -> str: @@ -28,6 +28,7 @@ def gen_conf(colours: dict[str, str]) -> str: conf += f"${name} = {colour}\n" return conf + def gen_lua(colours: dict[str, str]) -> str: lua = "return {\n" for name, colour in colours.items(): @@ -35,6 +36,7 @@ def gen_lua(colours: dict[str, str]) -> str: lua += "}" return lua + def gen_scss(colours: dict[str, str]) -> str: scss = "" for name, colour in colours.items():