concord 2.5.0

A terminal user interface client for Discord
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
use super::*;
use crate::tui::ui::emoji_overlay::{EmojiSlot, overlay_emoji_slots};

pub(in crate::tui::ui) fn render_channels(
    frame: &mut Frame,
    area: Rect,
    state: &DashboardState,
    emoji_images: &[EmojiImage<'_>],
) {
    let dashboard = state;
    let focused = state.focus() == FocusPane::Channels;
    let filter_query = state.channel_pane_filter_query();
    let block = panel_block("Channels", focused);
    let inner = block.inner(area);
    frame.render_widget(block, area);

    // Guild name, plus a boost line for boosted guilds when the pane can spare
    // the row. A short pane keeps the name only and still shows every channel.
    let boost_label = selected_channel_boost_label(state);
    let header_area = Rect {
        height: inner.height.min(channel_pane_header_height(state)),
        ..inner
    };
    if header_area.height > 0 {
        let width = header_area.width as usize;
        let server_name = selected_channel_server_label(state);
        let mut lines = vec![Line::from(Span::styled(
            truncate_display_width(&server_name, width),
            theme::current().style(theme::HighlightGroup::Heading),
        ))];
        if header_area.height >= 2
            && let Some(boost) = &boost_label
        {
            lines.push(Line::from(Span::styled(
                truncate_display_width(boost, width),
                theme::current().style(theme::HighlightGroup::Description),
            )));
        }
        frame.render_widget(Paragraph::new(lines), header_area);
    }

    let channels_area = Rect {
        y: inner.y.saturating_add(header_area.height),
        height: inner.height.saturating_sub(header_area.height),
        ..inner
    };

    let (list_area, filter_area) = split_pane_filter_area(channels_area, filter_query.is_some());

    let channel_entries = state.channel_pane_filtered_entries();
    let channel_rows = state.channel_pane_rows_from_entries(&channel_entries);
    let channel_line_count = channel_rows.len();
    let all_channel_entries;
    let populated_channel_entries = if state.channel_pane_filter_query().is_some() {
        all_channel_entries = state.channel_pane_entries();
        all_channel_entries.as_slice()
    } else {
        channel_entries.as_slice()
    };
    let populated_voice_channel_ids: HashSet<_> = populated_channel_entries
        .windows(2)
        .filter_map(|window| match (&window[0], &window[1]) {
            (
                ChannelPaneEntry::Channel { state: channel, .. }
                | ChannelPaneEntry::Thread { state: channel, .. },
                ChannelPaneEntry::VoiceParticipant { .. },
            ) => Some(channel.id),
            _ => None,
        })
        .collect();
    let channel_scroll = state.channel_scroll();
    let content_height = state.channel_content_height();
    let selected_line = state.focused_channel_selection_line(&channel_entries);
    let entries: Vec<_> = channel_rows
        .iter()
        .enumerate()
        .skip(channel_scroll)
        .take(content_height)
        .collect();
    let scrollbar_width = usize::from(vertical_scrollbar_visible(
        list_area,
        list_area.height as usize,
        channel_line_count,
    ));
    let available_width = (list_area.width as usize).saturating_sub(scrollbar_width);
    let selection_marker_width = selection_marker(false).content.width();
    let max_width = available_width.saturating_sub(selection_marker_width);
    let horizontal_scroll = state.channel_horizontal_scroll();
    let mut emoji_line_urls: Vec<(usize, usize, String)> = Vec::new();
    let items: Vec<ListItem> = entries
        .iter()
        .map(|(line_index, row)| {
            if let ChannelPaneRow::Activity {
                entry, activity, ..
            } = row
            {
                let ChannelPaneEntry::Channel {
                    state: channel,
                    branch,
                } = entry
                else {
                    unreachable!("only DM channel entries have activity rows");
                };
                let render = build_activity_render(activity, emoji_images, true);
                let dm_prefix_width = dm_presence_dot_span(channel).map_or_else(
                    || channel_prefix(&channel.kind).width(),
                    |span| span.content.width(),
                );
                let branch_width = branch.prefix().width();
                let leading_width = selection_marker_width + branch_width + dm_prefix_width;
                let activity_line = compact_activity_line(
                    render,
                    leading_width,
                    available_width,
                    horizontal_scroll,
                );
                if let Some(image) = activity_line.image {
                    emoji_line_urls.push((*line_index, image.column, image.url));
                }
                return ListItem::new(activity_line.line);
            }

            let ChannelPaneRow::Entry { entry, .. } = row else {
                unreachable!("activity rows return before entry rendering");
            };
            let is_selected = selected_line == Some(*line_index);
            let is_active = dashboard.is_active_channel_entry(entry);
            styled_list_item(
                match entry {
                    ChannelPaneEntry::CategoryHeader { state, collapsed } => {
                        let arrow = if *collapsed { "โ–ถ " } else { "โ–ผ " };
                        let label_width = max_width.saturating_sub(arrow.width());
                        let mut label_style =
                            theme::current().style(theme::HighlightGroup::CategoryHeading);
                        if dashboard.channel_notification_muted(state.id) {
                            label_style =
                                theme::current().apply(theme::HighlightGroup::Muted, label_style);
                        }
                        label_style = selected_text_style(is_selected, label_style);
                        Line::from(vec![
                            selection_marker(is_selected),
                            Span::raw(arrow),
                            Span::styled(
                                truncate_display_width_from(
                                    &state.name,
                                    horizontal_scroll,
                                    label_width,
                                ),
                                label_style,
                            ),
                        ])
                    }
                    ChannelPaneEntry::Channel { state, branch } => {
                        let branch_prefix = branch.prefix();
                        let dm_prefix_span = dm_presence_dot_span(state);
                        let channel_prefix = channel_prefix(&state.kind);
                        let prefix_width = dm_prefix_span
                            .as_ref()
                            .map_or_else(|| channel_prefix.width(), |span| span.content.width());
                        let populated_voice_channel =
                            state.is_voice() && populated_voice_channel_ids.contains(&state.id);
                        let base_style = active_text_style(is_active, Style::default());
                        let is_muted = dashboard.channel_notification_muted(state.id);
                        let unread = dashboard.sidebar_channel_unread(state.id);
                        let (badge, mut name_style) =
                            channel_unread_decoration(unread, base_style, is_active);
                        if state.is_voice() && dashboard.is_joined_voice_channel(state.id) {
                            name_style = theme::current()
                                .apply(theme::HighlightGroup::JoinedVoiceChannel, name_style);
                        }
                        if is_muted {
                            name_style =
                                theme::current().apply(theme::HighlightGroup::Muted, name_style);
                        }
                        name_style = selected_text_style(is_selected, name_style);
                        let badge = if state.guild_id.is_none()
                            && !is_active
                            && unread != ChannelUnreadState::Seen
                        {
                            let message_count = dashboard.channel_unread_message_count(state.id);
                            if message_count > 0 {
                                let count = u32::try_from(message_count).unwrap_or(u32::MAX);
                                Some(notification_count_badge(ChannelUnreadState::Notified(
                                    count,
                                )))
                            } else if unread == ChannelUnreadState::Unread {
                                Some(notification_count_badge(ChannelUnreadState::Notified(1)))
                            } else {
                                badge
                            }
                        } else {
                            badge
                        };
                        let badge = badge.map(|badge| selected_text_span(is_selected, badge));
                        let badge_width =
                            badge.as_ref().map(|span| span.content.width()).unwrap_or(0);
                        let request_tag = state.dm_request_tag();
                        // +3 reserves room for the surrounding " [" and "]".
                        let tag_width = request_tag
                            .map(|tag| tag.width().saturating_add(3))
                            .unwrap_or(0);
                        let label_width = max_width
                            .saturating_sub(branch_prefix.width())
                            .saturating_sub(prefix_width)
                            .saturating_sub(badge_width)
                            .saturating_sub(tag_width);
                        let mut spans = vec![
                            selection_marker(is_selected),
                            Span::styled(
                                branch_prefix,
                                theme::current().style(theme::HighlightGroup::Decoration),
                            ),
                        ];
                        if let Some(badge) = badge {
                            spans.push(badge);
                        }
                        if let Some(prefix_span) = dm_prefix_span {
                            spans.push(prefix_span);
                        } else if populated_voice_channel {
                            spans.push(Span::styled(
                                "๐Ÿ”Š",
                                theme::current().style(theme::HighlightGroup::ChannelTypeMarker),
                            ));
                            spans.push(Span::raw(" "));
                        } else {
                            spans.push(Span::styled(
                                channel_prefix,
                                theme::current().style(theme::HighlightGroup::ChannelTypeMarker),
                            ));
                        }
                        spans.push(Span::styled(
                            truncate_display_width_from(
                                &state.name,
                                horizontal_scroll,
                                label_width,
                            ),
                            name_style,
                        ));
                        if let Some(tag) = request_tag {
                            spans.push(Span::styled(
                                format!(" [{tag}]"),
                                theme::current().apply(
                                    theme::HighlightGroup::Emphasis,
                                    theme::current().style(theme::HighlightGroup::Description),
                                ),
                            ));
                        }
                        Line::from(spans)
                    }
                    ChannelPaneEntry::Thread {
                        state,
                        parent_branch,
                        branch,
                    } => {
                        let parent_prefix = parent_branch.participant_prefix();
                        let branch_prefix = branch.prefix();
                        let thread_prefix = if dashboard.is_forum_post_thread(state.id) {
                            "๐Ÿ’ฌ "
                        } else {
                            "๐Ÿงต "
                        };
                        let base_style = active_text_style(is_active, Style::default());
                        let is_muted = dashboard.channel_notification_muted(state.id);
                        let unread = dashboard.sidebar_channel_unread(state.id);
                        let (badge, mut name_style) =
                            channel_unread_decoration(unread, base_style, is_active);
                        if is_muted {
                            name_style =
                                theme::current().apply(theme::HighlightGroup::Muted, name_style);
                        }
                        name_style = selected_text_style(is_selected, name_style);
                        let badge_width =
                            badge.as_ref().map(|span| span.content.width()).unwrap_or(0);
                        let label_width = max_width
                            .saturating_sub(parent_prefix.width())
                            .saturating_sub(branch_prefix.width())
                            .saturating_sub(thread_prefix.width())
                            .saturating_sub(badge_width);
                        let mut spans = vec![
                            selection_marker(is_selected),
                            Span::styled(
                                parent_prefix,
                                theme::current().style(theme::HighlightGroup::Decoration),
                            ),
                            Span::styled(
                                branch_prefix,
                                theme::current().style(theme::HighlightGroup::Decoration),
                            ),
                        ];
                        if let Some(badge) = badge {
                            spans.push(badge);
                        }
                        spans.push(Span::styled(
                            thread_prefix,
                            theme::current().style(theme::HighlightGroup::ChannelTypeMarker),
                        ));
                        spans.push(Span::styled(
                            truncate_display_width_from(
                                &state.name,
                                horizontal_scroll,
                                label_width,
                            ),
                            name_style,
                        ));
                        Line::from(spans)
                    }
                    ChannelPaneEntry::VoiceParticipant {
                        participant,
                        parent_branch,
                        ..
                    } => {
                        let branch_prefix = parent_branch.participant_prefix();
                        let label_style = if participant.speaking {
                            theme::current().style(theme::HighlightGroup::VoiceSpeaking)
                        } else {
                            theme::current().style(theme::HighlightGroup::Muted)
                        };
                        let prefix = "  โ€ข ";
                        let label_width = max_width
                            .saturating_sub(branch_prefix.width())
                            .saturating_sub(prefix.width());
                        Line::from(vec![
                            selection_marker(is_selected),
                            Span::styled(
                                branch_prefix,
                                theme::current().style(theme::HighlightGroup::Decoration),
                            ),
                            Span::styled(
                                prefix,
                                theme::current().style(theme::HighlightGroup::Decoration),
                            ),
                            Span::styled(
                                voice_participant_label(
                                    participant,
                                    horizontal_scroll,
                                    label_width,
                                ),
                                selected_text_style(is_selected, label_style),
                            ),
                        ])
                    }
                },
                is_selected,
            )
        })
        .collect();

    let list = List::new(items);
    frame.render_widget(list, list_area);

    if state.show_custom_emoji() {
        overlay_emoji_slots(
            frame,
            list_area,
            emoji_images,
            &[],
            emoji_line_urls
                .iter()
                .map(|(line_index, column, url)| EmojiSlot {
                    row_in_list: *line_index as isize - channel_scroll as isize,
                    col: list_area.x as isize + *column as isize,
                    max_width: u16::MAX,
                    url: url.clone(),
                }),
        );
    }

    render_pane_filter_bar_with_cursor(
        frame,
        filter_area,
        filter_query,
        state.channel_pane_filter_cursor(),
        focused,
    );

    render_vertical_scrollbar(
        frame,
        list_area,
        state.channel_scroll(),
        list_area.height as usize,
        channel_line_count,
    );
}

fn selected_channel_server_label(state: &DashboardState) -> String {
    state
        .selected_guild_id()
        .and_then(|guild_id| state.guild_name(guild_id))
        .unwrap_or("Direct Messages")
        .to_owned()
}

fn selected_guild_is_boosted(state: &DashboardState) -> bool {
    matches!(
        state.selected_guild_boost(),
        Some((tier, count)) if tier.level() != 0 || count != 0
    )
}

/// Header rows the channel pane reserves: the guild name, plus one for the boost
/// line. Single source shared by the renderer, the scroll viewport, and
/// hit-testing so they cannot drift and clip the last channel row.
pub(in crate::tui::ui) fn channel_pane_header_height(state: &DashboardState) -> u16 {
    if selected_guild_is_boosted(state) {
        2
    } else {
        1
    }
}

fn selected_channel_boost_label(state: &DashboardState) -> Option<String> {
    if !selected_guild_is_boosted(state) {
        return None;
    }
    let (tier, count) = state.selected_guild_boost()?;
    let boosts = if count == 1 { "boost" } else { "boosts" };
    Some(format!("โšก Level {} ยท {count} {boosts}", tier.level()))
}

fn voice_participant_label(
    participant: &crate::discord::VoiceParticipantState,
    horizontal_scroll: usize,
    max_width: usize,
) -> String {
    let mut indicators = String::new();
    if participant.self_stream {
        indicators.push_str(" ๐Ÿ”ด");
    }
    if participant.mute || participant.self_mute {
        indicators.push_str(" ๐Ÿ”‡");
    }
    if participant.deaf || participant.self_deaf {
        indicators.push_str(" ๐ŸŽง");
    }

    let indicator_width = indicators.width();
    if indicator_width == 0 {
        return truncate_display_width_from(
            &participant.display_name,
            horizontal_scroll,
            max_width,
        );
    }
    if max_width <= indicator_width {
        return truncate_display_width(&indicators, max_width);
    }

    format!(
        "{}{}",
        truncate_display_width_from(
            &participant.display_name,
            horizontal_scroll,
            max_width.saturating_sub(indicator_width),
        ),
        indicators
    )
}