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
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
use std::collections::HashSet;

use crate::discord::{ChannelState, ChannelUnreadState};
use crate::tui::text_input::TextInputState;
use crate::{
    discord::ids::{Id, marker::GuildMarker},
    tui::fuzzy::{FuzzyMatchQuality, FuzzyScore, fuzzy_name_match_score},
};

use crate::tui::fuzzy::fuzzy_text_score;
use crate::tui::keybindings::SelectionAction;

use super::super::{
    ActiveGuildScope, DashboardState, channel_tree,
    model::{ChannelSwitcherItem, GuildPaneEntry},
    presentation::{is_direct_message_channel, sort_direct_message_channels},
};
use crate::discord::AppCommand;
use crate::tui::state::popups::{ActiveModalPopupKind, ModalPopup, SelectablePopupState};

#[derive(Debug)]
pub(in crate::tui::state) struct ChannelSwitcherState {
    query: TextInputState,
    selection: SelectablePopupState,
    base_items: Vec<ChannelSwitcherItem>,
    query_items: Option<Vec<ChannelSwitcherItem>>,
}

impl ChannelSwitcherState {
    fn new(base_items: Vec<ChannelSwitcherItem>) -> Self {
        Self {
            query: TextInputState::default(),
            selection: SelectablePopupState::default(),
            base_items,
            query_items: None,
        }
    }

    fn visible_items(&self) -> &[ChannelSwitcherItem] {
        self.query_items.as_deref().unwrap_or(&self.base_items)
    }

    fn visible_len(&self) -> usize {
        self.visible_items().len()
    }

    fn refresh_query_items(&mut self) {
        let query = self.query.value().trim();
        self.query_items =
            (!query.is_empty()).then(|| filter_channel_switcher_items(&self.base_items, query));
    }
}

impl DashboardState {
    pub fn open_channel_switcher(&mut self) {
        let items = self.all_channel_switcher_items();
        self.popups.modal = Some(ModalPopup::ChannelSwitcher(ChannelSwitcherState::new(
            items,
        )));
    }

    pub fn close_channel_switcher(&mut self) {
        if self.is_active_modal_popup(ActiveModalPopupKind::ChannelSwitcher) {
            self.popups.clear_modal();
        }
    }

    pub fn channel_switcher_query(&self) -> Option<&str> {
        self.popups
            .channel_switcher()
            .map(|switcher| switcher.query.value())
    }

    pub fn channel_switcher_query_cursor_byte_index(&self) -> Option<usize> {
        let switcher = self.popups.channel_switcher()?;
        Some(switcher.query.cursor_byte_index())
    }

    pub fn selected_channel_switcher_index(&self) -> Option<usize> {
        let switcher = self.popups.channel_switcher()?;
        Some(switcher.selection.selected_for_len(switcher.visible_len()))
    }

    pub fn channel_switcher_items(&self) -> Vec<ChannelSwitcherItem> {
        self.popups
            .channel_switcher()
            .map(|switcher| switcher.visible_items().to_vec())
            .unwrap_or_default()
    }

    pub fn move_channel_switcher_down(&mut self) {
        let len = self
            .popups
            .channel_switcher()
            .map(ChannelSwitcherState::visible_len)
            .unwrap_or_default();
        if let Some(switcher) = self.popups.channel_switcher_mut() {
            switcher.selection.move_down(len);
        }
    }

    pub fn move_channel_switcher_up(&mut self) {
        if let Some(switcher) = self.popups.channel_switcher_mut() {
            switcher.selection.move_up();
        }
    }

    pub fn set_channel_switcher_view_height(&mut self, height: usize) {
        let len = self
            .popups
            .channel_switcher()
            .map(ChannelSwitcherState::visible_len)
            .unwrap_or(0);
        if let Some(switcher) = self.popups.channel_switcher_mut() {
            switcher.selection.set_view_height_and_sync(height, len);
        }
    }

    pub fn channel_switcher_scroll(&self) -> usize {
        self.popups
            .channel_switcher()
            .map(|switcher| switcher.selection.scroll())
            .unwrap_or(0)
    }

    pub(super) fn page_channel_switcher_selection(&mut self, action: SelectionAction) {
        if let Some(switcher) = self.popups.channel_switcher_mut() {
            switcher.selection.page(switcher.visible_len(), action);
        }
    }

    pub fn select_channel_switcher_item(&mut self, row: usize) -> bool {
        let Some(switcher) = self.popups.channel_switcher_mut() else {
            return false;
        };
        if row >= switcher.visible_len() {
            return false;
        }
        switcher.selection.select(row);
        true
    }

    pub fn push_channel_switcher_char(&mut self, value: char) {
        if let Some(switcher) = self.popups.channel_switcher_mut() {
            switcher.query.insert_char(value);
            switcher.selection.select(0);
            switcher.refresh_query_items();
        }
    }

    pub fn pop_channel_switcher_char(&mut self) {
        if let Some(switcher) = self.popups.channel_switcher_mut()
            && switcher.query.delete_previous_grapheme()
        {
            switcher.selection.select(0);
            switcher.refresh_query_items();
        }
    }

    pub fn move_channel_switcher_query_cursor_left(&mut self) {
        if let Some(switcher) = self.popups.channel_switcher_mut() {
            switcher.query.move_left();
        }
    }

    pub fn move_channel_switcher_query_cursor_right(&mut self) {
        if let Some(switcher) = self.popups.channel_switcher_mut() {
            switcher.query.move_right();
        }
    }

    pub fn activate_selected_channel_switcher_item(&mut self) -> Option<AppCommand> {
        let item = {
            let switcher = self.popups.channel_switcher()?;
            let selected = switcher.selection.selected_for_len(switcher.visible_len());
            switcher.visible_items().get(selected)?.clone()
        };

        let Some(channel) = self.discord.cache.channel(item.channel_id) else {
            self.close_channel_switcher();
            return None;
        };
        let guild_id = channel.guild_id;
        let parent_id = channel.parent_id;
        self.close_channel_switcher();

        match guild_id {
            Some(guild_id) => {
                self.activate_guild(ActiveGuildScope::Guild(guild_id));
                if let Some(parent_id) = parent_id {
                    self.navigation
                        .channels
                        .collapsed_channel_categories
                        .remove(&parent_id);
                }
                self.restore_channel_cursor(Some(item.channel_id));
                self.activate_channel(item.channel_id);
                Some(AppCommand::SubscribeGuildChannel {
                    guild_id,
                    channel_id: item.channel_id,
                })
            }
            None => {
                self.activate_guild(ActiveGuildScope::DirectMessages);
                self.restore_channel_cursor(Some(item.channel_id));
                self.activate_channel(item.channel_id);
                Some(AppCommand::SubscribeDirectMessage {
                    channel_id: item.channel_id,
                })
            }
        }
    }

    fn all_channel_switcher_items(&self) -> Vec<ChannelSwitcherItem> {
        let mut base = Vec::new();
        self.push_direct_message_switcher_items(&mut base);

        let mut seen = HashSet::new();
        for entry in self.guild_pane_entries() {
            let GuildPaneEntry::Guild { state: guild, .. } = entry else {
                continue;
            };
            if seen.insert(guild.id) {
                self.push_guild_channel_switcher_items(&mut base, guild.id, &guild.name);
            }
        }

        let recent = self.recent_channel_switcher_items(&base);
        if !recent.is_empty() {
            for item in base.iter_mut() {
                item.group_order = item.group_order.saturating_add(1);
            }
        }

        let mut items = recent;
        items.extend(base);
        for (index, item) in items.iter_mut().enumerate() {
            item.original_index = index;
        }
        items
    }

    fn recent_channel_switcher_items(
        &self,
        base: &[ChannelSwitcherItem],
    ) -> Vec<ChannelSwitcherItem> {
        let mut recent = Vec::new();
        let mut seen = HashSet::new();
        for channel_id in &self.navigation.channels.recent_channel_ids {
            if Some(*channel_id) == self.navigation.channels.active_channel_id {
                continue;
            }
            if !seen.insert(*channel_id) {
                continue;
            }
            let Some(item) = base.iter().find(|item| item.channel_id == *channel_id) else {
                continue;
            };
            let mut item = item.clone();
            item.group_label = "Recent Channels".to_owned();
            item.parent_label = item.guild_name.clone();
            item.depth = 0;
            item.group_order = 0;
            recent.push(item);
        }
        recent
    }

    fn push_direct_message_switcher_items(&self, items: &mut Vec<ChannelSwitcherItem>) {
        let mut channels = self.discord.cache.channels_for_guild(None);
        channels.retain(|channel| !channel.is_category() && !channel.is_thread());
        sort_direct_message_channels(&mut channels);
        let group_order = items.len();
        for channel in channels {
            push_channel_switcher_item(
                items,
                ChannelSwitcherItemInput {
                    guild_id: None,
                    guild_name: None,
                    group_label: "Direct Messages",
                    parent_label: None,
                    channel,
                    depth: 0,
                    group_order,
                    unread: self.channel_unread(channel.id),
                    unread_message_count: self.channel_unread_message_count(channel.id),
                },
            );
        }
    }

    fn push_guild_channel_switcher_items(
        &self,
        items: &mut Vec<ChannelSwitcherItem>,
        guild_id: Id<GuildMarker>,
        guild_name: &str,
    ) {
        // Threads stay in the list: the tree helpers skip them, so they only
        // surface as nested entries under their parent in the loop below.
        let channels = self
            .discord
            .cache
            .viewable_channels_for_guild(Some(guild_id));
        let group_order = items.len();
        for root in channel_tree::sorted_channel_tree_roots(&channels) {
            if !root.is_category() {
                self.push_channel_and_child_threads(
                    items,
                    &channels,
                    guild_id,
                    guild_name,
                    root,
                    None,
                    0,
                    group_order,
                );
                continue;
            }

            for child in channel_tree::sorted_category_children(&channels, root.id) {
                self.push_channel_and_child_threads(
                    items,
                    &channels,
                    guild_id,
                    guild_name,
                    child,
                    Some(root.name.as_str()),
                    1,
                    group_order,
                );
            }
        }
    }

    /// Push a channel followed by its joined, non-archived threads. Forum posts
    /// are threads parented to a forum; the switcher lists forum channels but
    /// not their posts, so we stop before descending into forums.
    #[allow(clippy::too_many_arguments)]
    fn push_channel_and_child_threads(
        &self,
        items: &mut Vec<ChannelSwitcherItem>,
        channels: &[&ChannelState],
        guild_id: Id<GuildMarker>,
        guild_name: &str,
        channel: &ChannelState,
        parent_label: Option<&str>,
        depth: usize,
        group_order: usize,
    ) {
        push_channel_switcher_item(
            items,
            ChannelSwitcherItemInput {
                guild_id: Some(guild_id),
                guild_name: Some(guild_name),
                group_label: guild_name,
                parent_label,
                channel,
                depth,
                group_order,
                unread: self.channel_unread(channel.id),
                unread_message_count: self.channel_unread_message_count(channel.id),
            },
        );

        if channel.is_forum() {
            return;
        }

        let thread_parent_label = match parent_label {
            Some(category) => format!("{category} / {}", channel.name),
            None => channel.name.clone(),
        };
        for thread in channel_tree::sorted_child_threads(channels.iter().copied(), channel.id) {
            // Match the channel pane: only joined, non-archived threads appear.
            if !thread.current_user_joined_thread || thread.thread_archived().unwrap_or(false) {
                continue;
            }
            push_channel_switcher_item(
                items,
                ChannelSwitcherItemInput {
                    guild_id: Some(guild_id),
                    guild_name: Some(guild_name),
                    group_label: guild_name,
                    parent_label: Some(thread_parent_label.as_str()),
                    channel: thread,
                    depth: depth.saturating_add(1),
                    group_order,
                    unread: self.channel_unread(thread.id),
                    unread_message_count: self.channel_unread_message_count(thread.id),
                },
            );
        }
    }
}

struct ChannelSwitcherItemInput<'a> {
    guild_id: Option<Id<GuildMarker>>,
    guild_name: Option<&'a str>,
    group_label: &'a str,
    parent_label: Option<&'a str>,
    channel: &'a ChannelState,
    depth: usize,
    group_order: usize,
    unread: ChannelUnreadState,
    unread_message_count: usize,
}

fn push_channel_switcher_item(
    items: &mut Vec<ChannelSwitcherItem>,
    input: ChannelSwitcherItemInput<'_>,
) {
    let ChannelSwitcherItemInput {
        guild_id,
        guild_name,
        group_label,
        parent_label,
        channel,
        depth,
        group_order,
        unread,
        unread_message_count,
    } = input;
    if channel.is_category() {
        return;
    }
    let original_index = items.len();
    items.push(ChannelSwitcherItem {
        channel_id: channel.id,
        guild_id,
        guild_name: guild_name.map(str::to_owned),
        group_label: group_label.to_owned(),
        parent_label: parent_label.map(str::to_owned),
        channel_label: channel_switcher_channel_label(channel),
        unread,
        unread_message_count,
        search_name: format!("{} / {}", group_label, channel.name),
        depth,
        group_order,
        original_index,
    });
}

fn channel_switcher_match_score(
    item: &ChannelSwitcherItem,
    query: &str,
) -> Option<(FuzzyMatchQuality, FuzzyScore)> {
    let query = query.trim();
    if let Some(prefix) = channel_switcher_query_label_prefix(query)
        && !item.channel_label.starts_with(prefix)
    {
        return None;
    }
    let channel_query = channel_switcher_search_channel_name(query);
    let channel_name = channel_switcher_search_channel_name(&item.channel_label);
    if let Some(score) = fuzzy_name_match_score(channel_name, channel_query) {
        return Some(score);
    }

    fuzzy_text_score(&item.search_name, query).map(|score| (FuzzyMatchQuality::Context, score))
}

fn filter_channel_switcher_items(
    items: &[ChannelSwitcherItem],
    query: &str,
) -> Vec<ChannelSwitcherItem> {
    let mut scored: Vec<(FuzzyMatchQuality, FuzzyScore, ChannelSwitcherItem)> = items
        .iter()
        .filter_map(|item| {
            channel_switcher_match_score(item, query)
                .map(|(quality, score)| (quality, score, item.clone()))
        })
        .collect();
    scored.sort_by_key(|(quality, score, item)| {
        (*quality, *score, item.group_order, item.original_index)
    });
    scored.into_iter().map(|(_, _, item)| item).collect()
}

fn channel_switcher_search_channel_name(channel_label: &str) -> &str {
    // Match known icon tokens rather than splitting on the first space, since
    // thread and forum names can themselves contain spaces.
    for prefix in CHANNEL_SWITCHER_ICON_PREFIXES {
        if let Some(rest) = channel_label.strip_prefix(prefix) {
            return rest;
        }
    }
    // A typed query has no trailing space (e.g. "#general", "@alice").
    channel_label
        .strip_prefix('#')
        .or_else(|| channel_label.strip_prefix('@'))
        .unwrap_or(channel_label)
}

fn channel_switcher_query_label_prefix(query: &str) -> Option<char> {
    query
        .chars()
        .next()
        .filter(|prefix| matches!(prefix, '#' | '@'))
}

/// Must stay in sync with the prefixes emitted by
/// `channel_switcher_channel_label` so the search helper can strip them back off.
const CHANNEL_SWITCHER_ICON_PREFIXES: [&str; 4] = ["# ", "@ ", "๐Ÿงต ", "๐Ÿ“ "];

fn channel_switcher_channel_label(channel: &ChannelState) -> String {
    if is_direct_message_channel(channel) {
        match channel.kind.as_str() {
            "dm" | "Private" => format!("@ {}", channel.name),
            _ => channel.name.clone(),
        }
    } else if channel.is_thread() {
        format!("๐Ÿงต {}", channel.name)
    } else if channel.is_forum() {
        format!("๐Ÿ“ {}", channel.name)
    } else {
        format!("# {}", channel.name)
    }
}