Skip to content

Commit

Permalink
Implement use_colors (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
pederhan authored Aug 29, 2024
1 parent 18539d0 commit e1286df
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
4 changes: 3 additions & 1 deletion zabbix_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Authors:
# rafael@e-mc2.net / https://e-mc2.net/
#
# Copyright (c) 2014-2017 USIT-University of Oslo
# Copyright (c) 2014-2024 USIT-University of Oslo
#
# This file is part of Zabbix-cli
# https://github.com/unioslo/zabbix-cli
Expand Down Expand Up @@ -134,6 +134,7 @@ def main_callback(
return

from zabbix_cli.logs import configure_logging
from zabbix_cli.output.console import configure_console
from zabbix_cli.state import get_state

if should_skip_configuration(ctx):
Expand All @@ -155,6 +156,7 @@ def main_callback(
logger.debug("Zabbix-CLI started.")

configure_logging(conf.logging)
configure_console(conf)
state.configure(conf)

# TODO: look at order of evaluation here. What takes precedence?
Expand Down
26 changes: 26 additions & 0 deletions zabbix_cli/output/console.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

import os
from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any
from typing import Dict
from typing import List
Expand All @@ -16,6 +18,9 @@
from zabbix_cli.output.style import Icon
from zabbix_cli.state import get_state

if TYPE_CHECKING:
from zabbix_cli.config.model import Config

# stdout console used to print results
console = Console(theme=RICH_THEME)

Expand Down Expand Up @@ -175,3 +180,24 @@ def print_path(path: Path) -> None:
highlight=False,
soft_wrap=True,
)


def disable_color() -> None:
"""Disable color output in consoles."""
console._color_system = None # pyright: ignore[reportPrivateUsage]
err_console._color_system = None # pyright: ignore[reportPrivateUsage]
# HACK: set env var to disable color in Typer console
os.environ["NO_COLOR"] = "1"


def enable_color() -> None:
"""Enable color output in consoles if supported."""
console._color_system = console._detect_color_system() # pyright: ignore[reportPrivateUsage]
err_console._color_system = console._detect_color_system() # pyright: ignore[reportPrivateUsage]
os.unsetenv("NO_COLOR") # remove hack


def configure_console(config: Config) -> None:
"""Configure console output based on the application configuration."""
if not config.app.use_colors:
disable_color()

0 comments on commit e1286df

Please sign in to comment.