zellij-tile 0.44.2

A small client-side library for writing Zellij plugins
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
use std::ops::Bound;
use std::ops::RangeBounds;

#[derive(Debug, Default, Clone)]
pub struct Text {
    text: String,
    selected: bool,
    opaque: bool,
    indices: Vec<Vec<usize>>,
}

impl Text {
    pub fn new<S: AsRef<str>>(content: S) -> Self
    where
        S: ToString,
    {
        Text {
            text: content.to_string(),
            selected: false,
            opaque: false,
            indices: vec![],
        }
    }
    pub fn selected(mut self) -> Self {
        self.selected = true;
        self
    }
    pub fn opaque(mut self) -> Self {
        self.opaque = true;
        self
    }
    pub fn dim_indices(mut self, mut indices: Vec<usize>) -> Self {
        const DIM_LEVEL: usize = 4;
        self.pad_indices(DIM_LEVEL);
        self.indices
            .get_mut(DIM_LEVEL)
            .map(|i| i.append(&mut indices));
        self
    }
    pub fn dim_range<R: RangeBounds<usize>>(mut self, indices: R) -> Self {
        const DIM_LEVEL: usize = 4;
        self.pad_indices(DIM_LEVEL);
        let start = match indices.start_bound() {
            Bound::Unbounded => 0,
            Bound::Included(s) => *s,
            Bound::Excluded(s) => *s,
        };
        let end = match indices.end_bound() {
            Bound::Unbounded => self.text.chars().count(),
            Bound::Included(s) => *s + 1,
            Bound::Excluded(s) => *s,
        };
        let indices = (start..end).into_iter();
        self.indices
            .get_mut(DIM_LEVEL)
            .map(|i| i.append(&mut indices.into_iter().collect()));
        self
    }
    pub fn dim_substring<S: AsRef<str>>(mut self, substr: S) -> Self {
        let substr = substr.as_ref();
        let mut start = 0;

        while let Some(pos) = self.text[start..].find(substr) {
            let abs_pos = start + pos;
            self = self.dim_range(abs_pos..abs_pos + substr.chars().count());
            start = abs_pos + substr.len();
        }

        self
    }
    pub fn dim_all(self) -> Self {
        const DIM_LEVEL: usize = 4;
        self.color_range(DIM_LEVEL, ..)
    }
    pub fn unbold_indices(mut self, mut indices: Vec<usize>) -> Self {
        const UNBOLD_LEVEL: usize = 5;
        self.pad_indices(UNBOLD_LEVEL);
        self.indices
            .get_mut(UNBOLD_LEVEL)
            .map(|i| i.append(&mut indices));
        self
    }
    pub fn unbold_range<R: RangeBounds<usize>>(mut self, indices: R) -> Self {
        const UNBOLD_LEVEL: usize = 5;
        self.pad_indices(UNBOLD_LEVEL);
        let start = match indices.start_bound() {
            Bound::Unbounded => 0,
            Bound::Included(s) => *s,
            Bound::Excluded(s) => *s,
        };
        let end = match indices.end_bound() {
            Bound::Unbounded => self.text.chars().count(),
            Bound::Included(s) => *s + 1,
            Bound::Excluded(s) => *s,
        };
        let indices = (start..end).into_iter();
        self.indices
            .get_mut(UNBOLD_LEVEL)
            .map(|i| i.append(&mut indices.into_iter().collect()));
        self
    }
    pub fn unbold_substring<S: AsRef<str>>(mut self, substr: S) -> Self {
        let substr = substr.as_ref();
        let mut start = 0;

        while let Some(pos) = self.text[start..].find(substr) {
            let abs_pos = start + pos;
            self = self.unbold_range(abs_pos..abs_pos + substr.chars().count());
            start = abs_pos + substr.len();
        }

        self
    }
    pub fn unbold_all(self) -> Self {
        const UNBOLD_LEVEL: usize = 5;
        self.color_range(UNBOLD_LEVEL, ..)
    }
    pub fn error_color_indices(mut self, mut indices: Vec<usize>) -> Self {
        const ERROR_COLOR_LEVEL: usize = 6;
        self.pad_indices(ERROR_COLOR_LEVEL);
        self.indices
            .get_mut(ERROR_COLOR_LEVEL)
            .map(|i| i.append(&mut indices));
        self
    }
    pub fn error_color_range<R: RangeBounds<usize>>(mut self, indices: R) -> Self {
        const ERROR_COLOR_LEVEL: usize = 6;
        self.pad_indices(ERROR_COLOR_LEVEL);
        let start = match indices.start_bound() {
            Bound::Unbounded => 0,
            Bound::Included(s) => *s,
            Bound::Excluded(s) => *s,
        };
        let end = match indices.end_bound() {
            Bound::Unbounded => self.text.chars().count(),
            Bound::Included(s) => *s + 1,
            Bound::Excluded(s) => *s,
        };
        let indices = (start..end).into_iter();
        self.indices
            .get_mut(ERROR_COLOR_LEVEL)
            .map(|i| i.append(&mut indices.into_iter().collect()));
        self
    }
    pub fn error_color_substring<S: AsRef<str>>(mut self, substr: S) -> Self {
        let substr = substr.as_ref();
        let mut start = 0;

        while let Some(pos) = self.text[start..].find(substr) {
            let abs_pos = start + pos;
            self = self.error_color_range(abs_pos..abs_pos + substr.chars().count());
            start = abs_pos + substr.len();
        }

        self
    }
    pub fn error_color_nth_substring<S: AsRef<str>>(
        self,
        substr: S,
        occurrence_index: usize,
    ) -> Self {
        const ERROR_COLOR_LEVEL: usize = 6;
        let substr = substr.as_ref();
        let mut start = 0;
        let mut count = 0;

        while let Some(pos) = self.text[start..].find(substr) {
            if count == occurrence_index {
                let abs_pos = start + pos;
                return self.color_range(ERROR_COLOR_LEVEL, abs_pos..abs_pos + substr.len());
            }
            count += 1;
            start = start + pos + substr.len();
        }

        self
    }

    pub fn error_color_last_substring<S: AsRef<str>>(self, substr: S) -> Self {
        const ERROR_COLOR_LEVEL: usize = 6;
        let substr = substr.as_ref();
        let mut start = 0;
        let mut last_pos = None;

        while let Some(pos) = self.text[start..].find(substr) {
            last_pos = Some(start + pos);
            start = start + pos + substr.len();
        }

        if let Some(abs_pos) = last_pos {
            return self.color_range(ERROR_COLOR_LEVEL, abs_pos..abs_pos + substr.len());
        }
        self
    }

    pub fn error_color_all(self) -> Self {
        const ERROR_COLOR_LEVEL: usize = 6;
        self.color_range(ERROR_COLOR_LEVEL, ..)
    }
    pub fn success_color_indices(mut self, mut indices: Vec<usize>) -> Self {
        const SUCCESS_COLOR_LEVEL: usize = 7;
        self.pad_indices(SUCCESS_COLOR_LEVEL);
        self.indices
            .get_mut(SUCCESS_COLOR_LEVEL)
            .map(|i| i.append(&mut indices));
        self
    }
    pub fn success_color_range<R: RangeBounds<usize>>(mut self, indices: R) -> Self {
        const SUCCESS_COLOR_LEVEL: usize = 7;
        self.pad_indices(SUCCESS_COLOR_LEVEL);
        let start = match indices.start_bound() {
            Bound::Unbounded => 0,
            Bound::Included(s) => *s,
            Bound::Excluded(s) => *s,
        };
        let end = match indices.end_bound() {
            Bound::Unbounded => self.text.chars().count(),
            Bound::Included(s) => *s + 1,
            Bound::Excluded(s) => *s,
        };
        let indices = (start..end).into_iter();
        self.indices
            .get_mut(SUCCESS_COLOR_LEVEL)
            .map(|i| i.append(&mut indices.into_iter().collect()));
        self
    }
    pub fn success_color_substring<S: AsRef<str>>(mut self, substr: S) -> Self {
        let substr = substr.as_ref();
        let mut start = 0;

        while let Some(pos) = self.text[start..].find(substr) {
            let abs_pos = start + pos;
            self = self.success_color_range(abs_pos..abs_pos + substr.chars().count());
            start = abs_pos + substr.len();
        }

        self
    }
    pub fn success_color_nth_substring<S: AsRef<str>>(
        self,
        substr: S,
        occurrence_index: usize,
    ) -> Self {
        const SUCCESS_COLOR_LEVEL: usize = 7;
        let substr = substr.as_ref();
        let mut start = 0;
        let mut count = 0;

        while let Some(pos) = self.text[start..].find(substr) {
            if count == occurrence_index {
                let abs_pos = start + pos;
                return self.color_range(SUCCESS_COLOR_LEVEL, abs_pos..abs_pos + substr.len());
            }
            count += 1;
            start = start + pos + substr.len();
        }

        self
    }

    pub fn success_color_last_substring<S: AsRef<str>>(self, substr: S) -> Self {
        const SUCCESS_COLOR_LEVEL: usize = 7;
        let substr = substr.as_ref();
        let mut start = 0;
        let mut last_pos = None;

        while let Some(pos) = self.text[start..].find(substr) {
            last_pos = Some(start + pos);
            start = start + pos + substr.len();
        }

        if let Some(abs_pos) = last_pos {
            return self.color_range(SUCCESS_COLOR_LEVEL, abs_pos..abs_pos + substr.len());
        }
        self
    }

    pub fn success_color_all(self) -> Self {
        const SUCCESS_COLOR_LEVEL: usize = 7;
        self.color_range(SUCCESS_COLOR_LEVEL, ..)
    }
    pub fn color_indices(mut self, index_level: usize, mut indices: Vec<usize>) -> Self {
        self.pad_indices(index_level);
        self.indices
            .get_mut(index_level)
            .map(|i| i.append(&mut indices));
        self
    }
    pub fn color_range<R: RangeBounds<usize>>(mut self, index_level: usize, indices: R) -> Self {
        self.pad_indices(index_level);
        let start = match indices.start_bound() {
            Bound::Unbounded => 0,
            Bound::Included(s) => *s,
            Bound::Excluded(s) => *s,
        };
        let end = match indices.end_bound() {
            Bound::Unbounded => self.text.chars().count(),
            Bound::Included(s) => *s + 1,
            Bound::Excluded(s) => *s,
        };
        let indices = (start..end).into_iter();
        self.indices
            .get_mut(index_level)
            .map(|i| i.append(&mut indices.into_iter().collect()));
        self
    }

    pub fn color_substring<S: AsRef<str>>(mut self, index_level: usize, substr: S) -> Self {
        let substr = substr.as_ref();
        let mut start = 0;
        while let Some(pos) = self.text[start..].find(substr) {
            let abs_pos = start + pos;
            let char_start = self.text[..abs_pos].chars().count();
            let char_end = char_start + substr.chars().count();
            self = self.color_range(index_level, char_start..char_end);
            start = abs_pos + substr.len();
        }
        self
    }

    pub fn color_all(self, index_level: usize) -> Self {
        self.color_range(index_level, ..)
    }

    pub fn color_nth_substring<S: AsRef<str>>(
        self,
        index_level: usize,
        substr: S,
        occurrence_index: usize,
    ) -> Self {
        let substr = substr.as_ref();
        let mut start = 0;
        let mut count = 0;

        while let Some(pos) = self.text[start..].find(substr) {
            if count == occurrence_index {
                let abs_pos = start + pos;
                return self.color_range(index_level, abs_pos..abs_pos + substr.len());
            }
            count += 1;
            start = start + pos + substr.len();
        }

        self
    }

    pub fn color_last_substring<S: AsRef<str>>(self, index_level: usize, substr: S) -> Self {
        let substr = substr.as_ref();
        let mut start = 0;
        let mut last_pos = None;

        while let Some(pos) = self.text[start..].find(substr) {
            last_pos = Some(start + pos);
            start = start + pos + substr.len();
        }

        if let Some(abs_pos) = last_pos {
            return self.color_range(index_level, abs_pos..abs_pos + substr.len());
        }
        self
    }

    pub fn content(&self) -> &str {
        &self.text
    }
    fn pad_indices(&mut self, index_level: usize) {
        if self.indices.get(index_level).is_none() {
            for _ in self.indices.len()..=index_level {
                self.indices.push(vec![]);
            }
        }
    }
    pub fn serialize(&self) -> String {
        let text = self
            .text
            .to_string()
            .as_bytes()
            .iter()
            .map(|b| b.to_string())
            .collect::<Vec<_>>()
            .join(",");
        let mut indices = String::new();
        for index_variants in &self.indices {
            indices.push_str(&format!(
                "{}$",
                index_variants
                    .iter()
                    .map(|i| i.to_string())
                    .collect::<Vec<_>>()
                    .join(",")
            ));
        }

        let mut prefix = "".to_owned();

        if self.selected {
            prefix = format!("x{}", prefix);
        }

        if self.opaque {
            prefix = format!("z{}", prefix);
        }

        format!("{}{}{}", prefix, indices, text)
    }
    pub fn len(&self) -> usize {
        self.text.chars().count()
    }
}

pub fn print_text(text: Text) {
    print!("\u{1b}Pztext;{}\u{1b}\\", text.serialize())
}

pub fn print_text_with_coordinates(
    text: Text,
    x: usize,
    y: usize,
    width: Option<usize>,
    height: Option<usize>,
) {
    let width = width.map(|w| w.to_string()).unwrap_or_default();
    let height = height.map(|h| h.to_string()).unwrap_or_default();
    print!(
        "\u{1b}Pztext;{}/{}/{}/{};{}\u{1b}\\",
        x,
        y,
        width,
        height,
        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()
    )
}