Skip to content

Commit 00dc580

Browse files
committed
Refactored utils functions
1 parent ea272f0 commit 00dc580

File tree

2 files changed

+52
-30
lines changed

2 files changed

+52
-30
lines changed

bashplotlib/utils/commandhelp.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
Usage messages for bashplotlib system commands
6+
"""
17

28
hist = {
39
"usage": """hist is a command for making histograms. it accepts a series of values in one of the following formats:
410
1) txt file w/ 1 column of numbers
5-
2) standard in piped from another command line cat or curl
6-
11+
2) standard in piped from another command line cat or curl
12+
713
for some examples of how to use hist, you can type the command:
814
hist --demo
915
or visit https://github.com/glamp/bashplotlib/blob/master/examples/sample.sh
1016
"""
1117
}
1218

1319
scatter = {
14-
"usage": """scatterplot is a command for making xy plots. it accepts a series of x values and a series of y values in the
20+
"usage": """scatterplot is a command for making xy plots. it accepts a series of x values and a series of y values in the
1521
following formats:
1622
1) a txt file or standard in value w/ 2 comma seperated columns of x,y values
1723
2) 2 txt files. 1 w/ designated x values and another with designated y values.
18-
24+
1925
scatter -x <xcoords> -y <ycoords>
2026
cat <file_with_x_and_y_coords> | scatter
21-
2227
"""
2328
}
24-

bashplotlib/utils/helpers.py

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,52 @@
1+
#!/usr/bin/evn python
2+
# -*- coding: utf-8 -*-
13

4+
"""
5+
Various helpful function for bashplotlib
6+
"""
7+
8+
import sys
29

310
bcolours = {
4-
"white": '\033[97m',
5-
"aqua": '\033[96m',
6-
"pink": '\033[95m',
7-
"blue": '\033[94m',
11+
"white": '\033[97m',
12+
"aqua": '\033[96m',
13+
"pink": '\033[95m',
14+
"blue": '\033[94m',
815
"yellow": '\033[93m',
9-
"green": '\033[92m',
10-
"red": '\033[91m',
11-
"grey": '\033[90m',
12-
"ENDC": '\033[0m'
16+
"green": '\033[92m',
17+
"red": '\033[91m',
18+
"grey": '\033[90m',
19+
"black": '\033[30m',
20+
"ENDC": '\033[39m',
1321
}
1422

23+
1524
def get_colour(colour):
16-
return bcolours.get(colour, bcolours['white'])
25+
"""
26+
Get the escape code sequence for a colour
27+
"""
28+
return bcolours.get(colour, bcolours['ENDC'])
1729

18-
def printcolor(txt, sameline=False, color=get_colour("white")):
30+
31+
def printcolour(text, sameline=False, colour=get_colour("ENDC")):
32+
"""
33+
Print color text using escape codes
34+
"""
1935
if sameline:
20-
if color=='\033[97m':
21-
print txt,
22-
else:
23-
print color + txt + bcolours["ENDC"],
36+
sep = ''
2437
else:
25-
if color=='\033[97m':
26-
print txt
27-
else:
28-
print color + txt + bcolours["ENDC"]
38+
sep = '\n'
39+
sys.stdout.write(get_colour(colour) + text + bcolours["ENDC"] + sep)
40+
2941

3042
def drange(start, stop, step=1.0, include_stop=False):
31-
"generate between 2 numbers w/ optional step, optionally include upper bound"
32-
if step==0:
43+
"""
44+
Generate between 2 numbers w/ optional step, optionally include upper bound
45+
"""
46+
if step == 0:
3347
step = 0.01
3448
r = start
49+
3550
if include_stop:
3651
while r <= stop:
3752
yield r
@@ -43,9 +58,12 @@ def drange(start, stop, step=1.0, include_stop=False):
4358
r += step
4459
r = round(r, 10)
4560

61+
4662
def box_text(text, width, offset=0):
47-
box = " "*offset + "-"*(width+2) + "\n"
48-
box += " "*offset + "|"+ text.center(width) + "|" + "\n"
49-
box += " "*offset + "-"*(width+2)
63+
"""
64+
Return text inside an ascii textbox
65+
"""
66+
box = " " * offset + "-" * (width+2) + "\n"
67+
box += " " * offset + "|" + text.center(width) + "|" + "\n"
68+
box += " " * offset + "-" * (width+2)
5069
return box
51-

0 commit comments

Comments
 (0)