Made a menu to navigate picowesome while running pico.pi by gamaral. Using splore to navigate large directories of carts was tedious. Mainly because there was no way to back out of a directory besides scrolling to the top.
One challenge I ran into was there was no way to use ls() on a nested directory or run cd from the cart (that I could find). So my initial version had the entire gamelist in the menu cart. This worked well but the file was to big to upload here and felt not in the spirit of writing this app as a pico-8 cart.
So I wrote a python script to move and rename all the carts to one directory so they could all be accessible by ls(). This caused a notable slower initial menu cart load time on a raspberry pi 3. Pico.pi takes ~18s to boot and then ~12s after pico-8 does its boot chime. With those carts nested in directories that 12s is saved.
So I'm going to refactor back to nested directories but I wanted to post this version because it is a minimum viable product. Now I'm just going to focus on writing the new version of the quick menu.
Here's my python script. If there's interest I can explain how to use it in the comment below.
from os import walk, path from pathlib import Path import string from shutil import copy2 def main(): catagories = \ ['action adventure', 'beat em up', 'gun fps', 'misc', 'platform', \ 'race', 'shoot em up', 'sport', 'adventure visual', \ 'celeste hacks', 'hacks', 'music rythm', 'puzzle', 'rpg', \ 'simulation', 'strategy gestion', 'vs fighting', 'tools'] carts = list_files("./carts") new_carts = add_prefix_to_files(carts, catagories) copy_to_output('./carts/', './output/carts/', carts, new_carts) def copy_to_output(base_directory, output_directory, filenames, new_filenames): # mkdir $output_directory Path(output_directory).mkdir(parents=True, exist_ok=True) dir_index = 0 #print(filenames.keys()) for directory in filenames.keys(): file_index = 0 for filename in filenames[directory]: # Source directory src = base_directory + directory +'/' # Source filename src += filenames[directory][file_index] # Destination directory dst = output_directory # Destination filename dst += new_filenames[directory][file_index].lower() copy2(src, dst) file_index += 1 dir_index += 1 def add_prefix_to_files(filenames, directories): new_filenames = {} dir_index = 0 for directory in directories: file_index = 0 new_filenames[directory] = [] for filename in filenames[directory]: prefix = '.' prefix += alphanumeric_formatted(dir_index, 1) # directory code prefix += alphanumeric_formatted(file_index, 2) # file number code new_filenames[directory].append(prefix + filename) file_index += 1 dir_index += 1 return new_filenames letters = "-" + string.digits + string.ascii_uppercase + "_" + string.ascii_lowercase def alphanumeric_formatted(n, digits): """ Return string of int as 64 bit alphanumeric with a specific number of digits >>> alphanumeric_formatted(4098, 4) '-0-1' >>> alphanumeric_formatted(4098,2) '-1' """ s = int_to_64bit_alphanumeric(n) if len(s) > digits: return s[len(s)-digits:] return s.rjust(digits, letters[0]) def int_to_64bit_alphanumeric(n): """ Return string of int as a 64 bit alphanumeric >>> int_to_64bit_alphanumeric(65) '00' >>> int_to_64bit_alphanumeric(0) '-' >>> int_to_64bit_alphanumeric(1) '0' >>> int_to_64bit_alphanumeric(4097) '0-0' """ if n > 63: return int_to_64bit_alphanumeric(n>>6) + int_to_64bit_alphanumeric(n&63) return letters[n] def list_files(startpath): carts = {} for root, dirs, files in walk(startpath): # Only grab files if directory has no files if dirs == []: carts[path.basename(root)] = sorted(files) return carts if __name__=="__main__": main() |
[Please log in to post a comment]