Welcome, guest | Sign In | My Account | Store | Cart

This recipe shows how to create a simple text-based ruler for your command-line console. It can help you find the position of your own program's output on the line, or to find the positions and lengths of fields in fixed- or variable-length records in a text file, fields in CSV files, etc.

Python, 34 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
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
# ruler.py
"""
Program to display a ruler on the console.
Author: Vasudev Ram
Copyright 2016 Vasudev Ram - http://jugad2.blogspot.com

Description: Program to display a ruler on the command-line screen.
The ruler consists of repeated occurrences of the characters:
0123456789, concatenated, with interals of 10's marked above or below.

Purpose: By running this program, you can use its output as a ruler,
to find the position of your own program's output on the line, or to 
find the positions and lengths of fields in fixed- or variable-length 
records in a text file, fields in CSV files, etc.
"""

REPS = 8

def ruler(sep=' ', reps=REPS):
    for i in range(reps):
        print str(i) + ' ' * 4 + sep + ' ' * 3,
    print '0123456789' * reps

def main():

    # Without divider.
    ruler()

    # With various dividers.
    for sep in '|+!':
        ruler(sep)

if __name__ == '__main__':
    main()

See the post and discussion here for more details:

http://jugad2.blogspot.in/2016/04/a-quick-console-ruler-in-python.html