tslime 0.1.2

A lightweight terminal screensaver simulating slime mold growth patterns
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
//! Layout-aware position tracking for overlays.
//!
//! Replaces fragile hardcoded row indices with semantic content identifiers.

/// Identifies specific content elements within an overlay layout.
///
/// Used to look up where content lands in an overlay (e.g. for rich-text
/// coloring) without hardcoding row indices.
///
/// # Example
/// ```
/// use tslime::overlay::{OverlayLayout, ContentId};
///
/// let mut layout = OverlayLayout::new();
/// layout.add_content(ContentId::LightnessSlider);
/// layout.add_content(ContentId::ChromaSlider);
///
/// // Later, get the actual row index:
/// let l_row = layout.row_of(ContentId::LightnessSlider).unwrap();
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ContentId {
    // Palette Editor Content
    /// Stop selector row (diamond indicators for palette colors).
    StopSelector,
    /// Label row for color stops ("1 2 3 ... 11 ALL").
    StopLabels,
    /// Lightness slider row.
    LightnessSlider,
    /// Chroma slider row.
    ChromaSlider,
    /// Hue slider row.
    HueSlider,
    /// First hint row (arrow key indicators).
    HintArrows,
    /// Second hint row (adjust indicators).
    HintAdjust,
    /// Third hint row (Tab navigation).
    HintTab,
    /// Gradient preview strip row.
    GradientStrip,
    /// Color info row.
    ColorInfo,

    // Controls Overlay Content
    /// Tab indicator row (●/○ markers).
    TabIndicators,
    /// Tab label row (SIM, ENV, APP, etc.).
    TabLabels,
    /// Parameter row for sensor angle.
    SensorAngleRow,
    /// Parameter row for sensor distance.
    SensorDistanceRow,
    /// Parameter row for turn angle.
    TurnAngleRow,
    /// Parameter row for step size.
    StepSizeRow,
    /// Parameter row for decay factor.
    DecayRow,
    /// Parameter row for deposit amount.
    DepositRow,
    /// Parameter row for time scale.
    TimeScaleRow,
    /// Footer row with key hints.
    FooterHints,

    // Dashboard Content
    /// FPS row with progress bar.
    FpsRow,
    /// Trail usage row.
    TrailRow,
    /// Entropy row.
    EntropyRow,
    /// CPU usage row.
    CpuRow,
    /// Memory usage row.
    MemoryRow,

    // Generic Content
    /// Title row.
    Title,
    /// Separator line.
    Separator,
    /// Empty spacer row.
    Empty,
    /// Custom content with a string identifier.
    Custom(&'static str),
}

/// Types of rows in an overlay layout.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RowType {
    /// Empty spacer row.
    Empty,
    /// Row containing identifiable content.
    Content(ContentId),
    /// Separator line.
    Separator,
    /// Title row.
    Title,
}

/// Tracks the layout of an overlay with semantic content identifiers.
///
/// Maps content identifiers to their actual row indices, so overlays can
/// reference content by meaning rather than position.
///
/// # Example
/// ```
/// use tslime::overlay::{OverlayLayout, ContentId};
///
/// let mut layout = OverlayLayout::new();
///
/// // Build the layout
/// layout.add_empty();
/// layout.add_title();
/// layout.add_empty();
/// let slider_row = layout.add_content(ContentId::LightnessSlider);
/// layout.add_content(ContentId::ChromaSlider);
///
/// // Query the layout
/// assert_eq!(layout.row_of(ContentId::LightnessSlider), Some(slider_row));
/// assert_eq!(layout.row_count(), 5);
/// ```
#[derive(Debug, Clone)]
pub struct OverlayLayout {
    rows: Vec<RowType>,
}

impl Default for OverlayLayout {
    fn default() -> Self {
        Self::new()
    }
}

impl OverlayLayout {
    /// Creates a new empty layout.
    pub fn new() -> Self {
        Self { rows: Vec::new() }
    }

    /// Adds an empty spacer row and returns its index.
    pub fn add_empty(&mut self) -> usize {
        let idx = self.rows.len();
        self.rows.push(RowType::Empty);
        idx
    }

    /// Adds a title row and returns its index.
    pub fn add_title(&mut self) -> usize {
        let idx = self.rows.len();
        self.rows.push(RowType::Title);
        idx
    }

    /// Adds a separator row and returns its index.
    pub fn add_separator(&mut self) -> usize {
        let idx = self.rows.len();
        self.rows.push(RowType::Separator);
        idx
    }

    /// Adds a content row with the given identifier and returns its index.
    ///
    /// # Example
    /// ```
    /// use tslime::overlay::{OverlayLayout, ContentId};
    ///
    /// let mut layout = OverlayLayout::new();
    /// let slider_idx = layout.add_content(ContentId::LightnessSlider);
    /// ```
    pub fn add_content(&mut self, id: ContentId) -> usize {
        let idx = self.rows.len();
        self.rows.push(RowType::Content(id));
        idx
    }

    /// Returns the row index of the first occurrence of the given content.
    ///
    /// Returns `None` if the content is not found in the layout.
    ///
    /// # Example
    /// ```
    /// use tslime::overlay::{OverlayLayout, ContentId};
    ///
    /// let mut layout = OverlayLayout::new();
    /// layout.add_content(ContentId::LightnessSlider);
    /// layout.add_content(ContentId::ChromaSlider);
    ///
    /// assert_eq!(layout.row_of(ContentId::LightnessSlider), Some(0));
    /// assert_eq!(layout.row_of(ContentId::ChromaSlider), Some(1));
    /// assert_eq!(layout.row_of(ContentId::HueSlider), None);
    /// ```
    pub fn row_of(&self, id: ContentId) -> Option<usize> {
        self.rows
            .iter()
            .position(|row| matches!(row, RowType::Content(row_id) if *row_id == id))
    }

    /// Returns all row indices containing the given content.
    ///
    /// This is useful when the same content type appears multiple times.
    pub fn rows_of(&self, id: ContentId) -> Vec<usize> {
        self.rows
            .iter()
            .enumerate()
            .filter_map(|(idx, row)| {
                if matches!(row, RowType::Content(row_id) if *row_id == id) {
                    Some(idx)
                } else {
                    None
                }
            })
            .collect()
    }

    /// Returns the total number of rows in the layout.
    pub fn row_count(&self) -> usize {
        self.rows.len()
    }

    /// Returns the type of row at the given index.
    ///
    /// Returns `None` if the index is out of bounds.
    pub fn row_type(&self, index: usize) -> Option<RowType> {
        self.rows.get(index).copied()
    }

    /// Returns true if the layout contains the given content.
    pub fn contains(&self, id: ContentId) -> bool {
        self.row_of(id).is_some()
    }

    /// Returns an iterator over all rows.
    pub fn iter(&self) -> impl Iterator<Item = (usize, RowType)> + '_ {
        self.rows.iter().enumerate().map(|(idx, row)| (idx, *row))
    }

    /// Returns the index of the first content row matching the predicate.
    ///
    /// Unlike `row_of`, which matches a specific `ContentId`, this matches
    /// a category (e.g., any slider).
    pub fn first_of_category<F>(&self, predicate: F) -> Option<usize>
    where
        F: Fn(ContentId) -> bool,
    {
        self.rows.iter().enumerate().find_map(|(idx, row)| {
            if let RowType::Content(id) = row {
                if predicate(*id) {
                    return Some(idx);
                }
            }
            None
        })
    }
}

/// Extension trait for easier content matching.
pub trait ContentIdExt {
    /// Returns true if this is a slider content.
    fn is_slider(&self) -> bool;
    /// Returns true if this is a hint content.
    fn is_hint(&self) -> bool;
    /// Returns true if this is a parameter row.
    fn is_parameter(&self) -> bool;
}

impl ContentIdExt for ContentId {
    fn is_slider(&self) -> bool {
        matches!(
            self,
            ContentId::LightnessSlider | ContentId::ChromaSlider | ContentId::HueSlider
        )
    }

    fn is_hint(&self) -> bool {
        matches!(
            self,
            ContentId::HintArrows | ContentId::HintAdjust | ContentId::HintTab
        )
    }

    fn is_parameter(&self) -> bool {
        matches!(
            self,
            ContentId::SensorAngleRow
                | ContentId::SensorDistanceRow
                | ContentId::TurnAngleRow
                | ContentId::StepSizeRow
                | ContentId::DecayRow
                | ContentId::DepositRow
                | ContentId::TimeScaleRow
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_empty_layout() {
        let layout = OverlayLayout::new();
        assert_eq!(layout.row_count(), 0);
        assert_eq!(layout.row_of(ContentId::LightnessSlider), None);
    }

    #[test]
    fn test_add_rows() {
        let mut layout = OverlayLayout::new();

        let empty_idx = layout.add_empty();
        let title_idx = layout.add_title();
        let sep_idx = layout.add_separator();
        let content_idx = layout.add_content(ContentId::LightnessSlider);

        assert_eq!(empty_idx, 0);
        assert_eq!(title_idx, 1);
        assert_eq!(sep_idx, 2);
        assert_eq!(content_idx, 3);
        assert_eq!(layout.row_count(), 4);
    }

    #[test]
    fn test_row_lookup() {
        let mut layout = OverlayLayout::new();

        layout.add_empty();
        layout.add_content(ContentId::LightnessSlider);
        layout.add_content(ContentId::ChromaSlider);
        layout.add_empty();
        layout.add_content(ContentId::HueSlider);

        assert_eq!(layout.row_of(ContentId::LightnessSlider), Some(1));
        assert_eq!(layout.row_of(ContentId::ChromaSlider), Some(2));
        assert_eq!(layout.row_of(ContentId::HueSlider), Some(4));
        assert_eq!(layout.row_of(ContentId::StopSelector), None);
    }

    #[test]
    fn test_contains() {
        let mut layout = OverlayLayout::new();
        layout.add_content(ContentId::LightnessSlider);

        assert!(layout.contains(ContentId::LightnessSlider));
        assert!(!layout.contains(ContentId::ChromaSlider));
    }

    #[test]
    fn test_rows_of_multiple() {
        let mut layout = OverlayLayout::new();

        layout.add_content(ContentId::HintArrows);
        layout.add_content(ContentId::HintAdjust);
        layout.add_content(ContentId::HintTab);

        let hint_rows: Vec<_> = layout
            .iter()
            .filter(|(_, row)| matches!(row, RowType::Content(id) if id.is_hint()))
            .map(|(idx, _)| idx)
            .collect();

        assert_eq!(hint_rows, vec![0, 1, 2]);
    }

    #[test]
    fn test_content_id_ext() {
        assert!(ContentId::LightnessSlider.is_slider());
        assert!(ContentId::ChromaSlider.is_slider());
        assert!(ContentId::HueSlider.is_slider());
        assert!(!ContentId::StopSelector.is_slider());

        assert!(ContentId::HintArrows.is_hint());
        assert!(!ContentId::LightnessSlider.is_hint());

        assert!(ContentId::SensorAngleRow.is_parameter());
        assert!(!ContentId::LightnessSlider.is_parameter());
    }

    #[test]
    fn test_row_type_access() {
        let mut layout = OverlayLayout::new();
        layout.add_empty();
        layout.add_title();
        layout.add_separator();
        layout.add_content(ContentId::LightnessSlider);

        assert_eq!(layout.row_type(0), Some(RowType::Empty));
        assert_eq!(layout.row_type(1), Some(RowType::Title));
        assert_eq!(layout.row_type(2), Some(RowType::Separator));
        assert_eq!(
            layout.row_type(3),
            Some(RowType::Content(ContentId::LightnessSlider))
        );
        assert_eq!(layout.row_type(99), None);
    }

    #[test]
    fn test_first_of_category() {
        let mut layout = OverlayLayout::new();

        layout.add_empty();
        layout.add_content(ContentId::SensorAngleRow);
        layout.add_content(ContentId::SensorDistanceRow);
        layout.add_content(ContentId::LightnessSlider);

        let first_param = layout.first_of_category(|id| id.is_parameter());
        let first_slider = layout.first_of_category(|id| id.is_slider());

        assert_eq!(first_param, Some(1));
        assert_eq!(first_slider, Some(3));
    }
}