Skip to content

Commit 03e511e

Browse files
authored
update files app (#156)
Fixed dissapearing sd directory when going back with '..' Fixed missing offset on hamburger menu
1 parent 94363f1 commit 03e511e

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

src/launcher/files.py

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
from lib.hydra.i18n import I18n
2020

2121

22-
_MH_DISPLAY_HEIGHT = const(240)
23-
_MH_DISPLAY_WIDTH = const(320)
22+
_MH_DISPLAY_HEIGHT = const(135)
23+
_MH_DISPLAY_WIDTH = const(240)
2424

2525

2626
_TRANS = const("""[
@@ -44,7 +44,7 @@
4444

4545
_ITEMS_PER_SCREEN = const(_MH_DISPLAY_HEIGHT // 32)
4646
_ITEMS_PER_SCREEN_MINUS = const(_ITEMS_PER_SCREEN - 1)
47-
_LEFT_PADDING = const(_MH_DISPLAY_WIDTH // 20)
47+
_LEFT_PADDING = const(_MH_DISPLAY_WIDTH // 24)
4848

4949
# calculate padding around items based on amount of unused space
5050
_CHAR_PADDING = const((_MH_DISPLAY_HEIGHT - (_ITEMS_PER_SCREEN * 32)) // _ITEMS_PER_SCREEN)
@@ -61,9 +61,9 @@
6161
# for horizontal text scroll animation:
6262
_SCROLL_TIME = const(5000) # ms per one text scroll
6363

64+
# Delimiter for multiple main.py paths
6465
_PATH_JOIN = const("|//|")
6566

66-
6767
# hamburger menu icon:
6868
_HAMBURGER_WIDTH = const(32)
6969
_HAMBURGER_X = const(_DISPLAY_WIDTH_HALF - (_HAMBURGER_WIDTH // 2))
@@ -173,7 +173,7 @@ def draw(self):
173173

174174
# special stylilng on menu button
175175
if mytext == "/.../":
176-
self.draw_hamburger_menu(tft, idx * _LINE_HEIGHT, self.config.palette[clr_idx])
176+
self.draw_hamburger_menu(tft, idx * _LINE_HEIGHT + _TOP_PADDING, self.config.palette[clr_idx])
177177
break # hamburger menu is always last
178178

179179
# special styling for directories
@@ -258,6 +258,41 @@ def ping_pong_ease(value: int, modulo: int) -> float:
258258
return (fac)
259259

260260

261+
262+
def path_join(*args) -> str:
263+
"""Join multiple paths together."""
264+
path = "/".join(args)
265+
# Remove any repeated slashes
266+
while "//" in path:
267+
path = path.replace("//", "/")
268+
if path.endswith("/") and len(path) > 1:
269+
path = path[:-1]
270+
return path
271+
272+
273+
def path_split(path_str) -> tuple[str, str]:
274+
"""Split last element off of path. (similar to os.path.split)."""
275+
# Early return on root dir
276+
if path_str == "/":
277+
return "/", ""
278+
279+
*head, tail = path_str.split("/")
280+
head = path_join(*head)
281+
return head, tail
282+
283+
284+
def prev_dir():
285+
"""Move back to the previous directory (similar to "..")."""
286+
# this funciton is needed because ".." doesn't work as expected with SDCards
287+
# If you chdir("..") on "/sd" you will not move.
288+
# And, if you chdir("..") back to "/" from anywhere else,
289+
# "/sd" won't show up on os.listdir()
290+
head = path_split(os.getcwd())[0]
291+
if not head:
292+
head = "/"
293+
os.chdir(head)
294+
295+
261296
def parse_files() -> tuple[list, dict]:
262297
"""Get a list of directories/files, and a dictionary of which is which.
263298
@@ -325,11 +360,11 @@ def ext_options(overlay):
325360

326361
source_path, file_name = clipboard
327362

328-
source = f"{source_path}/{file_name}".replace('//', '/')
329-
dest = f"{cwd}/{file_name}".replace('//', '/')
363+
source = path_join(source_path, file_name)
364+
dest = path_join(cwd, file_name)
330365

331366
if source == dest:
332-
dest = f"{cwd}/{file_name}.bak".replace('//', '/')
367+
dest += ".bak"
333368

334369
with open(source, "rb") as old_file, open(dest, "wb") as new_file:
335370
while True:
@@ -450,11 +485,7 @@ def handle_input(key, view, file_list, dir_dict) -> tuple[list, dict]:
450485

451486
elif key == "BSPC":
452487
beep.play(("D3", "B3", "G3"), 30)
453-
# previous directory
454-
if os.getcwd() == "/sd":
455-
os.chdir("/")
456-
else:
457-
os.chdir("..")
488+
prev_dir()
458489
file_list, dir_dict = refresh_files(view)
459490

460491
elif key == kb.aux_action:

0 commit comments

Comments
 (0)