#!/usr/bin/python # The MIT License (MIT) # # Copyright (c) 2026 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. from itertools import islice from sys import argv, exit, stderr, stdin, stdout from time import sleep info = ''' teletype [options...] [files...] Simulate the cadence of old-fashioned teletype machines, by slowing down the output of ASCII/UTF-8 symbols from the inputs given. All options can start with either a single or a double leading dash: -h, -help show this help message ''' def run_teletype(src): 'Emit data runes with a delay for each symbol.' prev = '\n' for line in src: # ignore trailing LF/CRLF from lines line = line.rstrip('\r\n').rstrip('\n') # emit runes with a delay, which is the whole point of this script; # the exception is ANSI-style runs of bytes, which are emitted all # together ansi = False for e in list(line): # an escape byte marks the start of an ANSI-style sequence if e == '\x1b': ansi = True if not ansi: # delay each visible item's appeareance by 15 ms sleep(0.015) # an `m` means the end of an ANSI-style sequence, even if there # wasn't one preceding it if e == 'm': ansi = False # emit the item, ensuring it shows right away stdout.write(e) stdout.flush() # time-bunch line-feeds together, by waiting only before the first # one in its run of line-feed bytes if prev != '': # wait for 750 ms before ending the line(s) sleep(0.75) prev = line # ensure a line-feed ends each line, even the last one stdout.write('\n') stdout.flush() try: # handle the help option, if given if len(argv) > 1 and argv[1] in ('-h', '--h', '-help', '--help'): print(info.strip()) exit(0) # ignore leading end-of-options `--`, if present skip = 2 if len(argv) > 1 and argv[1] == '--' else 1 # handle all named inputs, in the order given for name in islice(argv, skip, None): with open(name, encoding='utf-8') as inp: run_teletype(inp) # read from stdin, if no named inputs were given if len(argv) - skip < 1: run_teletype(stdin) except BrokenPipeError: # quit quietly, instead of showing a confusing error message stderr.close() except KeyboardInterrupt: exit(2) except Exception as e: # chances are, func `open` failed because some file given doesn't exist print(str(e), file=stderr) exit(1)