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
|
# GUIDE
|
||||||
# type freely! CTRL+D clears the text.
|
# 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.
|
# backspace, space, and return function as expected.
|
||||||
# keys not recognised WILL print "UNKNOWN 0x??", but this is useful for extra key scans
|
|
||||||
#
|
# keys not recognised WILL print "UNKNOWN 0x??",
|
||||||
# WORK IN PROGRESS
|
# unless PRINT_UNKNOWN_KEYS is set to False
|
||||||
# CTRL + S to save text to the SD Card
|
# UP, DOWN, LEFT, RIGHT will print,
|
||||||
# CTRL + O to open a text file
|
# unless PRINT_COMMAND_KEYS is set to False
|
||||||
# UP and DOWN scrolling - (use cursor )
|
|
||||||
|
# TODO:
|
||||||
|
# UP and DOWN scrolling
|
||||||
|
# look into folders in SD card
|
||||||
######
|
######
|
||||||
|
|
||||||
import board
|
import board
|
||||||
@@ -19,6 +25,17 @@ import sdcardio
|
|||||||
import storage
|
import storage
|
||||||
import os
|
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
|
## 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
|
||||||
@@ -36,6 +53,10 @@ except OSError:
|
|||||||
|
|
||||||
keyb = Keyboard()
|
keyb = Keyboard()
|
||||||
|
|
||||||
|
PRINT_UNKNOWN_KEYS = False
|
||||||
|
PRINT_COMMAND_KEYS = False
|
||||||
|
COMMAND_KEYS = ["UP", "DOWN", "LEFT", "RIGHT"]
|
||||||
|
|
||||||
page = "editor"
|
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"
|
||||||
@@ -100,15 +121,7 @@ while True:
|
|||||||
elif key == "CTRLO":
|
elif key == "CTRLO":
|
||||||
board.DISPLAY.root_group = open_group
|
board.DISPLAY.root_group = open_group
|
||||||
page="open"
|
page="open"
|
||||||
files_text = ""
|
file_menu()
|
||||||
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
|
|
||||||
|
|
||||||
elif key == "ESC":
|
elif key == "ESC":
|
||||||
if page == "editor":
|
if page == "editor":
|
||||||
@@ -121,6 +134,11 @@ while True:
|
|||||||
else:
|
else:
|
||||||
## The actual editor
|
## The actual editor
|
||||||
if page == "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
|
## WRAPPING
|
||||||
## calculate wrapping when entering a character
|
## calculate wrapping when entering a character
|
||||||
lines = text.split("\n")
|
lines = text.split("\n")
|
||||||
@@ -150,4 +168,27 @@ while True:
|
|||||||
|
|
||||||
## The open file menu - for selecting a file
|
## The open file menu - for selecting a file
|
||||||
elif page == "open":
|
elif page == "open":
|
||||||
|
if key == "UP":
|
||||||
|
if open_cursor == 0:
|
||||||
pass
|
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