colore 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- colore/__init__.py +2 -0
- colore/__main__.py +16 -0
- colore/colors.py +82 -0
- colore/filter.py +57 -0
- colore/help.py +124 -0
- colore-1.0.0.dist-info/LICENSE +662 -0
- colore-1.0.0.dist-info/METADATA +933 -0
- colore-1.0.0.dist-info/RECORD +11 -0
- colore-1.0.0.dist-info/WHEEL +5 -0
- colore-1.0.0.dist-info/entry_points.txt +2 -0
- colore-1.0.0.dist-info/top_level.txt +1 -0
colore/__init__.py
ADDED
colore/__main__.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
from .filter import process
|
|
3
|
+
from .help import show_help
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def main():
|
|
7
|
+
if len(sys.argv) == 3 and (sys.argv[1] in ("-f", "--file")):
|
|
8
|
+
process(sys.argv[2])
|
|
9
|
+
elif len(sys.argv) == 2 and (sys.argv[1] in ("-h", "--help")):
|
|
10
|
+
show_help()
|
|
11
|
+
else:
|
|
12
|
+
process()
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
if __name__ == "__main__":
|
|
16
|
+
main()
|
colore/colors.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Functions to define a fistful of color names associated with their ANSI codes,
|
|
3
|
+
to use them in a terminal
|
|
4
|
+
"""
|
|
5
|
+
import os
|
|
6
|
+
|
|
7
|
+
user_home = os.path.expanduser('~')
|
|
8
|
+
|
|
9
|
+
CUSTOM_ALIASES_PATHS = (user_home + "/.colore_aliases",
|
|
10
|
+
user_home + "/.config/colore_aliases")
|
|
11
|
+
COLOR_END = "\033[0m"
|
|
12
|
+
DEFAULT_COLOR = "\033[31m"
|
|
13
|
+
|
|
14
|
+
FG = {
|
|
15
|
+
"black": "30",
|
|
16
|
+
"red": "31",
|
|
17
|
+
"green": "32",
|
|
18
|
+
"yellow": "33",
|
|
19
|
+
"blue": "34",
|
|
20
|
+
"magenta": "35",
|
|
21
|
+
"cyan": "36",
|
|
22
|
+
"lgrey": "37",
|
|
23
|
+
"grey": "90",
|
|
24
|
+
"lred": "91",
|
|
25
|
+
"lgreen": "92",
|
|
26
|
+
"lyellow": "93",
|
|
27
|
+
"lblue": "94",
|
|
28
|
+
"lmagenta": "95",
|
|
29
|
+
"lcyan": "96",
|
|
30
|
+
"white": "97",
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
BG = {
|
|
34
|
+
"_black": "40",
|
|
35
|
+
"_red": "41",
|
|
36
|
+
"_green": "42",
|
|
37
|
+
"_yellow": "43",
|
|
38
|
+
"_blue": "44",
|
|
39
|
+
"_magenta": "45",
|
|
40
|
+
"_cyan": "46",
|
|
41
|
+
"_lgrey": "37",
|
|
42
|
+
"_grey": "100",
|
|
43
|
+
"_lred": "101",
|
|
44
|
+
"_lgreen": "102",
|
|
45
|
+
"_lyellow": "103",
|
|
46
|
+
"_lblue": "104",
|
|
47
|
+
"_lmagenta": "105",
|
|
48
|
+
"_lcyan": "106",
|
|
49
|
+
"_white": "107",
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def color_code(cc):
|
|
54
|
+
return f"\033[{cc}m"
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def basic_colors():
|
|
58
|
+
fg = {x: color_code(y) for x, y in FG.items()}
|
|
59
|
+
bg = {x: color_code(y) for x, y in BG.items()}
|
|
60
|
+
return {**fg, **bg}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def custom_aliases():
|
|
64
|
+
colors_dict = {}
|
|
65
|
+
for path in CUSTOM_ALIASES_PATHS:
|
|
66
|
+
if os.path.isfile(path):
|
|
67
|
+
with open(path) as f:
|
|
68
|
+
lines = [l for l in f.read().splitlines() if
|
|
69
|
+
len(l) > 0 and l[0] != "#"]
|
|
70
|
+
for line in lines:
|
|
71
|
+
name, value = line.split(" ")
|
|
72
|
+
colors_dict[name] = color_code(value)
|
|
73
|
+
break
|
|
74
|
+
return colors_dict
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def all_colors():
|
|
78
|
+
basic = basic_colors()
|
|
79
|
+
combined = {x[0] + y[0]: color_code(x[1] + ";" + y[1]) for x in FG.items()
|
|
80
|
+
for y in BG.items()}
|
|
81
|
+
custom = custom_aliases()
|
|
82
|
+
return {**basic, **combined, **custom}
|
colore/filter.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Filters stdin, coloring the lines containing the indicated pattern with its
|
|
3
|
+
associated color
|
|
4
|
+
"""
|
|
5
|
+
import re
|
|
6
|
+
import sys
|
|
7
|
+
|
|
8
|
+
from .colors import COLOR_END, DEFAULT_COLOR, all_colors, color_code
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def from_args():
|
|
12
|
+
return get_pairs(sys.argv[1:])
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def from_file(path):
|
|
16
|
+
try:
|
|
17
|
+
with open(path) as f:
|
|
18
|
+
pairs = f.read().splitlines()
|
|
19
|
+
except OSError as e:
|
|
20
|
+
print(e)
|
|
21
|
+
sys.exit()
|
|
22
|
+
return get_pairs(pairs)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def get_pairs(pairs):
|
|
26
|
+
pattern_color_code = re.compile("^[\d;]*$")
|
|
27
|
+
colors_dict = all_colors()
|
|
28
|
+
patterns = []
|
|
29
|
+
colors = []
|
|
30
|
+
for i in range(0, len(pairs), 2):
|
|
31
|
+
try:
|
|
32
|
+
compiled = re.compile(pairs[i])
|
|
33
|
+
except re.error as e:
|
|
34
|
+
print(e)
|
|
35
|
+
sys.exit()
|
|
36
|
+
patterns.append(compiled)
|
|
37
|
+
if pattern_color_code.search(pairs[i + 1]):
|
|
38
|
+
colors.append(color_code(pairs[i + 1]))
|
|
39
|
+
else:
|
|
40
|
+
colors.append(colors_dict.get(pairs[i + 1], DEFAULT_COLOR))
|
|
41
|
+
return patterns, colors
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def filter_lines(pairs):
|
|
45
|
+
patterns, colors = pairs
|
|
46
|
+
end_line = COLOR_END + "\n"
|
|
47
|
+
for line in sys.stdin:
|
|
48
|
+
for i in range(len(patterns)):
|
|
49
|
+
if patterns[i].search(line):
|
|
50
|
+
line = colors[i] + line.rstrip("\n") + end_line
|
|
51
|
+
break
|
|
52
|
+
print(line, end="")
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def process(path=None):
|
|
56
|
+
pairs = from_file(path) if path else from_args()
|
|
57
|
+
filter_lines(pairs)
|
colore/help.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
|
|
3
|
+
text_help = """
|
|
4
|
+
Highlight each input line that contains the PATTERN using COLOR.
|
|
5
|
+
Several pattern/color pairs can be indicated.
|
|
6
|
+
|
|
7
|
+
USE: colore [PATTERN1 COLOR1] [PATTERN2 COLOR2]...
|
|
8
|
+
|
|
9
|
+
Examples:
|
|
10
|
+
ls / | colore bin 31 var 33 lib "32;100"
|
|
11
|
+
ls / | colore bin red var yellow lib green_grey
|
|
12
|
+
|
|
13
|
+
PATTERNs can be any Python regular expression, including wildcards and special
|
|
14
|
+
characters. For example, you can use the wildcard `\d` or the class `[0-9]` to
|
|
15
|
+
indicate any number, the characters `^` and `$` to indicate the beginning and
|
|
16
|
+
end of a line respectively, etc.
|
|
17
|
+
|
|
18
|
+
COLORs can be defined using numerical ANSI codes (see man console_codes(4)).
|
|
19
|
+
For example, red text would be represented by the number `31`, green background
|
|
20
|
+
by the number `42`, and underline by the number `4`. Joining these three codes
|
|
21
|
+
with semicolons of the form `31;42;4` would obtain the code for underlined red
|
|
22
|
+
text on a green background.
|
|
23
|
+
|
|
24
|
+
The order is irrelevant. The semicolon `;` must be escaped with a slash `\`
|
|
25
|
+
(so the above example would be `31\;42\;4`), or the entire color code
|
|
26
|
+
|
|
27
|
+
COLOR can also be any of the following aliases:
|
|
28
|
+
|
|
29
|
+
Front colors:
|
|
30
|
+
|
|
31
|
+
black: 30
|
|
32
|
+
red: 31
|
|
33
|
+
green: 32
|
|
34
|
+
yellow: 33
|
|
35
|
+
blue: 34
|
|
36
|
+
magenta: 35
|
|
37
|
+
cyan: 36
|
|
38
|
+
lgrey: 37
|
|
39
|
+
grey: 90
|
|
40
|
+
lred: 91
|
|
41
|
+
lgreen: 92
|
|
42
|
+
lyellow: 93
|
|
43
|
+
lblue: 94
|
|
44
|
+
lmagenta: 95
|
|
45
|
+
lcyan: 96
|
|
46
|
+
white: 97
|
|
47
|
+
|
|
48
|
+
Baclkground colors:
|
|
49
|
+
|
|
50
|
+
_black: 40
|
|
51
|
+
_red: 41
|
|
52
|
+
_green: 42
|
|
53
|
+
_yellow: 43
|
|
54
|
+
_blue: 44
|
|
55
|
+
_magenta: 45
|
|
56
|
+
_cyan: 46
|
|
57
|
+
_lgrey: 37
|
|
58
|
+
_grey: 100
|
|
59
|
+
_lred: 101
|
|
60
|
+
_lgreen: 102
|
|
61
|
+
_lyellow: 103
|
|
62
|
+
_lblue: 104
|
|
63
|
+
_lmagenta: 105
|
|
64
|
+
_lcyan: 106
|
|
65
|
+
_white: 107
|
|
66
|
+
|
|
67
|
+
Combinations of foreground color and background color, such as withe_red or
|
|
68
|
+
lblue_blue, are also valid names.
|
|
69
|
+
|
|
70
|
+
If is specified a color name that does not exist and is not defined as an
|
|
71
|
+
alias, the color `31` (`red`) will be used.
|
|
72
|
+
|
|
73
|
+
Other Options:
|
|
74
|
+
|
|
75
|
+
-h, --help show this help (if it is the only argument).
|
|
76
|
+
|
|
77
|
+
-f, --file FILEMANE gets PATTERNS and COLORS from a file instead
|
|
78
|
+
of arguments.
|
|
79
|
+
|
|
80
|
+
The format of FILEMANE must be the following:
|
|
81
|
+
|
|
82
|
+
pattern
|
|
83
|
+
color
|
|
84
|
+
pattern_2
|
|
85
|
+
color_2
|
|
86
|
+
pattern_3
|
|
87
|
+
color_3
|
|
88
|
+
|
|
89
|
+
So each line containing a pattern must be followed by a line with the color
|
|
90
|
+
code or name that will be assigned to the lines containing that pattern.
|
|
91
|
+
|
|
92
|
+
For example:
|
|
93
|
+
|
|
94
|
+
data_[\d]*\.text
|
|
95
|
+
green
|
|
96
|
+
output\.log
|
|
97
|
+
blue_yellow
|
|
98
|
+
error\.log
|
|
99
|
+
grid
|
|
100
|
+
^[\d]?$
|
|
101
|
+
32;44
|
|
102
|
+
|
|
103
|
+
Custom color aliases:
|
|
104
|
+
|
|
105
|
+
Custom color names can be defined in one of the following files:
|
|
106
|
+
|
|
107
|
+
~/.colore_aliases
|
|
108
|
+
~/.config/colore_aliases
|
|
109
|
+
|
|
110
|
+
The color configuration file must consist of one line for each color.
|
|
111
|
+
Each line will begin with the alias for the color, followed by a space,
|
|
112
|
+
and the numbers that define the color separated by semicolons:
|
|
113
|
+
|
|
114
|
+
rojo_sub 31;1;4
|
|
115
|
+
verde 32
|
|
116
|
+
bold_underline 1;4
|
|
117
|
+
|
|
118
|
+
Blank lines or lines starting with the "#" character will be ignored.
|
|
119
|
+
|
|
120
|
+
User-created names take precedence over predefined names.
|
|
121
|
+
"""
|
|
122
|
+
|
|
123
|
+
def show_help():
|
|
124
|
+
print(text_help)
|