diff options
| author | user <user@clank> | 2026-08-08 21:06:40 +0200 |
|---|---|---|
| committer | user <user@clank> | 2026-08-08 21:06:40 +0200 |
| commit | 97b744a57e3ba53169781a9357d8dc86b15d387e (patch) | |
| tree | 258562cbe750017fc08180e3d92a96c12689a47d /zellij-utils/src | |
| parent | sl0p: uniform pane gap + aspect-corrected horizontal gap (diff) | |
| download | sl0ppij-97b744a57e3ba53169781a9357d8dc86b15d387e.tar.gz sl0ppij-97b744a57e3ba53169781a9357d8dc86b15d387e.tar.xz sl0ppij-97b744a57e3ba53169781a9357d8dc86b15d387e.zip | |
sl0p: CSS-style 1/2/4-value shorthand for inner padding
`padding` now accepts:
padding 1 -> all four sides
padding 1 2 -> up/down = 1, left/right = 2
padding 1 2 3 4 -> up=1 down=2 left=3 right=4
NOTE the 4-value order is UP DOWN LEFT RIGHT as requested, deliberately NOT
CSS's clockwise top-right-bottom-left. Documented on PanePadding and at the
parser, since the similarity to CSS invites the wrong assumption.
3 or 5 values are rejected with a message naming the accepted forms, rather
than being silently reinterpreted. Negative values clamp to 0.
ASPECT: only the 1-value form is aspect-corrected (left/right multiplied by
gap_aspect) so `padding 1` looks square on a non-square cell grid. The 2- and
4-value forms are literal: specifying axes separately means the caller is
already controlling horizontal size, and silently doubling it would surprise.
Style now carries a PanePadding {top,bottom,left,right} instead of an x/y
pair, which also collapses the reconfigure plumbing back to a single field
rather than growing another parameter per side.
to_kdl always emits the explicit 4-value form: emitting the 1-value form
would re-apply gap_aspect on the next read and silently double the padding
on every config round-trip.
Baselines held: zellij-utils 435/5, zellij-server 1421/30.
Diffstat (limited to 'zellij-utils/src')
| -rw-r--r-- | zellij-utils/src/data.rs | 34 | ||||
| -rw-r--r-- | zellij-utils/src/input/config.rs | 45 | ||||
| -rw-r--r-- | zellij-utils/src/input/theme.rs | 12 | ||||
| -rw-r--r-- | zellij-utils/src/kdl/mod.rs | 64 | ||||
| -rw-r--r-- | zellij-utils/src/plugin_api/event.rs | 3 | ||||
| -rw-r--r-- | zellij-utils/src/plugin_api/style.rs | 3 |
6 files changed, 137 insertions, 24 deletions
diff --git a/zellij-utils/src/data.rs b/zellij-utils/src/data.rs index aa455b35..218db216 100644 --- a/zellij-utils/src/data.rs +++ b/zellij-utils/src/data.rs @@ -1413,6 +1413,32 @@ impl std::str::FromStr for PaneTitleAlignment { } } +/// Inner padding between a pane's frame and its content, per side. +/// +/// Configured CSS-shorthand style, but note the 4-value order is +/// **up down left right** (as requested), NOT the CSS clockwise +/// top-right-bottom-left. See the KDL parser for the accepted forms. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default, Serialize, Deserialize)] +pub struct PanePadding { + pub top: usize, + pub bottom: usize, + pub left: usize, + pub right: usize, +} + +impl PanePadding { + pub fn uniform(n: usize) -> Self { + Self { top: n, bottom: n, left: n, right: n } + } + /// vertical (up/down) and horizontal (left/right) + pub fn axes(v: usize, h: usize) -> Self { + Self { top: v, bottom: v, left: h, right: h } + } + pub fn is_zero(&self) -> bool { + self.top == 0 && self.bottom == 0 && self.left == 0 && self.right == 0 + } +} + #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default, Serialize, Deserialize)] pub struct Style { pub colors: Styling, @@ -1436,11 +1462,9 @@ pub struct Style { /// an equal distance. See FrameConfig::gap_aspect. pub pane_gap_x: usize, pub pane_gap_y: usize, - /// INNER padding: cells between a pane's frame and its content. Purely - /// cosmetic breathing room inside the pane; does not move the frame. - /// Also aspect-corrected. - pub pane_padding_x: usize, - pub pane_padding_y: usize, + /// INNER padding: cells between a pane's frame and its content, per side. + /// Purely cosmetic breathing room inside the pane; does not move the frame. + pub pane_padding: PanePadding, } #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)] diff --git a/zellij-utils/src/input/config.rs b/zellij-utils/src/input/config.rs index be08adb9..db86de54 100644 --- a/zellij-utils/src/input/config.rs +++ b/zellij-utils/src/input/config.rs @@ -1393,6 +1393,51 @@ mod config_test { } #[test] + fn pane_padding_accepts_1_2_and_4_value_shorthand() { + let parse = |padding: &str| { + let contents = format!( + "ui {{\n pane_frames {{\n padding {}\n }}\n}}", + padding + ); + Config::from_kdl(&contents, None).map(|c| c.ui.pane_frames.padding) + }; + + // 1 value: all sides, and left/right are aspect-corrected (x2 default) + // so that `padding 1` reads as square on a non-square cell grid + let p = parse("1").unwrap(); + assert_eq!((p.top, p.bottom), (1, 1), "1-value: vertical"); + assert_eq!((p.left, p.right), (2, 2), "1-value: horizontal is aspect-corrected"); + + // 2 values: up/down then left/right, taken LITERALLY (no aspect scaling - + // the caller is already controlling the horizontal size) + let p = parse("1 2").unwrap(); + assert_eq!((p.top, p.bottom, p.left, p.right), (1, 1, 2, 2), "2-value"); + + // 4 values: up down left right (NOT css clockwise order) + let p = parse("1 2 3 4").unwrap(); + assert_eq!((p.top, p.bottom, p.left, p.right), (1, 2, 3, 4), "4-value"); + + // 3 and 5 are rejected rather than silently reinterpreted + assert!(parse("1 2 3").is_err(), "3 values must be rejected"); + assert!(parse("1 2 3 4 5").is_err(), "5 values must be rejected"); + + // negatives clamp to 0 instead of wrapping to a huge usize + let p = parse("-4").unwrap(); + assert!(p.is_zero(), "negative padding must clamp to zero, got {:?}", p); + } + + #[test] + fn pane_padding_aspect_is_configurable() { + let contents = "ui {\n pane_frames {\n gap_aspect 1\n padding 1\n }\n}"; + let p = Config::from_kdl(contents, None).unwrap().ui.pane_frames.padding; + assert_eq!( + (p.top, p.left), + (1, 1), + "gap_aspect 1 disables horizontal correction" + ); + } + + #[test] fn can_define_ui_configuration_in_configfile() { let config_contents = r#" ui { diff --git a/zellij-utils/src/input/theme.rs b/zellij-utils/src/input/theme.rs index f4f2ec80..f1e0adf1 100644 --- a/zellij-utils/src/input/theme.rs +++ b/zellij-utils/src/input/theme.rs @@ -30,8 +30,9 @@ pub struct FrameConfig { /// outer gap between panes, expressed in ROWS (see Style::pane_gap_y). /// Columns are scaled by `gap_aspect` so the gap looks square. pub gap: usize, - /// inner padding between a pane's frame and its content, in ROWS - pub padding: usize, + /// inner padding between a pane's frame and its content, per side. + /// Already resolved at parse time (including any aspect correction). + pub padding: crate::data::PanePadding, /// Columns per row. Terminal cells are roughly twice as tall as they are /// wide, so 1 row of gap needs ~2 columns to look like the same distance. /// 0 is treated as 1 (no correction). @@ -49,7 +50,7 @@ impl Default for FrameConfig { hide_session_name: false, title_alignment: Default::default(), gap: 0, - padding: 0, + padding: Default::default(), gap_aspect: 2, } } @@ -63,9 +64,8 @@ impl FrameConfig { pub fn resolved_gap(&self) -> (usize, usize) { (self.gap * self.aspect(), self.gap) } - /// (columns, rows) of inner padding - pub fn resolved_padding(&self) -> (usize, usize) { - (self.padding * self.aspect(), self.padding) + pub fn resolved_padding(&self) -> crate::data::PanePadding { + self.padding } } diff --git a/zellij-utils/src/kdl/mod.rs b/zellij-utils/src/kdl/mod.rs index c2500ebb..b5670195 100644 --- a/zellij-utils/src/kdl/mod.rs +++ b/zellij-utils/src/kdl/mod.rs @@ -3,7 +3,7 @@ use crate::data::{ BareKey, Direction, FloatingPaneCoordinates, InputMode, KeyWithModifier, LayoutInfo, LayoutMetadata, MultiplayerColors, Palette, PaletteColor, PaneId, PaneInfo, PaneManifest, PermissionType, Resize, SessionInfo, StyleDeclaration, Styling, TabInfo, WebSharing, - DEFAULT_STYLES, + DEFAULT_STYLES, PanePadding, }; use crate::envs::EnvironmentVariables; use crate::home::{find_default_config_dir, get_layout_dir}; @@ -5548,16 +5548,53 @@ impl UiConfig { }; // negative values are meaningless here and would wrap on `as usize`, // so clamp at 0 rather than silently producing a gigantic inset - let gap = kdl_children_property_first_arg_as_i64!(pane_frames, "gap") - .unwrap_or(0) - .max(0) as usize; - let padding = kdl_children_property_first_arg_as_i64!(pane_frames, "padding") - .unwrap_or(0) - .max(0) as usize; // default 2: terminal cells are ~2x taller than wide let gap_aspect = kdl_children_property_first_arg_as_i64!(pane_frames, "gap_aspect") .unwrap_or(2) .max(1) as usize; + let gap = kdl_children_property_first_arg_as_i64!(pane_frames, "gap") + .unwrap_or(0) + .max(0) as usize; + // `padding` accepts CSS-style shorthand, but note the 4-value order + // is UP DOWN LEFT RIGHT (not CSS's clockwise top-right-bottom-left): + // padding 1 -> all four sides + // padding 1 2 -> up/down = 1, left/right = 2 + // padding 1 2 3 4 -> up=1 down=2 left=3 right=4 + // + // ASPECT: the single-value form is aspect-corrected (left/right are + // multiplied by gap_aspect) so `padding 1` looks square. The 2- and + // 4-value forms are taken literally, because specifying axes + // separately means you are already controlling the horizontal size + // and silently doubling it would be surprising. + let padding = match pane_frames.children().and_then(|c| c.get("padding")) { + Some(node) => { + let values: Vec<i64> = kdl_entries_as_i64!(node).flatten().collect(); + let clamp = |v: i64| v.max(0) as usize; + match values.len() { + 0 => Default::default(), + 1 => PanePadding::axes(clamp(values[0]), clamp(values[0]) * gap_aspect), + 2 => PanePadding::axes(clamp(values[0]), clamp(values[1])), + 4 => PanePadding { + top: clamp(values[0]), + bottom: clamp(values[1]), + left: clamp(values[2]), + right: clamp(values[3]), + }, + n => { + return Err(ConfigError::new_kdl_error( + format!( + "padding takes 1, 2 or 4 values (got {}): \ +`padding <all>`, `padding <up/down> <left/right>`, or `padding <up> <down> <left> <right>`", + n + ), + node.span().offset(), + node.span().len(), + )) + }, + } + }, + None => Default::default(), + }; let frame_config = FrameConfig { rounded_corners, hide_session_name, @@ -5607,10 +5644,19 @@ impl UiConfig { gap.push(KdlValue::Base10(self.pane_frames.gap as i64)); frame_config_children.nodes_mut().push(gap); } - if self.pane_frames.padding > 0 { + if !self.pane_frames.padding.is_zero() { has_ui_config = true; + // always the explicit 4-value form: the 1-value form would be + // re-multiplied by gap_aspect when read back let mut padding = KdlNode::new("padding"); - padding.push(KdlValue::Base10(self.pane_frames.padding as i64)); + for v in [ + self.pane_frames.padding.top, + self.pane_frames.padding.bottom, + self.pane_frames.padding.left, + self.pane_frames.padding.right, + ] { + padding.push(KdlValue::Base10(v as i64)); + } frame_config_children.nodes_mut().push(padding); } if self.pane_frames.gap_aspect != 2 { diff --git a/zellij-utils/src/plugin_api/event.rs b/zellij-utils/src/plugin_api/event.rs index 87af043e..7bb1c7ac 100644 --- a/zellij-utils/src/plugin_api/event.rs +++ b/zellij-utils/src/plugin_api/event.rs @@ -2495,8 +2495,7 @@ fn serialize_mode_update_event_with_non_default_values() { pane_title_alignment: Default::default(), pane_gap_x: Default::default(), pane_gap_y: Default::default(), - pane_padding_x: Default::default(), - pane_padding_y: Default::default(), + pane_padding: Default::default(), }, capabilities: PluginCapabilities { arrow_fonts: false }, session_name: Some("my awesome test session".to_owned()), diff --git a/zellij-utils/src/plugin_api/style.rs b/zellij-utils/src/plugin_api/style.rs index 266ab7f8..527f9841 100644 --- a/zellij-utils/src/plugin_api/style.rs +++ b/zellij-utils/src/plugin_api/style.rs @@ -28,8 +28,7 @@ impl TryFrom<ProtobufStyle> for Style { pane_title_alignment: Default::default(), pane_gap_x: Default::default(), pane_gap_y: Default::default(), - pane_padding_x: Default::default(), - pane_padding_y: Default::default(), + pane_padding: Default::default(), }) } } |
