Skip to content

Commit

Permalink
fix(layouts): suspend commands in remote layouts (zellij-org#3697)
Browse files Browse the repository at this point in the history
* fix(layouts): suspend commands in remote layouts

* style(fmt): rustfmt
  • Loading branch information
imsnif authored and Tomcat-42 committed Nov 9, 2024
1 parent 5e94dba commit 3c1db3d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
7 changes: 6 additions & 1 deletion zellij-utils/src/input/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ impl Action {
.or_else(|| config.and_then(|c| c.options.layout_dir))
.or_else(|| get_layout_dir(find_default_config_dir()));

let mut should_start_layout_commands_suspended = false;
let (path_to_raw_layout, raw_layout, swap_layouts) = if let Some(layout_url) =
layout_path.to_str().and_then(|l| {
if l.starts_with("http://") || l.starts_with("https://") {
Expand All @@ -541,6 +542,7 @@ impl Action {
None
}
}) {
should_start_layout_commands_suspended = true;
(
layout_url.to_owned(),
Layout::stringified_from_url(layout_url)
Expand All @@ -551,7 +553,7 @@ impl Action {
Layout::stringified_from_path_or_default(Some(&layout_path), layout_dir)
.map_err(|e| format!("Failed to load layout: {}", e))?
};
let layout = Layout::from_str(&raw_layout, path_to_raw_layout, swap_layouts.as_ref().map(|(f, p)| (f.as_str(), p.as_str())), cwd).map_err(|e| {
let mut layout = Layout::from_str(&raw_layout, path_to_raw_layout, swap_layouts.as_ref().map(|(f, p)| (f.as_str(), p.as_str())), cwd).map_err(|e| {
let stringified_error = match e {
ConfigError::KdlError(kdl_error) => {
let error = kdl_error.add_src(layout_path.as_path().as_os_str().to_string_lossy().to_string(), String::from(raw_layout));
Expand Down Expand Up @@ -583,6 +585,9 @@ impl Action {
};
stringified_error
})?;
if should_start_layout_commands_suspended {
layout.recursively_add_start_suspended_including_template(Some(true));
}
let mut tabs = layout.tabs();
if !tabs.is_empty() {
let swap_tiled_layouts = Some(layout.swap_tiled_layouts.clone());
Expand Down
38 changes: 33 additions & 5 deletions zellij-utils/src/input/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,7 @@ impl Layout {
layout_dir: &Option<PathBuf>,
layout_info: LayoutInfo,
) -> Result<Layout, ConfigError> {
let mut should_start_layout_commands_suspended = false;
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());
Expand All @@ -1182,17 +1183,27 @@ impl Layout {
Self::stringified_from_default_assets(&PathBuf::from(layout_name))?;
(Some(path_to_layout), stringified_layout, swap_layouts)
},
LayoutInfo::Url(url) => (Some(url.clone()), Self::stringified_from_url(&url)?, None),
LayoutInfo::Url(url) => {
should_start_layout_commands_suspended = true;
(Some(url.clone()), Self::stringified_from_url(&url)?, None)
},
LayoutInfo::Stringified(stringified_layout) => (None, stringified_layout, None),
};
Layout::from_kdl(
let mut 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,
)
);
if should_start_layout_commands_suspended {
layout
.iter_mut()
.next()
.map(|l| l.recursively_add_start_suspended_including_template(Some(true)));
}
layout
}
pub fn stringified_from_path_or_default(
layout_path: Option<&PathBuf>,
Expand Down Expand Up @@ -1259,7 +1270,8 @@ impl Layout {
Err(e) => Err(ConfigError::DownloadError(format!("{}", e))),
}
})?;
let layout = Layout::from_kdl(&raw_layout, Some(url.into()), None, None)?;
let mut layout = Layout::from_kdl(&raw_layout, Some(url.into()), None, None)?;
layout.recursively_add_start_suspended_including_template(Some(true));
let config = Config::from_kdl(&raw_layout, Some(config))?; // this merges the two config, with
Ok((layout, config))
}
Expand Down Expand Up @@ -1486,7 +1498,23 @@ impl Layout {
}
}
}

pub fn recursively_add_start_suspended_including_template(
&mut self,
start_suspended: Option<bool>,
) {
if let Some((tiled_panes_template, floating_panes_template)) = self.template.as_mut() {
tiled_panes_template.recursively_add_start_suspended(start_suspended);
for floating_pane in floating_panes_template.iter_mut() {
floating_pane.add_start_suspended(start_suspended);
}
}
for (_tab_name, tiled_panes, floating_panes) in self.tabs.iter_mut() {
tiled_panes.recursively_add_start_suspended(start_suspended);
for floating_pane in floating_panes.iter_mut() {
floating_pane.add_start_suspended(start_suspended);
}
}
}
fn swap_layout_and_path(path: &Path) -> Option<(String, String)> {
// Option<path, stringified_swap_layout>
let mut swap_layout_path = PathBuf::from(path);
Expand Down

0 comments on commit 3c1db3d

Please sign in to comment.