瀏覽代碼

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
cube 9 月之前
父節點
當前提交
48c3a79d3a
共有 1 個文件被更改,包括 67 次插入26 次删除
  1. 67
    26
      CARDPUTER CircuitPython/projects/text_editor.py

+ 67
- 26
CARDPUTER CircuitPython/projects/text_editor.py 查看文件

@@ -1,12 +1,18 @@
1 1
 # GUIDE
2 2
 # type freely! CTRL+D clears the text.
3
+# CTRL + S to Save
4
+# CTRL + O to Open
5
+# DESIGNED TO WORK WITH SD CARD !!! 
3 6
 # backspace, space, and return function as expected.
4
-# keys not recognised WILL print "UNKNOWN 0x??", but this is useful for extra key scans
5
-#
6
-# WORK IN PROGRESS
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 )
7
+
8
+# keys not recognised WILL print "UNKNOWN 0x??",
9
+# unless PRINT_UNKNOWN_KEYS is set to False
10
+# UP, DOWN, LEFT, RIGHT will print,
11
+# unless PRINT_COMMAND_KEYS is set to False
12
+
13
+# TODO:
14
+# UP and DOWN scrolling
15
+# look into folders in SD card
10 16
 ######
11 17
 
12 18
 import board
@@ -19,6 +25,17 @@ import sdcardio
19 25
 import storage
20 26
 import os
21 27
 
28
+def file_menu():
29
+    files_text = ""
30
+    index = 0
31
+    for file in os.listdir("/sd"):
32
+        filename = str(file)
33
+        if index == open_cursor:
34
+            filename = "> " + filename
35
+        files_text = files_text + filename+"\n"
36
+        index += 1
37
+    files_group.text = files_text
38
+
22 39
 ## SD Card set up for saving text to SD card
23 40
 spi = busio.SPI(board.SD_SCK, MOSI=board.SD_MOSI, MISO=board.SD_MISO)
24 41
 cs = board.SD_CS
@@ -36,6 +53,10 @@ except OSError:
36 53
 
37 54
 keyb = Keyboard()
38 55
 
56
+PRINT_UNKNOWN_KEYS = False
57
+PRINT_COMMAND_KEYS = False
58
+COMMAND_KEYS = ["UP", "DOWN", "LEFT", "RIGHT"]
59
+
39 60
 page = "editor"
40 61
 
41 62
 intro_text = "Press CTRL+D to clear text\n"+SD_status+"\n\n"
@@ -75,7 +96,7 @@ open_cursor = 0
75 96
 
76 97
 while True:
77 98
     key = keyb.scan()
78
-    
99
+
79 100
     if key == "BACKSP":
80 101
         if page == "editor":
81 102
             text = text[:-1]
@@ -100,15 +121,7 @@ while True:
100 121
     elif key == "CTRLO":
101 122
         board.DISPLAY.root_group = open_group
102 123
         page="open"
103
-        files_text = ""
104
-        index = 0
105
-        for file in os.listdir("/sd"):
106
-            filename = str(file)
107
-            if index == open_cursor:
108
-                filename = "> " + filename
109
-            files_text = files_text + filename+"\n"
110
-            index += 1
111
-        files_group.text = files_text
124
+        file_menu()
112 125
         
113 126
     elif key == "ESC":
114 127
         if page == "editor":
@@ -121,15 +134,20 @@ while True:
121 134
     else:
122 135
         ## The actual editor
123 136
         if page == "editor":
124
-            ## WRAPPING
125
-            ## calculate wrapping when entering a character 
126
-            lines = text.split("\n")
127
-            current_line = lines[len(lines)-1]
128
-            if len(current_line) > 35:
129
-                text=text+"\n" # <- start a new line here
130
-            ##############################################
131
-            text = text+key
132
-            text_area.text = text
137
+            if "UNKNOWN" in key and PRINT_UNKNOWN_KEYS == False:
138
+                pass
139
+            elif key in COMMAND_KEYS and PRINT_COMMAND_KEYS == False:
140
+                pass
141
+            else:
142
+                ## WRAPPING
143
+                ## calculate wrapping when entering a character 
144
+                lines = text.split("\n")
145
+                current_line = lines[len(lines)-1]
146
+                if len(current_line) > 35:
147
+                    text=text+"\n" # <- start a new line here
148
+                ##############################################
149
+                text = text+key
150
+                text_area.text = text
133 151
 
134 152
         ## The save file menu - for typing the filename
135 153
         elif page == "save":
@@ -150,4 +168,27 @@ while True:
150 168
 
151 169
         ## The open file menu - for selecting a file
152 170
         elif page == "open":
153
-            pass
171
+            if key == "UP":
172
+                if open_cursor == 0:
173
+                    pass
174
+                else:
175
+                    open_cursor -= 1
176
+                    file_menu()
177
+            elif key == "DOWN":
178
+                if open_cursor == len(os.listdir("/sd"))-1:
179
+                    pass
180
+                else:
181
+                    open_cursor += 1
182
+                    file_menu()
183
+            elif key == "\n":
184
+                text = ""
185
+                fn = os.listdir("/sd")[open_cursor]
186
+                with open("/sd/"+fn, "r") as f:
187
+                    lines = f.readlines()
188
+                for line in lines:
189
+                    text = text + line
190
+                text_area.text = text
191
+                filename_text = fn
192
+                filename_group.text=filename_text
193
+                page = "editor"
194
+                board.DISPLAY.root_group = intro_text_area