From 48c3a79d3a6515de3616338acab04af0f9b48184 Mon Sep 17 00:00:00 2001 From: cube Date: Fri, 7 Mar 2025 13:12:23 +0000 Subject: [PATCH] Update 'CARDPUTER CircuitPython/projects/text_editor.py' almost a fully functional text editor aside from no line scrolling - can save files and open them. no error handling has been set for folders, i assume they will break the program ;P --- .../projects/text_editor.py | 93 +++++++++++++------ 1 file changed, 67 insertions(+), 26 deletions(-) diff --git a/CARDPUTER CircuitPython/projects/text_editor.py b/CARDPUTER CircuitPython/projects/text_editor.py index 934436f..181ccf1 100644 --- a/CARDPUTER CircuitPython/projects/text_editor.py +++ b/CARDPUTER CircuitPython/projects/text_editor.py @@ -1,12 +1,18 @@ # GUIDE # type freely! CTRL+D clears the text. +# CTRL + S to Save +# CTRL + O to Open +# DESIGNED TO WORK WITH SD CARD !!! # backspace, space, and return function as expected. -# keys not recognised WILL print "UNKNOWN 0x??", but this is useful for extra key scans -# -# WORK IN PROGRESS -# CTRL + S to save text to the SD Card -# CTRL + O to open a text file -# UP and DOWN scrolling - (use cursor ) + +# keys not recognised WILL print "UNKNOWN 0x??", +# unless PRINT_UNKNOWN_KEYS is set to False +# UP, DOWN, LEFT, RIGHT will print, +# unless PRINT_COMMAND_KEYS is set to False + +# TODO: +# UP and DOWN scrolling +# look into folders in SD card ###### import board @@ -19,6 +25,17 @@ import sdcardio import storage import os +def file_menu(): + files_text = "" + index = 0 + for file in os.listdir("/sd"): + filename = str(file) + if index == open_cursor: + filename = "> " + filename + files_text = files_text + filename+"\n" + index += 1 + files_group.text = files_text + ## SD Card set up for saving text to SD card spi = busio.SPI(board.SD_SCK, MOSI=board.SD_MOSI, MISO=board.SD_MISO) cs = board.SD_CS @@ -36,6 +53,10 @@ except OSError: keyb = Keyboard() +PRINT_UNKNOWN_KEYS = False +PRINT_COMMAND_KEYS = False +COMMAND_KEYS = ["UP", "DOWN", "LEFT", "RIGHT"] + page = "editor" intro_text = "Press CTRL+D to clear text\n"+SD_status+"\n\n" @@ -75,7 +96,7 @@ open_cursor = 0 while True: key = keyb.scan() - + if key == "BACKSP": if page == "editor": text = text[:-1] @@ -100,15 +121,7 @@ while True: elif key == "CTRLO": board.DISPLAY.root_group = open_group page="open" - files_text = "" - index = 0 - for file in os.listdir("/sd"): - filename = str(file) - if index == open_cursor: - filename = "> " + filename - files_text = files_text + filename+"\n" - index += 1 - files_group.text = files_text + file_menu() elif key == "ESC": if page == "editor": @@ -121,15 +134,20 @@ while True: else: ## The actual editor if page == "editor": - ## WRAPPING - ## calculate wrapping when entering a character - lines = text.split("\n") - current_line = lines[len(lines)-1] - if len(current_line) > 35: - text=text+"\n" # <- start a new line here - ############################################## - text = text+key - text_area.text = text + if "UNKNOWN" in key and PRINT_UNKNOWN_KEYS == False: + pass + elif key in COMMAND_KEYS and PRINT_COMMAND_KEYS == False: + pass + else: + ## WRAPPING + ## calculate wrapping when entering a character + lines = text.split("\n") + current_line = lines[len(lines)-1] + if len(current_line) > 35: + text=text+"\n" # <- start a new line here + ############################################## + text = text+key + text_area.text = text ## The save file menu - for typing the filename elif page == "save": @@ -150,4 +168,27 @@ while True: ## The open file menu - for selecting a file elif page == "open": - pass + if key == "UP": + if open_cursor == 0: + pass + else: + open_cursor -= 1 + file_menu() + elif key == "DOWN": + if open_cursor == len(os.listdir("/sd"))-1: + pass + else: + open_cursor += 1 + file_menu() + elif key == "\n": + text = "" + fn = os.listdir("/sd")[open_cursor] + with open("/sd/"+fn, "r") as f: + lines = f.readlines() + for line in lines: + text = text + line + text_area.text = text + filename_text = fn + filename_group.text=filename_text + page = "editor" + board.DISPLAY.root_group = intro_text_area