diff options
author | Aram Drevekenin <aram@poor.dev> | 2024-02-28 16:12:12 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-28 16:12:12 +0100 |
commit | 6340b5e312cd4c52fa7a967b44aa331253c64779 (patch) | |
tree | 425a42f6c42599f5e348df54028321ff04f65739 | |
parent | eba597afc5373fc02062adf296be70db04ee9725 (diff) |
fix(plugins): respect cwd and start filepicker at filesystem root (#3161)
* allow padding cwd to plugins
* improve strider performance and fix alias cwd issue
* style(fmt): rustfmt
25 files changed, 397 insertions, 55 deletions
diff --git a/default-plugins/fixture-plugin-for-tests/src/main.rs b/default-plugins/fixture-plugin-for-tests/src/main.rs index fe740f740..0da45a2f4 100644 --- a/default-plugins/fixture-plugin-for-tests/src/main.rs +++ b/default-plugins/fixture-plugin-for-tests/src/main.rs @@ -1,5 +1,6 @@ use serde::{Deserialize, Serialize}; use std::collections::BTreeMap; +use std::io::prelude::*; use zellij_tile::prelude::*; // This is a fixture plugin used only for tests in Zellij @@ -300,6 +301,10 @@ impl ZellijPlugin for State { LayoutInfo::BuiltIn("compact".to_owned()), ); }, + Key::Ctrl('8') => { + let mut file = std::fs::File::create("/host/hi-from-plugin.txt").unwrap(); + file.write_all(b"Hi there!").unwrap(); + }, _ => {}, }, Event::CustomMessage(message, payload) => { diff --git a/default-plugins/strider/src/main.rs b/default-plugins/strider/src/main.rs index 2e06240af..1ad2e70ce 100644 --- a/default-plugins/strider/src/main.rs +++ b/default-plugins/strider/src/main.rs @@ -43,6 +43,9 @@ impl ZellijPlugin for State { Key::Down | Key::Char('j') => { let currently_selected = self.selected(); let next = self.selected().saturating_add(1); + if next >= self.files.len() { + refresh_directory(self); + } *self.selected_mut() = min(self.files.len().saturating_sub(1), next); if currently_selected != self.selected() { should_render = true; @@ -76,6 +79,9 @@ impl ZellijPlugin for State { Mouse::ScrollDown(_) => { let currently_selected = self.selected(); let next = self.selected().saturating_add(1); + if next >= self.files.len() { + refresh_directory(self); + } *self.selected_mut() = min(self.files.len().saturating_sub(1), next); if currently_selected != self.selected() { should_render = true; @@ -121,6 +127,7 @@ impl ZellijPlugin for State { } fn render(&mut self, rows: usize, cols: usize) { + self.current_rows = Some(rows); for i in 0..rows { if self.selected() < self.scroll() { *self.scroll_mut() = self.selected(); diff --git a/default-plugins/strider/src/state.rs b/default-plugins/strider/src/state.rs index be4ede4b6..230e92ea8 100644 --- a/default-plugins/strider/src/state.rs +++ b/default-plugins/strider/src/state.rs @@ -19,6 +19,7 @@ pub struct State { pub loading: bool, pub loading_animation_offset: u8, pub should_open_floating: bool, + pub current_rows: Option<usize>, } impl State { @@ -40,7 +41,7 @@ impl State { pub fn traverse_dir_or_open_file(&mut self) { if let Some(f) = self.files.get(self.selected()) { match f.clone() { - FsEntry::Dir(p, _) => { + FsEntry::Dir(p) => { self.path = p; refresh_directory(self); }, @@ -55,14 +56,14 @@ impl State { #[derive(PartialEq, Eq, PartialOrd, Ord, Clone)] pub enum FsEntry { - Dir(PathBuf, usize), + Dir(PathBuf), File(PathBuf, u64), } impl FsEntry { pub fn name(&self) -> String { let path = match self { - FsEntry::Dir(p, _) => p, + FsEntry::Dir(p) => p, FsEntry::File(p, _) => p, }; path.file_name().unwrap().to_string_lossy().into_owned() @@ -70,7 +71,7 @@ impl FsEntry { pub fn as_line(&self, width: usize) -> String { let info = match self { - FsEntry::Dir(_, s) => s.to_string(), + FsEntry::Dir(_s) => "".to_owned(), FsEntry::File(_, s) => pb::convert(*s as f64), }; let space = width.saturating_sub(info.len()); @@ -89,22 +90,27 @@ impl FsEntry { } pub(crate) fn refresh_directory(state: &mut State) { - state.files = read_dir(Path::new(ROOT).join(&state.path)) - .unwrap() - .filter_map(|res| { - res.and_then(|d| { - if d.metadata()?.is_dir() { - let children = read_dir(d.path())?.count(); - Ok(FsEntry::Dir(d.path(), children)) - } else { - let size = d.metadata()?.len(); - Ok(FsEntry::File(d.path(), size)) - } - }) - .ok() - .filter(|d| !d.is_hidden_file() || !state.hide_hidden_files) - }) - .collect(); - + // TODO: might be good to do this asynchronously with a worker + let mut max_lines = (state.current_rows.unwrap_or(50) + state.scroll()) * 2; // 100 is arbitrary for performance reasons + let mut files = vec![]; + for entry in read_dir(Path::new(ROOT).join(&state.path)).unwrap() { + if let Ok(entry) = entry { + if max_lines == 0 { + break; + } + let entry_metadata = entry.metadata().unwrap(); + let entry = if entry_metadata.is_dir() { + FsEntry::Dir(entry.path()) + } else { + let size = entry_metadata.len(); + FsEntry::File(entry.path(), size) + }; + if !entry.is_hidden_file() || !state.hide_hidden_files { + max_lines = max_lines.saturating_sub(1); + files.push(entry); + } + } + } + state.files = files; state.files.sort_unstable(); } diff --git a/zellij-server/src/plugins/mod.rs b/zellij-server/src/plugins/mod.rs index 80a590453..e09364432 100644 --- a/zellij-server/src/plugins/mod.rs +++ b/zellij-server/src/plugins/mod.rs @@ -217,6 +217,7 @@ pub(crate) fn plugin_thread_main( skip_cache, ) => { run_plugin_or_alias.populate_run_plugin_if_needed(&plugin_aliases); + let cwd = run_plugin_or_alias.get_initial_cwd().or(cwd); let run_plugin = run_plugin_or_alias.get_run_plugin(); match wasm_bridge.load_plugin( &run_plugin, @@ -363,12 +364,15 @@ pub(crate) fn plugin_thread_main( for run_instruction in extracted_run_instructions { if let Some(Run::Plugin(run_plugin_or_alias)) = run_instruction { let run_plugin = run_plugin_or_alias.get_run_plugin(); + let cwd = run_plugin_or_alias + .get_initial_cwd() + .or_else(|| cwd.clone()); let skip_cache = false; let (plugin_id, _client_id) = wasm_bridge.load_plugin( &run_plugin, Some(tab_index), size, - None, + cwd, skip_cache, Some(client_id), None, @@ -537,7 +541,6 @@ pub(crate) fn plugin_thread_main( source_plugin_id, message, } => { - let cwd = message.new_plugin_args.as_ref().and_then(|n| n.cwd.clone()); let mut pipe_messages = vec![]; let skip_cache = message .new_plugin_args @@ -564,7 +567,7 @@ pub(crate) fn plugin_thread_main( PipeSource::Plugin(source_plugin_id), &plugin_url, &Some(message.plugin_config), - &cwd, + &None, skip_cache, should_float, &pane_id_to_replace.map(|p| p.into()), @@ -698,10 +701,11 @@ fn pipe_to_specific_plugins( cwd.clone(), ) { Ok(run_plugin_or_alias) => { + let initial_cwd = run_plugin_or_alias.get_initial_cwd(); let all_plugin_ids = wasm_bridge.get_or_load_plugins( run_plugin_or_alias, size, - cwd.clone(), + initial_cwd.or_else(|| cwd.clone()), skip_cache, should_float, pane_id_to_replace.is_some(), diff --git a/zellij-server/src/plugins/plugin_map.rs b/zellij-server/src/plugins/plugin_map.rs index e03cf1728..1d30596ec 100644 --- a/zellij-server/src/plugins/plugin_map.rs +++ b/zellij-server/src/plugins/plugin_map.rs @@ -246,10 +246,12 @@ impl PluginMap { .plugin .userspace_configuration .clone(); + let initial_cwd = running_plugin.plugin_env.plugin.initial_cwd.clone(); Some(RunPlugin { _allow_exec_host_cmd: false, location: run_plugin_location, configuration: run_plugin_configuration, + initial_cwd, }) } else { None diff --git a/zellij-server/src/plugins/unit/plugin_tests.rs b/zellij-server/src/plugins/unit/plugin_tests.rs index 07212c3b3..8fa262474 100644 --- a/zellij-server/src/plugins/unit/plugin_tests.rs +++ b/zellij-server/src/plugins/unit/plugin_tests.rs @@ -571,6 +571,7 @@ pub fn load_new_plugin_from_hd() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -650,6 +651,7 @@ pub fn load_new_plugin_with_plugin_alias() { name: "fixture_plugin_for_tests".to_owned(), configuration: Default::default(), run_plugin: None, + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -725,6 +727,7 @@ pub fn plugin_workers() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -804,6 +807,7 @@ pub fn plugin_workers_persist_state() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -893,6 +897,7 @@ pub fn can_subscribe_to_hd_events() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -970,6 +975,7 @@ pub fn switch_to_mode_plugin_command() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -1041,6 +1047,7 @@ pub fn switch_to_mode_plugin_command_permission_denied() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -1112,6 +1119,7 @@ pub fn new_tabs_with_layout_plugin_command() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -1197,6 +1205,7 @@ pub fn new_tab_plugin_command() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -1268,6 +1277,7 @@ pub fn go_to_next_tab_plugin_command() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -1338,6 +1348,7 @@ pub fn go_to_previous_tab_plugin_command() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -1408,6 +1419,7 @@ pub fn resize_focused_pane_plugin_command() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -1478,6 +1490,7 @@ pub fn resize_focused_pane_with_direction_plugin_command() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -1548,6 +1561,7 @@ pub fn focus_next_pane_plugin_command() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -1618,6 +1632,7 @@ pub fn focus_previous_pane_plugin_command() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -1688,6 +1703,7 @@ pub fn move_focus_plugin_command() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -1758,6 +1774,7 @@ pub fn move_focus_or_tab_plugin_command() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -1828,6 +1845,7 @@ pub fn edit_scrollback_plugin_command() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -1898,6 +1916,7 @@ pub fn write_plugin_command() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -1968,6 +1987,7 @@ pub fn write_chars_plugin_command() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -2038,6 +2058,7 @@ pub fn toggle_tab_plugin_command() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -2108,6 +2129,7 @@ pub fn move_pane_plugin_command() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -2178,6 +2200,7 @@ pub fn move_pane_with_direction_plugin_command() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -2248,6 +2271,7 @@ pub fn clear_screen_plugin_command() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -2319,6 +2343,7 @@ pub fn scroll_up_plugin_command() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -2390,6 +2415,7 @@ pub fn scroll_down_plugin_command() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -2460,6 +2486,7 @@ pub fn scroll_to_top_plugin_command() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -2530,6 +2557,7 @@ pub fn scroll_to_bottom_plugin_command() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -2600,6 +2628,7 @@ pub fn page_scroll_up_plugin_command() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -2670,6 +2699,7 @@ pub fn page_scroll_down_plugin_command() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -2740,6 +2770,7 @@ pub fn toggle_focus_fullscreen_plugin_command() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -2810,6 +2841,7 @@ pub fn toggle_pane_frames_plugin_command() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -2880,6 +2912,7 @@ pub fn toggle_pane_embed_or_eject_plugin_command() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -2950,6 +2983,7 @@ pub fn undo_rename_pane_plugin_command() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -3020,6 +3054,7 @@ pub fn close_focus_plugin_command() { _allow_exec_host_cmd: false, location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), configuration: Default::default(), + ..Default::default() }); let tab_index = 1; let client_id = 1; @@ -3090,6 +3125,7 @@ pub fn toggle_active_tab_sync_plugin_command |