Skip to content

Commit

Permalink
fix: show filename instead of full path when opening files
Browse files Browse the repository at this point in the history
  • Loading branch information
justinpombrio committed Jun 2, 2024
1 parent 03d5d2f commit 7bafa82
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
17 changes: 10 additions & 7 deletions scripts/init.rhai
Original file line number Diff line number Diff line change
Expand Up @@ -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 + "/..");
});

Expand Down
13 changes: 13 additions & 0 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,18 @@ fn list_files_and_dirs(dir: &str) -> Result<rhai::Map, SynlessError> {
Ok(map)
}

fn path_file_name(path: &str) -> Result<rhai::Dynamic, SynlessError> {
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)
Expand Down Expand Up @@ -547,6 +559,7 @@ impl<F: Frontend<Style = Style> + 'static> Runtime<F> {

// Filesystem
register!(module, list_files_and_dirs(dir: &str)?);
register!(module, path_file_name(path: &str)?);

// Doc management
register!(module, rt.current_dir()?);
Expand Down

0 comments on commit 7bafa82

Please sign in to comment.