tuigram-cli 1.2.1

Telegram in your terminal: a fast, vim-style Telegram client built on TDLib
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
//! The chat list: two rows per chat, its title over its last message (or
//! "typing…" while someone is), with the chat's photo on the left (a square
//! of color if it has none), and a blank row between chats unless that's
//! turned off. With folders, a row of tabs over it says which one is shown,
//! with a line under them joined to the border.

use std::collections::HashMap;

use ratatui::Frame;
use ratatui::layout::{Constraint, Layout, Rect};
use ratatui::style::{Style, Stylize};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, List, ListItem, ListState, Paragraph};
use ratatui_image::FontSize;
use ratatui_image::sliced::{SignedPosition, SlicedImage};
use unicode_width::UnicodeWidthStr;

use crate::chats::{self, Chats, Tab};
use crate::images::Images;
use crate::theme::Colors;

use super::messages::Names;
use super::{LOCK_WIDTH, activity, border, highlight, lock_spans, title_style, truncate};

/// Rows of text per chat, so also the height of its photo.
const PHOTO_ROWS: u16 = 2;
/// Narrower than this inside its border, the list leaves photos out to keep
/// room for titles.
const MIN_WIDTH_FOR_PHOTOS: u16 = 24;
/// After a muted chat's name.
const MUTED: &str = " 🔕";
/// On the right of a pinned chat with nothing unread.
const PINNED: &str = "📌";
/// A folder's tab shows at most this many columns of its name.
const TAB_NAME_COLS: usize = 16;

/// What the list shows, from the app's state.
pub struct ChatList<'a> {
    pub chats: &'a Chats,
    /// Display names by user id, for who's typing in groups.
    pub users: &'a HashMap<i64, String>,
    pub selected: Option<i64>,
    pub loading: bool,
    pub focused: bool,
    /// A popup that may be drawn over the list is open.
    pub covered: bool,
    /// A blank row between chats.
    pub gaps: bool,
}

pub fn draw(frame: &mut Frame, area: Rect, list: &ChatList, images: &mut Images, colors: &Colors) {
    let chats = list.chats;
    let filter = chats.filter();
    let names = Names {
        users: list.users,
        chats,
    };
    let mut title = if filter.is_empty() {
        format!(" Chats ({}) ", chats.ids().len())
    } else {
        format!(
            " Chats ({} of {}) /{filter} ",
            chats.ids().len(),
            chats.total()
        )
    };
    if list.loading {
        title.push_str("loading… ");
    }
    let block = Block::bordered()
        .title(title)
        .border_style(border(list.focused, colors));
    let inner = block.inner(area);
    frame.render_widget(block, area);
    let tabs = chats.tabs();
    let inner = if tabs.is_empty() {
        inner
    } else {
        let [bar, rule, rest] = Layout::vertical([
            Constraint::Length(1),
            Constraint::Length(1),
            Constraint::Fill(1),
        ])
        .areas(inner);
        let line = tab_line(&tabs, chats.shown(), bar.width.into(), colors);
        frame.render_widget(line, bar);
        // Across the whole list, meeting the border on both sides.
        let rule_line = format!("├{}┤", "─".repeat(rule.width.into()));
        let rule = Rect {
            x: area.x,
            width: rule.width + 2,
            ..rule
        };
        frame.render_widget(Line::styled(rule_line, border(list.focused, colors)), rule);
        rest
    };
    let photo_cols = if inner.width >= MIN_WIDTH_FOR_PHOTOS {
        photo_cols(images.font_size())
    } else {
        0
    };
    // The photo and a space after it.
    let indent = if photo_cols > 0 { photo_cols + 1 } else { 0 };
    // What's left for text after the 1-column cursor bar and the photo.
    let width = inner.width.saturating_sub(1 + indent) as usize;
    let rows: Vec<_> = chats
        .ids()
        .iter()
        .filter_map(|&id| chats.get(id).map(|chat| (id, chat)))
        .collect();
    let selected = list
        .selected
        .and_then(|id| rows.iter().position(|&(x, _)| x == id));

    let items: Vec<ListItem> = rows
        .iter()
        .enumerate()
        .map(|(i, &(id, chat))| {
            let is_selected = selected == Some(i);
            // Drawn by hand: List's highlight symbol only marks an item's first row.
            let bar = if is_selected {
                Span::from("▌").fg(colors.accent)
            } else {
                Span::from(" ")
            };
            let gap = Span::from(" ".repeat(indent.into()));
            let muted = chats.muted(id);
            // The unread count, grey for a muted chat as in Telegram; else
            // a pin for a pinned one.
            let badge = if chat.unread > 0 {
                let bg = if muted { colors.muted } else { colors.primary };
                Span::from(format!(" {} ", chat.unread))
                    .fg(colors.bg)
                    .bg(bg)
            } else if chats.pinned(id) {
                Span::from(PINNED)
            } else {
                Span::from("")
            };
            let title = chats.title(id).unwrap_or_default();
            let mark = chats.badge(id);
            let mark_w = mark.map_or(0, |m| m.mark().width());
            let mute_w = if muted { MUTED.width() } else { 0 };
            let secret = chats.is_secret(id);
            let lock_w = if secret { LOCK_WIDTH } else { 0 };
            let badge_w = badge.content.width();
            let title = truncate(
                title,
                width.saturating_sub(lock_w + badge_w + mark_w + mute_w + 1),
            );
            let pad = width.saturating_sub(lock_w + title.width() + mark_w + mute_w + badge_w);
            let style = title_style(chats, id, colors).bold();
            let mut first = vec![bar.clone(), gap.clone()];
            if secret {
                first.extend(lock_spans(colors));
            }
            first.extend(highlight(&title, filter, style, colors));
            if let Some(mark) = mark {
                first.push(super::badge_span(mark, colors));
            }
            if muted {
                first.push(Span::from(MUTED).fg(colors.muted));
            }
            first.push(Span::from(" ".repeat(pad)));
            first.push(badge);
            // Highlighted by hand too, so the blank row below stays blank.
            let row_style = if is_selected {
                Style::new().bg(colors.selection)
            } else {
                Style::new()
            };
            let second = match activity(chat, &names, None) {
                Some(doing) => Span::from(truncate(&doing, width)).fg(colors.activity),
                None => Span::from(truncate(&chat.preview, width)).fg(colors.subtle),
            };
            let mut lines = vec![
                Line::from(first).style(row_style),
                Line::from(vec![bar, gap, second]).style(row_style),
            ];
            if list.gaps {
                lines.push(Line::default());
            }
            ListItem::new(lines)
        })
        .collect();

    let empty = match () {
        _ if !items.is_empty() => None,
        _ if !filter.is_empty() => Some("No chats match"),
        _ if list.loading => Some("Loading…"),
        // The main list is empty only until it loads.
        _ if chats.shown() != chats::List::Main => Some("No chats here"),
        _ => None,
    };
    if let Some(empty) = empty {
        frame.render_widget(Paragraph::new(empty).fg(colors.muted).centered(), inner);
        return;
    }
    let mut state = ListState::default().with_selected(selected);
    frame.render_stateful_widget(List::new(items), inner, &mut state);
    if photo_cols == 0 {
        return;
    }
    // The list draws only whole chats, from where it scrolled to.
    let item_rows = PHOTO_ROWS + u16::from(list.gaps);
    let shown = rows
        .iter()
        .skip(state.offset())
        .take(usize::from(inner.height / item_rows));
    for (i, &(id, _)) in shown.enumerate() {
        let area = Rect {
            x: inner.x + 1,
            y: inner.y + i as u16 * item_rows,
            width: photo_cols,
            height: PHOTO_ROWS,
        };
        draw_photo(frame, area, id, list, images, colors);
    }
}

/// The tabs over the list, the one shown filled in, each with its count of
/// unread unmuted chats. When they don't all fit, they scroll so the one
/// shown does, with `‹` / `›` where more are hidden.
fn tab_line(tabs: &[Tab], shown: chats::List, width: usize, colors: &Colors) -> Line<'static> {
    let label = |tab: &Tab| {
        let name = truncate(&tab.name, TAB_NAME_COLS);
        match tab.unread {
            0 => format!(" {name} "),
            n => format!(" {name} {n} "),
        }
    };
    let labels: Vec<String> = tabs.iter().map(label).collect();
    let widths: Vec<usize> = labels.iter().map(|l| l.width()).collect();
    let current = tabs.iter().position(|t| t.list == shown).unwrap_or(0);
    // Columns tabs `from..=to` take, with a space between them and room for
    // the marks at both ends.
    let span = |from: usize, to: usize| {
        let gaps = to - from;
        let marks = usize::from(from > 0) + usize::from(to + 1 < tabs.len());
        widths[from..=to].iter().sum::<usize>() + gaps + marks
    };
    let mut first = 0;
    while first < current && span(first, current) > width {
        first += 1;
    }
    let mut last = current;
    while last + 1 < tabs.len() && span(first, last + 1) <= width {
        last += 1;
    }
    let mut spans = Vec::new();
    if first > 0 {
        spans.push(Span::from("‹").fg(colors.muted));
    }
    for (i, label) in labels.into_iter().enumerate().take(last + 1).skip(first) {
        if i > first {
            spans.push(Span::from(" "));
        }
        let style = if i == current {
            Style::new().fg(colors.bg).bg(colors.accent).bold()
        } else if tabs[i].unread > 0 {
            Style::new().fg(colors.fg)
        } else {
            Style::new().fg(colors.subtle)
        };
        spans.push(Span::styled(label, style));
    }
    if last + 1 < tabs.len() {
        spans.push(Span::from("›").fg(colors.muted));
    }
    Line::from(spans)
}

/// Columns for a round photo two rows high: about twice the rows, since
/// cells are about twice as tall as they are wide.
fn photo_cols(font: FontSize) -> u16 {
    let (width, height) = (f64::from(font.width.max(1)), f64::from(font.height.max(1)));
    ((f64::from(PHOTO_ROWS) * height / width).round() as u16).clamp(3, 5)
}

/// The chat's photo, else a square of its accent color. Saved Messages gets
/// the app's own color instead of your photo, as Telegram shows it apart.
fn draw_photo(
    frame: &mut Frame,
    area: Rect,
    chat_id: i64,
    list: &ChatList,
    images: &mut Images,
    colors: &Colors,
) {
    let chats = list.chats;
    if chats.is_saved(chat_id) {
        frame.render_widget(Block::new().bg(colors.primary), area);
        return;
    }
    if images.draws_photos()
        && let Some(photo) = chats.get(chat_id).and_then(|c| c.photo.as_ref())
    {
        images.want_avatar(photo, area.width, area.height);
        if let Some(image) = images.avatar(photo, area.width, area.height, list.covered) {
            frame.render_widget(SlicedImage::new(image, SignedPosition::from((0, 0))), area);
            return;
        }
    }
    let color = colors.names[chats.accent(chat_id)];
    frame.render_widget(Block::new().bg(color), area);
}

#[cfg(test)]
mod tests {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;
    use ratatui::buffer::Buffer;
    use ratatui::style::Color;
    use ratatui_image::picker::{Picker, ProtocolType};

    use std::sync::LazyLock;

    use tdlib_rs::enums::{ChatAction, MessageSender};
    use tdlib_rs::types::MessageSenderUser;

    use super::*;
    use crate::chats::ChatPhoto;
    use crate::images::Key;

    /// What the kitty protocol puts in every cell of an image.
    const KITTY_CELL: char = '\u{10EEEE}';

    /// Halfblocks need no terminal query, and their cells are 10x20 px, so
    /// photos are 4 columns wide.
    fn images(protocol: ProtocolType) -> Images {
        let mut picker = Picker::halfblocks();
        picker.set_protocol_type(protocol);
        Images::new(picker, tokio::sync::mpsc::unbounded_channel().0)
    }

    fn photo(file_id: i32) -> Option<ChatPhoto> {
        Some(ChatPhoto {
            file_id,
            path: None,
            thumbnail: None,
        })
    }

    /// Makes the photo with this file id ready to draw.
    fn add_photo(images: &mut Images, file_id: i32) {
        let key = Key {
            file_id,
            cols: 4,
            rows: 2,
            thumbnail: false,
            avatar: true,
        };
        let red = image::RgbaImage::from_pixel(40, 40, image::Rgba([255, 0, 0, 255]));
        images.insert_ready(key, red.into());
    }

    fn list(chats: &Chats, selected: Option<i64>) -> ChatList<'_> {
        static NO_USERS: LazyLock<HashMap<i64, String>> = LazyLock::new(HashMap::new);
        ChatList {
            chats,
            users: &NO_USERS,
            gaps: true,
            selected,
            loading: false,
            focused: true,
            covered: false,
        }
    }

    #[test]
    fn telegrams_scam_and_official_marks_follow_the_name() {
        use crate::chats::{Badge, Peer};
        let mut chats = Chats::default();
        chats.add_local(1, "Telegram", None).peer = Some(Peer::User(777000));
        chats.add_local(2, "Telegram", None).peer = Some(Peer::User(42));
        chats.set_badge(Peer::User(777000), Some(Badge::Official));
        chats.set_badge(Peer::User(42), Some(Badge::Scam));
        chats.refresh();
        let buf = render(
            &list(&chats, None),
            &mut images(ProtocolType::Halfblocks),
            40,
            10,
        );
        let rows: Vec<String> = (0..buf.area.height)
            .map(|y| (0..buf.area.width).map(|x| buf[(x, y)].symbol()).collect())
            .collect();
        assert!(rows.iter().any(|r| r.contains("Telegram ✓")), "{rows:#?}");
        assert!(
            rows.iter().any(|r| r.contains("Telegram SCAM")),
            "{rows:#?}"
        );
    }

    #[test]
    fn a_secret_chat_has_a_lock_and_its_own_color_beside_the_other_chat() {
        let mut chats = Chats::default();
        chats.add_local(1, "Chardy", None);
        chats.add_local(2, "Chardy", None).secret_id = Some(7);
        chats.refresh();
        let colors = Colors::default();
        let buf = render(
            &list(&chats, None),
            &mut images(ProtocolType::Halfblocks),
            40,
            10,
        );
        let rows: Vec<String> = (0..buf.area.height)
            .map(|y| (0..buf.area.width).map(|x| buf[(x, y)].symbol()).collect())
            .collect();
        // An emoji's second cell is blank in the test buffer.
        let secret = rows.iter().position(|r| r.contains("🔒  Chardy")).unwrap();
        let other = rows
            .iter()
            .position(|r| r.contains("Chardy") && !r.contains('🔒'))
            .unwrap();
        assert_ne!(secret, other);
        let y = secret as u16;
        let x = (0..buf.area.width)
            .find(|&x| buf[(x, y)].symbol() == "C")
            .unwrap();
        assert_eq!(buf[(x, y)].fg, colors.secret);
        // A name can start with 🔒 too, but can't color what's behind it.
        let lock = (0..buf.area.width)
            .find(|&x| buf[(x, y)].symbol() == "🔒")
            .unwrap();
        assert_eq!(buf[(lock, y)].bg, colors.secret);
    }

    #[test]
    fn pinned_chats_show_a_pin_and_muted_ones_a_bell_and_a_grey_count() {
        use tdlib_rs::types::ChatNotificationSettings;
        let mut chats = Chats::default();
        chats
            .add_local(1, "Pinned", None)
            .set_pinned(chats::List::Main, true);
        chats.add_local(2, "Quiet", None).unread = 4;
        chats.set_notifications(
            2,
            ChatNotificationSettings {
                mute_for: 3600,
                ..ChatNotificationSettings::default()
            },
        );
        chats.refresh();
        let colors = Colors::default();
        let buf = render(
            &list(&chats, None),
            &mut images(ProtocolType::Halfblocks),
            40,
            10,
        );
        let rows: Vec<String> = (0..buf.area.height)
            .map(|y| (0..buf.area.width).map(|x| buf[(x, y)].symbol()).collect())
            .collect();
        let pinned = rows.iter().position(|r| r.contains("Pinned")).unwrap();
        let quiet = rows.iter().position(|r| r.contains("Quiet")).unwrap();
        assert!(pinned < quiet, "pinned first, even before unread chats");
        assert!(rows[pinned].contains('📌'), "{rows:#?}");
        assert!(rows[quiet].contains("Quiet 🔕"), "{rows:#?}");
        let x = rows[quiet].chars().position(|c| c == '4').unwrap() as u16;
        assert_eq!(buf[(x, quiet as u16)].bg, colors.muted, "a grey count");
    }

    fn render(list: &ChatList, images: &mut Images, width: u16, height: u16) -> Buffer {
        let colors = Colors::default();
        let mut terminal = Terminal::new(TestBackend::new(width, height)).unwrap();
        terminal
            .draw(|f| draw(f, f.area(), list, images, &colors))
            .unwrap();
        terminal.backend().buffer().clone()
    }

    /// The symbols of one row, from column `x` on, `width` cells.
    fn cells(buf: &Buffer, x: u16, y: u16, width: u16) -> String {
        (x..x + width).map(|x| buf[(x, y)].symbol()).collect()
    }

    fn is_kitty_image(buf: &Buffer, x: u16, y: u16) -> bool {
        buf[(x, y)].symbol().contains(KITTY_CELL)
    }

    /// True when the cells from column `x` on, `width` of them, are blank
    /// and filled with `color`.
    fn is_square(buf: &Buffer, x: u16, y: u16, width: u16, color: Color) -> bool {
        (x..x + width).all(|x| buf[(x, y)].symbol() == " " && buf[(x, y)].bg == color)
    }

    #[test]
    fn chats_without_a_photo_get_a_square_of_their_color() {
        let colors = Colors::default();
        let mut chats = Chats::default();
        chats.add_local(1, "Alice Smith", None);
        chats.add_local(2, "bob", photo(20));
        chats.set_accent(1, 3);
        chats.refresh();
        let mut images = images(ProtocolType::Halfblocks);
        add_photo(&mut images, 20);
        let buf = render(&list(&chats, None), &mut images, 40, 10);

        // Border, cursor bar, then the 4-column square and a space.
        assert!(is_square(&buf, 2, 1, 4, colors.names[3]));
        assert!(is_square(&buf, 2, 2, 4, colors.names[3]));
        assert_eq!(cells(&buf, 6, 1, 12), " Alice Smith");
        assert!(!is_square(&buf, 2, 3, 4, colors.names[3]), "a blank row");
        assert_eq!(cells(&buf, 6, 4, 4), " bob", "the next chat");
        assert!(
            is_square(&buf, 2, 4, 4, colors.names[0]),
            "halfblocks are too coarse for a photo"
        );
    }

    #[test]
    fn the_selected_chat_is_highlighted_but_not_the_blank_row_below_it() {
        let colors = Colors::default();
        let mut chats = Chats::default();
        chats.add_local(1, "Alice Smith", None);
        chats.refresh();
        let buf = render(
            &list(&chats, Some(1)),
            &mut images(ProtocolType::Halfblocks),
            40,
            10,
        );
        for y in [1, 2] {
            assert_eq!(buf[(1, y)].symbol(), "▌");
            assert_eq!(buf[(7, y)].bg, colors.selection);
            assert_eq!(buf[(38, y)].bg, colors.selection, "the whole row");
        }
        assert_eq!(buf[(1, 3)].symbol(), " ");
        assert_ne!(buf[(7, 3)].bg, colors.selection);
    }

    #[test]
    fn typing_shows_in_place_of_the_last_message_while_it_lasts() {
        let colors = Colors::default();
        let mut chats = Chats::default();
        chats.add_local(1, "Alice", None).is_private = true;
        chats.add_local(2, "Climbing club", None);
        chats.refresh();
        let user = |user_id| MessageSender::User(MessageSenderUser { user_id });
        chats.set_action(1, None, &user(7), &ChatAction::Typing);
        chats.set_action(2, None, &user(7), &ChatAction::Typing);
        chats.set_action(2, None, &user(8), &ChatAction::Typing);
        let users = HashMap::from([(7, "Alice".to_string()), (8, "Bob".to_string())]);
        let draw = |chats: &Chats| {
            let list = ChatList {
                users: &users,
                ..list(chats, None)
            };
            render(&list, &mut images(ProtocolType::Halfblocks), 40, 10)
        };

        // Each chat: its title, then the row under it, then a blank row.
        let buf = draw(&chats);
        assert_eq!(cells(&buf, 7, 2, 7), "typing…");
        assert_eq!(buf[(7, 2)].fg, colors.activity);
        assert_eq!(cells(&buf, 7, 5, 25), "Alice and Bob are typing…");

        chats.set_action(1, None, &user(7), &ChatAction::Cancel);
        let buf = draw(&chats);
        assert_eq!(cells(&buf, 7, 2, 7), "       ", "back to the last message");
    }

    #[test]
    fn photos_are_drawn_beside_the_chats_scrolled_into_view() {
        let colors = Colors::default();
        let mut chats = Chats::default();
        let mut images = images(ProtocolType::Kitty);
        for id in 1..=30 {
            chats.add_local(id, &format!("chat {id}"), photo(id as i32));
            // Odd chats have photos ready, even ones still show squares.
            if id % 2 == 1 {
                add_photo(&mut images, id as i32);
            }
        }
        chats.refresh();
        // 15 rows inside the border: chats 16 to 20, the selected one last.
        let buf = render(&list(&chats, Some(20)), &mut images, 40, 17);
        assert_eq!(cells(&buf, 7, 1, 7), "chat 16");
        assert!(is_square(&buf, 2, 1, 4, colors.names[0]));
        assert_eq!(cells(&buf, 7, 4, 7), "chat 17");
        assert!(is_kitty_image(&buf, 2, 4) && is_kitty_image(&buf, 5, 5));
        assert!(!is_kitty_image(&buf, 2, 6), "the blank row");
        assert_eq!(cells(&buf, 7, 13, 7), "chat 20");
        assert!(!is_kitty_image(&buf, 2, 13));
    }

    #[test]
    fn without_gaps_chats_and_their_photos_follow_each_other() {
        let mut chats = Chats::default();
        let mut images = images(ProtocolType::Kitty);
        for id in 1..=3 {
            chats.add_local(id, &format!("chat {id}"), photo(id as i32));
            add_photo(&mut images, id as i32);
        }
        chats.refresh();
        let compact = ChatList {
            gaps: false,
            ..list(&chats, None)
        };
        let buf = render(&compact, &mut images, 40, 10);
        assert_eq!(cells(&buf, 7, 1, 6), "chat 1");
        assert_eq!(cells(&buf, 7, 3, 6), "chat 2");
        assert_eq!(cells(&buf, 7, 5, 6), "chat 3");
        assert!(is_kitty_image(&buf, 2, 3) && is_kitty_image(&buf, 5, 4));
    }

    #[test]
    fn a_new_photo_waits_while_a_popup_could_hide_it() {
        let colors = Colors::default();
        let mut chats = Chats::default();
        chats.add_local(1, "Alice", photo(10));
        chats.refresh();
        let mut images = images(ProtocolType::Kitty);
        add_photo(&mut images, 10);

        // Kitty sends the image with its first drawing; under a popup, that
        // would be lost.
        let mut covered = list(&chats, None);
        covered.covered = true;
        let buf = render(&covered, &mut images, 40, 10);
        assert!(is_square(&buf, 2, 1, 4, colors.names[0]));

        let buf = render(&list(&chats, None), &mut images, 40, 10);
        assert!(is_kitty_image(&buf, 2, 1));
        let buf = render(&covered, &mut images, 40, 10);
        assert!(is_kitty_image(&buf, 2, 1), "already sent");
    }

    #[test]
    fn saved_messages_is_a_square_of_the_app_color() {
        let colors = Colors::default();
        let mut chats = Chats::default();
        chats.add_local(7, "Eric", photo(10));
        chats.set_my_id(7);
        chats.refresh();
        let mut images = images(ProtocolType::Kitty);
        add_photo(&mut images, 10);
        let buf = render(&list(&chats, None), &mut images, 40, 10);
        assert!(is_square(&buf, 2, 1, 4, colors.primary));
        assert_eq!(cells(&buf, 7, 1, 14), "Saved Messages");
    }

    #[test]
    fn folders_are_tabs_over_the_list_scrolled_to_the_one_shown() {
        use tdlib_rs::types::{ChatFolderInfo, ChatFolderName, FormattedText};
        let colors = Colors::default();
        let folder = |id, name: &str| ChatFolderInfo {
            id,
            name: ChatFolderName {
                text: FormattedText {
                    text: name.into(),
                    entities: Vec::new(),
                },
                animate_custom_emoji: false,
            },
            ..ChatFolderInfo::default()
        };
        let mut chats = Chats::default();
        chats.add_local(1, "Alice", None);
        chats.set_folders(
            &[folder(1, "Friends"), folder(2, "Work"), folder(3, "Games")],
            0,
        );
        chats.set_unread_in(chats::List::Folder(2), 4);
        chats.refresh();
        let rows = |chats: &Chats, width| {
            let buf = render(
                &list(chats, None),
                &mut images(ProtocolType::Halfblocks),
                width,
                10,
            );
            let row: String = cells(&buf, 1, 1, width - 2);
            (buf, row)
        };

        let (buf, row) = rows(&chats, 40);
        assert_eq!(row.trim_end(), " All   Friends   Work 4   Games");
        assert_eq!(buf[(2, 1)].bg, colors.accent, "the one shown");
        let rule = format!("├{}┤", "─".repeat(38));
        assert_eq!(cells(&buf, 0, 2, 40), rule, "a line under the tabs");
        assert_eq!(buf[(0, 2)].fg, colors.accent, "the border's color");
        assert_eq!(cells(&buf, 7, 3, 5), "Alice", "the chats under it");

        chats.show(chats::List::Folder(3));
        chats.refresh();
        let (_, row) = rows(&chats, 20);
        assert_eq!(row.trim_end(), "‹ Work 4   Games", "{row}");
        let (_, row) = rows(&chats, 40);
        assert!(row.contains("Games"));
        let (buf, _) = rows(&chats, 40);
        assert!(
            cells(&buf, 1, 3, 38).contains("No chats here"),
            "Alice isn't in Games"
        );
    }

    #[test]
    fn narrow_lists_leave_photos_out() {
        let mut chats = Chats::default();
        chats.add_local(1, "Alice Smith", None);
        chats.refresh();
        let buf = render(
            &list(&chats, None),
            &mut images(ProtocolType::Halfblocks),
            20,
            10,
        );
        assert_eq!(cells(&buf, 2, 1, 11), "Alice Smith");
    }
}