Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a rainbow function #39

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ blue, lightblue, purple, light purple, orange, yellow
#### List of all styles

```python
bold, bg, under, strike, italic
bold, bg, under, strike, italic, rainbow
```

#### List of all labels
Expand Down
3 changes: 2 additions & 1 deletion huepy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
__all__ = ['bad', 'bg', 'black', 'blue', 'bold', 'cyan', 'good', 'green',
'grey', 'info', 'italic', 'lblue', 'lightblue', 'lcyan', 'lgreen',
'lightgreen', 'lpurple', 'lightpurple', 'lred', 'lightred','orange',
'purple', 'que', 'red', 'run','strike', 'under', 'white', 'yellow']
'purple', 'que', 'red', 'run','strike', 'under', 'white', 'yellow',
'rainbow']

__version__ = '1.2.1'

Expand Down
18 changes: 16 additions & 2 deletions huepy/huepy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@

print_mode = False


def _rainbow(string):
colors = [val for key,val in COMMANDS.items() if isinstance(val,int)]
out = "".join([_gen(string[i],'',colors[i%len(colors)]) for i in range(len(string))])
return out


COMMANDS = {
# Lables
'info': (33, '[!] '),
Expand Down Expand Up @@ -45,13 +52,20 @@
'italic': '3',
'under': '4',
'strike': '09',

# Special
'rainbow' : _rainbow,
}


def _gen(string, prefix, key):
colored = prefix if prefix else string
not_colored = string if prefix else ''
result = '\033[{}m{}\033[0m{}'.format(key, colored, not_colored)
is_special = callable(key)
if is_special:
result = key(string)
else:
result = '\033[{}m{}\033[0m{}'.format(key, colored, not_colored)
if print_mode:
print(result)
else:
Expand All @@ -61,4 +75,4 @@ def _gen(string, prefix, key):
for key, val in COMMANDS.items():
value = val[0] if isinstance(val, tuple) else val
prefix = val[1] if isinstance(val, tuple) else ''
locals()[key] = lambda s, prefix=prefix, key=value: _gen(s, prefix, key)
locals()[key] = lambda s, prefix=prefix, key=value: _gen(s, prefix, key)