diff --git a/tilde-train.py b/tilde-train.py index 40fbf33..3ed807d 100755 --- a/tilde-train.py +++ b/tilde-train.py @@ -1,56 +1,56 @@ #!/usr/bin/env python3 -## _ _ _ _ _ _ -## | |_(_) |__| |___ | |_ _ _ __ _(_)_ _ -## | _| | / _` / -_)| _| '_/ _` | | ' \ -## \__|_|_\__,_\___(_)__|_| \__,_|_|_||_| -## -## tilde.train is an instance of TerminalTrain. It was originally developed -## by cmccabe on tilde.town (https://tildegit.org/cmccabe/TerminalTrain) but is now -## maintained by vilmibm. -## -## If you want to contribute code improvements, create a pull request here: -## https://git.tilde.town/vilmibm/tilde-train -## -## ----------------- -## -## WHAT IS IT? -## like sl (the ls typo prank), but each car on the train is a x*y character ascii art -## produced by tilde.town. -## -## ( original sl source code of sl? https://github.com/mtoyoda/sl ) -## -## TODO: -## * loosen the restriction on allowable characters in train cars. right now, limited -## to characters in python's string.printable list. -## * turn main loop into a function, so cmd line arg reader can call it (with -p) and quit. -## * figure out why tilde.train doesn't work in some terminals (sthg sthg unicode...) -## * BUGFIX-1 - something about inclusion default cars adding extra "links" to the train. -## * the -p (print train) option should print all cars, not limited to the max_cars value. -## * related to BUGFIX-1, that seems to impact spacers (links) between cars. -## * allow users to create multiple frames so their cars can be animated (difficulty=med+) -## * allow user configurable speed and number of train cars -## * allow users to move the train up or down with arrow keys -## -- worked with asciimatics, but python curses blocks on getch() -## +# _ _ _ _ _ _ +# | |_(_) |__| |___ | |_ _ _ __ _(_)_ _ +# | _| | / _` / -_)| _| '_/ _` | | ' \ +# \__|_|_\__,_\___(_)__|_| \__,_|_|_||_| +# +# tilde.train is an instance of TerminalTrain. It was originally developed +# by cmccabe on tilde.town (https://tildegit.org/cmccabe/TerminalTrain) but is now +# maintained by vilmibm. +# +# If you want to contribute code improvements, create a pull request here: +# https://git.tilde.town/vilmibm/tilde-train +# +# ----------------- +# +# WHAT IS IT? +# like sl (the ls typo prank), but each car on the train is a x*y character ascii art +# produced by tilde.town. +# +# ( original sl source code of sl? https://github.com/mtoyoda/sl ) +# +# TODO: +# * loosen the restriction on allowable characters in train cars. right now, limited +# to characters in python's string.printable list. +# * turn main loop into a function, so cmd line arg reader can call it (with -p) and quit. +# * figure out why tilde.train doesn't work in some terminals (sthg sthg unicode...) +# * BUGFIX-1 - something about inclusion default cars adding extra "links" to the train. +# * the -p (print train) option should print all cars, not limited to the max_cars value. +# * related to BUGFIX-1, that seems to impact spacers (links) between cars. +# * allow users to create multiple frames so their cars can be animated (difficulty=med+) +# * allow user configurable speed and number of train cars +# * allow users to move the train up or down with arrow keys +# -- worked with asciimatics, but python curses blocks on getch() +# -from random import shuffle ## allowing us to randomize selection of cars. -import glob ## allowing us to search the file system for .choochoo files. -import sys ## so we can read command line arguments. +from random import shuffle # allowing us to randomize selection of cars. +import glob # allowing us to search the file system for .choochoo files. +import sys # so we can read command line arguments. import curses from signal import signal, SIGINT -import time ## allowing the loop steps of train animation to be slowed -import string ## for input validation +import time # allowing the loop steps of train animation to be slowed +import string # for input validation from inspect import cleandoc from pathlib import Path traincarFN = ".choochoo" -max_x = 35 ## max length of train car. -max_y = 10 ## max height of train car. -max_cars = 10 ## most cars to include in one train. -print_train = False ## print train to file (instead of the screen scroll) +max_x = 35 # max length of train car. +max_y = 10 # max height of train car. +max_cars = 10 # most cars to include in one train. +print_train = False # print train to file (instead of the screen scroll) -train = [""]*max_y ## empty train of correct height. +train = [""]*max_y # empty train of correct height. cars: list[list[str]] = [] engine = [ @@ -134,10 +134,10 @@ def test_user_car(): choochoo_list = choochoo_string.split("\n") car = "\n".join(choochoo_list) - car2 = car.replace("\t", "") ## do not allow tabs - car2 = car2.replace("\v", "") ## do not allow vertical tabs - car2 = car2.replace("\f", "") ## do not allow line feeds - car2 = car2.replace("\r", "") ## do not allow carriage returns + car2 = car.replace("\t", "") # do not allow tabs + car2 = car2.replace("\v", "") # do not allow vertical tabs + car2 = car2.replace("\f", "") # do not allow line feeds + car2 = car2.replace("\r", "") # do not allow carriage returns car2 = ''.join([i if string.printable.find(i) >= 0 else ' ' for i in car2]) print("") @@ -194,42 +194,42 @@ def link_car(car: list[str]): return car def validate_car(car: list[str]): -## this function (1) checks that a train car isn't too tall or too long -## (2) pads it vertically or on the right side if it is too short or if -## not all lines are the same length and (3) removes bad characters. +# this function (1) checks that a train car isn't too tall or too long +# (2) pads it vertically or on the right side if it is too short or if +# not all lines are the same length and (3) removes bad characters. car_str = "\n".join(car) car_str = ''.join([i if ord(i) < 128 else ' ' for i in car_str]) car_list = car_str.split("\n") - ## remove blank lines from top and bottom of car, - ## so we can estimate its true size. + # remove blank lines from top and bottom of car, + # so we can estimate its true size. while car_list[0].strip() == "": - car_list.pop(0) ## clear top + car_list.pop(0) # clear top while car_list[(len(car_list)-1)].strip() == "": - car_list.pop() ## clear bottom + car_list.pop() # clear bottom - ## len(choochoo_list) is the height of the train car, in number of rows. + # len(choochoo_list) is the height of the train car, in number of rows. if len(car_list) > max_y+1 or len(car_list) == 0: - return 0 ## train car too tall or non-existant; skip it. + return 0 # train car too tall or non-existant; skip it. - ## vertically pad short cars with 1 space (lines will be lengthened later). + # vertically pad short cars with 1 space (lines will be lengthened later). while len(car_list) < max_y: car_list = [" "] + car_list for idx,row in enumerate(car_list): car_list[idx] = row.rstrip() - longest_line = len(max(car_list, key=len)) ## longest line in .choochoo file. + longest_line = len(max(car_list, key=len)) # longest line in .choochoo file. for idx,row in enumerate(car_list): - if len(row) > max_x+1: ## check length of each row in .choochoo file. - return 0 ## train car too long; skip it. + if len(row) > max_x+1: # check length of each row in .choochoo file. + return 0 # train car too long; skip it. elif "\t" in row or "\v" in row or "\f" in row or "\r" in row: return 0 # skip if contains tabs, vert tabs, line feeds or carriage ret elif len(row) < longest_line: padding = " "*(longest_line - len(row)) - car_list[idx] += padding ## add padding spaces. + car_list[idx] += padding # add padding spaces. return car_list @@ -238,19 +238,19 @@ def print_all_cars(): for fname in glob.glob('/home/*/' + traincarFN): try: with open(fname, 'r') as myfile: - ## print fname ## debug, print file path and name + # print fname # debug, print file path and name choochoo_string = myfile.read() choochoo_list = choochoo_string.split("\n") if len(choochoo_list) > max_y+1: - continue ## the train car was too tall; skip it. + continue # the train car was too tall; skip it. - car = validate_car(choochoo_list) ## printing is only a DEBUG feature. + car = validate_car(choochoo_list) # printing is only a DEBUG feature. if car != 0: print("") print(fname + ":") - print("\n".join(car)) ## print the car to stdout + print("\n".join(car)) # print the car to stdout - ## HOW TO CLOSE THE FILE HANDLE? fname.close(), close(fname), ...? + # HOW TO CLOSE THE FILE HANDLE? fname.close(), close(fname), ...? except: pass; # print "Cannot open " + fname # for debuggering purposes @@ -286,12 +286,12 @@ def chuggachugga(stdscr: curses.window): stdscr.refresh() time.sleep(.03) -# ev = stdscr.getch() ## seems like curses waits for input, which won't work here. +# ev = stdscr.getch() # seems like curses waits for input, which won't work here. # if ev in (ord('Q'), ord('q')): # return -# elif ev == curses.KEY_UP: ## up +# elif ev == curses.KEY_UP: # up # y_pos += 1 -# elif ev == curses.KEY_DOWN: ## down +# elif ev == curses.KEY_DOWN: # down # y_pos -= 1 @@ -318,28 +318,28 @@ if len(sys.argv) == 2 and ("-a" in sys.argv[1] or "all" in sys.argv[1]): sys.exit() -## start a loop that collects all .choochoo files and processes on in each loop +# start a loop that collects all .choochoo files and processes on in each loop for fname in glob.glob('/home/*/' + traincarFN): car_len = 1 try: with open(fname, 'r') as myfile: - ## print fname ## debug, print file path and name + # print fname # debug, print file path and name choochoo_string = myfile.read() choochoo_list = choochoo_string.split("\n") if len(choochoo_list) > max_y+1: - continue ## the train car was too tall; skip it. + continue # the train car was too tall; skip it. - car = validate_car(choochoo_list) ## printing is only a DEBUG feature. + car = validate_car(choochoo_list) # printing is only a DEBUG feature. if car != 0: - cars.append(car) ## start a list of lists (list of cars) here. + cars.append(car) # start a list of lists (list of cars) here. - ## HOW TO CLOSE THE FILE HANDLE? fname.close(), close(fname), ...? + # HOW TO CLOSE THE FILE HANDLE? fname.close(), close(fname), ...? except: pass; - ##print "Cannot open " + fname # for debuggering purposes + #print "Cannot open " + fname # for debuggering purposes while len(cars) < max_cars: - cars.append([*default_car]) ## add copies of default cars if train too short + cars.append([*default_car]) # add copies of default cars if train too short shuffle(cars) cars = cars[0:max_cars]