From b10ccb8b6f7c90a1942d60a728dc9bcc3cc40fd0 Mon Sep 17 00:00:00 2001 From: Zykino Date: Fri, 5 Apr 2024 15:02:36 +0200 Subject: feat(plugins): format UI components API (#3193) * feat: Add serialization methods to ui components * Revert: do not modify the `print` method at all --------- Co-authored-by: Zykino <3809938+Zykino@users.noreply.github.com> --- zellij-tile/src/ui_components/nested_list.rs | 38 +++++++++++++++++++ zellij-tile/src/ui_components/ribbon.rs | 57 ++++++++++++++++++++++++++++ zellij-tile/src/ui_components/table.rs | 23 +++++++++++ zellij-tile/src/ui_components/text.rs | 23 +++++++++++ 4 files changed, 141 insertions(+) diff --git a/zellij-tile/src/ui_components/nested_list.rs b/zellij-tile/src/ui_components/nested_list.rs index 15bb890c5..442c27fb6 100644 --- a/zellij-tile/src/ui_components/nested_list.rs +++ b/zellij-tile/src/ui_components/nested_list.rs @@ -1,4 +1,5 @@ use super::Text; +use std::borrow::Borrow; use std::ops::RangeBounds; #[derive(Debug, Default, Clone)] @@ -71,3 +72,40 @@ pub fn print_nested_list_with_coordinates( x, y, width, height, items ) } + +pub fn serialize_nested_list(items: I) -> String +where + I: IntoIterator, + I::Item: Borrow, +{ + let items = items + .into_iter() + .map(|i| i.borrow().serialize()) + .collect::>() + .join(";"); + format!("\u{1b}Pznested_list;{}\u{1b}\\", items) +} + +pub fn serialize_nested_list_with_coordinates( + items: I, + x: usize, + y: usize, + width: Option, + height: Option, +) -> String +where + I: IntoIterator, + I::Item: Borrow, +{ + let width = width.map(|w| w.to_string()).unwrap_or_default(); + let height = height.map(|h| h.to_string()).unwrap_or_default(); + let items = items + .into_iter() + .map(|i| i.borrow().serialize()) + .collect::>() + .join(";"); + format!( + "\u{1b}Pznested_list;{}/{}/{}/{};{}\u{1b}\\", + x, y, width, height, items + ) +} diff --git a/zellij-tile/src/ui_components/ribbon.rs b/zellij-tile/src/ui_components/ribbon.rs index c0c1880e4..f1922fa74 100644 --- a/zellij-tile/src/ui_components/ribbon.rs +++ b/zellij-tile/src/ui_components/ribbon.rs @@ -1,4 +1,5 @@ use super::Text; +use std::borrow::Borrow; pub fn print_ribbon(text: Text) { print!("\u{1b}Pzribbon;{}\u{1b}\\", text.serialize()); @@ -22,3 +23,59 @@ pub fn print_ribbon_with_coordinates( text.serialize() ); } + +pub fn serialize_ribbon(text: &Text) -> String { + format!("\u{1b}Pzribbon;{}\u{1b}\\", text.serialize()) +} + +pub fn serialize_ribbon_with_coordinates( + text: &Text, + x: usize, + y: usize, + width: Option, + height: Option, +) -> String { + let width = width.map(|w| w.to_string()).unwrap_or_default(); + let height = height.map(|h| h.to_string()).unwrap_or_default(); + + format!( + "\u{1b}Pzribbon;{}/{}/{}/{};{}\u{1b}\\", + x, + y, + width, + height, + text.serialize() + ) +} + +pub fn serialize_ribbon_line(ribbons: I) -> String +where + I: IntoIterator, + I::Item: Borrow, +{ + ribbons + .into_iter() + .map(|r| serialize_ribbon(r.borrow())) + .collect() +} + +pub fn serialize_ribbon_line_with_coordinates( + ribbons: I, + x: usize, + y: usize, + width: Option, + height: Option, +) -> String +where + I: IntoIterator, + I::Item: Borrow, +{ + let mut ribbons = ribbons.into_iter(); + let Some(first) = ribbons.next() else { + return String::new(); + }; + + let mut result = serialize_ribbon_with_coordinates(first.borrow(), x, y, width, height); + result.push_str(&serialize_ribbon_line(ribbons)); + result +} diff --git a/zellij-tile/src/ui_components/table.rs b/zellij-tile/src/ui_components/table.rs index df1b833bf..5ef07d7a3 100644 --- a/zellij-tile/src/ui_components/table.rs +++ b/zellij-tile/src/ui_components/table.rs @@ -59,3 +59,26 @@ pub fn print_table_with_coordinates( table.serialize() ) } + +pub fn serialize_table(table: &Table) -> String { + format!("\u{1b}Pztable;{}", table.serialize()) +} + +pub fn serialize_table_with_coordinates( + table: &Table, + x: usize, + y: usize, + width: Option, + height: Option, +) -> String { + let width = width.map(|w| w.to_string()).unwrap_or_default(); + let height = height.map(|h| h.to_string()).unwrap_or_default(); + format!( + "\u{1b}Pztable;{}/{}/{}/{};{}\u{1b}\\", + x, + y, + width, + height, + table.serialize() + ) +} diff --git a/zellij-tile/src/ui_components/text.rs b/zellij-tile/src/ui_components/text.rs index e9ae25aaa..da934dd43 100644 --- a/zellij-tile/src/ui_components/text.rs +++ b/zellij-tile/src/ui_components/text.rs @@ -105,3 +105,26 @@ pub fn print_text_with_coordinates( text.serialize() ) } + +pub fn serialize_text(text: &Text) -> String { + format!("\u{1b}Pztext;{}\u{1b}\\", text.serialize()) +} + +pub fn serialize_text_with_coordinates( + text: &Text, + x: usize, + y: usize, + width: Option, + height: Option, +) -> String { + let width = width.map(|w| w.to_string()).unwrap_or_default(); + let height = height.map(|h| h.to_string()).unwrap_or_default(); + format!( + "\u{1b}Pztext;{}/{}/{}/{};{}\u{1b}\\", + x, + y, + width, + height, + text.serialize() + ) +} -- cgit v1.2.3