Skip to content

Commit 7d0195b

Browse files
committed
bargraph.py: be more explicit about whitespace fill.
1 parent 42b04e9 commit 7d0195b

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

examples/bargraph.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import sys
23
import time
34

@@ -34,30 +35,34 @@
3435

3536
try:
3637
while True:
38+
tcols, _ = os.get_terminal_size()
3739
values = as7262.get_calibrated_values()
3840
values = [int(x/y*MAX_VALUE) for x,y in zip(list(values), list(baseline))]
3941
values = [int(min(value, MAX_VALUE) / MAX_VALUE * BAR_WIDTH) for value in values]
4042
red, orange, yellow, green, blue, violet = [(BAR_CHAR * value) + (' ' * (BAR_WIDTH - value)) for value in values]
4143

4244
sys.stdout.write('\x1b[0;1H')
43-
sys.stdout.write(u""" Spectrometer Bar Graph
44-
---------------------------------
45-
|Red: {}{}\x1b[0m|
46-
|Orange: {}{}\x1b[0m|
47-
|Yellow: {}{}\x1b[0m|
48-
|Green: {}{}\x1b[0m|
49-
|Blue: {}{}\x1b[0m|
50-
|Violet: {}{}\x1b[0m|
51-
---------------------------------
52-
45+
bargraph =u""" Spectrometer Bar Graph
46+
---------------------------------
47+
|Red: {}{}\x1b[0m|
48+
|Orange: {}{}\x1b[0m|
49+
|Yellow: {}{}\x1b[0m|
50+
|Green: {}{}\x1b[0m|
51+
|Blue: {}{}\x1b[0m|
52+
|Violet: {}{}\x1b[0m|
53+
---------------------------------
54+
5355
""".format(
5456
ANSI_COLOR_RED, red,
5557
ANSI_COLOR_YELLOW, orange,
5658
ANSI_COLOR_YELLOW, yellow,
5759
ANSI_COLOR_GREEN, green,
5860
ANSI_COLOR_BLUE, blue,
5961
ANSI_COLOR_MAGENTA, violet
60-
))
62+
)
63+
64+
bargraph = "\n".join(line.ljust(tcols, " ") for line in bargraph.split("\n"))
65+
sys.stdout.write(bargraph)
6166
sys.stdout.flush()
6267
time.sleep(0.5)
6368

0 commit comments

Comments
 (0)