youtui 0.0.37

A simple TUI YouTube Music player
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
use ratatui::prelude::Rect;
use ratatui::style::Color;

// Standard app colour scheme
pub const SELECTED_BORDER_COLOUR: Color = Color::Cyan;
pub const DESELECTED_BORDER_COLOUR: Color = Color::Reset;
// TODO: Implement in all locations.
pub const TEXT_COLOUR: Color = Color::Reset;
pub const BUTTON_BG_COLOUR: Color = Color::Gray;
pub const BUTTON_FG_COLOUR: Color = Color::Black;
pub const PROGRESS_BG_COLOUR: Color = Color::DarkGray;
pub const PROGRESS_FG_COLOUR: Color = Color::LightGreen;
pub const TABLE_HEADINGS_COLOUR: Color = Color::LightGreen;
pub const ROW_HIGHLIGHT_COLOUR: Color = Color::Blue;

/// Helper function to create a popup at bottom corner of chunk.
pub fn left_bottom_corner_rect(height: u16, width: u16, r: Rect) -> Rect {
    let r_x2 = r.x + r.width;
    let r_y2 = r.y + r.height;
    let x = r_x2.saturating_sub(width).max(r.x);
    let y = r_y2.saturating_sub(height).max(r.y);
    Rect {
        x,
        y,
        width: width.min(r_x2 - x),
        height: height.min(r_y2 - y),
    }
}
/// Helper function to create a popup below a chunk.
//  We pass in the max bounds that can be rendered by the application,
//  to avoid returning a Rect that is not drawable.
// TODO: Add a test to ensure this is returning correct area
pub fn below_left_rect(height: u16, width: u16, r: Rect, max_bounds: Rect) -> Rect {
    let y = r.y + r.height - 1;
    Rect {
        x: r.x,
        y,
        width: width.min(max_bounds.right().saturating_sub(r.x)),
        height: (height.saturating_add(1)).min(max_bounds.bottom().saturating_sub(y)),
    }
}
/// Helper function to create a popup in the center of a chunk.
pub fn centered_rect(height: u16, width: u16, r: Rect) -> Rect {
    Rect {
        x: (r.x + r.width / 2).saturating_sub(width / 2).max(r.x),
        y: (r.y + r.height / 2).saturating_sub(height / 2).max(r.y),
        width: width.min(r.width),
        height: height.min(r.height),
    }
}
/// Helper function to get the bottom line of a chunk, ignoring side borders.
/// Warning: Not currently bounds checked.
pub fn bottom_of_rect(r: Rect) -> Rect {
    Rect {
        x: r.x + 1,
        y: r.y + r.height - 1,
        width: r.width - 2,
        height: 1,
    }
}
/// Helper function to get the middle line of a chunk, ignoring side borders.
/// Warning: Not currently bounds checked.
pub fn middle_of_rect(r: Rect) -> Rect {
    Rect {
        x: r.x,
        y: r.y + (r.height - 1) / 2,
        width: r.width,
        height: 1,
    }
}
/// Helper function to get `offset` of a list widget like `List` or `Table`
/// after changing the size of the list.
pub fn get_offset_after_list_resize(
    prev_offset: usize,
    prev_cur: usize,
    prev_max_cur: usize,
    new_cur: usize,
    new_max_cur: usize,
) -> usize {
    // Calculate previous offset relative to the previous cur (as a signed int),
    // defaulting to zero if any issues with cast identified.
    let prev_offset_rel_cur = isize::try_from(prev_offset)
        .map(|prev_offset| prev_offset.saturating_sub_unsigned(prev_cur))
        .unwrap_or(0);
    // Calculate previous offset relative to the previous max cur (as a signed int),
    // defaulting to zero if any issues with cast required.
    let prev_offset_rel_max = isize::try_from(prev_offset)
        .map(|prev_offset| prev_offset.saturating_sub_unsigned(prev_max_cur))
        .unwrap_or(0);
    // Adjust offset accordingly to ensure the offset relative to cur is the same as
    // it was previously.
    let Ok(new_cur_isize) = isize::try_from(new_cur) else {
        return 0;
    };
    let Ok(new_max_cur_isize) = isize::try_from(new_max_cur) else {
        return 0;
    };
    let new_offset_using_rel_cur = new_cur_isize + prev_offset_rel_cur;
    let new_offset_using_rel_max = new_max_cur_isize + prev_offset_rel_max;
    let new_offset: usize = ((new_offset_using_rel_max + new_offset_using_rel_cur) / 2)
        .try_into()
        .unwrap_or(0);
    new_offset
}

#[cfg(test)]
mod tests {
    use super::{below_left_rect, centered_rect, left_bottom_corner_rect};
    use crate::drawutils::{get_offset_after_list_resize, middle_of_rect};
    use ratatui::layout::Rect;

    #[test]
    fn test_get_offset_after_list_resize_prev_upper_list() {
        let new_offset = get_offset_after_list_resize(30, 40, 50, 10, 10);
        assert_eq!(new_offset, 0);
    }
    #[test]
    fn test_get_offset_after_list_resize_prev_lower_list() {
        let new_offset = get_offset_after_list_resize(20, 40, 40, 10, 10);
        assert_eq!(new_offset, 0);
    }
    #[test]
    fn test_get_offset_after_list_resize_prev_no_change() {
        let prev_offset = 30;
        let new_offset = get_offset_after_list_resize(prev_offset, 40, 50, 40, 50);
        assert_eq!(prev_offset, new_offset);
    }
    fn bounds_check_rect(r: Rect, max_bounds: Rect) {
        assert!(r.left() >= max_bounds.left());
        assert!(r.right() <= max_bounds.right());
        assert!(r.bottom() <= max_bounds.bottom());
        assert!(r.top() >= max_bounds.top());
    }
    #[test]
    #[should_panic]
    fn test_bounds_check_rect() {
        // TODO: Rect constructor may make this neater.
        let r1 = Rect {
            x: 0,
            y: 0,
            height: 50,
            width: 50,
        };
        let m1 = Rect {
            x: 0,
            y: 50,
            height: 50,
            width: 50,
        };
        let r2 = Rect {
            x: 30,
            y: 30,
            height: 50,
            width: 50,
        };
        let m2 = Rect {
            x: 30,
            y: 30,
            height: 51,
            width: 51,
        };
        let r3 = Rect {
            x: 30,
            y: 30,
            height: 50,
            width: 50,
        };
        let m3 = Rect {
            x: 30,
            y: 30,
            height: 51,
            width: 50,
        };
        let r4 = Rect {
            x: 30,
            y: 30,
            height: 50,
            width: 50,
        };
        let m4 = Rect {
            x: 30,
            y: 30,
            height: 50,
            width: 51,
        };
        let r5 = Rect {
            x: 30,
            y: 30,
            height: 50,
            width: 50,
        };
        let m5 = Rect {
            x: 31,
            y: 31,
            height: 50,
            width: 50,
        };
        bounds_check_rect(r1, m1);
        bounds_check_rect(r2, m2);
        bounds_check_rect(r3, m3);
        bounds_check_rect(r4, m4);
        bounds_check_rect(r5, m5);
    }
    // These don't actually do anything as they don't try to draw...
    #[test]
    fn bounds_check_left_bottom_corner_rect() {
        left_bottom_corner_rect(
            u16::MAX,
            u16::MAX,
            Rect {
                x: 0,
                y: 0,
                height: 50,
                width: 50,
            },
        );
        left_bottom_corner_rect(
            u16::MAX,
            u16::MAX,
            Rect {
                x: 0,
                y: 50,
                height: 50,
                width: 50,
            },
        );
        left_bottom_corner_rect(
            u16::MAX,
            u16::MAX,
            Rect {
                x: 50,
                y: 0,
                height: 50,
                width: 50,
            },
        );
        left_bottom_corner_rect(
            u16::MAX,
            u16::MAX,
            Rect {
                x: 50,
                y: 50,
                height: 50,
                width: 50,
            },
        );
    }

    #[test]
    fn bounds_check_centered_rect() {
        let t_r1 = Rect {
            x: 0,
            y: 0,
            height: 50,
            width: 50,
        };
        let t_r2 = Rect {
            x: 0,
            y: 50,
            height: 50,
            width: 50,
        };
        let t_r3 = Rect {
            x: 50,
            y: 0,
            height: 50,
            width: 50,
        };
        let t_r4 = Rect {
            x: 50,
            y: 50,
            height: 50,
            width: 50,
        };
        let r1 = centered_rect(u16::MAX, u16::MAX, t_r1);
        let r2 = centered_rect(u16::MAX, u16::MAX, t_r2);
        let r3 = centered_rect(u16::MAX, u16::MAX, t_r3);
        let r4 = centered_rect(u16::MAX, u16::MAX, t_r4);
        // Unsure if these are correct of there is a better way to check.
        // TODO: Add a bounds check rect function.
        bounds_check_rect(r1, t_r1);
        bounds_check_rect(r2, t_r2);
        bounds_check_rect(r3, t_r3);
        bounds_check_rect(r4, t_r4);
    }
    #[test]
    fn test_middle_of_rect() {
        let r1 = Rect {
            x: 0,
            y: 0,
            width: 10,
            height: 3,
        };
        assert_eq!(
            middle_of_rect(r1),
            Rect {
                x: 0,
                y: 1,
                width: 10,
                height: 1
            }
        );
        let r2 = Rect {
            x: 0,
            y: 0,
            width: 10,
            height: 10,
        };
        assert_eq!(
            middle_of_rect(r2),
            Rect {
                x: 0,
                y: 4,
                width: 10,
                height: 1
            }
        );
        let r3 = Rect {
            x: 0,
            y: 10,
            width: 10,
            height: 5,
        };
        assert_eq!(
            middle_of_rect(r3),
            Rect {
                x: 0,
                y: 12,
                width: 10,
                height: 1
            }
        );
    }
    #[test]
    fn bounds_check_below_left_rect() {
        // TODO: Add more / generalized test cases.
        // TODO: Check hasn't exceeded max_bounds.
        // TODO: Check has appeared where we want it to.
        below_left_rect(
            u16::MAX,
            u16::MAX,
            Rect {
                x: 0,
                y: 0,
                height: 50,
                width: 50,
            },
            Rect {
                x: 100,
                y: 100,
                height: 1050,
                width: 1050,
            },
        );
        below_left_rect(
            u16::MAX,
            u16::MAX,
            Rect {
                x: 0,
                y: 50,
                height: 50,
                width: 50,
            },
            Rect {
                x: 100,
                y: 1050,
                height: 1050,
                width: 1050,
            },
        );
        below_left_rect(
            u16::MAX,
            u16::MAX,
            Rect {
                x: 50,
                y: 0,
                height: 50,
                width: 50,
            },
            Rect {
                x: 1050,
                y: 100,
                height: 1050,
                width: 1050,
            },
        );
        below_left_rect(
            u16::MAX,
            u16::MAX,
            Rect {
                x: 50,
                y: 50,
                height: 50,
                width: 50,
            },
            Rect {
                x: 1050,
                y: 1050,
                height: 1050,
                width: 1050,
            },
        );
    }
}