From 7bafa828ada033067b841081d6d30a7fbb796b7b Mon Sep 17 00:00:00 2001 From: Justin Date: Sun, 2 Jun 2024 10:17:29 -0400 Subject: [PATCH] fix: show filename instead of full path when opening files --- scripts/init.rhai | 17 ++++++++++------- src/runtime.rs | 13 +++++++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/scripts/init.rhai b/scripts/init.rhai index 54f6b02..ccdd009 100644 --- a/scripts/init.rhai +++ b/scripts/init.rhai @@ -36,16 +36,19 @@ fn open_file_menu(dir) { let contents = s::list_files_and_dirs(dir); let keymap = new_keymap(); for file in contents.files { - keymap.add_regular_candidate(file); + keymap.add_regular_candidate(s::path_file_name(file), file); } for child_dir in contents.dirs { - let prog = || { - // TODO could we hit recursion depth limit when opening many dirs? - open_file_menu(child_dir); - }; - keymap.bind_key_for_special_candidate("enter", child_dir, "ViewDirectory", prog); + let child_dir_copy = child_dir; + let prog = || open_file_menu(child_dir_copy); + keymap.bind_key_for_special_candidate( + "enter", + s::path_file_name(child_dir_copy) + "/", + "ViewDir", + prog + ); } - keymap.bind_key_for_special_candidate("enter", "..", "ViewDirectory", || { + keymap.bind_key_for_special_candidate("enter", "..", "ViewDir", || { open_file_menu(dir + "/.."); }); diff --git a/src/runtime.rs b/src/runtime.rs index b2c7d86..1172035 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -454,6 +454,18 @@ fn list_files_and_dirs(dir: &str) -> Result { Ok(map) } +fn path_file_name(path: &str) -> Result { + use std::path::Path; + let os_str = Path::new(path) + .file_name() + .ok_or_else(|| error!(FileSystem, "Path ends in `..`: {path}"))?; + + Ok(os_str + .to_str() + .ok_or_else(|| error!(FileSystem, "Path is not valid unicode: {path}"))? + .into()) +} + macro_rules! register { ($module:expr, $runtime:ident . $method:ident($( $param:ident : $type:ty ),*)) => { register!($module, $runtime . $method($( $param : $type ),*) as $method) @@ -547,6 +559,7 @@ impl + 'static> Runtime { // Filesystem register!(module, list_files_and_dirs(dir: &str)?); + register!(module, path_file_name(path: &str)?); // Doc management register!(module, rt.current_dir()?);