tuigram-cli 1.9.0

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
//! The shortcuts tab of the `?` popup.

use ratatui::Frame;
use ratatui::layout::Rect;
use ratatui::style::Stylize;
use ratatui::text::{Line, Span};
use ratatui::widgets::Paragraph;
use unicode_width::UnicodeWidthStr;

use crate::lang;
use crate::theme::Colors;

/// Every keyboard shortcut, grouped by where it works, in English: titles,
/// descriptions and keys written in words are put in the language set where
/// they're drawn. Keep this in step with the key handling in `app.rs` (and
/// the status bar hints).
const SHORTCUTS: &[(&str, &[(&str, &str)])] = &[
    (
        lang::key("Everywhere"),
        &[
            ("?", lang::key("This help, and settings")),
            (
                ":",
                lang::key("Type a command: info, secret, key, timer, leave, proxy or logout"),
            ),
            (
                "s",
                lang::key("Find a chat or person: a name, @username or t.me link"),
            ),
            (
                "I",
                lang::key("What the chat is, and who's in it: Enter on someone writes to them"),
            ),
            ("H", lang::key("Highlight or unhighlight the selected chat")),
            (
                "Ctrl-o / Ctrl-i",
                lang::key("Back to the chat or reply you left / forward again"),
            ),
            (
                "Ctrl-r",
                lang::key(
                    "Resize the panes: h / l, = as at first, saved at once; Esc or Enter is done",
                ),
            ),
            ("q", lang::key("Quit (press again to stop waiting)")),
            ("Ctrl-c", lang::key("Quit, except while writing")),
            (
                "",
                lang::key(
                    "Keys work in the Cyrillic layout too: н is y, р is h; typing takes letters as they are",
                ),
            ),
        ],
    ),
    (
        lang::key("Chat list"),
        &[
            ("j / k", lang::key("Move down / up")),
            ("gg / G", lang::key("First / last chat")),
            ("Ctrl-d / Ctrl-u", lang::key("Half a page down / up")),
            (
                "Enter / l",
                lang::key(
                    "Open the chat, at its first unread message (Enter / h with the list on the right)",
                ),
            ),
            ("i", lang::key("Open the chat and write")),
            (
                "gm",
                lang::key("Open the chat at a message that mentions you"),
            ),
            (
                "Tab / Shift-Tab",
                lang::key("Next / previous folder, then the archive"),
            ),
            ("/", lang::key("Filter chats by name")),
            (
                "p",
                lang::key("Pin or unpin the chat (on all your devices)"),
            ),
            ("m", lang::key("Mute or unmute the chat")),
            (
                "a",
                lang::key("Archive the chat, or move it out of the archive"),
            ),
            ("Esc", lang::key("Clear the filter")),
        ],
    ),
    (
        lang::key("Forum topics"),
        &[
            ("j / k", lang::key("Move down / up")),
            ("gg / G", lang::key("First / last topic")),
            (
                "Enter / l",
                lang::key("Open the topic's messages (Enter / h with the list on the right)"),
            ),
            ("i", lang::key("Open the topic and write")),
            (
                "Ctrl-r",
                lang::key("Resize the topics pane: h / l, = as at first"),
            ),
            (
                "h / Esc",
                lang::key("Back to the chat list (l with the list on the right)"),
            ),
        ],
    ),
    (
        lang::key("Messages"),
        &[
            ("j / k", lang::key("Newer / older message")),
            ("gg / G", lang::key("Oldest / newest message")),
            ("Ctrl-d / Ctrl-u", lang::key("Half a page newer / older")),
            (
                "Enter",
                lang::key(
                    "Send a message that wasn't sent again, show spoilers, view a photo shown only while open, play or pause a voice message, vote in a poll, list a bot's buttons, show the photo, or open the file or link",
                ),
            ),
            (
                "y",
                lang::key("Copy the text, a link, or the photo or file"),
            ),
            ("O", lang::key("Show the file in the file manager")),
            (
                "v",
                lang::key("Show the text to select part of it and copy that"),
            ),
            ("r", lang::key("Reply")),
            (
                "f",
                lang::key("Forward to another chat (an album goes whole)"),
            ),
            ("e", lang::key("Edit your message")),
            (
                "R",
                lang::key("React, or take your reaction back (/ finds an emoji by name)"),
            ),
            ("X", lang::key("Take back all your reactions")),
            (
                "J",
                lang::key("Join the public group or channel you're viewing"),
            ),
            ("d", lang::key("Delete")),
            ("P", lang::key("Pin the message (asks how), or unpin it")),
            (
                "gp",
                lang::key("Pinned messages: Enter goes to one, P unpins it"),
            ),
            ("gd", lang::key("Go to the message a reply answers")),
            (
                "gu",
                lang::key("Go to the first unread message, under the \"Unread messages\" line"),
            ),
            (
                "gm / gM",
                lang::key(
                    "Go to the oldest unread message that mentions you or answers yours (@ in the chat list), and seeing it reads it; once you've seen them all, to the one before / after the cursor",
                ),
            ),
            (
                "Tab",
                lang::key("Forward again, as Ctrl-i (most terminals send Ctrl-i as Tab)"),
            ),
            ("i", lang::key("Write a message")),
            (
                "a",
                lang::key("Attach a file by its path (Tab completes it)"),
            ),
            (
                "p",
                lang::key("Paste a photo, files or text from the clipboard"),
            ),
            (
                "/",
                lang::key(
                    "Search the whole chat; a has: search lists every match in a media browser",
                ),
            ),
            (
                "from: has:",
                lang::key(
                    "In a search: from:@alice, from:me, from:this or from:a name; has:photo (video, media, file, link, voice, note, gif, audio)",
                ),
            ),
            (
                "before: after:",
                lang::key("In a search: before:2025-10-01, after:2025-09-01 (that day counts)"),
            ),
            ("n / N", lang::key("Next older / newer match")),
            ("Q", lang::key("Close the chat, leaving only the chat list")),
            (
                "Esc",
                lang::key(
                    "End the search, cancel the edit, remove the files, cancel the reply, or go back",
                ),
            ),
            (
                "h",
                lang::key(
                    "Back to the chat list, or a forum's topics (l with the list on the right)",
                ),
            ),
        ],
    ),
    (
        lang::key("Writing"),
        &[
            ("Enter", lang::key("Send, or save the edit")),
            ("Alt-Enter / Ctrl-j", lang::key("New line")),
            ("Ctrl-u", lang::key("Delete back to the start of the line")),
            (
                lang::key("**bold**"),
                lang::key(
                    "Formatting: __italic__ ~~strike~~ ||spoiler|| `code` ```block``` [text](url)",
                ),
            ),
            (
                lang::key("@name / :smile"),
                lang::key("Suggests people in the group, or emoji; Tab puts one in"),
            ),
            (
                lang::key("/command"),
                lang::key("Starting a message: the bots' commands"),
            ),
            (
                "Ctrl-v",
                lang::key("Paste a photo, files or text from the clipboard"),
            ),
            (
                lang::key("Drop a file"),
                lang::key("Attach it; the text you write is its caption"),
            ),
            (
                "Ctrl-z",
                lang::key("Turn files just pasted back into their path as text"),
            ),
            (
                "Ctrl-t",
                lang::key("Send attached photos as files, uncompressed, or back"),
            ),
            ("Tab", lang::key("Stickers")),
            (
                "Esc / Ctrl-c",
                lang::key(
                    "Back to Normal mode; what's written stays as the chat's draft, on your other devices too",
                ),
            ),
        ],
    ),
    (
        lang::key("Selecting text (v)"),
        &[
            (
                "h / j / k / l",
                lang::key("Move a character, or a row on screen"),
            ),
            (
                "w / b / e",
                lang::key("Next word / word before / end of the word"),
            ),
            ("0 / $", lang::key("Start / end of the line")),
            ("g / G", lang::key("Start / end of the text")),
            ("Ctrl-d / Ctrl-u", lang::key("Half a page down / up")),
            (
                "v",
                lang::key(
                    "Start selecting where the cursor is, or stop; it takes in both ends, as in vim",
                ),
            ),
            ("y", lang::key("Copy what's selected and close")),
            ("Esc", lang::key("Stop selecting, then close")),
        ],
    ),
    (
        lang::key("Stickers"),
        &[
            ("h / j / k / l", lang::key("Move")),
            (
                "H / L",
                lang::key("Previous / next tab: Recent, Favorites, your sets"),
            ),
            (
                "Enter",
                lang::key("Send the sticker (as the reply, if you're replying)"),
            ),
            ("/", lang::key("Find stickers by emoji or word")),
            ("Tab / Esc", lang::key("Back to writing")),
        ],
    ),
    (
        lang::key("Photo viewer"),
        &[
            (
                "h / l",
                lang::key("The photo before / after it in the chat"),
            ),
            (
                "j / k",
                lang::key("Zoom in / out (also + / -), up to filling the window"),
            ),
            ("o", lang::key("Open the photo in your computer's viewer")),
            ("y", lang::key("Copy the photo")),
            ("Enter / Esc / q", lang::key("Close")),
        ],
    ),
    (
        lang::key("Video player"),
        &[
            (
                "Space",
                lang::key("Play (once) or pause, right in the message (size in Settings)"),
            ),
            ("z", lang::key("Twice as big in the chat, or back")),
            ("j / k", lang::key("Zoom in / out, on the whole window")),
            ("o", lang::key("Open the video in your computer's player")),
            ("Enter", lang::key("Fill the window; it closes once played")),
            ("Esc / q", lang::key("Close")),
        ],
    ),
    (
        lang::key("Bot buttons"),
        &[
            (
                "h / j / k / l",
                lang::key("Move, as the buttons are laid out"),
            ),
            ("Tab / Shift-Tab", lang::key("Next / previous button")),
            (
                "Enter",
                lang::key(
                    "Press it: the bot answers, a link opens (asking first), a reply button sends its words",
                ),
            ),
            ("Esc / q", lang::key("Close")),
        ],
    ),
    (
        lang::key("Secret chats"),
        &[
            (
                ":secret",
                lang::key(
                    "Start one with the person in this chat (asks first); it's kept on this computer only",
                ),
            ),
            (
                ":key",
                lang::key("Its key as a picture, to compare with the other person's"),
            ),
            (
                ":timer",
                lang::key("How long new messages last once they're seen"),
            ),
            (
                ":leave",
                lang::key("End it for both of you, and delete it here (asks first)"),
            ),
            (
                "Enter",
                lang::key("On a 🔥 photo: view it; it's covered again when you move on"),
            ),
        ],
    ),
    (
        lang::key("Search and command prompts"),
        &[
            (
                "Enter",
                lang::key("Search, keep the chat filter, or run the command"),
            ),
            (
                "Tab / Shift-Tab",
                lang::key(
                    "Complete a command or a search filter (again for the next one), or the path of a file to attach",
                ),
            ),
            ("Esc / Ctrl-c", lang::key("Cancel")),
        ],
    ),
    (
        lang::key("Forward and find popups"),
        &[
            (
                lang::key("Type"),
                lang::key("Search chats by name, or @username"),
            ),
            ("Up / Down", lang::key("Move (also Ctrl-p / Ctrl-n, Tab)")),
            ("Enter", lang::key("Forward there, or open it")),
            ("Esc", lang::key("Cancel")),
        ],
    ),
    (
        lang::key("Menus and popups"),
        &[
            ("j / k", lang::key("Move")),
            ("Enter", lang::key("Choose")),
            ("1-9", lang::key("Choose by number")),
            (
                "Space",
                lang::key("Tick a setting, or use a sound or a theme (Enter too); saved at once"),
            ),
            ("Esc / q", lang::key("Close")),
            ("Tab / h / l", lang::key("Switch tabs in this popup")),
            (
                "y / n",
                lang::key("Go ahead or not, when asked about a file, a link or joining"),
            ),
        ],
    ),
];

/// A key as the language set writes it: only keys written in words, such as
/// "Drop a file", have other words; the rest are looked up and stay as they are.
fn key_text(key: &'static str) -> &'static str {
    lang::t(key)
}

/// The longest key, which sets where the descriptions start.
fn key_width() -> usize {
    SHORTCUTS
        .iter()
        .flat_map(|(_, keys)| keys.iter())
        .map(|&(key, _)| key_text(key).width())
        .max()
        .unwrap_or(0)
}

/// The shortcuts as lines, in the language set: a heading per group, then
/// each key and what it does, wrapped to `width` columns under where the
/// descriptions start.
fn lines(colors: &Colors, width: usize) -> Vec<Line<'static>> {
    let key_width = key_width();
    // Indent, the keys and a gap before each description.
    let indent = 3 + key_width + 2;
    let room = width.saturating_sub(indent).max(1);
    let mut lines = Vec::new();
    for (i, &(group, keys)) in SHORTCUTS.iter().enumerate() {
        if i > 0 {
            lines.push(Line::default());
        }
        lines.push(
            Line::from(format!(" {}", lang::t(group)))
                .fg(colors.accent)
                .bold(),
        );
        for &(key, what) in keys {
            let key = key_text(key);
            let pad = " ".repeat(key_width - key.width());
            let pieces = super::messages::wrap(lang::t(what), room);
            for (n, (piece, _)) in pieces.into_iter().enumerate() {
                let lead = match n {
                    0 => format!("   {key}{pad}  "),
                    _ => " ".repeat(indent),
                };
                lines.push(Line::from(vec![
                    Span::from(lead).fg(colors.primary),
                    Span::from(piece),
                ]));
            }
        }
    }
    lines
}

/// Columns the widest line takes in the language set, plus one for the
/// scrollbar, for sizing the popup.
pub fn width() -> usize {
    let what = SHORTCUTS
        .iter()
        .flat_map(|(_, keys)| keys.iter())
        .map(|&(_, what)| lang::t(what).width())
        .max()
        .unwrap_or(0);
    // Indent, the keys, a gap, then the descriptions.
    3 + key_width() + 2 + what + 1
}

/// Rows the list takes unwrapped, for sizing the popup.
pub fn height() -> usize {
    SHORTCUTS
        .iter()
        .map(|(_, keys)| keys.len() + 2)
        .sum::<usize>()
        - 1
}

/// Draws the list from row `scroll`, first pulling `scroll` back if it's
/// past the end.
pub fn draw(frame: &mut Frame, area: Rect, scroll: &mut usize, colors: &Colors) {
    // The scrollbar takes the last column.
    let lines = lines(colors, usize::from(area.width).saturating_sub(1));
    let max = lines.len().saturating_sub(usize::from(area.height));
    *scroll = (*scroll).min(max);
    frame.render_widget(Paragraph::new(lines).scroll((*scroll as u16, 0)), area);
    super::scrollbar(frame, area, max, *scroll, colors);
}

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

    #[test]
    fn height_counts_every_line_in_every_language() {
        for language in Lang::ALL {
            lang::set(language);
            let lines = lines(&Colors::default(), usize::MAX);
            assert_eq!(height(), lines.len(), "{language:?}");
        }
    }

    #[test]
    fn width_leaves_room_for_every_line_and_the_scrollbar_in_every_language() {
        for language in Lang::ALL {
            lang::set(language);
            let widest = lines(&Colors::default(), usize::MAX)
                .iter()
                .map(Line::width)
                .max()
                .unwrap();
            assert_eq!(width(), widest + 1, "{language:?}");
        }
    }

    #[test]
    fn long_descriptions_wrap_under_where_descriptions_start() {
        let text = |l: &Line| -> String { l.spans.iter().map(|s| s.content.as_ref()).collect() };
        let wide = lines(&Colors::default(), usize::MAX);
        let narrow = lines(&Colors::default(), 60);
        assert!(narrow.len() > wide.len(), "some wrap");
        assert!(narrow.iter().all(|l| l.width() <= 60), "none runs past");
        // Every letter is still there, in order, the rest of a description
        // under its start, after the keys' column. Lines may also break
        // after punctuation, as in `||spoiler||`.
        let letters = |lines: &[Line]| -> String {
            let all: String = lines.iter().map(text).collect();
            all.chars().filter(|c| !c.is_whitespace()).collect()
        };
        assert_eq!(letters(&narrow), letters(&wide));
        let indent = 3 + key_width() + 2;
        let continued = narrow
            .iter()
            .map(text)
            .find(|l| l.starts_with(&" ".repeat(indent)));
        assert!(continued.is_some_and(|l| !l[indent..].starts_with(' ')));
    }

    #[test]
    fn keys_written_in_words_are_translated_and_the_rest_stay() {
        let text = |l: &Line| {
            l.spans
                .iter()
                .map(|s| s.content.as_ref())
                .collect::<String>()
        };
        let row = |key: &str| {
            let lines = lines(&Colors::default(), usize::MAX);
            lines.iter().map(text).find(|l| l.contains(key)).unwrap()
        };
        lang::set(Lang::Ru);
        assert!(row("Перетащить файл").contains("Прикрепить его"));
        assert!(row("Alt-Enter / Ctrl-j").contains("Новая строка"));

        lang::set(Lang::Vi);
        assert_eq!(row("Mọi nơi"), " Mọi nơi");
        assert!(row("Alt-Enter / Ctrl-j").contains("Xuống dòng"));
    }
}