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
This commit is contained in:
@@ -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"
|
||||
@@ -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,6 +134,11 @@ while True:
|
||||
else:
|
||||
## The actual editor
|
||||
if page == "editor":
|
||||
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")
|
||||
@@ -150,4 +168,27 @@ while True:
|
||||
|
||||
## The open file menu - for selecting a file
|
||||
elif page == "open":
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user