Skip to content

Commit b9f9f8e

Browse files
committed
add help text to launcher, add other hotkeys to editor bar, fix del key functionality
1 parent 5dcf983 commit b9f9f8e

File tree

3 files changed

+53
-21
lines changed

3 files changed

+53
-21
lines changed

builtin_apps/editor/adafruit_editor/dang.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def _blocking():
4242
special_keys = {
4343
"\x1b": ..., # all prefixes of special keys must be entered as Ellipsis
4444
"\x1b[": ...,
45+
"\x1b[3": ...,
4546
"\x1b[5": ...,
4647
"\x1b[6": ...,
4748
"\x1b[A": "KEY_UP",

builtin_apps/editor/adafruit_editor/editor.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
# usb_cdc.data.write(f"{message}\r\n".encode("utf-8"))
2424

2525
INPUT_DISPLAY_REFRESH_COOLDOWN = 0.3 # s
26+
SHOW_MEMFREE = False
2627

2728

2829
class MaybeDisableReload:
@@ -55,7 +56,8 @@ def os_exists(filename):
5556

5657

5758
def gc_mem_free_hint():
58-
return ""
59+
if not SHOW_MEMFREE:
60+
return ""
5961
if hasattr(gc, "mem_free"):
6062
gc.collect()
6163
return f" | free: {gc.mem_free()}"
@@ -224,16 +226,12 @@ def editor(stdscr, filename, visible_cursor): # pylint: disable=too-many-branch
224226
else:
225227
buffer = Buffer([""])
226228

229+
user_message = None
230+
227231
window = Window(curses.LINES - 1, curses.COLS - 1)
228232
cursor = Cursor()
229233
visible_cursor.text = buffer[0][0]
230234
last_refresh_time = -1
231-
# print("updating visible cursor")
232-
# visible_cursor.anchored_position = ((0 * 6) - 1, (0 * 12) + 20)
233-
# try:
234-
# visible_cursor.text = buffer.lines[0][0]
235-
# except IndexError:
236-
# visible_cursor.text = " "
237235

238236
stdscr.erase()
239237

@@ -258,10 +256,15 @@ def setline(row, line):
258256
for row in range(lastrow + 1, window.n_rows):
259257
setline(row, "~~ EOF ~~")
260258
row = curses.LINES - 1
261-
if util.readonly():
262-
line = f"{filename:12} (*ro) | ^C: quit{gc_mem_free_hint()}"
259+
260+
if user_message is None:
261+
if util.readonly():
262+
line = f"{filename:12} (mnt RO ^W) | ^R run | ^C: quit{gc_mem_free_hint()}"
263+
else:
264+
line = f"{filename:12} (mnt RW ^W) | ^R run | ^S save | ^X: save & exit | ^C: exit no save{gc_mem_free_hint()}"
263265
else:
264-
line = f"{filename:12} | ^X: write & exit | ^C: quit w/o save{gc_mem_free_hint()}"
266+
line = user_message
267+
user_message = None
265268
setline(row, line)
266269

267270
stdscr.move(*window.translate(cursor))
@@ -285,23 +288,24 @@ def setline(row, line):
285288
for row in buffer:
286289
print(row)
287290
print("---- end file contents ----")
288-
elif k == "\x13": # Ctrl-S
291+
elif k == "\x13": # Ctrl-S
289292
if not util.readonly():
290293
with open(filename, "w", encoding="utf-8") as f:
291294
for row in buffer:
292295
f.write(f"{row}\n")
296+
user_message = "Saved"
293297
else:
294298
print("Unable to Save due to readonly mode!")
295299
elif k == "\x11": # Ctrl-Q
296300
print("ctrl-Q")
297301
for row in buffer:
298302
print(row)
299-
elif k == "\x17": # Ctrl-W
303+
elif k == "\x17": # Ctrl-W
300304
boot_args_file = argv_filename("/boot.py")
301305
with open(boot_args_file, "w") as f:
302306
f.write(json.dumps([not util.readonly(), "/apps/editor/code.py", Path(filename).absolute()]))
303307
microcontroller.reset()
304-
elif k == "\x12": # Ctrl-R
308+
elif k == "\x12": # Ctrl-R
305309
print(f"Run: {filename}")
306310

307311
launcher_code_args_file = argv_filename("/code.py")
@@ -344,7 +348,13 @@ def setline(row, line):
344348
right(window, buffer, cursor)
345349
elif k in ("KEY_DELETE", "\x04"):
346350
print("delete")
347-
buffer.delete(cursor)
351+
if cursor.row < len(buffer.lines) - 1 or \
352+
cursor.col < len(buffer.lines[cursor.row]):
353+
buffer.delete(cursor)
354+
try:
355+
visible_cursor.text = buffer.lines[cursor.row][cursor.col]
356+
except IndexError:
357+
visible_cursor.text = " "
348358

349359
elif k in ("KEY_BACKSPACE", "\x7f", "\x08"):
350360
print(f"backspace {bytes(k, 'utf-8')}")

src/code.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515

1616
import supervisor
1717
import sys
18+
19+
import terminalio
1820
import usb
1921
import adafruit_pathlib as pathlib
2022
from adafruit_bitmap_font import bitmap_font
2123
from adafruit_display_text.text_box import TextBox
2224
from adafruit_display_text.bitmap_label import Label
25+
2326
from adafruit_displayio_layout.layouts.grid_layout import GridLayout
2427
from adafruit_anchored_tilegrid import AnchoredTileGrid
2528
import adafruit_imageload
@@ -63,14 +66,18 @@
6366

6467
font_file = "/fonts/terminal.lvfontbin"
6568
font = bitmap_font.load_font(font_file)
66-
main_group = displayio.Group(scale=scale)
69+
scaled_group = displayio.Group(scale=scale)
70+
71+
main_group = displayio.Group()
72+
main_group.append(scaled_group)
73+
6774
display.root_group = main_group
6875

6976
background_bmp = displayio.Bitmap(display.width, display.height, 1)
7077
bg_palette = displayio.Palette(1)
7178
bg_palette[0] = 0x222222
7279
bg_tg = displayio.TileGrid(bitmap=background_bmp, pixel_shader=bg_palette)
73-
main_group.append(bg_tg)
80+
scaled_group.append(bg_tg)
7481

7582
# load the mouse cursor bitmap
7683
mouse_bmp = displayio.OnDiskBitmap("launcher_assets/mouse_cursor.bmp")
@@ -199,12 +206,12 @@
199206
default_icon_palette.make_transparent(0)
200207
menu_grid = GridLayout(x=40, y=16, width=WIDTH, height=HEIGHT, grid_size=(config["width"], config["height"]),
201208
divider_lines=False)
202-
main_group.append(menu_grid)
209+
scaled_group.append(menu_grid)
203210

204211
menu_title_txt = Label(font, text="Fruit Jam OS")
205212
menu_title_txt.anchor_point = (0.5, 0.5)
206213
menu_title_txt.anchored_position = (display.width // (2 * scale), 2)
207-
main_group.append(menu_title_txt)
214+
scaled_group.append(menu_title_txt)
208215

209216
app_titles = []
210217
apps = []
@@ -248,6 +255,7 @@
248255
"icon": str(icon_file.absolute()) if icon_file is not None else None,
249256
"file": str(code_file.absolute())
250257
})
258+
251259
i += 1
252260

253261

@@ -356,15 +364,26 @@ def display_page(page_index):
356364
right_tg.anchored_position = ((display.width // scale) - 4, (display.height // 2 // scale) - 2)
357365
original_arrow_btn_color = left_palette[2]
358366

359-
main_group.append(left_tg)
360-
main_group.append(right_tg)
367+
scaled_group.append(left_tg)
368+
scaled_group.append(right_tg)
361369

362370
if len(apps) <= 6:
363371
right_tg.hidden = True
364372
left_tg.hidden = True
365373

366374
if mouse:
367-
main_group.append(mouse_tg)
375+
scaled_group.append(mouse_tg)
376+
377+
378+
help_txt = Label(terminalio.FONT, text="[Arrow]: Move\n[E]: Edit\n[Enter]: Run")
379+
# help_txt = TextBox(terminalio.FONT, width=88, height=30, align=TextBox.ALIGN_RIGHT, background_color=0x008800, text="[E]: Edit\n[Enter]: Run")
380+
help_txt.anchor_point = (0, 0)
381+
382+
help_txt.anchored_position = (2, 2)
383+
# help_txt.anchored_position = (display.width - 89, 1)
384+
385+
print(help_txt.bounding_box)
386+
main_group.append(help_txt)
368387

369388

370389
def atexit_callback():
@@ -534,6 +553,8 @@ def handle_key_press(key):
534553

535554
editor_launch_file = "apps/editor/code.py"
536555
write_argv(editor_launch_file, [apps[editor_index]["file"]])
556+
# with open(argv_filename(launch_file), "w") as f:
557+
# f.write(json.dumps([apps[editor_index]["file"]]))
537558

538559
supervisor.set_next_code_file(editor_launch_file, sticky_on_reload=False, reload_on_error=True,
539560
working_directory="/".join(editor_launch_file.split("/")[:-1]))

0 commit comments

Comments
 (0)