summaryrefslogtreecommitdiff
path: root/util/chargen
blob: fd9e73cbdab7d4dc24d2362661547520059ed42c (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
# Copyright 2019 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import sys


def chargen(modulo, max_chars):
    """Generate a stream of characters on the console.

    The stream is an ever incrementing pattern of characters from the
    following set: 0..9A..Za..z.

    Args:
      modulo:    an int, restart the pattern every modulo characters, if
                 modulo is non zero
      max_chars: an int, stop printing after this number of characters if non
                 zero, if zero - print indefinitely
    """

    base = "0"
    c = base
    counter = 0
    while True:
        sys.stdout.write(c)
        counter = counter + 1

        if (max_chars != 0) and (counter == max_chars):
            sys.stdout.write("\n")
            return

        if modulo and ((counter % modulo) == 0):
            c = base
            continue

        if c == "z":
            c = base
        elif c == "Z":
            c = "a"
        elif c == "9":
            c = "A"
        else:
            c = "%c" % (ord(c) + 1)


def main(args):
    """Process command line arguments and invoke chargen if args are valid"""

    modulo = 0
    max_chars = 0

    try:
        if len(args) > 0:
            modulo = int(args[0])
            if len(args) > 1:
                max_chars = int(args[1])
    except ValueError:
        sys.stderr.write("usage %s:" "['seq_length' ['max_chars']]\n")
        sys.exit(1)

    try:
        chargen(modulo, max_chars)
    except KeyboardInterrupt:
        print()


if __name__ == "__main__":
    main(sys.argv[1:])
    sys.exit(0)