Browse Source

Update 'CARDPUTER CircuitPython/projects/text_editor.py'

cube 9 months ago
parent
commit
5adb93218a
1 changed files with 48 additions and 9 deletions
  1. 48
    9
      CARDPUTER CircuitPython/projects/text_editor.py

+ 48
- 9
CARDPUTER CircuitPython/projects/text_editor.py View File

@@ -5,6 +5,8 @@
5 5
 #
6 6
 # WORK IN PROGRESS
7 7
 # CTRL + S to save text to the SD Card
8
+# CTRL + O to open a text file
9
+# UP and DOWN scrolling - (use cursor )
8 10
 ######
9 11
 
10 12
 import board
@@ -17,6 +19,39 @@ import sdcardio
17 19
 import storage
18 20
 import os
19 21
 
22
+def save_file():
23
+    save_text = "save file"
24
+    save_group = label.Label(terminalio.FONT, text=save_text)
25
+    save_group.x = 10
26
+    save_group.y = 10
27
+    board.DISPLAY.root_group = save_group
28
+
29
+def open_file():
30
+    open_text = "open file"
31
+    open_group = label.Label(terminalio.FONT, text=open_text)
32
+    open_group.x = 10
33
+    open_group.y = 10
34
+    board.DISPLAY.root_group = open_group
35
+
36
+def editor(text):
37
+    if page == "editor":
38
+        ## WRAPPING
39
+        ## calculate wrapping when entering a character 
40
+        lines = text.split("\n")
41
+        current_line = lines[len(lines)-1]
42
+        if len(current_line) > 35:
43
+            text=text+"\n" # <- start a new line here
44
+        ##############################################
45
+        text = text+key
46
+        text_area.text = text
47
+    return text
48
+
49
+def esc():
50
+    if page == "editor":
51
+        pass
52
+    elif page == "save" or page == "open":
53
+        board.DISPLAY.root_group = intro_text_area
54
+
20 55
 ## SD Card set up for saving text to SD card
21 56
 spi = busio.SPI(board.SD_SCK, MOSI=board.SD_MOSI, MISO=board.SD_MISO)
22 57
 cs = board.SD_CS
@@ -34,6 +69,8 @@ except OSError:
34 69
 
35 70
 keyb = Keyboard()
36 71
 
72
+page = "editor"
73
+
37 74
 intro_text = "Press CTRL+D to clear text\n"+SD_status+"\n\n"
38 75
 intro_text_area = label.Label(terminalio.FONT, text=intro_text, color="C3B")
39 76
 intro_text_area.x = 10
@@ -53,13 +90,15 @@ while True:
53 90
         text = text[:-1] 
54 91
     elif key == "CTRLD":
55 92
         text = ""
93
+    elif key == "CTRLS":
94
+        save_file()
95
+        page = "save"
96
+    elif key == "CTRLO":
97
+        open_file()
98
+        page="open"
99
+    elif key == "ESC":
100
+        esc()
101
+        page = "editor"
56 102
     else:
57
-        ## WRAPPING
58
-        ## calculate wrapping when entering a character 
59
-        lines = text.split("\n")
60
-        current_line = lines[len(lines)-1]
61
-        if len(current_line) > 35:
62
-            text=text+"\n" # <- start a new line here
63
-        ##############################################
64
-        text = text+key
65
-    text_area.text = text
103
+        text = editor(text)
104
+