-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
viewer.py
executable file
·114 lines (92 loc) · 2.98 KB
/
viewer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# -*- encoding: utf-8 -*-
# kb v0.1.6
# A knowledge base organizer
# Copyright © 2020, gnc.
# See /LICENSE for licensing information.
"""
kb viewer module
:Copyright: © 2020, gnc.
:License: GPLv3 (see /LICENSE).
"""
import re
from typing import Dict
from kb.styler import set_fg, reset
def colorize_string(string, color):
"""
This function applies the provided color to the specified string
Arguments:
msg - The message to be colored
color - The name of the color (or hex code), e.g., "red" or "#C0C0C0"
Returns:
A colored message
"""
return set_fg(color) + string + reset()
def colorize_row(row, markers=None):
"""
This function takes a string and a dictionary of markers
(where key is regex and value is color) and
the transforms the passed string (row) with colors.
Arguments:
row - the string
markers - a dictionary having as key strings
representing the purpose of the formatting
and having as values a list where
the first element is a regex and the second
element is the color value.
Example:
markers = {
"TITLE": ["^#.*", "blue"]
"WARNINGS": ["^!.*", "yellow"]
}
Returns:
A new message colored following the rules within the markers
dictionary
"""
colored_row = row
for mark in markers:
regex = re.compile(rf'{(markers[mark][0])}')
color = markers[mark][1]
match = regex.search(row)
if match:
colored_row = re.sub(regex, colorize_string(match.group(0).replace("\\","\\\\"), color), rf'{row}')
row = colored_row
return colored_row
def colorize_output(data, markers):
"""
This function takes an input a list of strings, for example they
can be taken from a file or any other source, processes them
and returns a list of formatted colored strings ready to be
visualized.
Arguments:
data - A list of strings.
markers - an object contains configured marks
Returns:
A new formatted list
"""
if markers is None:
return data
colorized_output = list()
for row in data:
colorized_output.append(colorize_row(row, markers))
return colorized_output
def view(filepath: str, markers: Dict[str, str], color: bool = True) -> None:
"""
Visualize the specified file with applied markers
if color is True.
Arguments:
filepath - the file to visualize on stdout
markers - the markers dictionary to apply
color - a boolean, if True color is enabled
"""
content = ""
with open(filepath) as fname:
content = fname.read()
# Print on screen with proper markers
lines = content.splitlines()
if color:
colored_lines = colorize_output(lines, markers)
for line in colored_lines:
print(line)
else:
for line in lines:
print(line)