From 0372ce21fc33530c2a65a509890dd09a43afee9a Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Wed, 4 Mar 2020 21:44:21 +0000 Subject: [PATCH] rewrite from 2018 --- LICENSE.txt | 21 ++++++++++++++++ README.md | 6 +++-- typeprint.py | 70 ++++++++++++++++++++++++---------------------------- 3 files changed, 57 insertions(+), 40 deletions(-) create mode 100644 LICENSE.txt diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..dc91e2a --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Harvey Tindall + +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. diff --git a/README.md b/README.md index a8d0ae6..29f429a 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,11 @@ # typeprint.py -A terrible python script to output the contents of a file as if it were being typed by one of the l33t hack3rs you get in films. I made this because vypr's [typeprint](http://github.com/vypr/typeprint) didn't have an easy way to change the speed of output. +Python script to print text/files character-by-character, originally intended for a retro terminal emulator. # Usage ``` -typeprint.py --ifile --speed <1 - 5> +typeprint.py [-f|--file] [-s|--speed] ``` +* `--speed`: specifies interval between each printed character. +* `--file`: specifies `` is a file, not text. diff --git a/typeprint.py b/typeprint.py index df5c1d2..7fddba0 100755 --- a/typeprint.py +++ b/typeprint.py @@ -1,44 +1,38 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 +import sys, time, argparse, errno, os +from pathlib import Path -import sys, time, getopt +def print_file(file, speed): + if not Path(file).is_file(): + raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), file) + else: + with open(file, "r") as rfile: + done = False + while not done: + pos = rfile.read(1) + sys.stdout.write(pos) + sys.stdout.flush() + if not pos: + done = True + time.sleep(speed) +def print_input(input, speed): + for letter in input: + sys.stdout.write(letter) + sys.stdout.flush() + time.sleep(speed) -def main(argv): - inputfile = '' - speed = '5' - try: - opts, args = getopt.getopt(argv,"hi:s",["ifile=", "speed="]) - except getopt.GetoptError: - print 'typeprint.py --ifile --speed <1 - 5>' - sys.exit(2) - for opt, arg in opts: - if opt in ("-i", "--ifile"): - inputfile = arg - elif opt in ("-s", "--speed"): - speed = arg - wspeed = 0 - if speed == '1': - wspeed = 0.5 - elif speed == '2': - wspeed = 0.4 - elif speed == '3': - wspeed = 0.3 - elif speed == '4': - wspeed = 0.2 - elif speed == '5': - wspeed = 0.1 - with open(inputfile, "r") as file: - while 1 == 1: - readnum = file.read(1) - time.sleep(wspeed) - sys.stdout.write(readnum) - sys.stdout.flush() - if not readnum: - sys.exit() - - - -main(sys.argv[1:]) +parser = argparse.ArgumentParser() +parser.add_argument("input", help="string or name of file to be printed.") +parser.add_argument("-f", "--file", help="specifies that the input is a file. If not used, input is interpreted as string.", action='store_true') +parser.add_argument("-s", "--speed", help="delay between printing each character in seconds. defaults to 0.1 if not specified.", type=float) +args = parser.parse_args() +if not args.speed: + args.speed = 0.1 +if args.file: + print_file(args.input, args.speed) +else: + print_input(args.input, args.speed)