outputformat 0.1.2__tar.gz → 0.1.4__tar.gz

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.
@@ -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,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/
@@ -4,3 +4,8 @@ from outputformat.title import bigtitle
4
4
  from outputformat.list import showlist
5
5
  from outputformat.plot import bar
6
6
  from outputformat.plot import barlist
7
+ from outputformat.dict import showdict
8
+ from outputformat.misc import br
9
+ from outputformat.misc import b
10
+ from outputformat.color import c
11
+ from outputformat.color import rainbow
@@ -0,0 +1,89 @@
1
+ import outputformat as ouf
2
+
3
+ # Just a shortcut for the ANSI end code
4
+ ansi_end = "\033[0;00m"
5
+
6
+ # list of possible color maps
7
+ cmaps = ["hsv", "blue-red", "green-red", "cool", "hot", "greys", "random"]
8
+
9
+
10
+ def ansi_hsv(hsv_tuple):
11
+ """Creates a ANSI code from a HSV tuple
12
+
13
+ Parameters
14
+ ----------
15
+ hsv_tuple : tuple
16
+ HSV color, tuple of 3 float values.
17
+
18
+ Returns
19
+ -------
20
+ string
21
+ ANSI code with the color
22
+
23
+ """
24
+
25
+ rgb_tuple = ouf.tools.hsv2rgb(hsv_tuple)
26
+
27
+ ansi_pre = f"\033[38;2;{rgb_tuple[0]};{rgb_tuple[1]};{rgb_tuple[2]}m"
28
+
29
+ return ansi_pre
30
+
31
+
32
+ def string_hsv(input, hsv_tuple):
33
+ """Apply HSV color to string"
34
+
35
+
36
+ Parameters
37
+ ----------
38
+ input : str
39
+ Text to be colored.
40
+ hsv_tuple : tuple
41
+ Tuple for HSV color (h ,s, v)
42
+
43
+ Returns
44
+ -------
45
+ str
46
+ String with the ANSI color
47
+
48
+ """
49
+ string = str(input)
50
+ ansi_pre = ansi_hsv(hsv_tuple)
51
+ ansi_post = ouf.color.ansi_end
52
+ return f"{ansi_pre}{string}{ansi_post}"
53
+
54
+
55
+ def c(input, color, cmap="hsv", bold=False, return_str=False):
56
+
57
+ string = str(input)
58
+ outputstring = ""
59
+
60
+ if color == "rainbow":
61
+ outputstring = ouf.rainbow(input, cmap=cmap, bold=bold, return_str=True)
62
+
63
+ else:
64
+ # Parse color
65
+ color = ouf.tools.parse_color(color, cmap)
66
+ outputstring = string_hsv(string, color)
67
+
68
+ if bold:
69
+ outputstring = ouf.b(outputstring, return_str=True)
70
+
71
+ if return_str:
72
+ return outputstring
73
+ else:
74
+ print(outputstring)
75
+
76
+
77
+ def rainbow(input, cmap="hsv", bold=False, return_str=False):
78
+ string = str(input)
79
+
80
+ outputstring = ""
81
+
82
+ for idx, char in enumerate(string):
83
+ ratio = idx / len(string)
84
+ outputstring += ouf.c(char, color=ratio, cmap=cmap, bold=bold, return_str=True)
85
+
86
+ if return_str:
87
+ return outputstring
88
+ else:
89
+ print(outputstring)
@@ -0,0 +1,53 @@
1
+ from outputformat.title import boxtitle
2
+ from outputformat.title import linetitle
3
+ from outputformat import tools
4
+
5
+
6
+ def showdict(data, style="line", title=False, return_str=False, precision=4):
7
+ # Start outputstring
8
+ outputstring = ""
9
+
10
+ # Get keys and values as lists
11
+ keys = list(data.keys())
12
+ values = list(data.values())
13
+
14
+ # Clean values
15
+ values = tools.prepare_data(values, precision=precision)
16
+
17
+ # Get the longest title to use the proper padding
18
+ longest_key = len(max(keys, key=len))
19
+
20
+ # Get markers and title
21
+ markers = tools.get_markers_and_title(style, title)
22
+ marker_first, marker_middle, marker_last, title = markers
23
+
24
+ if title:
25
+ outputstring += title + "\n"
26
+
27
+ for idx in range(len(data)):
28
+
29
+ marker = tools.decide_marker(
30
+ idx, marker_first, marker_middle, marker_last, style, data
31
+ )
32
+
33
+ # Get value to display
34
+ value = values[idx]
35
+
36
+ # In case value is a list, fix display
37
+ if isinstance(value, list):
38
+ clean_value = ""
39
+ for sub_idx, sub_value in enumerate(value):
40
+ clean_value += f"{sub_value}"
41
+ if sub_idx < len(value) - 1:
42
+ clean_value += ", "
43
+
44
+ value = clean_value
45
+
46
+ # Add to the outputstring
47
+ outputstring += f"{marker} {keys[idx]:.<{longest_key}}: {value}"
48
+ outputstring += "\n"
49
+
50
+ if return_str:
51
+ return outputstring
52
+ else:
53
+ print(outputstring)
@@ -1,4 +1,21 @@
1
- # Simply a list of shortcuts for the unicode emoji
1
+ # Simply a list of shortcuts for the emoji unicode
2
+
3
+ supported_list = [
4
+ "crazy",
5
+ "sad",
6
+ "circle_red",
7
+ "circle_orange",
8
+ "circle_yellow",
9
+ "circle_green",
10
+ "circle_white",
11
+ "circle_black",
12
+ "star",
13
+ "heart",
14
+ "thumbs_up",
15
+ "check",
16
+ "clap",
17
+ "bomb",
18
+ ]
2
19
 
3
20
  crazy = "\U0001F92A"
4
21
  sad = "\U0001F625"
@@ -0,0 +1,381 @@
1
+ # List of characters suported
2
+ # Every new font must include these
3
+ suported_chars = "0123456789abcdefghijklmnopqrstuvwxyz_-+!?.':= "
4
+
5
+ # List of styles (fonts) that can be used
6
+ # Every new font name must be included here
7
+ font_styles = ["small", "big"]
8
+
9
+ small = {
10
+ "0": ["█▀█", "█▄█"],
11
+ "1": ["▄█", " █"],
12
+ "2": ["▀█", "█▄"],
13
+ "3": ["▀▀█", "▄██"],
14
+ "4": ["█ █", "▀▀█"],
15
+ "5": ["█▀", "▄█"],
16
+ "6": ["█▄▄", "█▄█"],
17
+ "7": ["▀▀█", " █"],
18
+ "8": ["█▀█", "███"],
19
+ "9": ["█▀█", "▀▀█"],
20
+ "a": ["▄▀█", "█▀█"],
21
+ "b": ["█▄▄", "█▄█"],
22
+ "c": ["█▀▀", "█▄▄"],
23
+ "d": ["█▀▄", "█▄▀"],
24
+ "e": ["█▀▀", "██▄"],
25
+ "f": ["█▀▀", "█▀ "],
26
+ "g": ["█▀▀", "█▄█"],
27
+ "h": ["█ █", "█▀█"],
28
+ "i": ["█", "█"],
29
+ "j": [" █", "█▄█"],
30
+ "k": ["█▄▀", "█ █"],
31
+ "l": ["█ ", "█▄▄"],
32
+ "m": ["█▀▄▀█", "█ ▀ █"],
33
+ "n": ["█▄ █", "█ ▀█"],
34
+ "o": ["█▀█", "█▄█"],
35
+ "p": ["█▀█", "█▀▀"],
36
+ "q": ["█▀█", "▀▀█"],
37
+ "r": ["█▀█", "█▀▄"],
38
+ "s": ["█▀", "▄█"],
39
+ "t": ["▀█▀", " █ "],
40
+ "u": ["█ █", "█▄█"],
41
+ "v": ["█ █", "▀▄▀"],
42
+ "w": ["█ █ █", "▀▄▀▄▀"],
43
+ "x": ["▀▄▀", "█ █"],
44
+ "y": ["█▄█", " █ "],
45
+ "z": ["▀█", "█▄"],
46
+ "_": [" ", "▄▄▄"],
47
+ "-": [" ", "▀▀"],
48
+ "+": [" ▄ ", "▀█▀"],
49
+ "!": ["█", "▄"],
50
+ "?": ["█▀█", " ▄"],
51
+ ".": [" ", "▄"],
52
+ "'": ["▀", " "],
53
+ ":": ["▄", "▄"],
54
+ "=": ["▄▄", "▄▄"],
55
+ " ": [" ", " "],
56
+ }
57
+
58
+ big = {
59
+ "0": [
60
+ " ██████ ",
61
+ "██ ████",
62
+ "██ ██ ██",
63
+ "████ ██",
64
+ " ██████ ",
65
+ ],
66
+ "1": [
67
+ " ██",
68
+ "███",
69
+ " ██",
70
+ " ██",
71
+ " ██",
72
+ ],
73
+ "2": [
74
+ "██████ ",
75
+ " ██",
76
+ " █████ ",
77
+ "██ ",
78
+ "███████",
79
+ ],
80
+ "3": [
81
+ "██████ ",
82
+ " ██",
83
+ " █████ ",
84
+ " ██",
85
+ "██████ ",
86
+ ],
87
+ "4": [
88
+ "██ ██",
89
+ "██ ██",
90
+ "███████",
91
+ " ██",
92
+ " ██",
93
+ ],
94
+ "5": [
95
+ "███████",
96
+ "██ ",
97
+ "███████",
98
+ " ██",
99
+ "███████",
100
+ ],
101
+ "6": [
102
+ " ██████ ",
103
+ "██ ",
104
+ "███████ ",
105
+ "██ ██",
106
+ " ██████ ",
107
+ ],
108
+ "7": [
109
+ "███████",
110
+ " ██",
111
+ " ██ ",
112
+ " ██ ",
113
+ " ██ ",
114
+ ],
115
+ "8": [
116
+ " █████ ",
117
+ "██ ██",
118
+ " █████ ",
119
+ "██ ██",
120
+ " █████ ",
121
+ ],
122
+ "9": [
123
+ " █████ ",
124
+ "██ ██",
125
+ " ██████",
126
+ " ██",
127
+ " █████ ",
128
+ ],
129
+ "a": [
130
+ " █████ ",
131
+ "██ ██",
132
+ "███████",
133
+ "██ ██",
134
+ "██ ██",
135
+ ],
136
+ "b": [
137
+ "██████ ",
138
+ "██ ██",
139
+ "██████ ",
140
+ "██ ██",
141
+ "██████ ",
142
+ ],
143
+ "c": [
144
+ " ██████",
145
+ "██ ",
146
+ "██ ",
147
+ "██ ",
148
+ " ██████",
149
+ ],
150
+ "d": [
151
+ "██████ ",
152
+ "██ ██",
153
+ "██ ██",
154
+ "██ ██",
155
+ "██████ ",
156
+ ],
157
+ "e": [
158
+ "███████",
159
+ "██ ",
160
+ "█████ ",
161
+ "██ ",
162
+ "███████",
163
+ ],
164
+ "f": [
165
+ "███████",
166
+ "██ ",
167
+ "█████ ",
168
+ "██ ",
169
+ "██ ",
170
+ ],
171
+ "g": [
172
+ " ██████ ",
173
+ "██ ",
174
+ "██ ███",
175
+ "██ ██",
176
+ " ██████ ",
177
+ ],
178
+ "h": [
179
+ "██ ██",
180
+ "██ ██",
181
+ "███████",
182
+ "██ ██",
183
+ "██ ██",
184
+ ],
185
+ "i": [
186
+ "██",
187
+ "██",
188
+ "██",
189
+ "██",
190
+ "██",
191
+ ],
192
+ "j": [
193
+ " ██",
194
+ " ██",
195
+ " ██",
196
+ "██ ██",
197
+ " █████ ",
198
+ ],
199
+ "k": [
200
+ "██ ██",
201
+ "██ ██ ",
202
+ "█████ ",
203
+ "██ ██ ",
204
+ "██ ██",
205
+ ],
206
+ "l": [
207
+ "██ ",
208
+ "██ ",
209
+ "██ ",
210
+ "██ ",
211
+ "███████",
212
+ ],
213
+ "m": [
214
+ "███ ███",
215
+ "████ ████",
216
+ "██ ████ ██",
217
+ "██ ██ ██",
218
+ "██ ██",
219
+ ],
220
+ "n": [
221
+ "███ ██",
222
+ "████ ██",
223
+ "██ ██ ██",
224
+ "██ ██ ██",
225
+ "██ ████",
226
+ ],
227
+ "o": [
228
+ " ██████ ",
229
+ "██ ██",
230
+ "██ ██",
231
+ "██ ██",
232
+ " ██████ ",
233
+ ],
234
+ "p": [
235
+ "██████ ",
236
+ "██ ██",
237
+ "██████ ",
238
+ "██ ",
239
+ "██ ",
240
+ ],
241
+ "q": [
242
+ " ██████ ",
243
+ "██ ██",
244
+ "██ ██",
245
+ "██ ▄▄ ██",
246
+ " ████▀▄▄",
247
+ ],
248
+ "r": [
249
+ "██████ ",
250
+ "██ ██",
251
+ "██████ ",
252
+ "██ ██",
253
+ "██ ██",
254
+ ],
255
+ "s": [
256
+ "███████",
257
+ "██ ",
258
+ "███████",
259
+ " ██",
260
+ "███████",
261
+ ],
262
+ "t": [
263
+ "████████",
264
+ " ██ ",
265
+ " ██ ",
266
+ " ██ ",
267
+ " ██ ",
268
+ ],
269
+ "u": [
270
+ "██ ██",
271
+ "██ ██",
272
+ "██ ██",
273
+ "██ ██",
274
+ " ██████ ",
275
+ ],
276
+ "v": [
277
+ "██ ██",
278
+ "██ ██",
279
+ "██ ██",
280
+ " ██ ██ ",
281
+ " ████ ",
282
+ ],
283
+ "w": [
284
+ "██ ██",
285
+ "██ ██",
286
+ "██ █ ██",
287
+ "██ ███ ██",
288
+ " ███ ███ ",
289
+ ],
290
+ "x": [
291
+ "██ ██",
292
+ " ██ ██ ",
293
+ " ███ ",
294
+ " ██ ██ ",
295
+ "██ ██",
296
+ ],
297
+ "y": [
298
+ "██ ██",
299
+ " ██ ██ ",
300
+ " ████ ",
301
+ " ██ ",
302
+ " ██ ",
303
+ ],
304
+ "z": [
305
+ "███████",
306
+ " ███ ",
307
+ " ███ ",
308
+ " ███ ",
309
+ "███████",
310
+ ],
311
+ "_": [
312
+ " ",
313
+ " ",
314
+ " ",
315
+ " ",
316
+ "███████",
317
+ ],
318
+ "-": [
319
+ " ",
320
+ " ",
321
+ "█████",
322
+ " ",
323
+ " ",
324
+ ],
325
+ "+": [
326
+ " ",
327
+ " █ ",
328
+ "█████",
329
+ " █ ",
330
+ " ",
331
+ ],
332
+ "!": [
333
+ "██",
334
+ "██",
335
+ "██",
336
+ " ",
337
+ "██",
338
+ ],
339
+ "?": [
340
+ "██████ ",
341
+ " ██",
342
+ " ▄███ ",
343
+ " ▀▀ ",
344
+ " ██ ",
345
+ ],
346
+ ".": [
347
+ " ",
348
+ " ",
349
+ " ",
350
+ " ",
351
+ "██",
352
+ ],
353
+ "'": [
354
+ "██",
355
+ "█ ",
356
+ " ",
357
+ " ",
358
+ " ",
359
+ ],
360
+ ":": [
361
+ " ",
362
+ "██",
363
+ " ",
364
+ "██",
365
+ " ",
366
+ ],
367
+ "=": [
368
+ " ",
369
+ "████",
370
+ " ",
371
+ "████",
372
+ " ",
373
+ ],
374
+ " ": [
375
+ " ",
376
+ " ",
377
+ " ",
378
+ " ",
379
+ " ",
380
+ ],
381
+ }
@@ -45,34 +45,9 @@ def showlist(data, style="bullet", title=False, return_str=False, precision=4):
45
45
  # Prepare and clean data
46
46
  data = tools.prepare_data(data, precision=precision)
47
47
 
48
- if style in ["bullet", "bullets", "dot", "dots", ".", "*"]:
49
- marker_first = "•"
50
- marker_middle = "•"
51
- marker_last = "•"
52
-
53
- elif style in ["line", "-", "hang"]:
54
- marker_first = "╭"
55
- marker_middle = "├"
56
- marker_last = "╰"
57
-
58
- # Here we need a fancy title to go with the line
59
- if title:
60
- marker_first = marker_middle
61
- title = linetitle(title, style="hang", return_str=True)
62
-
63
- elif style in ["box", "line_box", "box_hang"]:
64
- marker_first = "╭"
65
- marker_middle = "├"
66
- marker_last = "╰"
67
-
68
- # Here we need a fancy title to go with the box
69
- if title:
70
- marker_first = marker_middle
71
- title = boxtitle(title, style="hang", return_str=True)
72
- else:
73
- marker_first = style
74
- marker_middle = style
75
- marker_last = style
48
+ # Get markers and title
49
+ markers = tools.get_markers_and_title(style, title)
50
+ marker_first, marker_middle, marker_last, title = markers
76
51
 
77
52
  # Add title, if we have one
78
53
  if title:
@@ -81,19 +56,9 @@ def showlist(data, style="bullet", title=False, return_str=False, precision=4):
81
56
  # Add list items
82
57
  for idx in range(len(data)):
83
58
 
84
- if idx == 0:
85
- marker = marker_first
86
-
87
- elif idx == len(data) - 1:
88
- marker = marker_last
89
-
90
- else:
91
- marker = marker_middle
92
-
93
- # In case ordinal list, override marker
94
- if style in ["ordinal", "#", "numbered", "num"]:
95
- maxdigits = len(str(len(data))) + 1
96
- marker = f"{int(idx+1)}.".zfill(maxdigits)
59
+ marker = tools.decide_marker(
60
+ idx, marker_first, marker_middle, marker_last, style, data
61
+ )
97
62
 
98
63
  # Generate string for this item
99
64
  outputstring += f"{marker} {data[idx]}\n"
@@ -0,0 +1,31 @@
1
+ def br(nlines=1, return_str=False):
2
+ """Short summary.
3
+
4
+ Parameters
5
+ ----------
6
+ nlines : int
7
+ Number of lines to break.
8
+ return_str : bool
9
+ If True, returns a string instead of printing.
10
+
11
+ """
12
+
13
+ outputstring = "\n" * (nlines - 1)
14
+
15
+ if return_str:
16
+ return outputstring
17
+ else:
18
+ print(outputstring)
19
+
20
+
21
+ def b(input, return_str=False):
22
+ """Bold the string"""
23
+ ansi_bold = "\033[1m"
24
+ ansi_end = "\033[0m"
25
+
26
+ outputstring = f"{ansi_bold}{str(input)}{ansi_end}"
27
+
28
+ if return_str:
29
+ return outputstring
30
+ else:
31
+ print(outputstring)
@@ -73,7 +73,7 @@ def bar(
73
73
  """
74
74
 
75
75
  # Check if value < maxvalue
76
- if value > maxvalue:
76
+ if value and value > maxvalue:
77
77
  raise ValueError(f"'value' cannot be bigger than 'maxvalue' {emoji.crazy}")
78
78
 
79
79
  if style in ["block"]:
@@ -120,25 +120,28 @@ def bar(
120
120
  empty = " " * len(str(style))
121
121
  end = "]"
122
122
 
123
- ratio = value / maxvalue
124
- nfill = int(ratio * length)
125
-
126
123
  # Start outputstring
127
124
  outputstring = ""
128
125
  if title:
129
126
  title = str(title)
130
127
 
131
128
  outputstring += f"{title:.<{title_pad}}: "
132
- outputstring += start
133
- outputstring += f"{fill*nfill}{empty*(length-nfill)}"
134
- outputstring += end
135
129
 
136
- if show_values:
137
- values_pad += values_precision + 1
138
- outputstring += f" {value:>{values_pad}.{values_precision}f}/{maxvalue:.{values_precision}f}"
130
+ # Check if it's not a NaN or None
131
+ if value == value and value:
132
+
133
+ ratio = value / maxvalue
134
+ nfill = int(ratio * length)
135
+ outputstring += start
136
+ outputstring += f"{fill*nfill}{empty*(length-nfill)}"
137
+ outputstring += end
138
+
139
+ if show_values:
140
+ values_pad += values_precision + 1
141
+ outputstring += f" {value:>{values_pad}.{values_precision}f}/{maxvalue:.{values_precision}f}"
139
142
 
140
- if show_percentage:
141
- outputstring += f" ({value/maxvalue:>7.2%})"
143
+ if show_percentage:
144
+ outputstring += f" ({value/maxvalue:>7.2%})"
142
145
 
143
146
  if return_str:
144
147
  return outputstring
@@ -221,12 +224,16 @@ def barlist(
221
224
  titles = [""] * len(values)
222
225
 
223
226
  # Negative values are not suported
224
- if min(values) < 0:
225
- raise ValueError("Negative values are not supported")
227
+ for value in values:
228
+ try:
229
+ if value < 0:
230
+ raise ValueError("Negative values are not supported")
231
+ except:
232
+ pass
226
233
 
227
234
  # Check if titles match values
228
235
  if len(values) != len(titles):
229
- errormsg = f"'values' and 'titles' mus have the same length {emoji.sad}"
236
+ errormsg = f"'values' and 'titles' must have the same length {emoji.sad}"
230
237
  errormsg += f"\ntotal values: {len(values)}"
231
238
  errormsg += f"\ntotal titles: {len(titles)}"
232
239
  raise ValueError(errormsg)
@@ -237,8 +244,11 @@ def barlist(
237
244
  # In case maxvalue is no given,
238
245
  # uses the max from all the values
239
246
  if not maxvalue:
240
- maxvalue = max(values)
247
+ # Get the max without breaking in case we find a "None"
248
+ maxvalue = max([v for v in values if v])
241
249
 
250
+ # Convert all titles to strings
251
+ titles = [str(t) for t in titles]
242
252
  # Get the longest title to use the proper padding
243
253
  longest_title = len(max(titles, key=len))
244
254
 
@@ -1,5 +1,6 @@
1
1
  from outputformat import fonts
2
2
  from outputformat import emoji
3
+ import outputformat as ouf
3
4
 
4
5
  # lh ➔ line horizontal
5
6
  # lv ➔ line vertical
@@ -9,7 +10,7 @@ from outputformat import emoji
9
10
  # br ➔ bottom right
10
11
 
11
12
 
12
- def linetitle(txt, style="-", return_str=False):
13
+ def linetitle(txt, style="-", color=False, cmap="cool", bold=False, return_str=False):
13
14
  """Simple line under text.
14
15
 
15
16
  Parameters
@@ -44,7 +45,7 @@ def linetitle(txt, style="-", return_str=False):
44
45
  outputstring = ""
45
46
 
46
47
  txt = str(txt)
47
- txt_size = len(txt)
48
+ txt_size = ouf.tools.real_string_length(txt)
48
49
  style = str(style)
49
50
 
50
51
  # bl = bottom left
@@ -54,7 +55,7 @@ def linetitle(txt, style="-", return_str=False):
54
55
  lh = "─"
55
56
  space = ""
56
57
 
57
- if style in ["=", "double"]:
58
+ elif style in ["=", "double"]:
58
59
  bl = "═"
59
60
  lh = "═"
60
61
  space = ""
@@ -72,13 +73,19 @@ def linetitle(txt, style="-", return_str=False):
72
73
  outputstring += f"{space}{txt}\n"
73
74
  outputstring += f"{bl}{lh*(txt_size-1)}{lh*len(space)}"
74
75
 
76
+ if color:
77
+ outputstring = ouf.c(outputstring, color, cmap=cmap, bold=bold, return_str=True)
78
+
79
+ if bold:
80
+ outputstring = ouf.b(outputstring, return_str=True)
81
+
75
82
  if return_str:
76
83
  return outputstring
77
84
  else:
78
85
  print(outputstring)
79
86
 
80
87
 
81
- def boxtitle(txt, style="-", return_str=False):
88
+ def boxtitle(txt, style="-", return_str=False, color=False, cmap="cool", bold=False):
82
89
  """Creates a text with a box decoration.
83
90
 
84
91
  Parameters
@@ -107,7 +114,7 @@ def boxtitle(txt, style="-", return_str=False):
107
114
  """
108
115
 
109
116
  txt = str(txt)
110
- txt_size = len(txt)
117
+ txt_size = ouf.tools.real_string_length(txt)
111
118
  style = str(style)
112
119
 
113
120
  if style in ["line", "-"]:
@@ -153,13 +160,19 @@ def boxtitle(txt, style="-", return_str=False):
153
160
  + (bl + (lh * (txt_size + 2) + br))
154
161
  )
155
162
 
163
+ if color:
164
+ outputstring = ouf.c(outputstring, color, cmap=cmap, bold=bold, return_str=True)
165
+
166
+ if bold:
167
+ outputstring = ouf.b(outputstring, return_str=True)
168
+
156
169
  if return_str:
157
170
  return outputstring
158
171
  else:
159
172
  print(outputstring)
160
173
 
161
174
 
162
- def bigtitle(txt, style="small", return_str=False):
175
+ def bigtitle(txt, style="small", return_str=False, color=False, cmap="cool"):
163
176
  """uses ASCII art to generate a big title.
164
177
 
165
178
  Parameters
@@ -214,6 +227,8 @@ def bigtitle(txt, style="small", return_str=False):
214
227
  outputstring += " "
215
228
  outputstring += "\n"
216
229
 
230
+ if color:
231
+ outputstring = ouf.c(outputstring, color, cmap=cmap, return_str=True)
217
232
  if return_str:
218
233
  return outputstring
219
234
  else:
@@ -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/
@@ -1,9 +1,14 @@
1
+ LICENSE
1
2
  README.md
3
+ pyproject.toml
2
4
  setup.py
3
5
  outputformat/__init__.py
6
+ outputformat/color.py
7
+ outputformat/dict.py
4
8
  outputformat/emoji.py
5
9
  outputformat/fonts.py
6
10
  outputformat/list.py
11
+ outputformat/misc.py
7
12
  outputformat/plot.py
8
13
  outputformat/title.py
9
14
  outputformat/tools.py
@@ -0,0 +1,3 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61", "wheel"]
3
+ build-backend = "setuptools.build_meta"
@@ -0,0 +1,16 @@
1
+ from setuptools import setup
2
+
3
+ setup(
4
+ name="outputformat",
5
+ packages=["outputformat"],
6
+ version="0.1.4",
7
+ description="Decorate and beautify output",
8
+ long_description="For usage, check https://delestro.com/2022/01/16/outputformat/",
9
+ author="Felipe Delestro",
10
+ author_email="delestro@gmail.com",
11
+ url="https://github.com/delestro/outputformat",
12
+ download_url="https://github.com/delestro/outputformat/archive/refs/tags/v0.1.4.tar.gz",
13
+ license="MIT",
14
+ keywords=["output", "print", "decorate", "format", "string", "beautify"],
15
+ python_requires=">=3.6",
16
+ )
@@ -1,23 +0,0 @@
1
- Metadata-Version: 1.2
2
- Name: outputformat
3
- Version: 0.1.2
4
- Summary: Decorate and beautify output
5
- Home-page: https://github.com/delestro/outputformat
6
- Author: Felipe Delestro
7
- Author-email: delestro@gmail.com
8
- License: MIT
9
- Download-URL: https://github.com/delestro/outputformat/archive/refs/tags/v0.1.2.tar.gz
10
- Description: It is recommended to use ouf as shortcut for outputformat import outputformat as ouf
11
- Main functions are:
12
- * ouf.boxtitle
13
- * ouf.linetitle
14
- * ouf.bigtitle
15
- * ouf.showlist
16
- * ouf.bar
17
- * ouf.barlist
18
- By default, functions print the result
19
- .You have the alternative to return a string instead, by passing the argument return_str=True
20
- (nothing will be printed in this case).
21
- Keywords: output,print,decorate,format,string,beautify
22
- Platform: UNKNOWN
23
- Requires-Python: >=3.6
@@ -1,78 +0,0 @@
1
- # List of characters suported
2
- # Every new font must include these
3
- suported_chars = "0123456789abcdefghijklmnopqrstuvwxyz_-!?.' "
4
-
5
- # List of styles (fonts) that can be used
6
- # Every new font name must be included here
7
- font_styles = ["small"]
8
-
9
- # small
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
- small = {
35
- "0": ["█▀█", "█▄█"],
36
- "1": ["▄█", " █"],
37
- "2": ["▀█", "█▄"],
38
- "3": ["▀▀█", "▄██"],
39
- "4": ["█ █", "▀▀█"],
40
- "5": ["█▀", "▄█"],
41
- "6": ["█▄▄", "█▄█"],
42
- "7": ["▀▀█", " █"],
43
- "8": ["█▀█", "███"],
44
- "9": ["█▀█", "▀▀█"],
45
- "a": ["▄▀█", "█▀█"],
46
- "b": ["█▄▄", "█▄█"],
47
- "c": ["█▀▀", "█▄▄"],
48
- "d": ["█▀▄", "█▄▀"],
49
- "e": ["█▀▀", "██▄"],
50
- "f": ["█▀▀", "█▀ "],
51
- "g": ["█▀▀", "█▄█"],
52
- "h": ["█ █", "█▀█"],
53
- "i": ["█", "█"],
54
- "j": [" █", "█▄█"],
55
- "k": ["█▄▀", "█ █"],
56
- "l": ["█ ", "█▄▄"],
57
- "m": ["█▀▄▀█", "█ ▀ █"],
58
- "n": ["█▄ █", "█ ▀█"],
59
- "o": ["█▀█", "█▄█"],
60
- "p": ["█▀█", "█▀▀"],
61
- "q": ["█▀█", "▀▀█"],
62
- "r": ["█▀█", "█▀▄"],
63
- "s": ["█▀", "▄█"],
64
- "t": ["▀█▀", " █ "],
65
- "u": ["█ █", "█▄█"],
66
- "v": ["█ █", "▀▄▀"],
67
- "w": ["█ █ █", "▀▄▀▄▀"],
68
- "x": ["▀▄▀", "█ █"],
69
- "y": ["█▄█", " █ "],
70
- "z": ["▀█", "█▄"],
71
- "_": [" ", "▄▄▄"],
72
- "-": [" ", "▀▀"],
73
- "!": ["█", "▄"],
74
- "?": ["█▀█", " ▄"],
75
- ".": [" ", "▄"],
76
- "'": ["▀", " "],
77
- " ": [" ", " "],
78
- }
@@ -1,28 +0,0 @@
1
- def prepare_data(input_data, precision):
2
- """
3
- Prepare the input data so that display is cleaner
4
- """
5
-
6
- data = input_data.copy()
7
-
8
- # Check if we have negative and positive values
9
- # If we don't have negative values, there's no point in adding "+"
10
- # To align the list
11
- negative_values = False
12
- for value in data:
13
- if isinstance(value, (int, float)) and value < 0:
14
- negative_values = True
15
-
16
- for idx, value in enumerate(data):
17
-
18
- if isinstance(value, float):
19
- data[idx] = f"{value:.{precision}f}"
20
- if negative_values and value > 0:
21
- data[idx] = "+" + data[idx]
22
-
23
- if isinstance(value, int):
24
- data[idx] = f"{value}"
25
- if negative_values and value > 0:
26
- data[idx] = "+" + data[idx]
27
-
28
- return data
@@ -1,23 +0,0 @@
1
- Metadata-Version: 1.2
2
- Name: outputformat
3
- Version: 0.1.2
4
- Summary: Decorate and beautify output
5
- Home-page: https://github.com/delestro/outputformat
6
- Author: Felipe Delestro
7
- Author-email: delestro@gmail.com
8
- License: MIT
9
- Download-URL: https://github.com/delestro/outputformat/archive/refs/tags/v0.1.2.tar.gz
10
- Description: It is recommended to use ouf as shortcut for outputformat import outputformat as ouf
11
- Main functions are:
12
- * ouf.boxtitle
13
- * ouf.linetitle
14
- * ouf.bigtitle
15
- * ouf.showlist
16
- * ouf.bar
17
- * ouf.barlist
18
- By default, functions print the result
19
- .You have the alternative to return a string instead, by passing the argument return_str=True
20
- (nothing will be printed in this case).
21
- Keywords: output,print,decorate,format,string,beautify
22
- Platform: UNKNOWN
23
- Requires-Python: >=3.6
@@ -1,28 +0,0 @@
1
- from setuptools import setup
2
-
3
- long_description = "It is recommended to use ouf as shortcut for outputformat import outputformat as ouf\n"
4
- long_description += "Main functions are:\n"
5
- long_description += "* ouf.boxtitle\n"
6
- long_description += "* ouf.linetitle\n"
7
- long_description += "* ouf.bigtitle\n"
8
- long_description += "* ouf.showlist\n"
9
- long_description += "* ouf.bar\n"
10
- long_description += "* ouf.barlist\n"
11
- long_description += "By default, functions print the result\n."
12
- long_description += "You have the alternative to return a string instead, by passing the argument return_str=True\n"
13
- long_description += "(nothing will be printed in this case)."
14
-
15
- setup(
16
- name="outputformat",
17
- packages=["outputformat"],
18
- version="0.1.2",
19
- description="Decorate and beautify output",
20
- long_description=long_description,
21
- author="Felipe Delestro",
22
- author_email="delestro@gmail.com",
23
- url="https://github.com/delestro/outputformat",
24
- download_url="https://github.com/delestro/outputformat/archive/refs/tags/v0.1.2.tar.gz",
25
- license="MIT",
26
- keywords=["output", "print", "decorate", "format", "string", "beautify"],
27
- python_requires=">=3.6",
28
- )
File without changes
File without changes