Skip to content

Instantly share code, notes, and snippets.

@deeplook
Created July 28, 2017 11:56
Show Gist options
  • Select an option

  • Save deeplook/945e6cd7ad1786c678a9d9ac9d14e94d to your computer and use it in GitHub Desktop.

Select an option

Save deeplook/945e6cd7ad1786c678a9d9ac9d14e94d to your computer and use it in GitHub Desktop.
Implement a "slow-motion" Unix 'cat' command.
#!/usr/bin/env python
"""
Implement a "slow-motion" Unix 'cat' command.
Intended to be used with tools like 'asciinema', see asciinema.org.
None of the options of Unix 'cat' are implemented here.
Examples:
slocat.py slocat.py --pause 0.1
echo -e "abc\nbbbb" | slocat.py - --pause 0.1
tree . | slocat.py - --pause 0.1
"""
from __future__ import print_function
import sys
import time
import argparse
DELAY = 0.1
__author__ = "Dinu Gherman"
__date__ = "2017-07-28"
__license__ = "GLP 3"
__version__ = "0.1"
def slow_cat(path, pause=DELAY):
"""
Output text file line by line, but with a short delay after each line.
"""
with sys.stdin if path == '-' else open(path) as f:
for line in f.readlines():
print(line.rstrip())
time.sleep(pause)
def _main():
"""
Command-line interface"
"""
desc = "Output lines of a file like with Unix 'cat', but in slow-motion."
parser = argparse.ArgumentParser(description=desc)
parser.add_argument("path", nargs='+', help="Path of some text file.")
parser.add_argument("--pause", metavar="PERIOD", default=DELAY,
type=float, help='Delay in seconds (default: %f).' % DELAY)
opts = parser.parse_args()
for path in opts.path:
slow_cat(path, opts.pause)
if __name__ == "__main__":
_main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment