diff --git a/CARDPUTER CircuitPython/projects/text_editor.py b/CARDPUTER CircuitPython/projects/text_editor.py index 110bec4..ebfd5d6 100644 --- a/CARDPUTER CircuitPython/projects/text_editor.py +++ b/CARDPUTER CircuitPython/projects/text_editor.py @@ -19,39 +19,6 @@ import sdcardio import storage import os -def save_file(): - save_text = "save file" - save_group = label.Label(terminalio.FONT, text=save_text) - save_group.x = 10 - save_group.y = 10 - board.DISPLAY.root_group = save_group - -def open_file(): - open_text = "open file" - open_group = label.Label(terminalio.FONT, text=open_text) - open_group.x = 10 - open_group.y = 10 - board.DISPLAY.root_group = open_group - -def editor(text): - 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 - return text - -def esc(): - if page == "editor": - pass - elif page == "save" or page == "open": - board.DISPLAY.root_group = intro_text_area - ## 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 @@ -79,26 +46,82 @@ intro_text_area.y = 10 text = "" text_area = label.Label(terminalio.FONT, text=text) text_area.x = 0 -text_area.y = 40 +text_area.y = 30 board.DISPLAY.root_group = intro_text_area intro_text_area.append(text_area) +filename_text = "" +save_text = "Save File - Enter Filename\nIf file exists, it will be overwritten" +save_group = label.Label(terminalio.FONT, text=save_text, color="C3B") +save_group.x = 10 +save_group.y = 10 +filename_group = label.Label(terminalio.FONT, text=filename_text) +filename_group.x = 0 +filename_group.y = 60 +save_group.append(filename_group) + +open_text = "open file" +open_group = label.Label(terminalio.FONT, text=open_text) +open_group.x = 10 +open_group.y = 10 + while True: key = keyb.scan() + if key == "BACKSP": - text = text[:-1] + if page == "editor": + text = text[:-1] + text_area.text = text + elif page == "save": + filename_text = filename_text[:-1] + filename_group.text = filename_text + elif key == "CTRLD": - text = "" + if page == "editor": + text = "" + text_area.text = text + elif page == "save": + filename_text = "" + filename_group.text = filename_text + elif key == "CTRLS": - save_file() + board.DISPLAY.root_group = save_group page = "save" + filename_group.text=filename_text + elif key == "CTRLO": - open_file() + board.DISPLAY.root_group = open_group page="open" + elif key == "ESC": - esc() + if page == "editor": + pass + elif page == "save" or page == "open": + board.DISPLAY.root_group = intro_text_area page = "editor" + + else: - text = editor(text) + ## 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 + + ## The save file menu - for typing the filename + elif page == "save": + if key == "\n": + pass # handle saving the file + elif not keyb.is_alphanumeric(key): + pass # dont allow special characters to be entered + else: + filename_text = filename_text+key + filename_group.text=filename_text \ No newline at end of file