rewrite from 2018

This commit is contained in:
Harvey Tindall 2020-03-04 21:44:21 +00:00
parent 4e298d749d
commit 0372ce21fc
3 changed files with 57 additions and 40 deletions

21
LICENSE.txt Normal file
View File

@ -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.

View File

@ -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 <input file> --speed <1 - 5>
typeprint.py [-f|--file] [-s|--speed] <speed> <input>
```
* `--speed`: specifies interval between each printed character.
* `--file`: specifies `<input>` is a file, not text.

View File

@ -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 <inputfile> --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)