outputformat 0.1.4__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.
- outputformat/__init__.py +11 -0
- outputformat/color.py +89 -0
- outputformat/dict.py +53 -0
- outputformat/emoji.py +33 -0
- outputformat/fonts.py +381 -0
- outputformat/list.py +69 -0
- outputformat/misc.py +31 -0
- outputformat/plot.py +277 -0
- outputformat/title.py +235 -0
- outputformat/tools.py +218 -0
- outputformat-0.1.4.dist-info/METADATA +24 -0
- outputformat-0.1.4.dist-info/RECORD +15 -0
- outputformat-0.1.4.dist-info/WHEEL +5 -0
- outputformat-0.1.4.dist-info/licenses/LICENSE +21 -0
- outputformat-0.1.4.dist-info/top_level.txt +1 -0
outputformat/tools.py
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
from outputformat.title import linetitle
|
|
2
|
+
from outputformat.title import boxtitle
|
|
3
|
+
from outputformat import emoji
|
|
4
|
+
import colorsys
|
|
5
|
+
from random import random
|
|
6
|
+
from re import sub
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def prepare_data(input_data, precision):
|
|
10
|
+
"""
|
|
11
|
+
Prepare the input data so that display is cleaner
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
data = input_data.copy()
|
|
15
|
+
|
|
16
|
+
# Check if we have negative and positive values
|
|
17
|
+
# If we don't have negative values, there's no point in adding "+"
|
|
18
|
+
# To align the list
|
|
19
|
+
negative_values = False
|
|
20
|
+
for value in data:
|
|
21
|
+
if isinstance(value, (int, float)) and value < 0:
|
|
22
|
+
negative_values = True
|
|
23
|
+
|
|
24
|
+
for idx, value in enumerate(data):
|
|
25
|
+
|
|
26
|
+
# In case float, adjust precision
|
|
27
|
+
if isinstance(value, float):
|
|
28
|
+
data[idx] = f"{value:.{precision}f}"
|
|
29
|
+
if negative_values and value > 0:
|
|
30
|
+
data[idx] = "+" + data[idx]
|
|
31
|
+
|
|
32
|
+
# In case int, just add "+" if needed
|
|
33
|
+
if isinstance(value, int):
|
|
34
|
+
data[idx] = f"{value}"
|
|
35
|
+
if negative_values and value > 0:
|
|
36
|
+
data[idx] = "+" + data[idx]
|
|
37
|
+
|
|
38
|
+
# In case list, be recursive
|
|
39
|
+
if isinstance(value, list):
|
|
40
|
+
data[idx] = prepare_data(value, precision=precision)
|
|
41
|
+
|
|
42
|
+
return data
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def get_markers_and_title(style, title=False):
|
|
46
|
+
|
|
47
|
+
if style in ["bullet", "bullets", "dot", "dots", ".", "*"]:
|
|
48
|
+
marker_first = "•"
|
|
49
|
+
marker_middle = "•"
|
|
50
|
+
marker_last = "•"
|
|
51
|
+
|
|
52
|
+
elif style in ["line", "-", "hang"]:
|
|
53
|
+
marker_first = "╭"
|
|
54
|
+
marker_middle = "├"
|
|
55
|
+
marker_last = "╰"
|
|
56
|
+
|
|
57
|
+
# Here we need a fancy title to go with the line
|
|
58
|
+
if title:
|
|
59
|
+
marker_first = marker_middle
|
|
60
|
+
title = linetitle(title, style="hang", return_str=True)
|
|
61
|
+
|
|
62
|
+
elif style in ["box", "line_box", "box_hang"]:
|
|
63
|
+
marker_first = "╭"
|
|
64
|
+
marker_middle = "├"
|
|
65
|
+
marker_last = "╰"
|
|
66
|
+
|
|
67
|
+
# Here we need a fancy title to go with the box
|
|
68
|
+
if title:
|
|
69
|
+
marker_first = marker_middle
|
|
70
|
+
title = boxtitle(title, style="hang", return_str=True)
|
|
71
|
+
else:
|
|
72
|
+
marker_first = style
|
|
73
|
+
marker_middle = style
|
|
74
|
+
marker_last = style
|
|
75
|
+
|
|
76
|
+
return marker_first, marker_middle, marker_last, title
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def decide_marker(idx, marker_first, marker_middle, marker_last, style, data):
|
|
80
|
+
|
|
81
|
+
if idx == 0:
|
|
82
|
+
marker = marker_first
|
|
83
|
+
|
|
84
|
+
elif idx == len(data) - 1:
|
|
85
|
+
marker = marker_last
|
|
86
|
+
|
|
87
|
+
else:
|
|
88
|
+
marker = marker_middle
|
|
89
|
+
|
|
90
|
+
# In case ordinal list, override marker
|
|
91
|
+
if style in ["ordinal", "#", "numbered", "num"]:
|
|
92
|
+
maxdigits = len(str(len(data))) + 1
|
|
93
|
+
marker = f"{int(idx+1)}.".zfill(maxdigits)
|
|
94
|
+
|
|
95
|
+
return marker
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def hsv2rgb(hsv_tuple):
|
|
99
|
+
|
|
100
|
+
"""Converts HSV floats to 8-bit RGB."""
|
|
101
|
+
h = hsv_tuple[0]
|
|
102
|
+
s = hsv_tuple[1]
|
|
103
|
+
v = hsv_tuple[2]
|
|
104
|
+
|
|
105
|
+
rgb_tuple = tuple(int(round(i * 255)) for i in colorsys.hsv_to_rgb(h, s, v))
|
|
106
|
+
|
|
107
|
+
return rgb_tuple
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def rgb2hsv(rgb_tuple):
|
|
111
|
+
|
|
112
|
+
"""Converts RGB floats to HSV."""
|
|
113
|
+
r = rgb_tuple[0]
|
|
114
|
+
g = rgb_tuple[1]
|
|
115
|
+
b = rgb_tuple[2]
|
|
116
|
+
|
|
117
|
+
hsv_tuple = colorsys.rgb_to_hsv(r, g, b)
|
|
118
|
+
|
|
119
|
+
return hsv_tuple
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def hex2rgb(hex_string):
|
|
123
|
+
hex = hex_string.lstrip("#")
|
|
124
|
+
|
|
125
|
+
rgb = []
|
|
126
|
+
try:
|
|
127
|
+
for i in (0, 2, 4):
|
|
128
|
+
decimal = int(hex[i : i + 2], 16)
|
|
129
|
+
rgb.append(decimal / 255)
|
|
130
|
+
except:
|
|
131
|
+
error_message = f"'{hex_string}' is not a valid hex color {emoji.sad}"
|
|
132
|
+
raise ValueError(error_message)
|
|
133
|
+
|
|
134
|
+
return tuple(rgb)
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def parse_color(color, cmap):
|
|
138
|
+
"""Guess the color that the user wants
|
|
139
|
+
|
|
140
|
+
Returns
|
|
141
|
+
-------
|
|
142
|
+
tuple
|
|
143
|
+
HSV color tuple (ouf default format for colors).
|
|
144
|
+
|
|
145
|
+
"""
|
|
146
|
+
default_saturation = 0.8
|
|
147
|
+
|
|
148
|
+
if color == "random":
|
|
149
|
+
color = (random(), default_saturation, 1)
|
|
150
|
+
elif color in ["red", "r"]:
|
|
151
|
+
color = (0.98, default_saturation, 1)
|
|
152
|
+
elif color in ["orange", "o"]:
|
|
153
|
+
color = (0.05, default_saturation, 1)
|
|
154
|
+
elif color in ["yellow", "y"]:
|
|
155
|
+
color = (0.14, default_saturation, 1)
|
|
156
|
+
elif color in ["green", "g"]:
|
|
157
|
+
color = (0.35, default_saturation, 1)
|
|
158
|
+
elif color in ["cyan", "c", "aqua", "a"]:
|
|
159
|
+
color = (0.5, default_saturation, 1)
|
|
160
|
+
elif color in ["blue", "b"]:
|
|
161
|
+
color = (0.58, default_saturation, 1)
|
|
162
|
+
elif color in ["indigo", "i"]:
|
|
163
|
+
color = (0.67, default_saturation, 1)
|
|
164
|
+
elif color in ["violet", "v", "purple"]:
|
|
165
|
+
color = (0.74, default_saturation, 1)
|
|
166
|
+
elif color in ["magenta", "m", "pink", "p"]:
|
|
167
|
+
color = (0.85, default_saturation, 1)
|
|
168
|
+
elif color in ["gray", "grey"]:
|
|
169
|
+
color = (0, 0, 0.7)
|
|
170
|
+
elif color in ["black", "k"]:
|
|
171
|
+
color = (0, 0, 0)
|
|
172
|
+
elif color in ["white", "w"]:
|
|
173
|
+
color = (0, 0, 1)
|
|
174
|
+
|
|
175
|
+
elif "#" in str(color):
|
|
176
|
+
color = rgb2hsv(hex2rgb(color))
|
|
177
|
+
|
|
178
|
+
# In case a single float is given we use a color map
|
|
179
|
+
if isinstance(color, float) and color >= 0 and color <= 1:
|
|
180
|
+
original_value = color
|
|
181
|
+
if cmap == "hsv":
|
|
182
|
+
color = (color, default_saturation, 1)
|
|
183
|
+
|
|
184
|
+
elif cmap == "green-red":
|
|
185
|
+
color = rgb2hsv([color, 1 - color, 0])
|
|
186
|
+
color = (color[0], default_saturation, 1)
|
|
187
|
+
|
|
188
|
+
elif cmap == "blue-red":
|
|
189
|
+
color = rgb2hsv([color, 0, 1 - color])
|
|
190
|
+
color = (color[0], default_saturation, 1)
|
|
191
|
+
|
|
192
|
+
elif cmap == "cool":
|
|
193
|
+
color = rgb2hsv([color, 0.8 - color, 1])
|
|
194
|
+
color = (color[0], default_saturation, 1)
|
|
195
|
+
|
|
196
|
+
elif cmap == "hot":
|
|
197
|
+
color = rgb2hsv([1, 0.8 - color, color / 2])
|
|
198
|
+
color = (color[0], default_saturation, 1)
|
|
199
|
+
|
|
200
|
+
elif cmap == "greys":
|
|
201
|
+
color = (0, 0, color)
|
|
202
|
+
|
|
203
|
+
elif cmap == "random":
|
|
204
|
+
color = (random(), default_saturation, 1)
|
|
205
|
+
|
|
206
|
+
return color
|
|
207
|
+
|
|
208
|
+
def real_string_length(txt):
|
|
209
|
+
"""Compute the length of the strings without the ANSI escape sequences
|
|
210
|
+
|
|
211
|
+
Returns
|
|
212
|
+
-------
|
|
213
|
+
int
|
|
214
|
+
real length of the string txt
|
|
215
|
+
|
|
216
|
+
"""
|
|
217
|
+
pattern = "\x1b\[[;\d]*m"
|
|
218
|
+
return len(sub(pattern,"",txt))
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: outputformat
|
|
3
|
+
Version: 0.1.4
|
|
4
|
+
Summary: Decorate and beautify output
|
|
5
|
+
Home-page: https://github.com/delestro/outputformat
|
|
6
|
+
Download-URL: https://github.com/delestro/outputformat/archive/refs/tags/v0.1.4.tar.gz
|
|
7
|
+
Author: Felipe Delestro
|
|
8
|
+
Author-email: delestro@gmail.com
|
|
9
|
+
License: MIT
|
|
10
|
+
Keywords: output,print,decorate,format,string,beautify
|
|
11
|
+
Requires-Python: >=3.6
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Dynamic: author
|
|
14
|
+
Dynamic: author-email
|
|
15
|
+
Dynamic: description
|
|
16
|
+
Dynamic: download-url
|
|
17
|
+
Dynamic: home-page
|
|
18
|
+
Dynamic: keywords
|
|
19
|
+
Dynamic: license
|
|
20
|
+
Dynamic: license-file
|
|
21
|
+
Dynamic: requires-python
|
|
22
|
+
Dynamic: summary
|
|
23
|
+
|
|
24
|
+
For usage, check https://delestro.com/2022/01/16/outputformat/
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
outputformat/__init__.py,sha256=dySpG8xxMAZL66Qx8kpiEWVH7BhPThR2UcHxstRc1uQ,408
|
|
2
|
+
outputformat/color.py,sha256=xAfBWhBK3-dCAcrhsw-omXbz392izJkvxtfoaR3Dkd0,1889
|
|
3
|
+
outputformat/dict.py,sha256=nE7KBNxTo4K7tSvThBDuW-gNyth9JI4Bef5x1jAzvjM,1484
|
|
4
|
+
outputformat/emoji.py,sha256=Tdi0QzyBtE7LKWk42FvVS4im_7QI-mlUs-QmfI_r2o0,632
|
|
5
|
+
outputformat/fonts.py,sha256=GP5yblsVwCeImp51-SzwYe-1NHx71_KYGipNrJd9-0Y,8568
|
|
6
|
+
outputformat/list.py,sha256=JDMMN5bgfp_vsqb8YIiaJwHcYvGbm5gqJmeyVylim9A,1997
|
|
7
|
+
outputformat/misc.py,sha256=_v7wxeT_NJkDFCvf9CffnGHRLjD8i1AwF5ksAfVm0YQ,606
|
|
8
|
+
outputformat/plot.py,sha256=DdQLjxHmfWk5frv9mMnnGEFqx_d5FGxTY-Q2NK3KlB8,7931
|
|
9
|
+
outputformat/title.py,sha256=xa2Tr8oEhz07XZ4fy5aMub5xMbxE6BLbC1gs9-vjsQE,6006
|
|
10
|
+
outputformat/tools.py,sha256=8PzizWGp5Bf_bkC2Udax8AFUno2vVgzthIhaN1V62EY,5948
|
|
11
|
+
outputformat-0.1.4.dist-info/licenses/LICENSE,sha256=zK79ZdBJbt4pzr5fRxZjCkPuGdyoVhDQ_2MXs-VzhlU,1072
|
|
12
|
+
outputformat-0.1.4.dist-info/METADATA,sha256=xF2DYjxIYed0s4KJLJnnAfiA8w_xULPeeAso9iEX7QA,666
|
|
13
|
+
outputformat-0.1.4.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
14
|
+
outputformat-0.1.4.dist-info/top_level.txt,sha256=-sQnJuMnV6UL8h_1Ajxw1MAFoEW2OxhpSpVEkb4x2h8,13
|
|
15
|
+
outputformat-0.1.4.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Felipe Delestro
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
outputformat
|