Skip to content

Commit f06e7ae

Browse files
committed
Initial commit
0 parents  commit f06e7ae

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed

README.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
== Bash color
2+
3+
=== Available colors
4+
5+
* BLACK
6+
* RED
7+
* GREEN
8+
* BROWN
9+
* BLUE
10+
* PURPLE
11+
* CYAN
12+
* LIGHT_GRAY
13+
* _OTHER
14+
* DEFAULT
15+
* DARK_GRAY
16+
* LIGHT_RED
17+
* LIGHT_GREEN
18+
* YELLOW
19+
* LIGHT_BLUE
20+
* LIGHT_PURPLE
21+
* LIGHT_CYAN
22+
* WHITE
23+
24+
=== Available effect
25+
26+
* BOLD
27+
* DIM # not working on Konsole
28+
* UNDERLINE
29+
* BLINK # not working on Konsole and gnome Terminal
30+
* INVERSE
31+
* HIDDEN # not working on Konsole
32+
33+
=== Example
34+
35+
```python
36+
from bash import colorize, RED, UNDERLINE
37+
38+
print(colorize('Red color', RED))
39+
print(colorize('Red background', background=RED))
40+
print(colorize('Underline', effect=UNDERLINE))
41+
```
42+
43+
=== API
44+
45+
``python
46+
def colorize(text, color=None, background=None, effects=[], color_256=None, background_256=None, with_end=True):
47+
```

bashcolor/__init__.py

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# see also: http://misc.flogisoft.com/bash/tip_colors_and_formatting
4+
5+
6+
RESET = 0
7+
BLACK = 30
8+
RED = 31
9+
GREEN = 32
10+
BROWN = 33
11+
BLUE = 34
12+
PURPLE = 35
13+
CYAN = 36
14+
LIGHT_GRAY = 37
15+
_OTHER = 38
16+
DEFAULT = 39
17+
DARK_GRAY = 90
18+
LIGHT_RED = 91
19+
LIGHT_GREEN = 92
20+
YELLOW = 93
21+
LIGHT_BLUE = 94
22+
LIGHT_PURPLE = 95
23+
LIGHT_CYAN = 96
24+
WHITE = 97
25+
26+
BOLD = 1
27+
DIM = 2 # not working on Konsole
28+
UNDERLINE = 4
29+
BLINK = 5 # not working on Konsole and gnome Terminal
30+
INVERSE = 7
31+
HIDDEN = 8 # not working on Konsole
32+
33+
_ESC = '\033'
34+
_ESC = '\x1b'
35+
_BACKGROUND = 10
36+
_RESET_EFFECT = 10
37+
38+
39+
def colorize(text, color=None, background=None, effects=[], color_256=None, background_256=None, with_end=True):
40+
start = []
41+
end = []
42+
43+
if color is not None:
44+
start.append(color)
45+
end.append(DEFAULT)
46+
elif color_256 is not None:
47+
start += [_OTHER, 5, color_256]
48+
end.append(DEFAULT)
49+
50+
if background is not None:
51+
start.append(background + _BACKGROUND)
52+
end.append(DEFAULT + _BACKGROUND)
53+
elif background_256 is not None:
54+
start += [_OTHER + _BACKGROUND, 5, background_256]
55+
end.append(DEFAULT + _BACKGROUND)
56+
57+
for effect in effects:
58+
start.append(effect)
59+
end.append(effect + _RESET_EFFECT)
60+
61+
start_code = "%s[%sm" % (_ESC, ';'.join([str(s) for s in start])) if text != '' else ''
62+
end_code = "%s[%sm" % (_ESC, ';'.join([str(e) for e in end])) if with_end else ''
63+
return "%s%s%s" % (start_code, text, end_code)
64+
65+
66+
def print_colors():
67+
color_pivot = [0]
68+
color_pivot += [e * 6 + 16 for e in range(37)]
69+
color_pivot.append(256)
70+
color_pivot_start = color_pivot[:-1]
71+
color_pivot_end = color_pivot[1:]
72+
color_table = [range(cs, ce) for cs, ce in zip(color_pivot_start, color_pivot_end)]
73+
74+
for ct in color_table:
75+
text = ''
76+
for c in ct:
77+
cs = str(c)
78+
padding = ''.join([' ' for e in range(3 - len(cs))])
79+
text += colorize(' %s%s ' % (padding, cs), background_256=c, with_end=False)
80+
print(text + colorize('', background=DEFAULT))
81+
82+
83+
if __name__ == "__main__":
84+
print_colors()

setup.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import os
4+
5+
from setuptools import setup, find_packages
6+
7+
here = os.path.abspath(os.path.dirname(__file__))
8+
README = (
9+
open(os.path.join(here, 'README.rst')).read()
10+
)
11+
12+
install_requires = [
13+
]
14+
15+
setup_requires = [
16+
]
17+
18+
tests_require = install_requires + [
19+
]
20+
21+
setup(
22+
name="bashcolor",
23+
version="0.9.0",
24+
description="A simple library to get colors in the bash terminal",
25+
long_description=README,
26+
classifiers=[
27+
"Programming Language :: Python :: 3",
28+
],
29+
author="Stéphane Brunner",
30+
author_email="[email protected]",
31+
url="https://github.com/sbrunner/bashcolor/",
32+
packages=find_packages(exclude=["*.tests", "*.tests.*"]),
33+
include_package_data=True,
34+
zip_safe=False,
35+
install_requires=install_requires,
36+
setup_requires=setup_requires,
37+
tests_require=tests_require,
38+
entry_points={
39+
"console_scripts": [
40+
"print_colors = bashcolor:print_colors",
41+
],
42+
}
43+
)

0 commit comments

Comments
 (0)