From a82e8d31fefeaf9b33235ab1833bff2ef2e13c80 Mon Sep 17 00:00:00 2001 From: santiagosayshey Date: Thu, 8 Feb 2024 08:44:01 +1030 Subject: [PATCH] ANSI Escape colors not being printed properly (#34) * Added config setting to allow ansi coloring * Adjusted README for configuring ansi support --- README.md | 17 ++++++++++++++--- helpers.py | 50 +++++++++++++++++++++++++++++++++++--------------- setup.py | 1 + 3 files changed, 50 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 9bd8e7b..2427d85 100644 --- a/README.md +++ b/README.md @@ -26,12 +26,23 @@ Profilarr is a Python-based tool designed to add import/export/sync functionalit - This will create a `config.yml` file in the same directory as `setup.py`. 4. Open the `config.yml` file in a text editor. - Add the URL and API key to the master instances of Radarr / Sonarr. - - If syncing, add the URL, API key and a name to each extra instance of Radarr / Sonarr. + - If syncing, add the URL, API key, and a name to each extra instance of Radarr / Sonarr. - If exporting, adjust the `export_path` to your desired export location. - - If importing non Dictionarry files, adjust the `import_path` to your desired import location. -5. Save the changes. + - If importing non-Dictionary files, adjust the `import_path` to your desired import location. +5. Configure ANSI Color Support (Optional): + - The Profilarr scripts use ANSI colors in terminal output for better readability. By default, this feature is enabled (`ansi_colors: true`). + - **If your terminal does not properly display ANSI colors** (e.g., you see codes like `←[94m` instead of colored text), you may want to disable this feature to improve readability. + - To disable ANSI colors, find the `settings` section in your `config.yml` file and change `ansi_colors` to `false`. + ```yaml + settings: + export_path: "./exports" + import_path: "./imports" + ansi_colors: false # Disable ANSI colors if your terminal doesn't support them + ``` +6. Save the changes to your `config.yml` file after making the necessary adjustments. ## 🚀 Usage + - If using Windows, use `python script.py` or `py script.py`. If on Linux, use `python3 script.py`. ### Importing diff --git a/helpers.py b/helpers.py index b5fc6ca..13cadda 100644 --- a/helpers.py +++ b/helpers.py @@ -3,6 +3,7 @@ import json import requests from requests.exceptions import ConnectionError, Timeout, TooManyRedirects import json +import sys class Colors: GREEN = '\033[92m' @@ -20,24 +21,41 @@ class Apps: } def print_message(message, message_type='', newline=True): - color = Colors.ENDC # default color - message_type = message_type.lower() + config = load_config() + ansi_colors = config['settings']['ansi_colors'] + + if ansi_colors: + # Initialize color as default. + color = Colors.ENDC + message_type = message_type.lower() - if message_type == 'green': - color = Colors.GREEN - elif message_type == 'red': - color = Colors.RED - elif message_type == 'yellow': - color = Colors.YELLOW - elif message_type == 'blue': - color = Colors.BLUE - elif message_type == 'purple': - color = Colors.PURPLE + # Assign color based on message type. + if message_type == 'green': + color = Colors.GREEN + elif message_type == 'red': + color = Colors.RED + elif message_type == 'yellow': + color = Colors.YELLOW + elif message_type == 'blue': + color = Colors.BLUE + elif message_type == 'purple': + color = Colors.PURPLE + + # Prepare the end color reset code. + end_color = Colors.ENDC - if newline: - print(color + message + Colors.ENDC) + # Print the colored message. + if newline: + print(color + message + end_color) + else: + print(color + message + end_color, end='') else: - print(color + message + Colors.ENDC, end='') + # Print the message without color if ANSI colors are disabled. + if newline: + print(message) + else: + print(message, end='') + def load_config(): @@ -119,6 +137,7 @@ def make_request(request_type, url, api_key, resource_type, json_payload=None): return None elif response.status_code == 401: print_message("Unauthorized. Check your API key.", "red") + sys.exit() elif response.status_code == 409: print_message("Conflict detected. The requested action could not be completed.", "red") else: @@ -126,6 +145,7 @@ def make_request(request_type, url, api_key, resource_type, json_payload=None): except (ConnectionError, Timeout, TooManyRedirects) as e: # Update the message here to suggest checking the application's accessibility print_message("Network error. Make sure the application is running and accessible.", "red") + sys.exit() return None diff --git a/setup.py b/setup.py index a61fc88..4e2eee5 100644 --- a/setup.py +++ b/setup.py @@ -17,6 +17,7 @@ instances: settings: export_path: "./exports" import_path: "./imports" + ansi_colors: "true" """