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>
This commit is contained in:
Zykino 2024-04-05 15:02:36 +02:00 committed by GitHub
parent cf18fb3867
commit b10ccb8b6f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 141 additions and 0 deletions

View file

@ -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<I>(items: I) -> String
where
I: IntoIterator,
I::Item: Borrow<NestedListItem>,
{
let items = items
.into_iter()
.map(|i| i.borrow().serialize())
.collect::<Vec<_>>()
.join(";");
format!("\u{1b}Pznested_list;{}\u{1b}\\", items)
}
pub fn serialize_nested_list_with_coordinates<I>(
items: I,
x: usize,
y: usize,
width: Option<usize>,
height: Option<usize>,
) -> String
where
I: IntoIterator,
I::Item: Borrow<NestedListItem>,
{
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::<Vec<_>>()
.join(";");
format!(
"\u{1b}Pznested_list;{}/{}/{}/{};{}\u{1b}\\",
x, y, width, height, items
)
}

View file

@ -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<usize>,
height: Option<usize>,
) -> 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<I>(ribbons: I) -> String
where
I: IntoIterator,
I::Item: Borrow<Text>,
{
ribbons
.into_iter()
.map(|r| serialize_ribbon(r.borrow()))
.collect()
}
pub fn serialize_ribbon_line_with_coordinates<I>(
ribbons: I,
x: usize,
y: usize,
width: Option<usize>,
height: Option<usize>,
) -> String
where
I: IntoIterator,
I::Item: Borrow<Text>,
{
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
}

View file

@ -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<usize>,
height: Option<usize>,
) -> 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()
)
}

View file

@ -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<usize>,
height: Option<usize>,
) -> 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()
)
}