Skip to content

Commit 229f84e

Browse files
adding text alignment option
1 parent 4ebb650 commit 229f84e

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

bashplotlib/scatterplot.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ def get_scale(series, is_y=False, steps=20):
2828
return scaled_series
2929

3030

31-
def _plot_scatter(xs, ys, size, pch, colour, title, cs):
31+
def _plot_scatter(xs, ys, size, pch, colour, title, cs, title_align):
3232
plotted = set()
3333

3434
if title:
35-
print(box_text(title, 2 * (len(get_scale(xs, False, size)) + 1)))
35+
print(box_text(title, 2 * (len(get_scale(xs, False, size)) + 1), title_align))
3636

3737
print("-" * (2 * (len(get_scale(xs, False, size)) + 2)))
3838
for y in get_scale(ys, True, size):
@@ -49,7 +49,7 @@ def _plot_scatter(xs, ys, size, pch, colour, title, cs):
4949
print(" |")
5050
print("-" * (2 * (len(get_scale(xs, False, size)) + 2)))
5151

52-
def plot_scatter(f, xs, ys, size, pch, colour, title):
52+
def plot_scatter(f, xs, ys, size, pch, colour, title, title_align):
5353
"""
5454
Form a complex number.
5555
@@ -81,7 +81,7 @@ def plot_scatter(f, xs, ys, size, pch, colour, title):
8181
with open(ys) as fh:
8282
ys = [float(str(row).strip()) for row in fh]
8383

84-
_plot_scatter(xs, ys, size, pch, colour, title, cs)
84+
_plot_scatter(xs, ys, size, pch, colour, title, cs, title_align)
8585

8686

8787

bashplotlib/utils/helpers.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,21 @@ def abbreviate(labels, rfill=' '):
7676
return abbrev
7777

7878

79-
def box_text(text, width, offset=0):
79+
def box_text(text, width, title_align, offset=0 ):
8080
"""
8181
Return text inside an ascii textbox
8282
"""
83+
84+
aligned_text = text.center(width)
85+
if title_align == "center":
86+
aligned_text = text.center(width)
87+
elif title_align == "left":
88+
aligned_text = text.ljust(width)
89+
elif title_align == "right":
90+
aligned_text = text.rjust(width)
91+
92+
8393
box = " " * offset + "+" + "-" * (width) + "+" + "\n"
84-
box += " " * offset + "|" + text.center(width) + "|" + "\n"
94+
box += " " * offset + "|" + aligned_text + "|" + "\n"
8595
box += " " * offset + "+" + "-" * (width) + "+"
8696
return box

0 commit comments

Comments
 (0)