https://github.com/aidinhamedi/python-color-print-v2

A simple and user friendly way to print colored text to the console.

https://github.com/aidinhamedi/python-color-print-v2

Science Score: 13.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (8.2%) to scientific vocabulary

Keywords

color colorfu colorprint print python python3 regular-expression
Last synced: 7 months ago · JSON representation

Repository

A simple and user friendly way to print colored text to the console.

Basic Info
  • Host: GitHub
  • Owner: AidinHamedi
  • License: mit
  • Language: Python
  • Default Branch: main
  • Homepage:
  • Size: 6.84 KB
Statistics
  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Topics
color colorfu colorprint print python python3 regular-expression
Created over 2 years ago · Last pushed over 2 years ago
Metadata Files
Readme License

README.md

Python-color-print-V2

Python

A Python function to print colored text to the console using advanced terminal colors.

Function Signature

python def print_Color(Input: str, print_END: str = '\n', start_char: str = '<', end_char: str = '>'):

Parameters

  • Input (str): The input string to be printed. '' is used to specify the color of the following text.
  • print_END (str): The string appended after the final output. Default is '\n'.
  • start_char (str): The character used as the start of the color specifier. Default is '<'.
  • end_char (str): The character used as the end of the color specifier. Default is '>'.

Usage

you can print a string in color. For example: python print_Color('<green>Hello, World!') This will print 'Hello, World!' in green.

Or like: python print_Color('hello <red>hello in red <green>hello in green')

Special Characters

The '<>' characters are used as separators for different parts of the string that need to be printed in different colors when using advanced mode.

Code snippet

```python import re

def printColor(Input: str, printEND: str = '\n', startchar: str = '<', endchar: str = '>'): """ Prints colored text to the console using advanced terminal colors.

Args:
    Input (str): The input string to be printed. '<color>' is used to specify the color of the following text.
    print_END (str): The string appended after the final output. Default is '\\n'.
    start_char (str): The character used as the start of the color specifier. Default is '<'.
    end_char (str): The character used as the end of the color specifier. Default is '>'.

Examples:
~~~python
    print_Color('Hello, World!') 
    # Prints 'Hello, World!' in normal color.

    print_Color('<red>Hello in red<green> Hello in green') 
    # Prints 'Hello in red' in red and 'Hello in green' in green.

    print_Color('~red!Hello in red', start_char='~', end_char='!') 
    # Prints 'Hello, World!' in normal color.

Note:
    If an invalid color is provided, an error message will be printed.
"""
color_code = {
    'black': '\x1b[0;30m',
    'red': '\x1b[0;31m',
    'green': '\x1b[0;32m',
    'yellow': '\x1b[0;33m',
    'blue': '\x1b[0;34m',
    'magenta': '\x1b[0;35m',
    'cyan': '\x1b[0;36m',
    'white': '\x1b[0;37m',
    'normal': '\x1b[0m',
    'bg_black': '\x1b[40m',
    'bg_red': '\x1b[41m',
    'bg_green': '\x1b[42m',
    'bg_yellow': '\x1b[43m',
    'bg_blue': '\x1b[44m',
    'bg_magenta': '\x1b[45m',
    'bg_cyan': '\x1b[46m',
    'bg_white': '\x1b[47m',
    'bg_normal': '\x1b[49m',
    'light_gray': '\x1b[0;90m',
    'light_red': '\x1b[0;91m',
    'light_green': '\x1b[0;92m',
    'light_yellow': '\x1b[0;93m',
    'light_blue': '\x1b[0;94m',
    'light_magenta': '\x1b[0;95m',
    'light_cyan': '\x1b[0;96m',
    'light_white': '\x1b[0;97m',
    'bg_light_gray': '\x1b[0;100m',
    'bg_light_red': '\x1b[0;101m',
    'bg_light_green': '\x1b[0;102m',
    'bg_light_yellow': '\x1b[0;103m',
    'bg_light_blue': '\x1b[0;104m',
    'bg_light_magenta': '\x1b[0;105m',
    'bg_light_cyan': '\x1b[0;106m',
    'bg_light_white': '\x1b[0;107m'
}
pattern = re.escape(start_char) + r'([^' + re.escape(end_char) + r']*)' + re.escape(end_char)
substrings = re.split(pattern, Input)
current_color = 'normal'
for i, sub_str in enumerate(substrings):
    if i % 2 == 0:
        print(color_code[current_color] + sub_str + color_code['normal'], end='')
        current_color = 'normal'
    else:
        color = sub_str.strip()
        if color in color_code:
            current_color = color
        else:
            print(f"\n[print_Color] ERROR: Invalid color!!! The input color: '{color}'")
print('', end=print_END)

```

Supported Colors

you can use the key word like 'black' and... to set the text color.

~~~ 'black': '\x1b[0;30m', 'red': '\x1b[0;31m', 'green': '\x1b[0;32m', 'yellow': '\x1b[0;33m', 'blue': '\x1b[0;34m', 'magenta': '\x1b[0;35m', 'cyan': '\x1b[0;36m', 'white': '\x1b[0;37m', 'normal': '\x1b[0m', 'bgblack': '\x1b[40m', 'bgred': '\x1b[41m', 'bggreen': '\x1b[42m', 'bgyellow': '\x1b[43m', 'bgblue': '\x1b[44m', 'bgmagenta': '\x1b[45m', 'bgcyan': '\x1b[46m', 'bgwhite': '\x1b[47m', 'bgnormal': '\x1b[49m', 'lightgray': '\x1b[0;90m', 'lightred': '\x1b[0;91m', 'lightgreen': '\x1b[0;92m', 'lightyellow': '\x1b[0;93m', 'lightblue': '\x1b[0;94m', 'lightmagenta': '\x1b[0;95m', 'lightcyan': '\x1b[0;96m', 'lightwhite': '\x1b[0;97m', 'bglightgray': '\x1b[0;100m', 'bglightred': '\x1b[0;101m', 'bglightgreen': '\x1b[0;102m', 'bglightyellow': '\x1b[0;103m', 'bglightblue': '\x1b[0;104m', 'bglightmagenta': '\x1b[0;105m', 'bglightcyan': '\x1b[0;106m', 'bglight_white': '\x1b[0;107m', 'underline': '\x1b[4m', 'bold': '\x1b[1m', 'blink': '\x1b[5m' ~~~

Owner

  • Name: Aidin
  • Login: AidinHamedi
  • Kind: user

Segmentation fault

GitHub Events

Total
Last Year

Issues and Pull Requests

Last synced: about 1 year ago

All Time
  • Total issues: 0
  • Total pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Total issue authors: 0
  • Total pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels