From 5adb93218aae621ee39010fea5b832a63e210551 Mon Sep 17 00:00:00 2001 From: cube Date: Wed, 5 Mar 2025 13:17:42 +0000 Subject: [PATCH] Update 'CARDPUTER CircuitPython/projects/text_editor.py' --- .../projects/text_editor.py | 57 ++++++++++++++++--- 1 file changed, 48 insertions(+), 9 deletions(-) diff --git a/CARDPUTER CircuitPython/projects/text_editor.py b/CARDPUTER CircuitPython/projects/text_editor.py index ea1c6ed..110bec4 100644 --- a/CARDPUTER CircuitPython/projects/text_editor.py +++ b/CARDPUTER CircuitPython/projects/text_editor.py @@ -5,6 +5,8 @@ # # 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 ) ###### import board @@ -17,6 +19,39 @@ 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 @@ -34,6 +69,8 @@ except OSError: keyb = Keyboard() +page = "editor" + intro_text = "Press CTRL+D to clear text\n"+SD_status+"\n\n" intro_text_area = label.Label(terminalio.FONT, text=intro_text, color="C3B") intro_text_area.x = 10 @@ -53,13 +90,15 @@ while True: text = text[:-1] elif key == "CTRLD": text = "" + elif key == "CTRLS": + save_file() + page = "save" + elif key == "CTRLO": + open_file() + page="open" + elif key == "ESC": + esc() + page = "editor" 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 \ No newline at end of file + text = editor(text) + \ No newline at end of file