concord 2.4.7

A terminal user interface client for Discord
Documentation
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
use super::activity::{ActivityLeading, build_activity_render};
use super::message::list::render_image_preview;
use super::*;
use crate::discord::ActivityKind;
use crate::tui::text::format_byte_size;
use ratatui::layout::Position;

mod action_menu;
mod attachment_viewer;
mod channel_switcher;
mod confirmation;
mod debug_log;
mod downloads;
mod folder_settings;
mod forum_post;
mod keymap;
mod notification_inbox;
mod options;
mod polls;
mod profile;
mod reactions;
mod search;
mod thread_edit;
mod toast;
mod url_picker;
mod voice_participant_audio;

const POPUP_GAUGE_WIDTH: usize = 28;

pub(super) use action_menu::{
    action_menu_area, leader_popup_area_for_state, render_channel_action_menu,
    render_guild_action_menu, render_leader_popup, render_member_action_menu,
    render_message_action_menu, render_thread_action_menu,
};
#[cfg(test)]
pub(super) use action_menu::{
    channel_action_menu_lines_for_test, message_action_menu_lines,
    message_action_menu_lines_with_keymap_options,
};
#[cfg(test)]
pub(super) use attachment_viewer::centered_viewer_preview_area;
pub(super) use attachment_viewer::render_attachment_viewer;
#[cfg(test)]
pub(super) use channel_switcher::{channel_switcher_cursor_position, channel_switcher_lines};
pub(super) use channel_switcher::{
    channel_switcher_item_index_at, channel_switcher_popup_area, channel_switcher_visible_items,
    render_channel_switcher_popup,
};
pub(super) use confirmation::{
    guild_leave_confirmation_popup_area_for_state, message_confirmation_popup_area_for_state,
    quit_confirmation_popup_area, render_guild_leave_confirmation, render_message_confirmation,
    render_notification_inbox_mark_all_confirmation, render_quit_confirmation,
    render_thread_delete_confirmation, thread_delete_confirmation_popup_area_for_state,
};
#[cfg(test)]
pub(super) use confirmation::{
    message_delete_confirmation_lines, message_pin_confirmation_lines,
    message_remove_embeds_confirmation_lines, quit_confirmation_lines,
};
#[cfg(test)]
pub(super) use debug_log::debug_log_popup_lines;
pub(super) use debug_log::{debug_log_popup_area_for_state, render_debug_log_popup};
#[cfg(test)]
pub(super) use downloads::downloads_popup_lines;
pub(super) use downloads::{
    downloads_popup_area, downloads_popup_line_count, render_downloads_popup,
};
#[cfg(test)]
pub(super) use folder_settings::folder_settings_input_line_for_test;
pub(super) use folder_settings::{folder_settings_popup_area, render_folder_settings_popup};
pub(super) use forum_post::{
    forum_post_composer_metrics, forum_post_composer_popup_area,
    forum_post_tag_picker_visible_items, render_forum_post_composer, render_forum_post_tag_picker,
};
#[cfg(test)]
pub(super) use keymap::keymap_help_popup_lines;
pub(super) use keymap::{
    keymap_popup_area, keymap_popup_text_area, keymap_popup_total_lines, render_keymap_help_popup,
};
pub(super) use notification_inbox::{
    notification_inbox_popup_area, render_notification_inbox_popup,
};
#[cfg(test)]
pub(super) use options::options_popup_lines;
pub(super) use options::{options_popup_area, options_popup_visible_items, render_options_popup};
#[cfg(test)]
pub(super) use polls::poll_vote_picker_lines;
pub(super) use polls::{poll_vote_picker_popup_area, render_poll_vote_picker};
pub(in crate::tui) use profile::user_profile_popup_area;
pub(super) use profile::{
    render_user_profile_popup, user_profile_popup_has_avatar, user_profile_popup_text_geometry,
    user_profile_popup_total_lines,
};
#[cfg(test)]
pub(super) use profile::{user_profile_popup_lines, user_profile_popup_lines_with_activities};
#[cfg(test)]
pub(super) use reactions::{
    emoji_reaction_picker_lines, emoji_reaction_picker_lines_for_width,
    emoji_reaction_picker_lines_with_own_reactions, filtered_emoji_reaction_picker_lines,
    reaction_list_lines_with_ready_urls, reaction_users_popup_lines,
};
pub(super) use reactions::{
    emoji_reaction_picker_popup_area_for_state, emoji_reaction_picker_visible_items_for_area,
    reaction_users_popup_area_for_state, render_emoji_reaction_picker, render_reaction_users_popup,
};
pub(super) use search::{
    render_search_popup, search_popup_area_for_state, search_popup_visible_items,
};
pub(super) use thread_edit::{
    render_thread_edit, render_thread_edit_tag_picker, thread_edit_metrics, thread_edit_popup_area,
    thread_edit_tag_picker_visible_items,
};
#[cfg(test)]
pub(super) use toast::toast_line;
pub(super) use toast::{render_toast, toast_area};
#[cfg(test)]
pub(super) use url_picker::message_url_picker_lines_for_width;
pub(super) use url_picker::{message_url_picker_popup_area, render_message_url_picker};
pub(super) use voice_participant_audio::{
    render_voice_participant_audio_popup, voice_participant_audio_popup_area,
};

fn popup_gauge_spacer() -> Span<'static> {
    Span::raw(" ".repeat(POPUP_GAUGE_WIDTH))
}

fn popup_gauge_line(
    x_offset: u16,
    min_label: &str,
    max_label: String,
    style: Style,
) -> Line<'static> {
    let leading_space = usize::from(x_offset).saturating_sub(min_label.len().saturating_add(1));
    Line::from(vec![
        Span::styled(format!("{}{min_label} ", " ".repeat(leading_space)), style),
        popup_gauge_spacer(),
        Span::styled(format!(" {max_label}"), style),
    ])
}

struct PopupGauge {
    x_offset: u16,
    width_margin: u16,
    y: u16,
    value: u16,
    maximum: u16,
    style: Style,
}

fn render_popup_gauge(frame: &mut Frame, inner: Rect, gauge: PopupGauge) {
    let gauge_width = inner
        .width
        .saturating_sub(gauge.width_margin)
        .min(POPUP_GAUGE_WIDTH as u16);
    if gauge_width == 0 {
        return;
    }
    let ratio = if gauge.maximum == 0 {
        0.0
    } else {
        f64::from(gauge.value) / f64::from(gauge.maximum)
    };
    frame.render_widget(
        Gauge::default()
            .ratio(ratio.clamp(0.0, 1.0))
            .label("")
            .gauge_style(gauge.style),
        Rect::new(
            inner.x.saturating_add(gauge.x_offset),
            gauge.y,
            gauge_width,
            1,
        ),
    );
}

pub(super) fn background_media_occlusion_areas(
    frame_area: Rect,
    state: &DashboardState,
) -> Vec<Rect> {
    let mut areas = Vec::new();

    if state.is_folder_settings_open() {
        areas.push(folder_settings_popup_area(frame_area));
    }
    if let Some(area) = active_modal_popup_area(frame_area, state) {
        areas.push(area);
    }

    let downloads = state.attachment_downloads();
    if !downloads.is_empty() {
        areas.push(downloads_popup_area(
            frame_area,
            downloads_popup_line_count(downloads.len()),
        ));
    }
    if let Some(toast) = state.toast_message() {
        areas.push(toast_area(frame_area, toast.text));
    }

    areas.into_iter().filter(|area| !area.is_empty()).collect()
}

fn active_modal_popup_area(frame_area: Rect, state: &DashboardState) -> Option<Rect> {
    let kind = state.active_modal_popup_kind()?;
    match kind {
        ActiveModalPopupKind::MessageActionMenu => {
            let actions = state.selected_message_action_items();
            (!actions.is_empty()).then(|| action_menu_area(frame_area, actions.len()))
        }
        ActiveModalPopupKind::GuildActionMenu => {
            let count = state.guild_action_row_count();
            (count > 0).then(|| action_menu_area(frame_area, count))
        }
        ActiveModalPopupKind::ChannelActionMenu => {
            let count = state.channel_action_row_count();
            (count > 0).then(|| action_menu_area(frame_area, count))
        }
        ActiveModalPopupKind::MemberActionMenu => {
            let count = state.selected_member_action_items().len();
            (count > 0).then(|| action_menu_area(frame_area, count))
        }
        ActiveModalPopupKind::MessageUrlPicker => {
            let urls = state.selected_message_url_items();
            (!urls.is_empty()).then(|| message_url_picker_popup_area(frame_area, urls.len()))
        }
        ActiveModalPopupKind::MessageConfirmation => {
            message_confirmation_popup_area_for_state(frame_area, state)
        }
        ActiveModalPopupKind::QuitConfirmation => Some(quit_confirmation_popup_area(frame_area)),
        ActiveModalPopupKind::GuildLeaveConfirmation => {
            guild_leave_confirmation_popup_area_for_state(frame_area, state)
        }
        ActiveModalPopupKind::ThreadDeleteConfirmation => {
            thread_delete_confirmation_popup_area_for_state(frame_area, state)
        }
        ActiveModalPopupKind::Options => Some(options_popup_area(frame_area, state)),
        ActiveModalPopupKind::AttachmentViewer => Some(attachment_viewer_popup(
            frame_area,
            state.attachment_viewer_zoom(),
        )),
        ActiveModalPopupKind::Leader => Some(leader_popup_area_for_state(frame_area, state)),
        ActiveModalPopupKind::UserProfile => Some(user_profile_popup_area(frame_area)),
        ActiveModalPopupKind::EmojiReactionPicker => {
            emoji_reaction_picker_popup_area_for_state(frame_area, state)
        }
        ActiveModalPopupKind::PollVotePicker => state
            .poll_vote_picker_items()
            .filter(|answers| !answers.is_empty())
            .map(|answers| poll_vote_picker_popup_area(frame_area, answers.len())),
        ActiveModalPopupKind::ReactionUsers => {
            reaction_users_popup_area_for_state(frame_area, state)
        }
        ActiveModalPopupKind::DebugLog => Some(debug_log_popup_area_for_state(frame_area, state)),
        ActiveModalPopupKind::KeymapHelp => Some(keymap_popup_area(frame_area)),
        ActiveModalPopupKind::ChannelSwitcher => Some(channel_switcher_popup_area(frame_area)),
        ActiveModalPopupKind::NotificationInbox => Some(notification_inbox_popup_area(frame_area)),
        ActiveModalPopupKind::Search => search_popup_area_for_state(frame_area, state),
        ActiveModalPopupKind::ForumPostComposer => Some(forum_post_composer_popup_area(frame_area)),
        ActiveModalPopupKind::ThreadEdit => Some(thread_edit_popup_area(frame_area)),
        ActiveModalPopupKind::ThreadActionMenu => {
            let count = state.thread_action_row_count();
            (count > 0).then(|| action_menu_area(frame_area, count))
        }
        ActiveModalPopupKind::VoiceParticipantAudio => {
            Some(voice_participant_audio_popup_area(frame_area))
        }
    }
}

/// Clears the popup area, draws the standard focused panel border, and
/// returns the inner content rect. Every modal popup opens with this
/// sequence and then renders its content into the returned rect.
fn render_modal_frame(frame: &mut Frame, popup: Rect, title: impl Into<String>) -> Rect {
    clear_area(frame, popup);
    let block = modal_block_owned(title.into());
    let inner = block.inner(popup);
    frame.render_widget(block, popup);
    inner
}

fn render_modal_paragraph(
    frame: &mut Frame,
    popup: Rect,
    title: impl Into<String>,
    lines: Vec<Line<'static>>,
) {
    let inner = render_modal_frame(frame, popup, title);
    frame.render_widget(Paragraph::new(lines).wrap(Wrap { trim: false }), inner);
}

fn popup_shortcut_help_text(items: &[(&str, &str)]) -> String {
    items
        .iter()
        .map(|(shortcut, description)| format!("[{shortcut}] {description}"))
        .collect::<Vec<_>>()
        .join(" · ")
}

fn popup_button_line(shortcut: &'static str, label: &'static str, active: bool) -> Line<'static> {
    popup_button_line_with_style(shortcut, label, active, Style::default())
}

fn popup_danger_button_line(
    shortcut: &'static str,
    label: &'static str,
    active: bool,
) -> Line<'static> {
    popup_button_line_with_style(
        shortcut,
        label,
        active,
        theme::current().style(theme::HighlightGroup::Error),
    )
}

fn popup_button_line_with_style(
    shortcut: &'static str,
    label: &'static str,
    active: bool,
    label_style: Style,
) -> Line<'static> {
    let active_style = |style| {
        if active {
            theme::current().apply(theme::HighlightGroup::ActiveField, style)
        } else {
            style
        }
    };
    Line::from(vec![
        Span::styled(
            editable_field_marker(active),
            active_style(Style::default()),
        ),
        Span::styled(
            format!("[{shortcut}] "),
            active_style(theme::current().style(theme::HighlightGroup::Shortcut)),
        ),
        Span::styled(label, active_style(label_style)),
    ])
}

fn truncate_line_to_display_width(line: Line<'static>, max_width: usize) -> Line<'static> {
    if max_width == 0 {
        return Line::default();
    }
    let mut remaining = max_width;
    let mut new_spans: Vec<Span<'static>> = Vec::with_capacity(line.spans.len() + 1);
    for span in line.spans {
        if remaining == 0 {
            break;
        }
        if span.content.width() <= remaining {
            remaining = remaining.saturating_sub(span.content.width());
            new_spans.push(span);
            continue;
        }
        let truncated = truncate_display_width(&span.content, remaining);
        remaining = remaining.saturating_sub(truncated.width());
        new_spans.push(Span::styled(truncated, span.style));
    }
    if remaining > 0 {
        new_spans.push(Span::styled(" ".repeat(remaining), line.style));
    }
    let mut truncated = Line::from(new_spans);
    truncated.style = line.style;
    truncated.alignment = line.alignment;
    truncated
}

fn truncate_popup_lines(lines: Vec<Line<'static>>, width: usize) -> Vec<Line<'static>> {
    lines
        .into_iter()
        .map(|line| truncate_line_to_display_width(line, width))
        .collect()
}

fn wrapped_styled_popup_lines(text: &str, width: usize, style: Style) -> Vec<Line<'static>> {
    if width == 0 {
        return vec![Line::from(Span::styled(String::new(), style))];
    }

    let mut lines = Vec::new();
    let mut current = String::new();
    for word in text.split_whitespace() {
        let candidate = if current.is_empty() {
            word.to_owned()
        } else {
            format!("{current} {word}")
        };

        if candidate.width() <= width {
            current = candidate;
            continue;
        }

        if !current.is_empty() {
            lines.push(Line::from(Span::styled(current, style)));
        }
        current = truncate_display_width(word, width);
    }

    if !current.is_empty() {
        lines.push(Line::from(Span::styled(current, style)));
    }
    if lines.is_empty() {
        lines.push(Line::from(Span::styled(String::new(), style)));
    }
    lines
}

fn push_wrapped_styled_popup_text(
    lines: &mut Vec<Line<'static>>,
    text: &str,
    width: usize,
    style: Style,
) {
    lines.extend(wrapped_styled_popup_lines(text, width, style));
}

fn selectable_popup_marker(selected: bool) -> Span<'static> {
    selection_marker_with("", selected)
}

fn editable_field_marker(active: bool) -> &'static str {
    if active { "" } else { "  " }
}

fn editable_field_label_style(active: bool, editing: bool) -> Style {
    if editing {
        theme::current().apply(
            theme::HighlightGroup::Strong,
            theme::current().style(theme::HighlightGroup::Editing),
        )
    } else if active {
        theme::current().style(theme::HighlightGroup::ActiveField)
    } else {
        theme::current().style(theme::HighlightGroup::Disabled)
    }
}

fn editable_field_value_style(active: bool, editing: bool) -> Style {
    if editing {
        theme::current().style(theme::HighlightGroup::Editing)
    } else if active {
        theme::current().style(theme::HighlightGroup::ActiveField)
    } else {
        theme::current().style(theme::HighlightGroup::Disabled)
    }
}

fn editable_tags_section_line(active: bool, required: bool) -> Line<'static> {
    let mut spans = vec![Span::styled(
        format!("{}tags:", editable_field_marker(active)),
        editable_field_label_style(active, false),
    )];
    if required {
        spans.push(Span::styled(
            " required",
            theme::current().style(theme::HighlightGroup::Error),
        ));
    }
    Line::from(spans)
}

fn selectable_popup_shortcut_span(shortcut: impl Into<String>) -> Span<'static> {
    Span::styled(
        shortcut.into(),
        theme::current().style(theme::HighlightGroup::Shortcut),
    )
}

fn selectable_popup_label_style(selected: bool, enabled: bool) -> Style {
    let mut style = if enabled {
        Style::default()
    } else {
        theme::current().style(theme::HighlightGroup::Disabled)
    };
    if selected {
        style = theme::current().apply(theme::HighlightGroup::SelectedRow, style);
    }
    style
}

fn shortcut_prefix(shortcut: Option<char>) -> String {
    shortcut
        .map(|shortcut| format!("[{shortcut}] "))
        .unwrap_or_else(|| "    ".to_owned())
}