Try this:
Put this text into striped.py
:
#!/usr/bin/env python3
# -*- coding: iso-8859-1 -*-
import sys
c = ['\x1b[36m', '\x1b[37m'] # alternate between these two, for odd/even lines.
print(
''.join([
c[n%2] + line
for n,line in enumerate(sys.stdin)
]),
'\n\x1b[0m'
)
Make it executable by chmod 755 striped.py
And then use it as in e.g. maketable | ./striped.py
... which will print every other line in different color.
To use less
with this output, add its -R
option as in ... | less -R
.
I tried to find a pair of colors that are distinguishable yet contrasting to each other.
Example to try out
$ striped.py < /etc/os-release | less -R
More
If you wish, you can have numbered lines; that can be done (in fancy fashion) by adding
rvson = '\x1b[7m'
rvsoff = '\x1b[27m'
on lines after c = ...
and then change
c[n%2] + line
with + f'{rvson}{n:>4}:{rvsoff} '
inserted, as in...
c[n%2] + f'{rvson}{n:>4}:{rvsoff} ' + line
Alternative
Use dimmed text instead of colors; change the c = ...
line to
c = ['\x1b[2m', '\x1b[22m']
Noted for this: Does not work in Windows/cmd.exe, it is simply ignored.
Ref:
ANSI escape code - Wikipedia
For Windows cmd users: add
import os
os.system("")
e.g. on a line after import sys
- this magically initializes (some) ANSI escape code support.
"That is the Windows way" - always some kind of special magic required.
Edit 2024-06-12:
Alternative: Make a striped background, using the "same" code as above.
#!/usr/bin/env python3
# -*- coding: iso-8859-1 -*-
import os
os.system("") # make this work in Win/cmd.exe
import sys
c = [
'\x1b[48;2;80;80;80m',
'\x1b[48;2;100;100;100m'
]
print(
''.join([
c[n%2] + '\x1b[2K' + line
for n,line in enumerate(sys.stdin)
])+'\x1b[0m',end=''
)
print()
Note:
*[48;2;80;80;80m
has the 4
for background color (use 3
for foreground),
and ;80;80;80
for R, G, and B - as they're the same here we get a grey tone.
Any other RGB values will also work, i.e. create your own color-version in pink;
;255;0;255
is "full" pink (bright magenta), do reduce the 255's to get a less colorful tone. Chose two slightly different tones and place them as a and b in inside c = [ a, b ]
And last but not least + '\x1b[2K' +
makes the background appear on the entire line, regardless of window size, line length or height - dependent only on c[n%2]
to set the color.