Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugins): add stringified layout to switch_session_with_layout #3503

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,10 @@ pub(crate) fn start_client(opts: CliArgs) {
config_without_layout.clone(),
),
LayoutInfo::Url(url) => Layout::from_url(&url, config_without_layout.clone()),
LayoutInfo::Stringified(stringified_layout) => Layout::from_stringified_layout(
&stringified_layout,
config_without_layout.clone(),
),
};
match new_session_layout {
Ok(new_session_layout) => {
Expand Down
2 changes: 1 addition & 1 deletion src/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub(crate) fn get_resurrectable_sessions() -> Vec<(String, Duration, Layout)> {
};
let layout = match Layout::from_kdl(
&raw_layout,
layout_file_name.display().to_string(),
Some(layout_file_name.display().to_string()),
None,
None,
) {
Expand Down
7 changes: 7 additions & 0 deletions zellij-server/src/plugins/zellij_exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,13 @@ fn switch_session(
) -> Result<()> {
// pane_id is (id, is_plugin)
let err_context = || format!("Failed to switch session");
if let Some(LayoutInfo::Stringified(stringified_layout)) = layout.as_ref() {
// we verify the stringified layout here to fail early rather than when parsing it at the
// session-switching phase
if let Err(e) = Layout::from_kdl(&stringified_layout, None, None, None) {
return Err(anyhow!("Failed to deserialize layout: {}", e));
}
}
let client_id = env.client_id;
let tab_position = tab_position.map(|p| p + 1); // ¯\_()_/¯
let connect_to_session = ConnectToSession {
Expand Down
177 changes: 100 additions & 77 deletions zellij-server/src/tab/unit/tab_integration_tests.rs

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions zellij-utils/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,7 @@ pub enum LayoutInfo {
BuiltIn(String),
File(String),
Url(String),
Stringified(String),
}

impl LayoutInfo {
Expand All @@ -1192,13 +1193,15 @@ impl LayoutInfo {
LayoutInfo::BuiltIn(name) => &name,
LayoutInfo::File(name) => &name,
LayoutInfo::Url(url) => &url,
LayoutInfo::Stringified(layout) => &layout,
}
}
pub fn is_builtin(&self) -> bool {
match self {
LayoutInfo::BuiltIn(_name) => true,
LayoutInfo::File(_name) => false,
LayoutInfo::Url(_url) => false,
LayoutInfo::Stringified(_stringified) => false,
}
}
}
Expand Down
35 changes: 24 additions & 11 deletions zellij-utils/src/input/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1142,15 +1142,20 @@ impl Layout {
let (path_to_raw_layout, raw_layout, raw_swap_layouts) = match layout_info {
LayoutInfo::File(layout_name_without_extension) => {
let layout_dir = layout_dir.clone().or_else(|| default_layout_dir());
Self::stringified_from_dir(
&PathBuf::from(layout_name_without_extension),
layout_dir.as_ref(),
)?
let (path_to_layout, stringified_layout, swap_layouts) =
Self::stringified_from_dir(
&PathBuf::from(layout_name_without_extension),
layout_dir.as_ref(),
)?;
(Some(path_to_layout), stringified_layout, swap_layouts)
},
LayoutInfo::BuiltIn(layout_name) => {
Self::stringified_from_default_assets(&PathBuf::from(layout_name))?
let (path_to_layout, stringified_layout, swap_layouts) =
Self::stringified_from_default_assets(&PathBuf::from(layout_name))?;
(Some(path_to_layout), stringified_layout, swap_layouts)
},
LayoutInfo::Url(url) => (url.clone(), Self::stringified_from_url(&url)?, None),
LayoutInfo::Url(url) => (Some(url.clone()), Self::stringified_from_url(&url)?, None),
LayoutInfo::Stringified(stringified_layout) => (None, stringified_layout, None),
};
Layout::from_kdl(
&raw_layout,
Expand Down Expand Up @@ -1208,7 +1213,7 @@ impl Layout {
Layout::stringified_from_path_or_default(layout_path, layout_dir)?;
let layout = Layout::from_kdl(
&raw_layout,
path_to_raw_layout,
Some(path_to_raw_layout),
raw_swap_layouts
.as_ref()
.map(|(r, f)| (r.as_str(), f.as_str())),
Expand All @@ -1226,10 +1231,18 @@ impl Layout {
Err(e) => Err(ConfigError::DownloadError(format!("{}", e))),
}
})?;
let layout = Layout::from_kdl(&raw_layout, url.into(), None, None)?;
let layout = Layout::from_kdl(&raw_layout, Some(url.into()), None, None)?;
let config = Config::from_kdl(&raw_layout, Some(config))?; // this merges the two config, with
Ok((layout, config))
}
pub fn from_stringified_layout(
stringified_layout: &str,
config: Config,
) -> Result<(Layout, Config), ConfigError> {
let layout = Layout::from_kdl(&stringified_layout, None, None, None)?;
let config = Config::from_kdl(&stringified_layout, Some(config))?; // this merges the two config, with
Ok((layout, config))
}
#[cfg(target_family = "wasm")]
pub fn from_url(url: &str, config: Config) -> Result<(Layout, Config), ConfigError> {
Err(ConfigError::DownloadError(format!(
Expand All @@ -1244,7 +1257,7 @@ impl Layout {
Layout::stringified_from_path_or_default(layout_path, layout_dir)?;
let layout = Layout::from_kdl(
&raw_layout,
path_to_raw_layout,
Some(path_to_raw_layout),
raw_swap_layouts
.as_ref()
.map(|(r, f)| (r.as_str(), f.as_str())),
Expand All @@ -1261,7 +1274,7 @@ impl Layout {
Layout::stringified_from_default_assets(layout_name)?;
let layout = Layout::from_kdl(
&raw_layout,
path_to_raw_layout,
Some(path_to_raw_layout),
raw_swap_layouts
.as_ref()
.map(|(r, f)| (r.as_str(), f.as_str())),
Expand All @@ -1276,7 +1289,7 @@ impl Layout {
swap_layouts: Option<(&str, &str)>, // Option<path_to_swap_layout, stringified_swap_layout>
cwd: Option<PathBuf>,
) -> Result<Layout, ConfigError> {
Layout::from_kdl(raw, path_to_raw_layout, swap_layouts, cwd)
Layout::from_kdl(raw, Some(path_to_raw_layout), swap_layouts, cwd)
}
pub fn stringified_from_dir(
layout: &PathBuf,
Expand Down
Loading
Loading