diff options
author | Aram Drevekenin <aram@poor.dev> | 2023-02-15 17:06:19 +0100 |
---|---|---|
committer | Aram Drevekenin <aram@poor.dev> | 2023-02-15 17:06:19 +0100 |
commit | 80b00191c7c24507c6a4827e655edf32f7bfb2af (patch) | |
tree | f66c50104a044cea98fdb243adeb777adc25fe80 /zellij-utils | |
parent | c7690d5210f1552b85557586ebc2f0b6877e637c (diff) |
style(fmt): rustfmt
Diffstat (limited to 'zellij-utils')
-rw-r--r-- | zellij-utils/src/cli.rs | 36 | ||||
-rw-r--r-- | zellij-utils/src/input/actions.rs | 36 | ||||
-rw-r--r-- | zellij-utils/src/input/layout.rs | 105 | ||||
-rw-r--r-- | zellij-utils/src/input/unit/layout_test.rs | 416 | ||||
-rw-r--r-- | zellij-utils/src/kdl/kdl_layout_parser.rs | 277 | ||||
-rw-r--r-- | zellij-utils/src/kdl/mod.rs | 41 |
6 files changed, 603 insertions, 308 deletions
diff --git a/zellij-utils/src/cli.rs b/zellij-utils/src/cli.rs index 4facc6443..292508be4 100644 --- a/zellij-utils/src/cli.rs +++ b/zellij-utils/src/cli.rs @@ -180,9 +180,13 @@ pub enum Sessions { #[derive(Debug, Subcommand, Clone, Serialize, Deserialize)] pub enum CliAction { /// Write bytes to the terminal. - Write { bytes: Vec<u8> }, + Write { + bytes: Vec<u8>, + }, /// Write characters to the terminal. - WriteChars { chars: String }, + WriteChars { + chars: String, + }, /// [increase|decrease] the focused panes area at the [left|down|up|right] border. Resize { resize: Resize, @@ -193,13 +197,19 @@ pub enum CliAction { /// Change focus to the previous pane FocusPreviousPane, /// Move the focused pane in the specified direction. [right|left|up|down] - MoveFocus { direction: Direction }, + MoveFocus { + direction: Direction, + }, /// Move focus to the pane or tab (if on screen edge) in the specified direction /// [right|left|up|down] - MoveFocusOrTab { direction: Direction }, + MoveFocusOrTab { + direction: Direction, + }, /// Change the location of the focused pane in the specified direction or rotate forwrads /// [right|left|up|down] - MovePane { direction: Option<Direction> }, + MovePane { + direction: Option<Direction>, + }, /// Rotate the location of the previous pane backwards MovePaneBackwards, /// Dump the focused pane to a file @@ -296,7 +306,9 @@ pub enum CliAction { cwd: Option<PathBuf>, }, /// Switch input mode of all connected clients [locked|pane|tab|resize|move|search|session] - SwitchMode { input_mode: InputMode }, + SwitchMode { + input_mode: InputMode, + }, /// Embed focused pane if floating or float focused pane if embedded TogglePaneEmbedOrFloating, /// Toggle the visibility of all fdirectionloating panes in the current Tab, open one if none exist @@ -304,7 +316,9 @@ pub enum CliAction { /// Close the focused pane. ClosePane, /// Renames the focused pane - RenamePane { name: String }, + RenamePane { + name: String, + }, /// Remove a previously set pane name UndoRenamePane, /// Go to the next tab. @@ -314,9 +328,13 @@ pub enum CliAction { /// Close the current tab. CloseTab, /// Go to tab with index [index] - GoToTab { index: u32 }, + GoToTab { + index: u32, + }, /// Renames the focused pane - RenameTab { name: String }, + RenameTab { + name: String, + }, /// Remove a previously set tab name UndoRenameTab, /// Create a new tab, optionally with a specified tab layout and name diff --git a/zellij-utils/src/input/actions.rs b/zellij-utils/src/input/actions.rs index 8183f3263..3882b0c3c 100644 --- a/zellij-utils/src/input/actions.rs +++ b/zellij-utils/src/input/actions.rs @@ -1,7 +1,9 @@ //! Definition of the actions that can be bound to keys. use super::command::RunCommandAction; -use super::layout::{FloatingPaneLayout, Layout, TiledPaneLayout, SwapTiledLayout, SwapFloatingLayout}; +use super::layout::{ + FloatingPaneLayout, Layout, SwapFloatingLayout, SwapTiledLayout, TiledPaneLayout, +}; use crate::cli::CliAction; use crate::data::InputMode; use crate::data::{Direction, Resize}; @@ -163,7 +165,13 @@ pub enum Action { PaneNameInput(Vec<u8>), UndoRenamePane, /// Create a new tab, optionally with a specified tab layout. - NewTab(Option<TiledPaneLayout>, Vec<FloatingPaneLayout>, Option<Vec<SwapTiledLayout>>, Option<Vec<SwapFloatingLayout>>, Option<String>), // the String is the tab name + NewTab( + Option<TiledPaneLayout>, + Vec<FloatingPaneLayout>, + Option<Vec<SwapTiledLayout>>, + Option<Vec<SwapFloatingLayout>>, + Option<String>, + ), // the String is the tab name /// Do nothing. NoOp, /// Go to the next tab. @@ -372,8 +380,16 @@ impl Action { if tabs.len() > 1 { return Err(format!("Tab layout cannot itself have tabs")); } else if !tabs.is_empty() { - let swap_tiled_layouts = if layout.swap_tiled_layouts.is_empty() { None } else { Some(layout.swap_tiled_layouts.clone()) }; - let swap_floating_layouts = if layout.swap_floating_layouts.is_empty() { None } else { Some(layout.swap_floating_layouts.clone()) }; + let swap_tiled_layouts = if layout.swap_tiled_layouts.is_empty() { + None + } else { + Some(layout.swap_tiled_layouts.clone()) + }; + let swap_floating_layouts = if layout.swap_floating_layouts.is_empty() { + None + } else { + Some(layout.swap_floating_layouts.clone()) + }; let (tab_name, layout, floating_panes_layout) = tabs.drain(..).next().unwrap(); let name = tab_name.or(name); @@ -385,8 +401,16 @@ impl Action { name, )]) } else { - let swap_tiled_layouts = if layout.swap_tiled_layouts.is_empty() { None } else { Some(layout.swap_tiled_layouts.clone()) }; - let swap_floating_layouts = if layout.swap_floating_layouts.is_empty() { None } else { Some(layout.swap_floating_layouts.clone()) }; + let swap_tiled_layouts = if layout.swap_tiled_layouts.is_empty() { + None + } else { + Some(layout.swap_tiled_layouts.clone()) + }; + let swap_floating_layouts = if layout.swap_floating_layouts.is_empty() { + None + } else { + Some(layout.swap_floating_layouts.clone()) + }; let (layout, floating_panes_layout) = layout.new_tab(); Ok(vec![Action::NewTab( Some(layout), diff --git a/zellij-utils/src/input/layout.rs b/zellij-utils/src/input/layout.rs index 0355212a2..a0d61b241 100644 --- a/zellij-utils/src/input/layout.rs +++ b/zellij-utils/src/input/layout.rs @@ -22,8 +22,8 @@ use std::str::FromStr; use super::plugins::{PluginTag, PluginsConfigError}; use serde::{Deserialize, Serialize}; -use std::convert::TryFrom; use std::collections::BTreeMap; +use std::convert::TryFrom; use std::vec::Vec; use std::{ fmt, @@ -176,13 +176,13 @@ impl Run { (Some(Run::Command(..)), Some(Run::Command(..))) => true, (Some(Run::EditFile(..)), Some(Run::EditFile(..))) => true, (Some(Run::Cwd(..)), Some(Run::Cwd(..))) => true, - _ => false + _ => false, } } pub fn is_terminal(run: &Option<Run>) -> bool { match run { Some(Run::Command(..)) | Some(Run::EditFile(..)) | Some(Run::Cwd(..)) | None => true, - _ => false + _ => false, } } } @@ -231,11 +231,14 @@ impl fmt::Display for RunPluginLocation { pub enum LayoutConstraint { MaxPanes(usize), MinPanes(usize), - NoConstraint + NoConstraint, } pub type SwapTiledLayout = (BTreeMap<LayoutConstraint, TiledPaneLayout>, Option<String>); // Option<String> is the swap layout name -pub type SwapFloatingLayout = (BTreeMap<LayoutConstraint, Vec<FloatingPaneLayout>>, Option<String>); // Option<String> is the swap layout name +pub type SwapFloatingLayout = ( + BTreeMap<LayoutConstraint, Vec<FloatingPaneLayout>>, + Option<String>, +); // Option<String> is the swap layout name #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Default)] pub struct Layout { @@ -375,8 +378,7 @@ impl TiledPaneLayout { Some(external_children_index) => { children_nodes.reverse(); for child_node in children_nodes.drain(..) { - self.children - .insert(external_children_index, child_node); + self.children.insert(external_children_index, child_node); } self.external_children_index = None; Ok(true) @@ -443,9 +445,7 @@ impl TiledPaneLayout { split_space(space, &layout_to_split, space)? }, - None => { - split_space(space, self, space)? - } + None => split_space(space, self, space)?, }; for (_pane_layout, pane_geom) in layouts.iter() { if !pane_geom.is_at_least_minimum_size() { @@ -511,10 +511,11 @@ impl TiledPaneLayout { }, None => { self.focus = Some(true); - } + }, } } - pub fn truncate(&mut self, max_panes: usize) -> usize { // returns remaining children length + pub fn truncate(&mut self, max_panes: usize) -> usize { + // returns remaining children length // if max_panes is 1, it means there's only enough panes for this node, // if max_panes is 0, this is probably the root layout being called with 0 max panes if max_panes <= 1 { @@ -523,10 +524,16 @@ impl TiledPaneLayout { self.children.truncate(max_panes); self.children.iter_mut().for_each(|l| l.children.clear()); } else { - let mut remaining_panes = max_panes - self.children.iter().filter(|c| c.children.is_empty()).count(); + let mut remaining_panes = max_panes + - self + .children + .iter() + .filter(|c| c.children.is_empty()) + .count(); for child in self.children.iter_mut() { if remaining_panes > 1 && child.children.len() > 0 { - remaining_panes = remaining_panes.saturating_sub(child.truncate(remaining_panes)); + remaining_panes = + remaining_panes.saturating_sub(child.truncate(remaining_panes)); } else { child.children.clear(); } @@ -540,7 +547,7 @@ impl TiledPaneLayout { } pub fn has_focused_node(&self) -> bool { if self.focus.map(|f| f).unwrap_or(false) { - return true + return true; }; for child in &self.children { if child.has_focused_node() { @@ -617,7 +624,14 @@ impl Layout { ) -> Result<(Layout, Config), 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)?; + 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)) } @@ -646,7 +660,9 @@ impl Layout { None => Layout::stringified_from_default_assets(layout), } } - pub fn stringified_from_path(layout_path: &Path) -> Result<(String, String, Option<(String, String)>), ConfigError> { + pub fn stringified_from_path( + layout_path: &Path, + ) -> Result<(String, String, Option<(String, String)>), ConfigError> { // (path_to_layout as String, stringified_layout, Option<path_to_swap_layout as String, stringified_swap_layout>) let mut layout_file = File::open(&layout_path) .or_else(|_| File::open(&layout_path.with_extension("kdl"))) @@ -656,9 +672,15 @@ impl Layout { let mut kdl_layout = String::new(); layout_file.read_to_string(&mut kdl_layout)?; - Ok((layout_path.as_os_str().to_string_lossy().into(), kdl_layout, swap_layout_and_path)) - } - pub fn stringified_from_default_assets(path: &Path) -> Result<(String, String, Option<(String, String)>), ConfigError> { + Ok(( + layout_path.as_os_str().to_string_lossy().into(), + kdl_layout, + swap_layout_and_path, + )) + } + pub fn stringified_from_default_assets( + path: &Path, + ) -> Result<(String, String, Option<(String, String)>), ConfigError> { // (path_to_layout as String, stringified_layout, Option<path_to_swap_layout as String, stringified_swap_layout>) // TODO: ideally these should not be hard-coded // we should load layouts by name from the config @@ -670,7 +692,7 @@ impl Layout { Some(( "Default swap layout".into(), Self::stringified_default_swap_from_assets()?, - )) + )), )), Some("strider") => Ok(( "Strider layout".into(), @@ -678,7 +700,7 @@ impl Layout { Some(( "Strider swap layout".into(), Self::stringified_strider_swap_from_assets()?, - )) + )), )), Some("disable-status-bar") => Ok(( "Disable Status Bar layout".into(), @@ -691,7 +713,7 @@ impl Layout { Some(( "Compact layout swap".into(), Self::stringified_compact_swap_from_assets()?, - )) + )), )), None | Some(_) => Err(ConfigError::IoPath( std::io::Error::new(std::io::ErrorKind::Other, "The layout was not found"), @@ -753,19 +775,28 @@ impl Layout { Ok(mut stringified_swap_layout_file) => { let mut swap_kdl_layout = String::new(); match stringified_swap_layout_file.read_to_string(&mut swap_kdl_layout) { - Ok(..) => { - Some((swap_layout_path.as_os_str().to_string_lossy().into(), swap_kdl_layout)) - }, + Ok(..) => Some(( + 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); + log::warn!( + "Failed to read swap layout file: {}. Error: {:?}", + swap_layout_path.as_os_str().to_string_lossy(), + e + ); None - } + }, } }, Err(e) => { - log::warn!("Failed to read swap layout file: {}. Error: {:?}", swap_layout_path.as_os_str().to_string_lossy(), e); + log::warn!( + "Failed to read swap layout file: {}. Error: {:?}", + swap_layout_path.as_os_str().to_string_lossy(), + e + ); None - } + }, } } } @@ -777,7 +808,11 @@ fn split_space( ) -> Result<Vec<(TiledPaneLayout, PaneGeom)>, &'static str> { let mut pane_positions = Vec::new(); let sizes: Vec<Option<SplitSize>> = if layout.children_are_stacked { - let mut sizes: Vec<Option<SplitSize>> = layout.children.iter().map(|_part| Some(SplitSize::Fixed(1))).collect(); + let mut sizes: Vec<Option<SplitSize>> = layout + .children + .iter() + .map(|_part| Some(SplitSize::Fixed(1))) + .collect(); if let Some(last_size) = sizes.last_mut() { *last_size = None; } @@ -809,7 +844,7 @@ fn split_space( let min_size_for_panes = sizes.iter().fold(0, |acc, size| match size { Some(SplitSize::Percent(_)) | None => acc + 1, // TODO: minimum height/width as relevant here - Some(SplitSize::Fixed(fixed)) => acc + fixed + Some(SplitSize::Fixed(fixed)) => acc + fixed, }); if min_size_for_panes > split_dimension_space.as_usize() { return Err("Not enough room for panes"); // TODO: use error infra @@ -844,7 +879,11 @@ fn split_space( Dimension::percent(free_percent / flex_parts as f64) }, }; - split_dimension.adjust_inner(total_split_dimension_space.as_usize().saturating_sub(total_fixed_size)); + split_dimension.adjust_inner( + total_split_dimension_space + .as_usize() + .saturating_sub(total_fixed_size), + ); total_pane_size += split_dimension.as_usize(); let geom = match layout.children_split_direction { diff --git a/zellij-utils/src/input/unit/layout_test.rs b/zellij-utils/src/input/unit/layout_test.rs index b54efcf88..f15fd709d 100644 --- a/zellij-utils/src/input/unit/layout_test.rs +++ b/zellij-utils/src/input/unit/layout_test.rs @@ -21,10 +21,13 @@ fn layout_with_one_pane() { "#; let layout = Layout::from_kdl(kdl_layout, "layout_file_name".into(), None, None).unwrap(); let expected_layout = Layout { - template: Some((TiledPaneLayout { - children: vec![TiledPaneLayout::default()], - ..Default::default() - }, vec![])), + template: Some(( + TiledPaneLayout { + children: vec![TiledPaneLayout::default()], + ..Default::default() + }, + vec![], + )), ..Default::default() }; assert_eq!(layout, expected_layout); @@ -41,14 +44,17 @@ fn layout_with_multiple_panes() { "#; let layout = Layout::from_kdl(kdl_layout, "layout_file_name".into(), None, None).unwrap(); let expected_layout = Layout { - template: Some((TiledPaneLayout { - children: vec![ - TiledPaneLayout::default(), - TiledPaneLayout::default(), - TiledPaneLayout::default(), - ], - ..Default::default() - }, vec![])), + template: Some(( + TiledPaneLayout { + children: vec![ + TiledPaneLayout::default(), + TiledPaneLayout::default(), + TiledPaneLayout::default(), + ], + ..Default::default() + }, + vec![], + )), ..Default::default() }; assert_eq!(layout, expected_layout); @@ -70,20 +76,23 @@ fn layout_with_nested_panes() { "#; let layout = Layout::from_kdl(kdl_layout, "layout_file_name".into(), None, None).unwrap(); let expected_layout = Layout { - template: Some((TiledPaneLayout { - children: vec![ - TiledPaneLayout { - children_split_direction: SplitDirection::Vertical, - children: vec![TiledPaneLayout::default(), TiledPaneLayout::default()], - ..Default::default() - }, - TiledPaneLayout { - children: vec![TiledPaneLayout::default(), TiledPaneLayout::default()], - ..Default::default() - }, - ], - ..Default::default() - }, vec![])), + template: Some(( + TiledPaneLayout { + children: vec![ + TiledPaneLayout { + children_split_direction: SplitDirection::Vertical, + children: vec![TiledPaneLayout::default(), TiledPaneLayout::default()], + ..Default::default() + }, + TiledPaneLayout { + children: vec![TiledPaneLayout::default(), TiledPaneLayout::default()], + ..Default::default() + }, + ], + ..Default::default() + }, + vec![], + )), ..Default::default() }; assert_eq!(layout, expected_layout); @@ -108,29 +117,32 @@ fn layout_with_floating_panes() { "#; let layout = Layout::from_kdl(kdl_layout, "layout_file_name".into(), None, None).unwrap(); let expected_layout = Layout { - template: Some((TiledPaneLayout::default(), vec![ - FloatingPaneLayout::default(), - FloatingPaneLayout { - x: Some(PercentOrFixed::Fixed(10)), - y: Some(PercentOrFixed::Percent(10)), - width: Some(PercentOrFixed::Fixed(10)), - height: Some(PercentOrFixed::Percent(10)), - ..Default::default() - }, - FloatingPaneLayout { - x: Some(PercentOrFixed::Fixed(10)), - y: Some(PercentOrFixed::Percent(10)), - ..Default::default() - }, - FloatingPaneLayout { - run: Some(Run::Command(RunCommand { - command: PathBuf::from("htop"), - hold_on_close: true, + template: Some(( + TiledPaneLayout::default(), + vec![ + FloatingPaneLayout::default(), + FloatingPaneLayout { + x: Some(PercentOrFixed::Fixed(10)), + y: Some(PercentOrFixed::Percent(10)), + width: Some(PercentOrFixed::Fixed(10)), + height: Some(PercentOrFixed::Percent(10)), ..Default::default() - })), - ..Default::default() - }, - ])), + }, + FloatingPaneLayout { + x: Some(PercentOrFixed::Fixed(10)), + y: Some(PercentOrFixed::Percent(10)), + ..Default::default() + }, + FloatingPaneLayout { + run: Some(Run::Command(RunCommand { + command: PathBuf::from("htop"), + hold_on_close: true, + ..Default::default() + })), + ..Default::default() + }, + ], + )), ..Default::default() }; assert_eq!(layout, expected_layout); @@ -149,10 +161,13 @@ fn layout_with_mixed_panes_and_floating_panes() { "#; let layout = Layout::from_kdl(kdl_layout, "layout_file_name".into(), None, None).unwrap(); let expected_layout = Layout { - template: Some((TiledPaneLayout { - children: vec![TiledPaneLayout::default(), TiledPaneLayout::default()], - ..Default::default() - }, vec![FloatingPaneLayout::default()])), + template: Some(( + TiledPaneLayout { + children: vec![TiledPaneLayout::default(), TiledPaneLayout::default()], + ..Default::default() + }, + vec![FloatingPaneLayout::default()], + )), ..Default::default() }; assert_eq!(layout, expected_layout); @@ -175,17 +190,20 @@ fn layout_with_floating_panes_template() { "#; let layout = Layout::from_kdl(kdl_layout, "layout_file_name".into(), None, None).unwrap(); let expected_layout = Layout { - template: Some((TiledPaneLayout { - children: vec![TiledPaneLayout::default()], - ..Default::default() - }, vec![ - FloatingPaneLayout::default(), - FloatingPaneLayout { - x: Some(PercentOrFixed::Fixed(10)), - y: Some(PercentOrFixed::Percent(10)), + template: Some(( + TiledPaneLayout { + children: vec![TiledPaneLayout::default()], ..Default::default() }, - ])), + vec![ + FloatingPaneLayout::default(), + FloatingPaneLayout { + x: Some(PercentOrFixed::Fixed(10)), + y: Some(PercentOrFixed::Percent(10)), + ..Default::default() + }, + ], + )), ..Default::default() }; assert_eq!(layout, expected_layout); @@ -207,27 +225,30 @@ fn layout_with_shared_tiled_and_floating_panes_template() { "#; let layout = Layout::from_kdl(kdl_layout, "layout_file_name".into(), None, None).unwrap(); let expected_layout = Layout { - template: Some((TiledPaneLayout { - children: vec![TiledPaneLayout { - run: Some(Run::Command(RunCommand { - command: PathBuf::from("htop"), - hold_on_close: true, - ..Default::default() - })), - ..Default::default() - }], - ..Default::default() - }, vec![ - FloatingPaneLayout::default(), - FloatingPaneLayout { - run: Some(Run::Command(RunCommand { - command: PathBuf::from("htop"), - hold_on_close: true, + template: Some(( + TiledPaneLayout { + children: vec![TiledPaneLayout { + run: Some(Run::Command(RunCommand { + command: PathBuf::from("htop"), + hold_on_close: true, + ..Default::default() + })), ..Default::default() - })), + }], ..Defau |