zellij_tile/ui_components/
nested_list.rs1use super::Text;
2use std::borrow::Borrow;
3use std::ops::RangeBounds;
4
5#[derive(Debug, Default, Clone)]
6pub struct NestedListItem {
7 indentation_level: usize,
8 content: Text,
9}
10
11impl NestedListItem {
12 pub fn new<S: AsRef<str>>(text: S) -> Self
13 where
14 S: ToString,
15 {
16 NestedListItem {
17 content: Text::new(text),
18 ..Default::default()
19 }
20 }
21 pub fn indent(mut self, indentation_level: usize) -> Self {
22 self.indentation_level = indentation_level;
23 self
24 }
25 pub fn selected(mut self) -> Self {
26 self.content = self.content.selected();
27 self
28 }
29 pub fn opaque(mut self) -> Self {
30 self.content = self.content.opaque();
31 self
32 }
33 pub fn color_indices(mut self, index_level: usize, indices: Vec<usize>) -> Self {
34 self.content = self.content.color_indices(index_level, indices);
35 self
36 }
37 pub fn color_range<R: RangeBounds<usize>>(mut self, index_level: usize, indices: R) -> Self {
38 self.content = self.content.color_range(index_level, indices);
39 self
40 }
41 pub fn color_substring<S: AsRef<str>>(mut self, index_level: usize, substr: S) -> Self {
42 self.content = self.content.color_substring(index_level, substr);
43 self
44 }
45 pub fn color_nth_substring<S: AsRef<str>>(
46 mut self,
47 index_level: usize,
48 substr: S,
49 occurrence_index: usize,
50 ) -> Self {
51 self.content = self
52 .content
53 .color_nth_substring(index_level, substr, occurrence_index);
54 self
55 }
56 pub fn color_last_substring<S: AsRef<str>>(mut self, index_level: usize, substr: S) -> Self {
57 self.content = self.content.color_last_substring(index_level, substr);
58 self
59 }
60 pub fn color_all(mut self, index_level: usize) -> Self {
61 self.content = self.content.color_all(index_level);
62 self
63 }
64 pub fn error_color_indices(mut self, indices: Vec<usize>) -> Self {
65 self.content = self.content.error_color_indices(indices);
66 self
67 }
68 pub fn error_color_range<R: RangeBounds<usize>>(mut self, indices: R) -> Self {
69 self.content = self.content.error_color_range(indices);
70 self
71 }
72 pub fn error_color_substring<S: AsRef<str>>(mut self, substr: S) -> Self {
73 self.content = self.content.error_color_substring(substr);
74 self
75 }
76 pub fn error_color_nth_substring<S: AsRef<str>>(
77 mut self,
78 substr: S,
79 occurrence_index: usize,
80 ) -> Self {
81 self.content = self
82 .content
83 .error_color_nth_substring(substr, occurrence_index);
84 self
85 }
86 pub fn error_color_last_substring<S: AsRef<str>>(mut self, substr: S) -> Self {
87 self.content = self.content.error_color_last_substring(substr);
88 self
89 }
90 pub fn error_color_all(mut self) -> Self {
91 self.content = self.content.error_color_all();
92 self
93 }
94 pub fn success_color_indices(mut self, indices: Vec<usize>) -> Self {
95 self.content = self.content.success_color_indices(indices);
96 self
97 }
98 pub fn success_color_range<R: RangeBounds<usize>>(mut self, indices: R) -> Self {
99 self.content = self.content.success_color_range(indices);
100 self
101 }
102 pub fn success_color_substring<S: AsRef<str>>(mut self, substr: S) -> Self {
103 self.content = self.content.success_color_substring(substr);
104 self
105 }
106 pub fn success_color_nth_substring<S: AsRef<str>>(
107 mut self,
108 substr: S,
109 occurrence_index: usize,
110 ) -> Self {
111 self.content = self
112 .content
113 .success_color_nth_substring(substr, occurrence_index);
114 self
115 }
116 pub fn success_color_last_substring<S: AsRef<str>>(mut self, substr: S) -> Self {
117 self.content = self.content.success_color_last_substring(substr);
118 self
119 }
120 pub fn success_color_all(mut self) -> Self {
121 self.content = self.content.success_color_all();
122 self
123 }
124 pub fn serialize(&self) -> String {
125 let mut serialized = String::new();
126 for _ in 0..self.indentation_level {
127 serialized.push('|');
128 }
129 format!("{}{}", serialized, self.content.serialize())
130 }
131}
132
133pub fn print_nested_list(items: Vec<NestedListItem>) {
135 let items = items
136 .into_iter()
137 .map(|i| i.serialize())
138 .collect::<Vec<_>>()
139 .join(";");
140 print!("\u{1b}Pznested_list;{}\u{1b}\\", items)
141}
142
143pub fn print_nested_list_with_coordinates(
144 items: Vec<NestedListItem>,
145 x: usize,
146 y: usize,
147 width: Option<usize>,
148 height: Option<usize>,
149) {
150 let width = width.map(|w| w.to_string()).unwrap_or_default();
151 let height = height.map(|h| h.to_string()).unwrap_or_default();
152 let items = items
153 .into_iter()
154 .map(|i| i.serialize())
155 .collect::<Vec<_>>()
156 .join(";");
157 print!(
158 "\u{1b}Pznested_list;{}/{}/{}/{};{}\u{1b}\\",
159 x, y, width, height, items
160 )
161}
162
163pub fn serialize_nested_list<I>(items: I) -> String
164where
165 I: IntoIterator,
166 I::Item: Borrow<NestedListItem>,
167{
168 let items = items
169 .into_iter()
170 .map(|i| i.borrow().serialize())
171 .collect::<Vec<_>>()
172 .join(";");
173 format!("\u{1b}Pznested_list;{}\u{1b}\\", items)
174}
175
176pub fn serialize_nested_list_with_coordinates<I>(
177 items: I,
178 x: usize,
179 y: usize,
180 width: Option<usize>,
181 height: Option<usize>,
182) -> String
183where
184 I: IntoIterator,
185 I::Item: Borrow<NestedListItem>,
186{
187 let width = width.map(|w| w.to_string()).unwrap_or_default();
188 let height = height.map(|h| h.to_string()).unwrap_or_default();
189 let items = items
190 .into_iter()
191 .map(|i| i.borrow().serialize())
192 .collect::<Vec<_>>()
193 .join(";");
194 format!(
195 "\u{1b}Pznested_list;{}/{}/{}/{};{}\u{1b}\\",
196 x, y, width, height, items
197 )
198}