diff options
| author | Aram Drevekenin <aram@poor.dev> | 2024-02-06 14:26:14 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-06 14:26:14 +0100 |
| commit | 6b20a958f4e8db5614fffb27fe5d32f07ddfe855 (patch) | |
| tree | c8383fb7941b5e0c23cb348a655f2ea0fafd8668 /zellij-utils/src/input | |
| parent | docs(readme): add sponsor (diff) | |
| download | sl0ppij-6b20a958f4e8db5614fffb27fe5d32f07ddfe855.tar.gz sl0ppij-6b20a958f4e8db5614fffb27fe5d32f07ddfe855.tar.xz sl0ppij-6b20a958f4e8db5614fffb27fe5d32f07ddfe855.zip | |
feat(sessions): welcome screen (#3112)
* prototype - can send layout name for new session from session-manager
* feat(sessions): ui for selecting layout for new session in the session-manager
* fix: send available layouts to plugins
* make tests compile
* fix tests
* improve ui
* fix: respect built-in layouts
* ui for built-in layouts
* some cleanups
* style(fmt): rustfmt
* welcome screen ui
* fix: make sure layout config is not shared between sessions
* allow disconnecting other users from current session and killing other sessions
* fix: respect default layout
* add welcome screen layout
* tests(plugins): new api methods
* fix(session-manager): do not quit welcome screen on esc and break
* fix(plugins): adjust permissions
* style(fmt): rustfmt
* style(fmt): fix warnings
Diffstat (limited to 'zellij-utils/src/input')
| -rw-r--r-- | zellij-utils/src/input/config.rs | 9 | ||||
| -rw-r--r-- | zellij-utils/src/input/keybinds.rs | 11 | ||||
| -rw-r--r-- | zellij-utils/src/input/layout.rs | 122 |
3 files changed, 124 insertions, 18 deletions
diff --git a/zellij-utils/src/input/config.rs b/zellij-utils/src/input/config.rs index 23c05d4f..eb8fe521 100644 --- a/zellij-utils/src/input/config.rs +++ b/zellij-utils/src/input/config.rs @@ -222,6 +222,15 @@ impl Config { Err(e) => Err(ConfigError::IoPath(e, path.into())), } } + pub fn merge(&mut self, other: Config) -> Result<(), ConfigError> { + self.options = self.options.merge(other.options); + self.keybinds.merge(other.keybinds.clone()); + self.themes = self.themes.merge(other.themes); + self.plugins = self.plugins.merge(other.plugins); + self.ui = self.ui.merge(other.ui); + self.env = self.env.merge(other.env); + Ok(()) + } } #[cfg(test)] diff --git a/zellij-utils/src/input/keybinds.rs b/zellij-utils/src/input/keybinds.rs index eaed03fd..ab13f711 100644 --- a/zellij-utils/src/input/keybinds.rs +++ b/zellij-utils/src/input/keybinds.rs @@ -65,6 +65,17 @@ impl Keybinds { } ret } + pub fn merge(&mut self, mut other: Keybinds) { + for (other_input_mode, mut other_input_mode_keybinds) in other.0.drain() { + let input_mode_keybinds = self + .0 + .entry(other_input_mode) + .or_insert_with(|| Default::default()); + for (other_action, other_action_keybinds) in other_input_mode_keybinds.drain() { + input_mode_keybinds.insert(other_action, other_action_keybinds); + } + } + } } // The unit test location. diff --git a/zellij-utils/src/input/layout.rs b/zellij-utils/src/input/layout.rs index 82dab15e..7f65d605 100644 --- a/zellij-utils/src/input/layout.rs +++ b/zellij-utils/src/input/layout.rs @@ -9,8 +9,8 @@ // If plugins should be able to depend on the layout system // then [`zellij-utils`] could be a proper place. use crate::{ - data::Direction, - home::find_default_config_dir, + data::{Direction, LayoutInfo}, + home::{default_layout_dir, find_default_config_dir}, input::{ command::RunCommand, config::{Config, ConfigError}, @@ -19,6 +19,7 @@ use crate::{ setup::{self}, }; +use std::cmp::Ordering; use std::fmt::{Display, Formatter}; use std::str::FromStr; @@ -819,6 +820,62 @@ impl Default for LayoutParts { } impl Layout { + // the first layout will either be the default one + pub fn list_available_layouts( + layout_dir: Option<PathBuf>, + default_layout_name: &Option<String>, + ) -> Vec<LayoutInfo> { + let mut available_layouts = layout_dir + .clone() + .or_else(|| default_layout_dir()) + .and_then(|layout_dir| match std::fs::read_dir(layout_dir) { + Ok(layout_files) => Some(layout_files), + Err(e) => { + log::error!("Failed to read layout dir: {:?}", e); + None + }, + }) + .map(|layout_files| { + let mut available_layouts = vec![]; + for file in layout_files { + if let Ok(file) = file { + if Layout::from_path_or_default_without_config( + Some(&file.path()), + layout_dir.clone(), + ) + .is_ok() + { + if let Some(file_name) = file.path().file_stem() { + available_layouts + .push(LayoutInfo::File(file_name.to_string_lossy().to_string())) + } + } + } + } + available_layouts + }) + .unwrap_or_else(Default::default); + let default_layout_name = default_layout_name + .as_ref() + .map(|d| d.as_str()) + .unwrap_or("default"); + available_layouts.push(LayoutInfo::BuiltIn("default".to_owned())); + available_layouts.push(LayoutInfo::BuiltIn("strider".to_owned())); + available_layouts.push(LayoutInfo::BuiltIn("disable-status-bar".to_owned())); + available_layouts.push(LayoutInfo::BuiltIn("compact".to_owned())); + available_layouts.sort_by(|a, b| { + let a_name = a.name(); + let b_name = b.name(); + if a_name == default_layout_name { + return Ordering::Less; + } else if b_name == default_layout_name { + return Ordering::Greater; + } else { + a_name.cmp(&b_name) + } + }); + available_layouts + } pub fn stringified_from_path_or_default( layout_path: Option<&PathBuf>, layout_dir: Option<PathBuf>, @@ -861,6 +918,40 @@ impl Layout { let config = Config::from_kdl(&raw_layout, Some(config))?; // this merges the two config, with Ok((layout, config)) } + pub fn from_path_or_default_without_config( + layout_path: Option<&PathBuf>, + layout_dir: Option<PathBuf>, + ) -> Result<Layout, ConfigError> { + let (path_to_raw_layout, raw_layout, raw_swap_layouts) = + Layout::stringified_from_path_or_default(layout_path, layout_dir)?; + let layout = Layout::from_kdl( + &raw_layout, + path_to_raw_layout, + raw_swap_layouts + .as_ref() + .map(|(r, f)| (r.as_str(), f.as_str())), + None, + )?; + Ok(layout) + } + pub fn from_default_assets( + layout_name: &Path, + _layout_dir: Option<PathBuf>, + config: Config, + ) -> Result<(Layout, Config), ConfigError> { + let (path_to_raw_layout, raw_layout, raw_swap_layouts) = + Layout::stringified_from_default_assets(layout_name)?; + let layout = Layout::from_kdl( + &raw_layout, + path_to_raw_layout, + raw_swap_layouts + .as_ref() + .map(|(r, f)| (r.as_str(), f.as_str())), + None, + )?; + let config = Config::from_kdl(&raw_layout, Some(config))?; // this merges the two config, with + Ok((layout, config)) + } pub fn from_str( raw: &str, path_to_raw_layout: String, @@ -951,6 +1042,11 @@ impl Layout { Self::stringified_compact_swap_from_assets()?, )), )), + Some("welcome") => Ok(( + "Welcome screen layout".into(), + Self::stringified_welcome_from_assets()?, + None, + )), None | Some(_) => Err(ConfigError::IoPath( std::io::Error::new(std::io::ErrorKind::Other, "The layout was not found"), path.into(), @@ -982,6 +1078,10 @@ impl Layout { Ok(String::from_utf8(setup::COMPACT_BAR_SWAP_LAYOUT.to_vec())?) } + pub fn stringified_welcome_from_assets() -> Result<String, ConfigError> { + Ok(String::from_utf8(setup::WELCOME_LAYOUT.to_vec())?) + } + pub fn new_tab(&self) -> (TiledPaneLayout, Vec<FloatingPaneLayout>) { self.template.clone().unwrap_or_default() } @@ -1024,24 +1124,10 @@ impl Layout { swap_layout_path.as_os_str().to_string_lossy().into(), swap_kdl_layout, )), - Err(e) => { - log::warn!( - "Failed to read swap layout file: {}. Error: {:?}", - swap_layout_path.as_os_str().to_string_lossy(), - e - ); - None - }, + Err(_e) => None, } }, - Err(e) => { - log::warn!( - "Failed to read swap layout file: {}. Error: {:?}", - swap_layout_path.as_os_str().to_string_lossy(), - e - ); - None - }, + Err(_e) => None, } } } |
