Skip to content

Commit

Permalink
ANSI Escape colors not being printed properly (#34)
Browse files Browse the repository at this point in the history
* Added config setting to allow ansi coloring
* Adjusted README for configuring ansi support
  • Loading branch information
santiagosayshey authored Feb 7, 2024
1 parent 63aeded commit a82e8d3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 20 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 37 additions & 17 deletions helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import requests
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
import json
import sys

class Colors:
GREEN = '\033[92m'
Expand All @@ -20,24 +21,41 @@ class Apps:
}

def print_message(message, message_type='', newline=True):
color = Colors.ENDC # default color
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

if newline:
print(color + message + Colors.ENDC)
config = load_config()
ansi_colors = config['settings']['ansi_colors']

if ansi_colors:
# Initialize color as default.
color = Colors.ENDC
message_type = message_type.lower()

# 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

# 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():
Expand Down Expand Up @@ -119,13 +137,15 @@ 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:
print_message(f"HTTP Error {response.status_code}.", "red")
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

1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
settings:
export_path: "./exports"
import_path: "./imports"
ansi_colors: "true"
"""

Expand Down

0 comments on commit a82e8d3

Please sign in to comment.