Update 'CARDPUTER CircuitPython/projects/text_editor.py'

This commit is contained in:
2025-03-05 13:17:42 +00:00
parent f116dfc4c2
commit 5adb93218a

View File

@@ -5,6 +5,8 @@
# #
# WORK IN PROGRESS # WORK IN PROGRESS
# CTRL + S to save text to the SD Card # CTRL + S to save text to the SD Card
# CTRL + O to open a text file
# UP and DOWN scrolling - (use cursor )
###### ######
import board import board
@@ -17,6 +19,39 @@ import sdcardio
import storage import storage
import os 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 ## SD Card set up for saving text to SD card
spi = busio.SPI(board.SD_SCK, MOSI=board.SD_MOSI, MISO=board.SD_MISO) spi = busio.SPI(board.SD_SCK, MOSI=board.SD_MOSI, MISO=board.SD_MISO)
cs = board.SD_CS cs = board.SD_CS
@@ -34,6 +69,8 @@ except OSError:
keyb = Keyboard() keyb = Keyboard()
page = "editor"
intro_text = "Press CTRL+D to clear text\n"+SD_status+"\n\n" 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 = label.Label(terminalio.FONT, text=intro_text, color="C3B")
intro_text_area.x = 10 intro_text_area.x = 10
@@ -53,13 +90,15 @@ while True:
text = text[:-1] text = text[:-1]
elif key == "CTRLD": elif key == "CTRLD":
text = "" text = ""
elif key == "CTRLS":
save_file()
page = "save"
elif key == "CTRLO":
open_file()
page="open"
elif key == "ESC":
esc()
page = "editor"
else: else:
## WRAPPING text = editor(text)
## 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